【问题标题】:How to fix " Incompatible pointer types sending 'void (void)' to parameter of type 'SEL _Nonnull' " in NSTimer如何在 NSTimer 中修复“不兼容的指针类型将 'void (void)' 发送到 'SEL _Nonnull' 类型的参数”
【发布时间】:2019-03-25 19:19:49
【问题描述】:

在 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 ++;
    }
}

【问题讨论】:

    标签: objective-c nstimer


    【解决方案1】:

    你必须创建一个@selector

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多