你好我有Array
它有NSDictionaries。
1st object->["111":title of the video]
2nd object->["123":title of the other]
3rd object->["133":title of another]
我们想要在此123
中搜索Array
密钥并获取其值。我该怎么做?
请帮我。
感谢
更新
var subCatTitles=[AnyObject]()
let dict=[catData![0]:catData![4]]
self.subCatTitles.append(dict)
答案 0 :(得分:1)
如果你的意思是你有这样的数组:
var anArray: [NSDictionary] = [
["111": "title of the video"],
["123": "title of the other"],
["133": "title of another"]
]
这将有效:
if let result = anArray.flatMap({$0["123"]}).first {
print(result) //->title of the other
} else {
print("no result")
}
(我假设"在重复"策略时先取得。)
但我强烈怀疑这种数据结构是否真的适合您的目的。
答案 1 :(得分:0)
起初,字典不是数组....
import Foundation
// it is better to use native swift dictionary, i use NSDictionary as you request
var d: NSDictionary = ["111":"title of the video","123":"title of the other","133":"title of another"]
if let value = d["123"] {
print("value for key: 123 is", value)
} else {
print("there is no value with key 123 in my dictionary")
}
// in case, you have an array of dictionaries
let arr = [["111":"title of the video"],["123":"title of the other"],["133":"title of another"]]
let values = arr.flatMap { (d) -> String? in
if let v = d["123"] {
return v
} else {
return nil
}
}
print(values) // ["title of the other"]