Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.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 初始化带有整数的NSMutableArray&;某种不需要的类型转换_Objective C_Nsmutablearray - Fatal编程技术网

Objective c 初始化带有整数的NSMutableArray&;某种不需要的类型转换

Objective c 初始化带有整数的NSMutableArray&;某种不需要的类型转换,objective-c,nsmutablearray,Objective C,Nsmutablearray,我想初始化一个带有8个整数的NSMutableArray。我在SO档案中发现的东西让我觉得很疯狂,但它似乎确实有效: _noClicks = [[NSMutableArray alloc] initWithObjects:[NSNumber numberWithUnsignedInteger:1], [NSNumber numberWithUnsignedInteger:1], [NSNumber numberWithUnsignedInteger:1], [NSNumber numberWit

我想初始化一个带有8个整数的NSMutableArray。我在SO档案中发现的东西让我觉得很疯狂,但它似乎确实有效:

_noClicks = [[NSMutableArray alloc] initWithObjects:[NSNumber numberWithUnsignedInteger:1], [NSNumber numberWithUnsignedInteger:1], [NSNumber numberWithUnsignedInteger:1], [NSNumber numberWithUnsignedInteger:1], [NSNumber numberWithUnsignedInteger:1], [NSNumber numberWithUnsignedInteger:1], [NSNumber numberWithUnsignedInteger:1], [NSNumber numberWithUnsignedInteger:1], [NSNumber numberWithUnsignedInteger:1],nil];
我知道Objective-c不是一种面向科学的语言,但肯定有更好的方法(我想这就是问题1)。顺便说一下,这段代码位于名为GameData的单例的
init
方法中,并与其他数组一起放入字典中

稍后,我使用这个整数数组,如下所示:

- (IBAction)buttonOneWasTouched:(id)sender {

    NSUInteger CAP = [[GameData gameData] getCurAnsPos];
    NSLog(@"%i", CAP); // CAP is current answer position and this seems to work
    NSMutableArray *noClks = [[GameData gameData].curData valueForKey:@"noClicks"];
    NSLog(@"%@", noClks); // gives the expected array of 1's
    NSLog(@"%@", [noClks class]); // _NSArrayM OK I think
    NSUInteger nC = [noClks objectAtIndex:CAP];
    NSLog(@"%i", nC); // This gives a crazy answer: 109613120
}
所以这里有点不对劲;为
nC
记录的巨大数字向我表明它已被存储为非无符号整数,但我不明白为什么。这就是问题2


最后,这可能就是线索,编译器给出了一个警告
指向整数转换的不兼容指针,初始化“NSUInteger”(也称为“unsigned int”)和类型为“id”的表达式位于第二行到最后一行,但奇怪的是,第一行具有相同的结构,但没有给出错误。这一定很简单,我最近所有的问题都很简单。蒂亚

对于第一个问题,我建议

_noClicks = [[NSMutableArray alloc] initWithCapacity:n];

for(int i=0;i<n;i++)
    [_noClicks addObject:[NSNumber numberWithInt:1]];


非常感谢。我本应该能够弄清楚循环过程。至于第二个问题,我认为索引处的对象将是一个整数,因为我将这样的at对象放在数组中。你能告诉我为什么这种想法是错误的吗?非常感谢。您没有在数组中放入NSInteger,而是在数组中放入了NSNumber,因此您应该像这样将其取出:NSInteger nC=[[noClks objectAtIndex:CAP]integerValue];
NSNumber *nC = [noClks objectAtIndex:CAP];
NSUInteger nC = [[noClks objectAtIndex:CAP] intValue];