Iphone 可变数组错误
我只是想更新“score”nsmutable数组中的分数,该数组是在我的应用程序运行时调用的(id)类型函数中定义的。所以我不知道哪里出了问题,我在函数体之前和之后都放了一个NSLog来检查发生了什么,我会在代码之后发布它们Iphone 可变数组错误,iphone,objective-c,ios,arrays,nsmutablearray,Iphone,Objective C,Ios,Arrays,Nsmutablearray,我只是想更新“score”nsmutable数组中的分数,该数组是在我的应用程序运行时调用的(id)类型函数中定义的。所以我不知道哪里出了问题,我在函数体之前和之后都放了一个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 ",
-(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]);
}
}
编辑:这是我的分数初始化代码:
-(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;
}
-(id)initNewScores{
//初始化20个级别
对于(int i=0;i<20;i++){
NSNumber*numb=[[NSNumber alloc]initWithInt:0];
[对象:麻木];
[麻木释放];
}
回归自我;
}
在输入该方法时显示分数的值。我打赌它是nil
。也就是说,你用来初始化可变数组的任何东西都不起作用。在输入该方法时显示分数的值。我打赌它是nil
。也就是说,无论您使用什么来初始化可变数组都无法工作。但我只是在所有20个索引上调用了objectAtIndex,如果它是nil,我不会得到一个错误吗?不,发送到nil的消息会导致nil。如果不是这样的话,所有的Cocoa都会在一个大的FireballeElementary错误中爆炸,即使是有经验的程序员也会这样做,忘记初始化他们的数组。。。始终首先寻找简单的解决方案!(myArray=[[NSMutableArray alloc]init])@PeterP在Cocoa中,发送到nil
的消息不会返回错误-它们通常只是没有操作,并且总是返回'falsy'值(nil
,0
,no
)。消息nil
是Cocoa的一项功能,不会抛出异常或类似的东西。感谢各位,只是分配和初始化我的数组起了作用,谢谢,我完全没有想到我的数组是nil,因为它应该初始化为所有0。谢谢,但我刚刚在所有20个索引上调用了objectAtIndex,如果它是nil,我不会得到一个错误吗?不,发送到nil的消息会导致nil。如果不是这样的话,所有的Cocoa都会在一个大的FireballeElementary错误中爆炸,即使是有经验的程序员也会这样做,忘记初始化他们的数组。。。始终首先寻找简单的解决方案!(myArray=[[NSMutableArray alloc]init])@PeterP在Cocoa中,发送到nil
的消息不会返回错误-它们通常只是没有操作,并且总是返回'falsy'值(nil
,0
,no
)。消息nil
是Cocoa的一项功能,不会抛出异常或类似的东西。感谢各位,只是分配和初始化我的数组起了作用,谢谢,我完全没有想到我的数组是nil,因为它应该初始化为所有0。谢谢