如何在NSMutableArray中存储和检索Array

时间:2012-08-21 11:04:11

标签: objective-c ios nsmutablearray

朋友您好我想在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  

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

由于 Deepak R

1 个答案:

答案 0 :(得分: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);
    }
}

非常简单:)