Как исправить «Несовместимые типы указателей, отправляющие void (void)» параметру типа «SEL _Nonnull» »в NSTimer - PullRequest
1 голос
/ 25 марта 2019

В Objective C я пытаюсь создать метод, который вызывается в селекторе NSTimer в конструкторе. Тем не менее, я получаю эту ошибку, которая приводит к сбою моего приложения при сборке:

Несовместимые типы указателей, отправляющие «void (void)» параметру типа «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 ++;
    }
}

1 Ответ

4 голосов
/ 25 марта 2019

Вы должны создать @selector

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