我可以在一个数组中存储多个NSMutableArray?

时间:2016-06-13 05:46:43

标签: ios objective-c arrays nsmutablearray

我有五个NSMutableArray每个NSMutableArray持有另一个对象数组...我想将所有五个NSMutableArray存储在一个数组中...我该怎么做?

    NSMutableArray *mA1 = [NSMutableArray array];
    NSMutableArray *mA2 = [NSMutableArray array];
    NSMutableArray *mA3 = [NSMutableArray array];
    NSMutableArray *mA4 = [NSMutableArray array];
    NSMutableArray *mA5 = [NSMutableArray array];

并将所有五个NSMutableArray存储在一个数组中..如何从该数组中检索NSMutableArray ....

4 个答案:

答案 0 :(得分:2)

for周期中执行此操作,无需使用5个实例编写重复代码:

NSMurableArray *container = [NSMutableArray new];
for (int index = 0; index < 5; index++) {
     [container addObject:[NSMutableArray new]];
}

然后通过container[2]

访问它们

答案 1 :(得分:1)

NSMutableArray *mA1 = [NSMutableArray array];
NSMutableArray *mA2 = [NSMutableArray array];
NSMutableArray *mA3 = [NSMutableArray array];
NSMutableArray *mA4 = [NSMutableArray array];
NSMutableArray *mA5 = [NSMutableArray array];


NSArray *combinedArray = [NSArray arrayWithObjects:mA1,mA2,mA3,mA4,mA5,nil];

or
NSArray *combinedArray = @[mA1,mA2,mA3,mA4,mA5]; //immutable
NSMutableArray *combinedArray = [@[mA1,mA2,mA3,mA4,mA5]mutableCopy]; // mutable

答案 2 :(得分:0)

你可以这样做。 首先将所有5个数组添加到NSMutableArray中,例如“mainArray”,如下所示

NSMutableArray *mainArray = [[NSMutablearray alloc]initWithObjects:mA1,mA2,mA3,mA4,mA5,nil];

这会将所有5个数组添加到主数组中。现在要从数组中检索它们,你可以这样做

NSMutablearray *array = [mainArray objectAtIndex:0];//here pass index of array you want to get

或者您使用for循环来迭代mainArray中的所有数组。 希望这会对你有所帮助

答案 3 :(得分:0)

NSMutableArray *mA1 = [NSMutableArray array];
NSMutableArray *mA2 = [NSMutableArray array];
NSMutableArray *mA3 = [NSMutableArray array];
NSMutableArray *mA4 = [NSMutableArray array];
NSMutableArray *mA5 = [NSMutableArray array];

NSArray *yourArray = @[mA1, mA2, mA3, mA4, mA5];//yourArray store all five NSMutableArray

for (int i = 0; i < yourArray.count; i++) {
    NSMutableArray *mA = array[i];//mA is the NSMutableArray(mA1 or other).
}