我想添加一个NSString到一个NSMutableArray(它将只保存NSStrings)。我没有收到任何错误,但是当我查询数组的count
时,它返回零,表示它没有添加该值。这里是我的代码(其获得一些XML,这绝对是100%的工作是):NSString不会将自己添加到NSMutableArray
// 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);
这NSLog的正确报告,在XML结果条目的数量,通常是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);
}
}
此NSLog还可以正确报告游戏的标题以及XML树中的标题。
// 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];
这最后的NSLog总是报告流派的数量为零。如果我稍后查询_listOfGamesReturnedFromSearch
数组,它将保存其他属性(名称),但流派计数为零 - 由于某种原因它不保存它们。
任何想法?
你在哪里声明并初始化你的数组? – DrummerB
@DrummerB显然没有。 – 2012-10-05 13:29:29
从我完成obj-c开始已经有一段时间了,但是您确定currentGame.gameGenres未设置为零吗? –