【发布时间】:2016-02-19 03:28:51
【问题描述】:
我有 NSDictionary 数组,我想检查 NSArray 中是否存在特定的 NSDictionary“键”。
我试过了
if let val = dict["key"] {
if let x = val {
println(x)
} else {
println("value is nil")
}
} else {
println("key is not present in dict")
}
和
let arrayOfKeys = dictionary.allKeys
if (arrayOfKeys.containsObject(yourKey)) {
}
else {
}
但这是针对单个数组对象的。还有
if ([[dictionary allKeys] containsObject:key]) {
// contains key
}
此方法适用于单个 NSDictionary,不适用于 NSArray。
还有
if readArray.contains(["A":1]) { ALToastView.toastInView(UIApplication.sharedApplication().keyWindow, withText: "Already added")
}else{
readArray.append(["A":0]
}
如果更改键“A”的值,此代码将在数组中再次添加相同的键
例如。我的数组包含字典[“A”:1],我想检查键“A”是否存在? 如何检查数组中存在的任何键?我需要迭代数组吗?提前致谢。
【问题讨论】:
标签: ios swift nsarray nsdictionary