1

Is there any way to add a value to an existing key on a NSMutableDictionary?

Here is snippet of my code

 NSMutableArray *mainFeedList = [NSMutableArray array];
  [mainFeedList addObjectsFromArray:feedList];

  for(int i = 0; i < mainFeedList.count; i++){

    NSMutableArray *allFeed = [NSMutableArray array];

    NSString *categoryId = [mainFeedList[i] valueForKey: @"categoryId"];

    [allFeed addObject:mainFeedList[i]];

    if(allFeed != nil && allFeed.count > 0) {

      [feedContent setObject:allFeed
                      forKey:[combinedCategories[(int)[categoryId integerValue]] valueForKey: @"name"]];
    }

Sample scenario:

NSMutableDictionary *mDict = @{@"key1":@"value1",@"key2": @"value2"};

I know that

[mDict setObject:mArray forKey:@"key1"];

will set an object to key1 but what I need is

add another object to key1 without replacing existing object (i need it both)

1
  • Cannot be done. Dictionaries must have unique keys. If there are duplicates, you wont be able to fetch using a dictionary. Commented Nov 30, 2016 at 11:47

4 Answers 4

1

A structure of any NSDictionary is "one key to one object". If you would like to build a structure which maps one key multiple objects, you need an NSDictionary that maps keys to collections, such as NSArray or NSMutableArray:

NSMutableDictionary *mDict = @{
    @"key1": [@[ @"value1" ] mutableCopy]
,   @"key2": [@[ @"value2" ] mutableCopy]
};

Now you can add values to keys without replacing the existing ones:

[mDict[@"key1"] addObject:@"value3"];
Sign up to request clarification or add additional context in comments.

5 Comments

Should i check if the key is existing then if existed add an object to that key
will this code [mDict[@"key1"] addObject:@"value3"]; add an object to key or it will just replace the current value
@SkyWalker Absolutely! This is just a quick illustration using the code from your question. addObject will add a second object for the same key, so you would end up with this structure: @"key1": @[ @"value1", @"value3" ]
NSMutableDictionary *dict =[[NSMutableDictionary alloc] init]; if([dict valueForKey:@"name"] != nil) { [dict setValue:@"value" forKey:@"name"]; } else { [dict[@"name"] addObject:@"value3"]; }
@SkyWalker That's not going to work, because the object you add needs to be a mutable array, i.e. adding the first object needs to look like this: ` [dict setValue:[@[@"value"] mutableCopy] forKey:@"name"];`
0

NSDictionary only allows a single object corresponding to a single key. If you would like to add multiple objects corresponding to a single key, if you have string type of object then you can use separators also to combine strings like:

[mDict setObject:[NSString stringWithFormat:@"%@,%@", [mDict objectforKey:@"key1"], @"value2"] forKey:@"key1"];

Otherwise, you have to take collections, which you have already defined in your question.

Comments

0

add another object to key1 without replacing existing object...

why not set an dict to key1?

before:

[dict setObject:@"a" forKey:@"key1"];

U wanna:

add @"b" to "key1", in dict;

why not like:

[dict setObject:@{@"a":@"subKey1", @"b":@"subKey2"} forKey:@"key1"];

Comments

0

I would suggest storing an array as a key in your dictionary like I do below :

// Setting the value for "key1" to an array holding your first value
NSMutableDictionary *mDict = @{@"key1":@["value1"],@"key2": @"value2"};

Now when I want to add a new value I would do this:

// Create temp array
NSMutableArray *temp = mDict[@"key1"];

// Add new object
[temp addObject:@"value3"];

// Convert temp NSMutableArray to an NSArray so you can store it in your dict
NSArray *newArray = [[NSArray alloc] initWithArray:temp];

// Replace old array stored in dict with new array
mDict[@"key1"] = newArray;

Furthermore, if you are not sure if an array is already stored for that key you can run a check and populate with an empty dictionary like below:

if (mDict[@"key1"] == nil) {
    mDict[@"key1"] = @[];
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.