Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

arrayByAddingObjectsFromArray?

I am not sure what I am doing wrong here? I have tried various combinations to try and copy an array into variable mmm. I am trying to learn how to create a 2D array and then run a loop to place init_array into 10 columns.

// NSMutableArray *mmm = [NSMutableArray arrayWithCapacity: 20];
NSMutableArray *kkk = [NSMutableArray arrayWithObjects: @"a", @"b", @"cat", @"dog", nil];
NSMutableArray *mmm; //= [NSMutableArray arrayWithObjects: @"1", @"2", @"3", @"4", nil];

[mmm arrayByAddingObjectsFromArray:kkk];

NSLog(@"Working: %@",[mmm objectAtIndex:3]);

thanks...

so this works from the given answer:

NSMutableArray *mmm = [NSMutableArray arrayWithCapacity: 20];
NSMutableArray *kkk = [NSMutableArray arrayWithObjects: @"a", @"b", @"cat", @"dog", nil];

[mmm addObjectsFromArray:kkk];

NSLog(@"Working: %@",[mmm objectAtIndex:3]);
like image 839
Kristen Martinson Avatar asked Jul 15 '11 03:07

Kristen Martinson


2 Answers

arrayByAddingObjectsFromArray: returns a new (autoreleased) NSArray object. What you want is addObjectsFromArray:.

like image 181
Wevah Avatar answered Sep 21 '22 04:09

Wevah


arrayByAddingObjectsFromArray: returns a new NSArray that includes the objects in the receiver followed by the objects in the argument. The code you posted there, with mmm unset, will probably just crash since mmm doesn't point to an NSArray object. If you had assigned an array to mmm, then it would return (@"1", @"2", @"3", @"4", @"a", @"b", @"cat", @"dog") — but you don't assign the result to any variable, so it just goes nowhere. You'd have to do something like NSArray *yetAnotherArray = [mmm arrayByAddingObjectsFromArray:kkk].

If you have an NSMutableArray and you want to add objects from another array, use addObjectsFromArray:.

like image 36
Chuck Avatar answered Sep 20 '22 04:09

Chuck