Jacob Lyles
Jacob Lyles

Reputation: 10390

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];
  [newPlayer setObject:life forKey:name];
  [players addObject:newPlayer];
}

Upvotes: 0

Views: 91

Answers (2)

Tom H
Tom H

Reputation: 1316

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)

Upvotes: 2

Chuck
Chuck

Reputation: 237060

In all likelihood, players is nil. Any message to nil returns 0 or nil.

Upvotes: 2

Related Questions