JSONExport
JSONExport copied to clipboard
"loop all over its keys" can't "dictionary[key] = dic[key]"
JSON:
{
"data": {
"modules": [
{
"grids": [
{
“A”: "http://xxx”,
}
]
},
{
"grids": [
{
“B”: 1
}
]
}
]
}
}
==> "grids.swift"
class Grid : NSObject, NSCoding, Mappable{
var b : Int?
class func newInstance(map: Map) -> Mappable?{
return Grid()
}
... ...
Can NOT export "A"。 so.
/**
Creates and returns a dictionary who is built up by combining all the dictionary elements in the passed array.
- parameter array: array of dictionaries.
- returns: dictionary that combines all the dictionary elements in the array.
*/
func unionDictionaryFromArrayElements(_ array: NSArray) -> NSDictionary
{
let dictionary = NSMutableDictionary()
for item in array{
if let dic = item as? NSDictionary{
//loop all over its keys
for key in dic.allKeys as! [String]{
//error : all keys = dictionary + dic
if let old = dictionary[key] as? NSArray,
let new = dic[key] as? [Any] {
dictionary[key] = old.addingObjects(from: new)
} else {
dictionary[key] = dic[key]
}
// dictionary[key] = dic[key]
}
}
}
return dictionary
}
Hi, Thanks for the suggested fix. I can apply it directly, but it is preferable if you create a PR so you can be credited for your efforts! Thanks