I am very new to Objective-C and haven't had a lot of experience with C so please excuse my ignorance. My question is this, is it possible to add an Array to a Dictionary? Here is what I'd do in Python, and it's very convenient:
if 'mykey' not in mydictionary:
mydictionary['mykey']=[] # init an empty list for 'mykey'
mydictionary['mykey'].append('someitem')
I want something like this, that works:
NSMutableDictionary *matchDict = [NSMutableDictionary dictionary];
NSMutableArray *matchArray = [NSMutableArray array];
while (blah):
[matchDict setObject: [matchArray addObject: myItem] forKey: myKey];
I looked everywhere with no luck, just about to give up. Any feedback will be appreciated!
Here is how to do it:
I had to ask this question myself 3 months ago, so don't worry too much about asking ;)
After thinking about it I ended up solving the problem. If you want to populate an array for each key dynamically, you do it like this:
This will yield {('value1',value2',etc...) : 'somekey'}
Thanks @Antal and @Firoze for all the help, you helped me to work it out. Just needed to stop thinking "Python". If either of you see any issue or a better implementation let me know.
It's not really different from what you did in python.
edit
So if you need to add arrays for more than one key you might do this:
Given an array of two keys:
And a dictionary...
Here is how you could create an array for each key:
Hopefully that gives you the idea.