Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trouble with initializing NSMutableArray in my Singleton

I am getting a weird error, and I can't figure it out. This takes place inside of a class that is created with the singleton pattern:

- (NSMutableArray *) getCurrentClasses
{
    NSMutableArray *current_classes = [[NSMutableArray init] alloc];
    NSLog([NSString stringWithFormat:@"%d", [current_classes count]]);
    ...
}

When I run this, even though I literally just initialized current_classes, it gives me this error in log:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFArray count]: method sent to an uninitialized mutable array object'

Does anyone know what this is happening? I initialized it literally last line.

Thanks

like image 496
Ethan Avatar asked Nov 04 '09 17:11

Ethan


1 Answers

You mixed up the alloc/init calls. alloc comes first. It should be:

NSMutableArray *current_classes = [[NSMutableArray alloc] init];
like image 88
mipadi Avatar answered Oct 17 '22 07:10

mipadi