NSArray add objects from other array

29,324

Solution 1

once see this one ,

NSArray *newArray=[[NSArray alloc]initWithObjects:@"hi",@"how",@"are",@"you",nil];
    NSArray *newArray1=[[NSArray alloc]initWithObjects:@"hello",nil];
    NSArray *newArray2=[[NSArray alloc]initWithObjects:newArray,newArray1,nil];
    NSString *str=[newArray2 componentsJoinedByString:@","];
    NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:@"()\n "];
    str = [[str componentsSeparatedByCharactersInSet:doNotWant] componentsJoinedByString: @""];
     NSArray *resultArray=[str componentsSeparatedByString:@","];
    NSLog(@"%@",resultArray);

O/P:-

(
    hi,
    how,
    are,
    you,
    hello
)

Solution 2

Don't know if this counts as a sufficiently simple solution to your problem, but this is the straight forward way to do it (as alluded to by other answerers, too):

NSMutableArray *allMyObjects = [NSMutableArray arrayWithArray: array1]; 
[allMyObjects addObjectsFromArray: array2]; 
[allMyObjects addObjectsFromArray: array3]; 

Solution 3

You can call addObjectsFromArray: method on your allMyObjects array.

Share:
29,324
Matrosov Oleksandr
Author by

Matrosov Oleksandr

Updated on July 31, 2022

Comments

  • Matrosov Oleksandr
    Matrosov Oleksandr almost 2 years

    I have three NSArray objects. I need to add all objects for this array to NSArray that is called allMyObjects.

    Have NSArray standard solution to make it for example via initialization method or do I need make custom method to retrieve all objects from other arrays and put all retrieved objects to my allMyObjects array?