0

I have the following array, however, is there a way to add a new key and value ( such as age) for each dictionary item in the NSMutableArray after the following initialization.

NSMutableArray* names = [NSMutableArray arrayWithObjects:
                  [NSDictionary dictionaryWithObjectsAndKeys:
                   @"Joe",@"firstname",
                   @"Bloggs",@"surname",
                   nil],
                  [NSDictionary dictionaryWithObjectsAndKeys:
                   @"Simon",@"firstname",
                   @"Templar",@"surname",
                   nil],
                  [NSDictionary dictionaryWithObjectsAndKeys:
                   @"Amelia",@"firstname",
                   @"Pond",@"surname",
                   nil],
                  nil];
0

3 Answers 3

3

You can do this using dip linking .

Here is Sample Code:

NSMutableDictionary mutableCopy = (NSMutableDictionary )CFPropertyListCreateDeepCopy(kCFAllocatorDefault, (CFDictionaryRef)originalDictionary, kCFPropertyListMutableContainers);

https://developer.apple.com/reference/corefoundation/1429997-cfpropertylistcreatedeepcopy?language=objc

Sign up to request clarification or add additional context in comments.

Comments

1

If you can define your array of dictionaries as mutable dictionaries, then you can do what you want like this:

NSMutableArray* names = [NSMutableArray arrayWithObjects:
                         [NSMutableDictionary dictionaryWithObjectsAndKeys:
                          @"Joe",@"firstname",
                          @"Bloggs",@"surname",
                          nil],
                         [NSMutableDictionary dictionaryWithObjectsAndKeys:
                          @"Simon",@"firstname",
                          @"Templar",@"surname",
                          nil],
                         [NSMutableDictionary dictionaryWithObjectsAndKeys:
                          @"Amelia",@"firstname",
                          @"Pond",@"surname",
                          nil],
                         nil];
for (NSMutableDictionary *dic in names) {
    dic[@"age"] = @21;
}

Notice that each of the elements in the initial array are now NSMutableDictionary intsances instead of NSDictioanry. If for some reason you cannot do that, then you would need to get each dictionary from the array, convert it to a mutable instance and then replace the relevant array element with the new dictionary instance.

If you can't create NSMutableDictionary instances for the array, then the code you need would look something like this:

NSMutableArray* names = [NSMutableArray arrayWithObjects:
                         [NSDictionary dictionaryWithObjectsAndKeys:
                          @"Joe",@"firstname",
                          @"Bloggs",@"surname",
                          nil],
                         [NSDictionary dictionaryWithObjectsAndKeys:
                          @"Simon",@"firstname",
                          @"Templar",@"surname",
                          nil],
                         [NSDictionary dictionaryWithObjectsAndKeys:
                          @"Amelia",@"firstname",
                          @"Pond",@"surname",
                          nil],
                         nil];
[names enumerateObjectsUsingBlock:^(NSDictionary *dic, NSUInteger idx, BOOL * _Nonnull stop) {
    NSMutableDictionary *dic2 = [dic mutableCopy];
    dic2[@"age"] = @21;
    names[idx] = dic2;
}];

2 Comments

you bring a new variable dic into the picture, but I do not see how do you change names object. By the way, AFnetworking convert the json object into NSDictionary
See the for loop to see how dic came into being. The code is simply iterating through each element in the array. Since you presented the code with the array being created via code, the above solution would work. But if you get the array via an API call, then that won't work. You'd need to convert the dictionary to a mutable dictionary as I mentioned, first.
1

If Want to add key and value in Dictionary then take NSMutableDictionary And add key and value in dictionary.

Below is Code for That:

NSMutableArray* names = [NSMutableArray arrayWithObjects:
                             [NSDictionary dictionaryWithObjectsAndKeys:
                              @"Joe",@"firstname",
                              @"Bloggs",@"surname",
                              nil],
                             [NSDictionary dictionaryWithObjectsAndKeys:
                              @"Simon",@"firstname",
                              @"Templar",@"surname",
                              nil],
                             [NSDictionary dictionaryWithObjectsAndKeys:
                              @"Amelia",@"firstname",
                              @"Pond",@"surname",
                              nil],
                             nil];
    for (int i=0; i<names.count; i++) {
        NSMutableDictionary *name = [NSMutableDictionary dictionaryWithDictionary:names[i]];
        NSDictionary *test = [NSDictionary dictionaryWithObject:@"Test" forKey:@"Demo"];
        [name addEntriesFromDictionary:test];
        names[i] = name;
    }
    NSLog(@"%@",names);

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.