OWolf
OWolf

Reputation: 5132

Adding custom objects to NSMutable array not working, addObject returns empty array

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

Upvotes: 0

Views: 499

Answers (2)

user529758
user529758

Reputation:

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

objectsArray = [[NSMutableArray alloc] init];

beforehand.

Upvotes: 1

danh
danh

Reputation: 62676

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.

Upvotes: 1

Related Questions