0

私は目的のCで関数を書いています.それは私が得たものです:

int rndValue = (((int)arc4random()/0x100000000)*width);
timer1 = [NSTimer scheduledTimerWithTimeInterval:.01 
                                          target:self 
                           [self performSelector:@selector(doItAgain1:)  
                                      withObject:rndValue] 
                                        userInfo:nil
                                         repeats:YES];

セレクターはこのメソッドを呼び出し、パラメーターを渡します。

-(void)doItAgain1:(int)xValuex{
}

この段階で、トップ コードは構文エラーを生成します。Syntax error: 'Expected ] before performSelector'問題は何ですか?よろしくお願いします

4

2 に答える 2

2

その行はおそらく読むべきです

timer1 = [NSTimer scheduledTimerWithTimeInterval:.01 
         target:self selector:@selector(doItAgain1:) 
         userInfo:nil repeats:YES];

この呼び出しでメソッド引数を送信することはできません。そのためには、次のようにする必要があります。

NSInvocation *inv = [NSInvocation invocationWithMethodSignature:
    [self methodSignatureForSelector:@selector(doItAgain1:)]];

[inv setSelector:@selector(doItAgain1:)];
[inv setTarget:self];
[inv setArgument:&rndValue atIndex:2];

timer1 = [NSTimer scheduledTimerWithTimeInterval:(NSTimeInterval).01 
         invocation:inv 
         repeats:YES];
于 2013-01-25T16:56:39.673 に答える
1

これはより正しいでしょう:

[NSTimer scheduledTimerWithTimeInterval:.01 target:self 
                 selector:@selector(doItAgain1:)
                 userInfo:[NSNumber numberWithInt:rndValue] repeats:YES];

また、この方法で呼び出すセレクターの構文は次のとおりである必要があることに注意してください。

- (void)doItAgain1:(NSTimer*)timer {

   int rndValue = [timer.userInfo intValue];
   ...
}

このようなタイマーセレクターに引数を指定することはできません。したがって、それをオブジェクトintに変換するためのトリックです。NSNumber

于 2013-01-25T16:54:32.513 に答える