如何在NSMutableArray中存储和检索数组

Dee*_*k R 1 objective-c nsmutablearray ios

你好朋友我想在NSMutableAray中存储NSMutableAray并想要检索它.我能够存储但无法访问它.我的代码如下:

NSMutableArray *muteArray;
NSMutableArray *muteArray_Master;
NSMutableArray *mutArrar_Level1;            


NSArray *Children = [dictionary objectForKey:@"Children"];

if (Children.count > 0) {
    muteArray = [NSMutableArray arrayWithArray:Children];
}

muteArray_Master= [[NSMutableArray alloc] init];
mutArrar_Level1= [[NSMutableArray alloc] init];



[muteArray_Master addObject:muteArray]; // muteArray_Master array added one object successfully 

i = [muteArray_Master count]; // Here I have i=1

mutArrar_Level1 = [NSMutableArray arrayWithArray:[muteArray_Master objectAtIndex:i-1]]; // But here mutArrar_Level1 not producing correct data.still it is with 0 objects  
Run Code Online (Sandbox Code Playgroud)

所以我的要求是存储多个NSMutableArray NSMutableArray并在另一个中访问all.

谢谢Deepak R.

Joh*_*rug 5

首先,我不敢告诉你,你需要处理你的变量名.请参阅https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CodingGuidelines/CodingGuidelines.html#//apple_ref/doc/uid/10000146-SW1

无论如何,这是你如何添加多个数组到一个数组:

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

for (int i = 0; i < 10; i++) {
    NSArray *childArray = [NSArray arrayWithObjects:
        [NSString stringWithFormat:@"a-%i", i], 
        [NSString stringWithFormat:@"b-%i", i], 
        [NSString stringWithFormat:@"c-%i", i],
        nil];

    [parentArray addObject:childArray];
}
Run Code Online (Sandbox Code Playgroud)

这就是你再次访问所有这些内容的方式:

for (int i = 0; i < 10; i++) {
    NSArray *childArray = [parentArray objectAtIndex:i];
    NSLog(@"Child Array %i", i);

    for (NSString *item in childArray) {
        NSLog(@"  Item: %@", item);
    }
}
Run Code Online (Sandbox Code Playgroud)

真的很容易:)