1

I have five NSMutableArray each NSMutableArray holding another object array ...I want to store all five NSMutableArray in one array...How can i do this?

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

And after store all five NSMutableArray in one array ..how can i retrieve NSMutableArray from that array ....

4 Answers 4

3

Do it in a for cycle, no need to write that repeating code with 5 instantiations:

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

Then access them by container[2]

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

Comments

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

Comments

0

You can do it like this. First add all your 5 array into NSMutableArray say "mainArray" like this

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

this will add all 5 arrays into main array. Now to retrieve them back from array you can do like this

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

or you use for loop to iterate all array from the mainArray.. Hope this will help you

Comments

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

2 Comments

where did you reference array?
@Joshua you mean reference yourArray? I foget to rename it. Now I have fixed it.

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.