如何在 NSTimer 中修复“将 'void (void)' 发送到 'SEL _Nonnull' 类型的参数的不兼容指针类型”
How to fix " Incompatible pointer types sending 'void (void)' to parameter of type 'SEL _Nonnull' " in NSTimer
在 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 ++;
}
}
您必须创建一个 @selector
_timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerMethod) userInfo:nil repeats:YES];
在 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 ++;
}
}
您必须创建一个 @selector
_timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerMethod) userInfo:nil repeats:YES];