1

你好朋友我想将 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  

所以我的要求是存储多个 NSMutableArrayNSMutableArray并在另一个中访问。

谢谢迪帕克 R

4

1 回答 1

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

这就是您再次访问所有这些的方式:

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);
    }
}

真的很容易:)

于 2012-08-21T11:18:39.910 回答