Friday, May 31, 2013

NSArray arrayByAddingObjectsFromArray example in Objective C (iOS).


NSArray arrayByAddingObjectsFromArray


Returns a new array that is a copy of the receiving array with the objects contained in another array added to the end.

- (NSArray *)arrayByAddingObjectsFromArray:(NSArray *)otherArray

Parameters
otherArray
An array.

Return Value of [NSArray arrayByAddingObjectsFromArray]
A new array that is a copy of the receiving array with the objects contained in otherArray added to the end.

NSArray arrayByAddingObjectsFromArray example.
NSArray *mainArray = [[NSArray alloc]initWithObjects:@"one",@"two",nil];
NSArray *addArray = [[NSArray alloc]initWithObjects:@"three",@"four",nil];
mainArray = [mainArray arrayByAddingObjectsFromArray:addArray];
[addArray release];
NSLog(@"%@", mainArray);
[mainArray release];

Example of [NSArray arrayByAddingObjectsFromArray].
NSArray *arr1 = [NSArray arrayWithObjects:[NSNumber numberWithInt:1],
                                          [NSNumber numberWithInt:2],
                                          [NSNumber numberWithInt:3], nil];
NSArray *arr2 = [NSArray arrayWithObjects:[NSNumber numberWithInt:4],
                                          [NSNumber numberWithInt:5],
                                          [NSNumber numberWithInt:6], nil];
NSArray *arr3 = [arr1 arrayByAddingObjectsFromArray:arr2];

NSArray arrayByAddingObjectsFromArray example.
NSArray  *obj1 = [[NSArray alloc] initWithObjects:@"1",@"0",@"0",nil];
NSArray  *obj2 = [[NSArray alloc] initWithObjects:@"0",@"3",@"0",nil];
NSArray  *obj3 = [[NSArray alloc] initWithObjects:@"0",@"0",@"5",nil];

scnArray = [[NSArray alloc] initWithArray:obj1];
scnArray = [scnArray arrayByAddingObjectsFromArray:obj2];
scnArray = [scnArray arrayByAddingObjectsFromArray:obj3];

End of NSArray arrayByAddingObjectsFromArray example article.