如何在NSTimer中修复“将'void(void)'发送到'SEL _Nonnull'类型的参数的不兼容指针类型“

时间:2019-03-25 19:19:50

标签: objective-c nstimer

在目标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 个答案:

答案 0 :(得分:4)

您必须创建一个@selector

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