我得到“文件”的 json作为响应,我想将它们存储在 single Array
. 由于我是swift
开发新手,我无法弄清楚如何做到这一点。任何帮助将不胜感激。我想要它们如下
let myFileArray = [file1, file2.....]
您可以使用flatMap
或compactMap
这样做:
如果您使用的是 swift 4.1,请compactMap
使用flatMap
.
// Your dictionary
let dictionary = response as? [String: Any]
// Create an empty Array
var myFileArray = [String]()
// Get the doc array
if let docs = dictionary?["doc"] as? [[String: Any]] {
// for each doc array
for doc in docs {
// get the nameChange array
if let nameChange = doc["nameChange"] as? [[String: Any]] {
// convert the inner dictionary to string array
let fileNameArray = nameChange.compactMap { (dictionary) -> String? in
return dictionary["file"] as? String
}
// append the array into main array
myFileArray.append(contentsOf: fileNameArray)
}
}
}
// Here is your final string array of filename
print(myFileArray)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句