1

Hello friends I want to store NSMutableAray's in NSMutableAray and want to retrieve it.I'm able to store it but could not access it. my code is below:

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  

So my requirement is to store more than one NSMutableArray's in NSMutableArray and access all in another.

Thanks Deepak R

0

1 Answer 1

4

First off, I'm afraid to tell you that you need to work on your variable names. See https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CodingGuidelines/CodingGuidelines.html#//apple_ref/doc/uid/10000146-SW1

Anyhow, here's how you add multiple arrays to an array:

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

And this is how you'd access all of them again:

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

Really easy :)

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks buddy and sorry about my variable naming.
No problem. You'll just do yourself and others on your team a favor if you adhere to the naming conventions. Also, please don't forget to upvote and accept answers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.