Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c NSString won';t将自身添加到NSMutableArray_Objective C_Xml_Nsmutablearray - Fatal编程技术网

Objective c NSString won';t将自身添加到NSMutableArray

Objective c NSString won';t将自身添加到NSMutableArray,objective-c,xml,nsmutablearray,Objective C,Xml,Nsmutablearray,我正在尝试将NSString添加到NSMutableArray(它将只包含NSString)。我没有得到任何错误,但是当我查询数组的计数时,它返回零,表示它没有添加值。下面是我的代码(它获取了一些XML,这绝对是100%有效的): 此NSLog正确报告XML结果中的条目数,通常在1-5之间 // put the genre names from that array into the current game's object if (numberOfGenres > 0) { f

我正在尝试将NSString添加到NSMutableArray(它将只包含NSString)。我没有得到任何错误,但是当我查询数组的
计数时,它返回零,表示它没有添加值。下面是我的代码(它获取了一些XML,这绝对是100%有效的):

此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总是报告类型数为零。如果我稍后查询从搜索返回的
\u gamesresreturnedfromsearch
数组的列表,它会很好地保存其他属性(name),但类型计数为零–出于某种原因,它没有保存它们


有什么想法吗?

这是未分配和初始化数组的典型症状-因此它保持为
nil
,并忽略所有发送的消息。Alloc init它会起作用。

你在哪里声明和初始化你的数组?@DrummerB显然哪里都没有。我已经做了obj-c有一段时间了,但你确定currentGame.GameGenes没有设置为nil吗?@H2CO3它可能是currentGame的一个属性,但没有正确初始化或根本没有初始化。(^^这已经被问了很多次。这不应该被问,甚至一次也不应该被问。)我已经对对象本身进行了alloc init,我是否也需要初始化它的所有属性?如果是,我应该在对象类的init方法中这样做吗?是的,你需要alloc/init你的属性,通常你在init方法中这样做。@lukech如果你对一个对象进行alloc init,所有的实例变量都将被初始化ed归零…现在阅读Objective-C运行时参考。归零,还是归零?没有区别吗?告诉某个对象它是零,而不告诉它它它是什么?@lukech因为Objective-C对象是指针,所以将它们设置为零意味着将它们设置为零(显然在我所知道的所有体系结构上,Objective-C都受支持)。是的,0不一定是NULL或nil指针的实际数值,但C标准规定,对于指针,当编译器遇到与0的比较时,它应该与NULL进行比较(反过来,在Objective-C中为nil)。
// 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);
    }
}
// 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];