Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 669
thefan12345 Avatar asked Dec 04 '25 17:12

thefan12345


1 Answers

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];
like image 148
i_am_jorf Avatar answered Dec 06 '25 08:12

i_am_jorf