NSArray从其他数组添加对象

时间:2013-03-29 13:39:11

标签: ios objective-c nsarray

我有三个NSArray个对象。我需要将此数组的所有对象添加到名为 allMyObjects NSArray

NSArray标准解决方案,例如通过初始化方法,或者我是否需要make custom方法从其他数组中检索所有对象并将所有检索到的对象放入 allMyObjects 数组?

4 个答案:

答案 0 :(得分:23)

不知道这对你的问题是否算是一个足够简单的解决方案,但这是做到这一点的直接方式(正如其他回答者所暗示的那样):

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

答案 1 :(得分:6)

一旦看到这个,

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
)

答案 2 :(得分:4)

您可以在addObjectsFromArray:阵列上调用allMyObjects方法。

答案 3 :(得分:0)

这里添加了存储代码,并从数组到数组获取数据。

将数组存储到数组

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

从Circle Array获取数据

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