Adding item to NSMutableArray with function results in no
I have the following utility method declared, where "players" is a NSMutableArray object and a property of my application's view controller. My problem is that "players" still has 0 objects after this method is called.
-(void)addPlayerNamed:(NSString *)name withLife:(NSNumber *)life{
NSMutableDictionary *newPlayer = [NSMutableDictionary dictionaryWithCapacity:1];
[newPlaye开发者_运维知识库r setObject:life forKey:name];
[players addObject:newPlayer];
}
Have you set players to anything? You have to call this before using it, maybe in the initialiser.
players=[[NSMutableArray alloc] init];
If players is a property, you probably also want to use
self.players
instead of
players
(Although the latter will still work)
In all likelihood, players
is nil. Any message to nil returns 0 or nil.
精彩评论