You can use flatMap
or compactMap
to do this like:
If you're using swift 4.1 use compactMap
otherwise use 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)