0

I am making a number of custom objects. I want to add each object to an array, but after the code below is run the array is still empty.

for (int i = 0; i<= numberOfObjectsWanted; i++) {
     CustomClass *object = [[CustomClass alloc]init];
     [objectsArray addObject:object];
}

objectsArray is an NSMutableArray

2 Answers 2

1

So if your array is nil, no message sends will have any effect on it...

objectsArray = [[NSMutableArray alloc] init];

beforehand.

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

Comments

1

You're not adding the object that was created. Try...

for (int i = 0; i<= numberOfObjectsWanted; i++) {
     CustomClass *object = [[CustomClass alloc]init];
     [objectsArray addObject:object];
}

EDIT - The new code in the question looks better now. The next thing to check is whether you have a good mutable array to begin with. Try NSLog(@"array is %@", objectsArray); inside the loop.

2 Comments

@Mrwolfy You sure the array is created properly and it's not nil?
Thanks so much danh. I accepted the other answer only because it's brevity might be more helpful for others.

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.