NSDictionary containing an NSArray

2020-08-01 05:31发布

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!

3条回答
来,给爷笑一个
2楼-- · 2020-08-01 05:56

Here is how to do it:

    [matchDict setObject:matchArray forKey:@"myKey"];

I had to ask this question myself 3 months ago, so don't worry too much about asking ;)

查看更多
爷的心禁止访问
3楼-- · 2020-08-01 06:06

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:

While (something)
{
    // check if key is already in dict.
    if ([_matchesDictionary objectForKey: hashKey]) 
        {
            // pull existing array from dict and add new entry to array for said key.
            NSMutableArray *keyArray = [_matchesDictionary objectForKey: hashKey];
            [keyArray addObject: newPath];

            // put the array back into the dict for said key.
            [_matchesDictionary setObject: keyArray forKey: hashKey];

     } else {

            // create new array, assign empty array to said key in dict.
            NSMutableArray *keyArray = [NSMutableArray array];
            [keyArray addObject: newPath];
            [_matchesDictionary setObject: keyArray forKey: hashKey];

     } 

}

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.

查看更多
甜甜的少女心
4楼-- · 2020-08-01 06:07

It's not really different from what you did in python.

NSMutableDictionary *matchDict = ...
NSMutableArray *matchArray = ...
[matchDict setObject:matchArray forKey:someKey];
// This is same as: mydictionary['mykey']=[]


// 'matchArray' is still a valid pointer so..
[matchArray addObject:someObj];

// or later if 'matchArray' were no longer in scope 
// it would look like this:

NSMutableArray* arrayForKey = [matchDict objectForKey:someKey];
[arrayForKey addObject:someObj];

// or more simply:
[[matchDict objectForKey:someKey] addObject:someObj];

// this is same as: mydictionary['mykey'].append('someitem')

edit

So if you need to add arrays for more than one key you might do this:

Given an array of two keys:

NSArray *keys = [NSArray arrayWithObjects:@"key0",@"key1", nil];

And a dictionary...

NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:[keys count]];

Here is how you could create an array for each key:

for (id key in keys) {

    // Create a new array each time...

    NSMutableArray *array = [NSMutableArray array];

    // and insert into dictionary as value for this key

    [dict setObject:array forKey:key];
}

Hopefully that gives you the idea.

查看更多
登录 后发表回答