aft*_*eep 1 contains swift swifty-json
我试图在 swift 中使用 contains 来查找数组上的值,但是当我使用变量作为谓词时,我得到一个“字符串不可转换为”S.Generator.Element”。
任何人都可以解释这意味着什么以及为什么会发生?
这是使用 swiftyJSON 加载和解析的原始 JSON:
{ "假期" : [ "01/01/2015", "12/01/2015", "23/03/2015", "02/04/2014", "03/04/2015", "01/05" /2015”、“18/05/2015”、“08/06/2015”、“15/06/2015”、“29/06/2015”、“20/07/2015”、“17/08/2015” ", "12/10/2015", "02/11/2015", "16/11/2015", "08/12/2015", "25/12/2015", "02/01/2016" ] }
这有效
var haystack:Array<JSON> = jsonData["holidays"].arrayValue
if(contains(haystack, "01/01/2015")) {
return true
}
Run Code Online (Sandbox Code Playgroud)
这不起作用:
var haystack:Array<JSON> = jsonData["holidays"].arrayValue
var needle:String = "01/01/2015"
if(contains(haystack, needle)) {
return true
}
Run Code Online (Sandbox Code Playgroud)
而不是调用arrayValue,尝试object:
if let haystack = jsonData["holidays"].object as? [String] {
let needle = "01/01/2015"
if contains(haystack, needle) {
return true
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1424 次 |
| 最近记录: |