3

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?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
thefan12345
  • 136
  • 16

1 Answers1

6

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];
i_am_jorf
  • 53,608
  • 15
  • 131
  • 222