Renz Manacmol
Renz Manacmol

Reputation: 939

Objective C. NSMutable Dictionary adding value to existing key

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)

Upvotes: 1

Views: 2397

Answers (4)

LoganHenderson
LoganHenderson

Reputation: 1442

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"] = @[];
}

Upvotes: 0

darkThanBlack
darkThanBlack

Reputation: 86

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"];

Upvotes: 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.

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726569

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"];

Upvotes: 1

Related Questions