I create an NSMutableArray as follows (note that winner is an instance variable):
winner = [NSMutableArray arrayWithObjects:@"11", @"12", @"13", nil];
When I set winner to nil or remove its objects like
[self.winner removeAllObjects];
my program will automatically shut down. How should I solve this?
Updated !!!
In case I code like this
self.winner = [NSMutableArray arrayWithObjects:@"11", @"12", @"13", nil];
it will call setter method which is
- (void)setWinner:(NSMutableArray *)newWinner
{
[winner release];
winner = [newWinner retain];
}
Do I still need to retain the array like
self.winner = [[NSMutableArray arrayWithObjects:@"11", @"12", @"13", nil] retain];
Are you calling
-removeAllObjectsin a different method? If so, then the problem is likely that you’ve failed to retain the array, and it has been destroyed between the assignment and your later reference.+arrayWithObjectsreturns an instance that has hadautoreleasecalled on it.Either use a synthesized property to set the instance variable, use a method that returns ownership of the object (like
+alloc), or add aretaincall: