1

I need to create a specific number instances of an object based on a variable. so the pseudo code looks kinda like this

for(int x; x < aInt; x++) {
    //create object and initialize it
}

how would I do that and create a different object each time with a different name and memory location?

2 Answers 2

2

Stick the reference in a NSMutableArray (or a NSArray, given that you appear to know the size in advance).

NSMutableArray *array = [[NSMutableArray alloc]init]

for(int x; x < aInt; x++) {
    //create object and initialize it
    YourObject *o = [[YourObject alloc]init];
    [array addObject:o];
    [o release];
}

// do whatever you need to do with the objects

A NSDictionary/NSMutableDictionary is certainly an option as well, depending on what your requirements are.

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

Comments

0

Just use a NSMutableArray:

NSMutableArray *objectsArray = [NSMutableArray arrayWithCapacity:(YourIntegerValue)];
for(int x; x < aInt; x++) {
    //create object and initialize it
    [objectsArray addObject:(YourObject)];
}

Don't forget to release the array after you're done working with it!

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.