Search code examples
objective-cnstimernsinteger

Pass integer in userInfo of NSTimer


I'm trying to pass an integer (testInt) through the userInfo field of NSTimer

timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(count:) userInfo:testInt repeats:YES];

However I'm getting an incompatible types error message.

Does anyone know how to pass a number through to the count method?


Solution

  • You need to box it to an NSNumber:

    timer = [NSTimer scheduledTimerWithTimeInterval:1.0 
                                             target:self 
                                           selector:@selector(count:) 
                                           userInfo:@(testInt)  // <-- @() around your int.
                                            repeats:YES];
    

    Then in -count:

    int testInt = [timer.userInfo intValue];