7

I have an NSArray that gives me the following data:

01/14/2013 13:28:06.559 IUser Reader [71164: c07] (
         {
         "id_acompanhante" = "";
         "id_evento" = 34;
         "user_id" = 1;
         "inserido_por" = "Himself";
         name = iUser;
         status = done;
         type = User;
     }
         {
         "id_acompanhante" = 1;
         "id_evento" = 34;
         "user_id" = 1;
         "inserido_por" = iUser;
         name = "Mayara Roca";
         status = naofeito;
         type = companion;
     }
)

How do I play this data to a NSMutableArray and add another field within each item. example:

  {
     "id_acompanhante" = "";
     "id_evento" = 34;
     "user_id" = 1;
     "inserido_por" = "Himself";
     name = IUser;
     status = done;
     type = User;
     tag = 2;
 }

I added one more field "TAG".

How do I do this?

3
  • Is everything inside the {} the content of your array or is it representing an individual array. Commented Jan 14, 2013 at 15:41
  • I've put the code in my answer to do add a TAG field to each dictionary within the array. Commented Jan 14, 2013 at 15:52
  • if you are adding a value to the key "TAG" in your dictionaries you you should go with Fogmeister's answer. Commented Jan 14, 2013 at 16:00

6 Answers 6

8
NSMutableArray *mutableArray = [NSMutableArray array];

for (NSDictionary *dictionary in yourArray) {
    NSMutableDictionary *mutDictionary = [dictionary mutableCopy];

    [mutDictionary setObject:[NSNumber numberWithInt:2] forKey:@"tag"];

    [mutableArray addObject:mutDictionary];
}

This is how to do what you asked. Making the array mutable will not allow you to change the contents of a dictionary inside it. You need to pull out each dictionary and make that mutable and then store it back in the array.

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

Comments

7

Easy

NSMutableArray *mArray = [NSMutableArray arrayWithArray:yourArray]
[mArray addObject:yourObject];

1 Comment

That is not what is being asked.
1

It's as easy as calling mutableCopy on your array:

NSArray * myArray = @[@"one", @"two", @"three"];
NSMutableArray * myMut = [myArray mutableCopy];
NSLog(@"My Mut = %@", myMut);

Hope that helps!

Update Here is an example that iterates over your original array, converts items to mutable dictionary and inserts the new property. You then toss your original array and use the myMut version.

NSArray * myArray = @[@{@"key1":@"value1"}, @{@"key2":@"value2"}];
NSMutableArray * myMut = [[NSMutableArray alloc] init];
for (NSDictionary * dict in myArray) {
    NSMutableDictionary * mutDict = [dict mutableCopy];
    [mutDict setObject:@"2" forKey:@"tag"]; // Add new properties
    [myMut addObject:mutDict];
}

1 Comment

My bad, I didn't read your question fully. My answer will let you convert the array to mutable, but looks like you have an array of NSDictionaries. To add a property to the dictionary; you'll need to make the dictionary mutable.
0

Try this

NSMutableArray *aMutableArray = [NSMutableArray arrayWithArray:anArray];

Comments

0

As far as I understand, you dont need a mutable array but you need to convert the NSDictionaries within your array to be mutable. This is also answered by others.

But I want to show a nifty trick how you can set add a key/value pair to all mutable Dictionaries with on line. I use much simpler data as you, so that it is easier to see, what happens.

//just an example array of mutable Dictionary
NSMutableArray *array = [NSMutableArray array];
[array addObject:[NSMutableDictionary dictionaryWithObject:@"one" forKey:@(1)]];
[array addObject:[NSMutableDictionary dictionaryWithObject:@"two" forKey:@(2)]];
[array addObject:[NSMutableDictionary dictionaryWithObject:@"three" forKey:@(3)]];

// and here is the trick
[array setValue:@"10" forKey:@"tag"];

The array now contains

(
{
    tag = 10;
    1 = one;
},
{
    tag = 10;
    2 = two;
},
{
    3 = three;
    tag = 10;
}
)

from the NSArray docs

setValue:forKey:
Invokes setValue:forKey: on each of the array's items using the specified value and key.

- (void)setValue:(id)value forKey:(NSString *)key

Comments

0
NSArray *theOldArray; // your old array
NSMutableArray *theMutableAry = [NSMutableArray array];
for (NSDictionary *dicItem in theOldArray)
{
    NSMutableDictionary *dicWithOneMoreField = [NSMutableDictionary dictionaryWithDictionary:dicItem];
    [dicWithOneMoreField setValue:@"your value for tag" forKey:@"tag"];
    [theMutableAry addObject:dicWithOneMoreField];
}

or try the other way

NSData* archivedData = [NSKeyedArchiver archivedDataWithRootObject:theOldArray];
NSMutableArray *mutableArray = [NSKeyedUnarchiver unarchiveObjectWithData:archivedData];
for (NSDictionary *dicItem in mutableArray)
{
    [dicItem setValue:@"your value for tag" forKey:@"tag"];
}

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.