我有像[[:String:Any]]
这样的数组
我有一个 value
string ,我想提取具有该键的元素无需循环(一行)。
为了检查它是否在那里,我使用了这个:
if(array.map{$0["NAME"] as! String}.contains(value)){
有没有办法在 这个 if
语句中提取这个字典?
最佳答案
为此,无需映射
数组。您可以使用 contains(where:)
if array.contains(where: { $0["name"] as? String == value }) {
print("Exist")
}
如果你想要数组中的对象(字典)也可以使用 first(where:)
if let dict = array.first(where: { $0["name"] as? String == value }) {
print(dict)
}
有关 first(where:)
的更多信息,请查看 SO Thread
关于swift - 根据键从字典数组中获取元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43522346/