Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
madordie opened this issue Nov 5, 2016 · 1 comment
Open

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

madordie opened this issue Nov 5, 2016 · 1 comment

Comments

@madordie
Copy link

madordie commented Nov 5, 2016

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
}
@Ahmed-Ali
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants