0

Trying to create dynamic NSMutableArrays in a foor loop

//Here's my for loop
for (Object *object in parseMethod.objects) {

}

objects is an NSMutableArray which stores the objects; based on the number of the objects I'd like to create unique array, something like:

NSMutableArray *array1;
NSMutableArray *array2;
NSMutableArray *array3;
//and so on...
NSMutableArray *array[n];

Anyone can share an answer how to achieve this?

3
  • What about creating another one NSMutableArray that will store other NSMutableArray's? Commented Mar 1, 2012 at 12:55
  • What are you trying to accomplish or how is it going to be used? Commented Mar 1, 2012 at 12:58
  • @Zaph: Trying to create dynamic arrays that will store some items; I have a method which parses an XML and for each item from the XML file I'd like to create a array in which I have to add objects... Commented Mar 1, 2012 at 13:05

1 Answer 1

3

I'm not entirely sure I understood your problem, but I think you want to create a number of arrays, based on the number of objects in another array, right? If so, try this:

NSMutableArray *arrayWithOtherArraysInIt = [NSMutableArray arrayWithCapacity:[objectsClass count]];

for (objectClass *object in objectsClass.objects) {

   [arrayWithOtherArraysInIt addObject:[NSMutableArray arrayWithCapacity:0]];

}

Edit: Oh and by the way, you should think about your naming conventions. You're not supposed to use lowercase class names (like you did with objectClass). And I wouldn't name an object directly after its class' name...

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

Comments

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.