Let\'s say I have a rather complex dictionary, like this one:
let dict: [String: Any] = [
\"countries\": [
\"japan\": [
\"capital\":
Pass your dictionary to this function, It will return you a flat dictionary, without any nested dict incorporated .
//SWIFT 3.0
func concatDict( dict: [String: Any])-> [String: Any]{
var dict = dict
for (parentKey, parentValue) in dict{
if let insideDict = parentValue as? [String: Any]{
let keys = insideDict.keys.map{
return parentKey + $0
}
for (key, value) in zip(keys, insideDict.values) {
dict[key] = value
}
dict[parentKey] = nil
dict = concatDict(dict: dict)
}
}
return dict
}