Scrungepipes
Scrungepipes

Reputation: 37581

Array count is 0 directly after calling addObject

Why is the count of otherArray 0 even though self.array has N items and the for loop is executing N times?

for (MyObject *obj in self.array) 
{
    [self.otherArray addObject:obj];
    NSLog(@"Num items: %d", [self.otherArray count]);
} 

self.otherArray is an NSMutableArray*

LATER: Doh!, forgot to call alloc/init (I come from a language where the equivalent of addObejct will create the array if necessary).

Upvotes: 0

Views: 218

Answers (2)

dtuckernet
dtuckernet

Reputation: 7895

In this case, the most common reason this would occur is if self.otherArray isn't initialized. If you add a check to self.otherArray, I would suspect it is nil. If you added:

self.otherArray = [[NSMutableArray alloc] init];

right before the other code, I suspect it would work as intended.

Upvotes: 0

PengOne
PengOne

Reputation: 48398

Best guess: You have not initialized self.otherArray correctly.

Second best guess: self.otherArray is not mutable.

Test this by posting your initialization code.

Upvotes: 2

Related Questions