0

I'm trying to add an NSString to an NSMutableArray (which will hold only NSStrings). I'm not getting any errors, but when I query the array's count, it returns zero, indicating that it didn't add the value. Here's my code (its getting some XML, which is definitely 100% working):

// get an array of genres for this game
NSArray *genreList = [self getAllItems:@"//genre" fileName:[NSURL URLWithString:queryURL]];
int numberOfGenres = [genreList count];
NSLog(@"This game has %u genres", numberOfGenres);

This NSLog reports correctly, the number of entries in the XML result, usually between 1-5.

// put the genre names from that array into the current game's object
if (numberOfGenres > 0) {
    for (int i = 0; i < numberOfGenres; i++) {
        NSString *currentGenreName = [NSString stringWithString:[[genreList objectAtIndex:i] objectForKey:@"name"]];
        [currentGame.gameGenres addObject: currentGenreName];
        NSLog(@"I added a genre to %@, called %@.", currentGame.gameName, currentGenreName);
    }
}

This NSLog also reports correctly, with the game's title, and the of that in the XML tree.

// save current game to game list
NSLog(@"I'm about to save %@ to the array, with its %u genres", currentGame.gameName, [currentGame.gameGenres count]);
[_listOfGamesReturnedFromSearch addObject:currentGame];

This last NSLog always reports the number of genres as zero. If I query the _listOfGamesReturnedFromSearch array later, it saved the other properties fine (name), but the genres count is zero – it didn't save them for some reason.

Any ideas?

Luke
  • 9,512
  • 15
  • 82
  • 146

1 Answers1

2

This is a typical symptom of not allocating and initializing the array - so it stays being nil and keeps ignoring all messages sent. Alloc-init it and it'll work.

  • I have done alloc init on the object itself, do I also need to initialise all of its properties? And if so, am I supposed to be doing that in the init method of the object class? – Luke Oct 05 '12 at 13:31
  • Yes, you need to alloc/init your properties and usually you do that in the init method. – DrummerB Oct 05 '12 at 13:32
  • 1
    @lukech if you alloc-init an object, all of the instance variables will be initialized to zero... READ the Objective-C runtime reference NOW. –  Oct 05 '12 at 13:32
  • To zero, or to nil? Is there not a difference? Telling something its zero, and not telling it its anything at all? – Luke Oct 05 '12 at 13:35
  • Objects (pointers) are set to nil (null pointer), primitive variables to zero. – DrummerB Oct 05 '12 at 13:37
  • 1
    @lukech since Obejctive-C objects are pointers, setting them to zero means setting them to nil (apparently on all architecture Objcetive-C is supported I know about). Yes, 0 is not necessarly the actual numerical value for a NULL or nil pointer, but the C standard specifies that in case of pointers, when the compiler encounters a comparison against 0, it should compare to NULL (which is, in turn, nil in Objective-C). –  Oct 05 '12 at 13:37