Oleksandr Matrosov
Oleksandr Matrosov

Reputation: 27123

NSArray add objects from other array

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?

Upvotes: 6

Views: 29911

Answers (4)

abdul sathar
abdul sathar

Reputation: 2396

Here am adding code for store and get datas from array to array.

To store array to array

NSMutableArray rowOneRoundData = [NSMutableArray arrayWithObjects: @"45",@"29",@"12",nil];
 NSMutableArray   rowTwoRoundData = [NSMutableArray arrayWithObjects: @"41",@"45",@"45",nil];
   NSMutableArray rowThreeRoundData = [NSMutableArray arrayWithObjects: @"12",@"45",@"22",nil];
   NSMutableArray rowFourRoundData = [NSMutableArray arrayWithObjects: @"45",@"12",@"61",nil];
   NSMutableArray rowFiveRoundData = [NSMutableArray arrayWithObjects: @"12",@"14",@"14",nil];
   NSMutableArray rowSixRoundData = [NSMutableArray arrayWithObjects: @"12",@"12",@"12",nil];
   NSMutableArray rowSevenRoundData = [NSMutableArray arrayWithObjects: @"12",@"36",@"83",nil];
   NSMutableArray rowEightRoundData = [NSMutableArray arrayWithObjects: @"37",@"57",@"45",nil];
   NSMutableArray rowNineRoundData = [NSMutableArray arrayWithObjects: @"12",@"93",@"83",nil];
   NSMutableArray rowTenRoundData = [NSMutableArray arrayWithObjects: @"16",@"16",@"16",nil];
   NSArray circleArray = [[NSArray alloc]initWithObjects:rowOneRoundData,rowTwoRoundData,rowThreeRoundData,rowFourRoundData,rowFiveRoundData,rowSixRoundData,rowSevenRoundData,rowEightRoundData,rowNineRoundData,rowTenRoundData, nil];

Get data from Circle Array

for (int i= 0; i<10;i++)
{
NSArray *retriveArrar = [[circleArray objectAtIndex:i] mutableCopy];
}

Upvotes: 0

Balu
Balu

Reputation: 8460

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
)

Upvotes: 6

Monolo
Monolo

Reputation: 18253

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]; 

Upvotes: 26

Levi
Levi

Reputation: 7343

You can call addObjectsFromArray: method on your allMyObjects array.

Upvotes: 4

Related Questions