0

Still have some difficulties to understand Obj-C's gestion of memory and some of its concepts.

So my main class contains a NSMutableArray containing some characters and their list of weapons. It's like this :

In Main class.h

@property (nonatomic, retain) NSMutableArray *players;

In Main class.m's init

for(int i = 0 ; i < 30 ; i++)
{
    [players addObject:[[PlayerInGame alloc] init:[self.tabPlayers objectAtIndex:i] :[self.tabWeapons:objectAtIndex:i]]];
}

PlayerInGame.h

@property (nonatomic, retain) NSMutableArray *weaponsList;

PlayerInGame.m

- (id) init : (Player*) player : (Weapon*) weapon
{
    [self.weaponsList addObject:weapon];
    // Then I try NSLog of weaponsList's count.
}

Here weaponsList is always empty. What is wrong?

Kjuly
  • 34,476
  • 22
  • 104
  • 118
Rob
  • 15,732
  • 22
  • 69
  • 107
  • possible duplicate of [Empty NSMutableArray , not sure why](http://stackoverflow.com/questions/11632308/empty-nsmutablearray-not-sure-why) – Vladimir Oct 15 '12 at 09:20

4 Answers4

3

You have to alloc your array before add any object in it. you can use following code in viewDidLoad method

 NSMutableArray *players = [[NSMutableArray allo]init]; 
 NSMutableArray weaponsList = [[[NSMutableArray alloc] init]
Rahul Patel
  • 5,858
  • 6
  • 46
  • 72
3

The other answers are right. On any other language if you reference a unallocated object you will get a NullPointerException. But in objective C the method sent to nil just returns 0 and it won't crash the app.. If you want further read, read this

That is why

[self.weaponsList addObject:weapon];

didn't crash, while in java if you try to add object to a unallocated array your program will crash.. As other answers pointed out, the statement

self.weaponsList = [[[NSMutableArray alloc] init] autorelease];

alloc memory to store array, and a reference is given back to to variable weaponList. Now weaponList != nil.

Community
  • 1
  • 1
Krishnabhadra
  • 34,169
  • 30
  • 118
  • 167
  • 1
    While the solution given is correct, the explanation is wrong. You don't get an exception because nil is considered to be a valid object by the runtime and any message sent to nil returns straight away with a return value of 0 (or equivalently nil). `[nil count]` therefore returns 0. – JeremyP Oct 15 '12 at 09:59
2

I've not seen weaponList object allocation. Do you initialize it?

self.weaponsList = [[[NSMutableArray alloc] init] autorelease];

PS: Minor advice. Method "- (id) init : (Player*) player : (Weapon*) weapon" signature will look better and being used easier if you change it as

- (id) initWithPlayer:(Player *)player weapon:(Weapon *)weapon
Orange
  • 553
  • 3
  • 20
2

I aslo suggest to change a bit your init syntax and init the array with object:

- (id) initWithPlayer:(Player *)aPlayer weapon:(Weapon *)aWeapon {
    self.weaponsList = [NSMutableArray arrayWithObject:aWeapon];
    [self.weaponsList retain];
    // remember to release inside dealloc, or use autorelease
}
Shebuka
  • 3,148
  • 1
  • 26
  • 43