奇怪的NSMutable Array错误

时间:2012-04-14 18:57:03

标签: iphone objective-c ios arrays nsmutablearray

我只是想在我的应用程序运行时调用的(id)类型函数中定义的'score'nsmutable数组中更新我的分数。所以我不知道出了什么问题,我在函数体之前插入了一个NSLog,之后检查发生了什么,我会在代码之后发布它们。

-(void)changeScore:(int)moves AtLevel:(int)theLevel{
for (int i = 0; i < 20; i++) {
    NSLog(@"At level %d we have a score of: %d ", i+1, [[scores objectAtIndex:i] intValue]);
}
if (theLevel <= 9){
    NSNumber* number = [[NSNumber alloc]initWithInt:50 - moves];
    [scores replaceObjectAtIndex:theLevel - 1 withObject:number];
    NSLog(@"this is number's int value: %d and this is thelevel - 1: %d", [number intValue], theLevel - 1);
}
if (theLevel > 9 && theLevel <= 19){
    NSNumber* number = [[NSNumber alloc]initWithInt:150-moves];
    [scores replaceObjectAtIndex:theLevel - 1 withObject:number];
}
for (int i = 0; i < 20; i++) {
    NSLog(@"At level %d we have a score of: %d", i+1, [[scores objectAtIndex:i] intValue]);
}
}

并且NSLog产生了这个:

2012-04-14 14:53:49.018 Arctic Defender[2068:f803] At level 1 we have a score of: 0 
2012-04-14 14:53:49.022 Arctic Defender[2068:f803] At level 2 we have a score of: 0 
2012-04-14 14:53:49.023 Arctic Defender[2068:f803] At level 3 we have a score of: 0 
2012-04-14 14:53:49.024 Arctic Defender[2068:f803] At level 4 we have a score of: 0 
2012-04-14 14:53:49.026 Arctic Defender[2068:f803] At level 5 we have a score of: 0 
2012-04-14 14:53:49.027 Arctic Defender[2068:f803] At level 6 we have a score of: 0 
2012-04-14 14:53:49.028 Arctic Defender[2068:f803] At level 7 we have a score of: 0 
2012-04-14 14:53:49.029 Arctic Defender[2068:f803] At level 8 we have a score of: 0 
2012-04-14 14:53:49.030 Arctic Defender[2068:f803] At level 9 we have a score of: 0 
2012-04-14 14:53:49.031 Arctic Defender[2068:f803] At level 10 we have a score of: 0 
2012-04-14 14:53:49.032 Arctic Defender[2068:f803] At level 11 we have a score of: 0 
2012-04-14 14:53:49.033 Arctic Defender[2068:f803] At level 12 we have a score of: 0 
2012-04-14 14:53:49.035 Arctic Defender[2068:f803] At level 13 we have a score of: 0 
2012-04-14 14:53:49.036 Arctic Defender[2068:f803] At level 14 we have a score of: 0 
2012-04-14 14:53:49.101 Arctic Defender[2068:f803] At level 15 we have a score of: 0 
2012-04-14 14:53:49.102 Arctic Defender[2068:f803] At level 16 we have a score of: 0 
2012-04-14 14:53:49.103 Arctic Defender[2068:f803] At level 17 we have a score of: 0 
2012-04-14 14:53:49.104 Arctic Defender[2068:f803] At level 18 we have a score of: 0 
2012-04-14 14:53:49.104 Arctic Defender[2068:f803] At level 19 we have a score of: 0 
2012-04-14 14:53:49.105 Arctic Defender[2068:f803] At level 20 we have a score of: 0 
2012-04-14 14:53:49.106 Arctic Defender[2068:f803] this is number's int value: 38 and this is thelevel - 1: 0
2012-04-14 14:53:49.111 Arctic Defender[2068:f803] At level 1 we have a score of: 0
2012-04-14 14:53:49.112 Arctic Defender[2068:f803] At level 2 we have a score of: 0
2012-04-14 14:53:49.113 Arctic Defender[2068:f803] At level 3 we have a score of: 0
2012-04-14 14:53:49.114 Arctic Defender[2068:f803] At level 4 we have a score of: 0
2012-04-14 14:53:49.114 Arctic Defender[2068:f803] At level 5 we have a score of: 0
2012-04-14 14:53:49.135 Arctic Defender[2068:f803] At level 6 we have a score of: 0
2012-04-14 14:53:49.136 Arctic Defender[2068:f803] At level 7 we have a score of: 0
2012-04-14 14:53:49.137 Arctic Defender[2068:f803] At level 8 we have a score of: 0
2012-04-14 14:53:49.138 Arctic Defender[2068:f803] At level 9 we have a score of: 0
2012-04-14 14:53:49.157 Arctic Defender[2068:f803] At level 10 we have a score of: 0
2012-04-14 14:53:49.159 Arctic Defender[2068:f803] At level 11 we have a score of: 0
2012-04-14 14:53:49.160 Arctic Defender[2068:f803] At level 12 we have a score of: 0
2012-04-14 14:53:49.160 Arctic Defender[2068:f803] At level 13 we have a score of: 0
2012-04-14 14:53:49.163 Arctic Defender[2068:f803] At level 14 we have a score of: 0
2012-04-14 14:53:49.165 Arctic Defender[2068:f803] At level 15 we have a score of: 0
2012-04-14 14:53:49.166 Arctic Defender[2068:f803] At level 16 we have a score of: 0
2012-04-14 14:53:49.167 Arctic Defender[2068:f803] At level 17 we have a score of: 0
2012-04-14 14:53:49.169 Arctic Defender[2068:f803] At level 18 we have a score of: 0
2012-04-14 14:53:49.170 Arctic Defender[2068:f803] At level 19 we have a score of: 0
2012-04-14 14:53:49.171 Arctic Defender[2068:f803] At level 20 we have a score of: 0

编辑:这是我的积分代码:

-(id)initNewScores{
//Initalize 20 levels
for (int i = 0; i < 20; i++) {
    NSNumber* numb = [[NSNumber alloc] initWithInt:0];
    [scores addObject:numb];
    [numb release];
}
return self;
}

1 个答案:

答案 0 :(得分:4)

在该方法的条目上显示scores的值。我打赌它是nil。即无论你用什么来初始化那个可变数组都不起作用。