JSONExport icon indicating copy to clipboard operation
JSONExport copied to clipboard

"loop all over its keys" can't "dictionary[key] = dic[key]"

Open madordie opened this issue 7 years ago • 1 comments

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
}

madordie avatar Nov 05 '16 04:11 madordie

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

Ahmed-Ali avatar Nov 28 '16 08:11 Ahmed-Ali