Creating NSMutableArray from NSArray Objective-C

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 to reproduce this data in NSMutableArray and add another field to each element. Example:

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

I added another "TAG" field.

How to do it?

+7
source share
6 answers
 NSMutableArray *mutableArray = [NSMutableArray array]; for (NSDictionary *dictionary in yourArray) { NSMutableDictionary *mutDictionary = [dictionary mutableCopy]; [mutDictionary setObject:[NSNumber numberWithInt:2] forKey:@"tag"]; [mutableArray addObject:mutDictionary]; } 

Here's how to do it. Creating a mutable array will not allow you to modify the contents of the dictionary inside it. You need to pull out each dictionary and make it mutable, and then save it back to the array.

+8
source

Easy

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

This is as simple as calling mutableCopy in your array:

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

Hope this helps!

Update Here's an example that iterates over your original array, converts the elements into a mutable dictionary, and inserts a new property. Then you throw your source array and use the version of myMut.

 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
source

try it

 NSMutableArray *aMutableArray = [NSMutableArray arrayWithArray:anArray]; 
0
source

As far as I understand, you do not need a mutable array, but you need to convert NS dictionaries inside your array into a variable. Others also respond to this.

But I want to show a great trick how you can set up adding a key / value pair to all mutable dictionaries online. I use much simpler data, since it’s easier for you to see what is happening.

 //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 NSArray docs

setValue:forKey:
Calls setValue: forKey: for each element of the array using the specified value and key.

 - (void)setValue:(id)value forKey:(NSString *)key 
0
source
 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 another way

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

All Articles