将多维nsmutablearray转换为单维nsmutablearray

时间:2013-05-03 14:14:36

标签: ios ipad nsmutablearray

在IOS中,我有这个数组:

multiDimensionArray = [NSMutableArray arrayWithObjects:[NSMutableArray arrayWithObjects:@"one",@"two", nil],[NSMutableArray arrayWithObjects:@"three", nil],[NSMutableArray arrayWithObjects:@"four", nil],nil];

然后我需要将其转换为:

singleDimensionArray = [NSMutableArray arrayWithObjects:[multiDimensionArray objectAtIndex:0], nil];

我得到1个对象(@“one,two”),而不是获得2个对象(@“one”和@“two”)。如何将其分为2个对象?谢谢!

2 个答案:

答案 0 :(得分:0)

您的新数组只包含一个包含2个额外对象的数组。所以现在做同样的事情来创建具有2个对象的新数组。

twoObjectsArray = [singleDimensionArray objectAtIndex:0];

for (NSString *string in twoObjectsArray) {
    NSLog(@"%@,string);
}

答案 1 :(得分:0)

NSMutableArray *array1=[[NSMutableArray alloc] initWithObjects:@"One",@"Two", nil];

NSMutableArray *array2 = [[NSMutableArray alloc] initWithObjects:@"Three",@"Four" ,nil];

NSMutableArray *array3 =[[NSMutableArray alloc] initWithObjects:array1,array2, nil];

NSMutableArray *array4=[[NSMutableArray alloc] init];

for (NSMutableArray *array in array3)
{
    for (int i=0; i<[array count]; i++)
    {
        [array4 addObject:[array objectAtIndex:i]];
    }
}

NSLog(@"Final Array = %@",array4);

array3=nil;
array2=nil;
array1=nil;