Search code examples
objective-cnstimer

How to fix " Incompatible pointer types sending 'void (void)' to parameter of type 'SEL _Nonnull' " in NSTimer


In Objective C, I am trying to create the method that it's called at the NSTimer selector within the constructor. However, I'm getting this error which causing my app to fail when I build it:

Incompatible pointer types sending 'void (void)' to parameter of type 'SEL _Nonnull'

@implementation Employee
void timerMethod(void);
- (id)init {
    self = [super init];
    if (self) {
        _person = [[Person alloc] init];
        _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:(timerMethod) userInfo:nil repeats:YES];
    }
    return self;
}

int counter = 0;
int timeUp = 10;
- (void) timerMethod {
    counter += 1;
    while (counter != timeUp) {
        NSLog(@"A new person has been added");
        [_timer invalidate];
        counter ++;
    }
}

Solution

  • You have to create a @selector

    _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerMethod) userInfo:nil repeats:YES];