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]);
arrayByAddingObjectsFromArray:
returns a new (autoreleased)NSArray
object. What you want isaddObjectsFromArray:
.arrayByAddingObjectsFromArray:
returns a new NSArray that includes the objects in the receiver followed by the objects in the argument. The code you posted there, withmmm
unset, will probably just crash sincemmm
doesn't point to an NSArray object. If you had assigned an array tommm
, 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 likeNSArray *yetAnotherArray = [mmm arrayByAddingObjectsFromArray:kkk]
.If you have an NSMutableArray and you want to add objects from another array, use
addObjectsFromArray:
.