1

NSRangeExceptionしようとするとエラーが発生しremoveObjectAt:0ます。それがどのように可能かさえわかりません。

-(void)removePoints:(ccTime *)tm
{

    NSLog(@"naughty %@",_naughtytoucharray);
    if ([_naughtytoucharray count]>0)
    {
        [_naughtytoucharray removeObjectAtIndex:0];
    }
}

ログに表示されます:

[4195:c07] naughty ( "{124, 98}", "{123, 98}", "{135, 97}", "{124, 98}", "{148, 94}", "{135, 97}", "{157, 93}", "{148, 94}", "{162, 92}", "{157, 93}", "{164, 92}", "{162, 92}" )

次に、次のエラーが表示されます。 *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 11 beyond bounds [0 .. 10]'

ブレークポイントを設定すると、すべてのオブジェクトが0x0

ここに画像の説明を入力

NSMutableArray割り当てられ、初期化されinit、入力されますccTouchesMoved

-(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event  {


    if (isSelected) {

        UITouch *touch = [ touches anyObject];
        CGPoint new_location = [touch locationInView: [touch view]];
        new_location = [[CCDirector sharedDirector] convertToGL:new_location];


        CGPoint oldTouchLocation = [touch previousLocationInView:touch.view];
        oldTouchLocation = [[CCDirector sharedDirector] convertToGL:oldTouchLocation];
        oldTouchLocation = [self convertToNodeSpace:oldTouchLocation];

        // add my touches to the naughty touch array
        [_naughtytoucharray addObject:NSStringFromCGPoint(new_location)];
        [_naughtytoucharray addObject:NSStringFromCGPoint(oldTouchLocation)];

    }
}

配列の最初の項目を削除できない理由は何ですか?

4

1 に答える 1

5

それはあなたのエラーがどこから来ているのかではありません。不平を言っているメソッドはobjectAtIndex:ではなくremoveObjectAtIndex:、エラーの原因となっているインデックスはゼロではなく11です。

配列からの読み取り中に、配列内に少なくとも12個のオブジェクトがあると想定しているコード内の場所を探します。11個しかなく、それらのインデックスは0から10です。

于 2012-11-26T21:32:34.033 に答える