Pass integer in userInfo of NSTimer

1.3k views Asked by At

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?

1

There are 1 answers

0
i_am_jorf On BEST ANSWER

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];