检查 NSArray 在 Swift 中是否包含 NSDictionary 键?
Check NSArray contain NSDictionary key in Swift?
我有 NSDictionary 数组,我想检查 NSArray 中是否存在特定的 NSDictionary "key"。
我试过了
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" 是否存在?
我如何检查数组中存在的任何键?我需要迭代数组吗?提前致谢。
参考以下示例:
var array = [[String:Any]]()
array.append(["key1":"value1"])
array.append(["key2":"value2"])
array.append(["key3":"value3"])
array.append(["key4":"value4"])
array.append(["key5":"value5"])
let key = "key5"
if let index = (array.indexOf { (dict) -> Bool in
dict[key] != nil
})
{
print("Key Found at = \(index) ")
} else {
print("Key not Found")
}
你可以用这个方法。基本上你遍历 dict 数组获取它的索引和 dict 的元组,如果 dict 包含你将索引存储在数组中的键。
let arrayOfDict = [
["key1":"value1", "key2":"value2"],
["key3":"value3", "key4":"value4", "key5":"value5"],
["key6":"value6", "key7":"value7", "key8":"value8"],
["key6":"value6", "key7":"value7", "key8":"value8"]
];
let keyToCheck = "key6"
var foundIndex = [Int]()
for (index, dict) in arrayOfDict.enumerate()
{
if let item = dict[keyToCheck] {
foundIndex.append(index)
}
}
if foundIndex.count > 0 {
print("Found at \(foundIndex)")
} else {
print ("not found")
}
如果要获取包含键和索引的字典,也可以使用此方法。
let newArray = arrayOfDict.enumerate().filter { (tuple:(index: Int, element: Dictionary<String, String>)) -> Bool in
if tuple.element.keys.contains(keyToCheck) {
return true
} else {
return false
}
}
newArray 将是类型为 (Int:[String:String]) 的元组数组,其中 tuple.0 将是索引,tuple.1 将是字典
我有 NSDictionary 数组,我想检查 NSArray 中是否存在特定的 NSDictionary "key"。
我试过了
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" 是否存在? 我如何检查数组中存在的任何键?我需要迭代数组吗?提前致谢。
参考以下示例:
var array = [[String:Any]]()
array.append(["key1":"value1"])
array.append(["key2":"value2"])
array.append(["key3":"value3"])
array.append(["key4":"value4"])
array.append(["key5":"value5"])
let key = "key5"
if let index = (array.indexOf { (dict) -> Bool in
dict[key] != nil
})
{
print("Key Found at = \(index) ")
} else {
print("Key not Found")
}
你可以用这个方法。基本上你遍历 dict 数组获取它的索引和 dict 的元组,如果 dict 包含你将索引存储在数组中的键。
let arrayOfDict = [
["key1":"value1", "key2":"value2"],
["key3":"value3", "key4":"value4", "key5":"value5"],
["key6":"value6", "key7":"value7", "key8":"value8"],
["key6":"value6", "key7":"value7", "key8":"value8"]
];
let keyToCheck = "key6"
var foundIndex = [Int]()
for (index, dict) in arrayOfDict.enumerate()
{
if let item = dict[keyToCheck] {
foundIndex.append(index)
}
}
if foundIndex.count > 0 {
print("Found at \(foundIndex)")
} else {
print ("not found")
}
如果要获取包含键和索引的字典,也可以使用此方法。
let newArray = arrayOfDict.enumerate().filter { (tuple:(index: Int, element: Dictionary<String, String>)) -> Bool in
if tuple.element.keys.contains(keyToCheck) {
return true
} else {
return false
}
}
newArray 将是类型为 (Int:[String:String]) 的元组数组,其中 tuple.0 将是索引,tuple.1 将是字典