I have a method which is passed a dict (whose values themselves consist of nested dicts), and I am using recursion to traverse this dict, and I want to store only the information I need in another dict which is also passed to the method as the second argument.
So, the method (ignoring some optional arguments that’s needed to aid recursion) looks like:
-(NSMutableDictionary*) extractData: (NSDictionary*)complicatedDict intoSimpleDict:(NSMutableDictionary*)simpleDict {
// do recursive traversal of complicatedDict and return simpleDict
}
I am facing two problems:
a. I need to pass an empty dict initially when I first call the method. How do I initialize an empty NSMutableDictionary to pass as the simpleDict parameter?
b. I am trying to group values having common key in the simpleDict. In JS (which is my most familiar language), I will implement it like:
if( !simpleDict.hasOwnProperty('some_key') ) {
simpleDict['some_key'] = [];
}
simpleDict['some_key'].push("value");
The above code would initially an array in each index of the dict, and push the value to it. How do I do this in objective-c?
a. Create an empty mutable dictionary simply like this
b. This is equivalent to your JS example