Ios 错误:将Objective-C指针隐式转换为';选择非空';NSTimer选择器中的圆弧不允许
我有这样一种方法:Ios 错误:将Objective-C指针隐式转换为';选择非空';NSTimer选择器中的圆弧不允许,ios,objective-c,nstimer,Ios,Objective C,Nstimer,我有这样一种方法: -(void)fastTapCartBack:(NSString*)ActionType self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:[self performSelector:@selector(fastTapCartBack:) withObject:@"FAST"] userInfo:nil repeats:NO] 我想在NSTimer中将其用作选择器,如下所
-(void)fastTapCartBack:(NSString*)ActionType
self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:[self performSelector:@selector(fastTapCartBack:) withObject:@"FAST"] userInfo:nil repeats:NO]
我想在NSTimer中将其用作选择器,如下所示:
-(void)fastTapCartBack:(NSString*)ActionType
self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:[self performSelector:@selector(fastTapCartBack:) withObject:@"FAST"] userInfo:nil repeats:NO]
但它有一个错误:
ARC不允许将Objective-C指针隐式转换为“SEL\u Nonnull”您正在传递一个方法调用
[self-performSelector:@selector(fastTapCartBack:)with Object:@“FAST”]
作为选择器,Objective-C不允许这样做
替换此
self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:[self performSelector:@selector(fastTapCartBack:) withObject:@"FAST"] userInfo:nil repeats:NO];
通过此
self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:[self performSelector:@selector(fastTapCartBack:) withObject:@"FAST"] userInfo:nil repeats:NO];
您应该使用NSInvocation方式
NSMethodSignature * signature = [ViewController instanceMethodSignatureForSelector:@selector(fastTapCartBack:)];
NSInvocation * invocation = [NSInvocation
invocationWithMethodSignature:signature];
[invocation setTarget:self];
[invocation setSelector:@selector(fastTapCartBack:)];
NSString * argument = @"FAST";
[invocation setArgument:&argument atIndex:2];
self.timer2 = [NSTimer scheduledTimerWithTimeInterval:1 invocation:invocation repeats:NO];
不能在目标/操作模式中传递第二个参数 但是,对于
NSTimer
有一个非常合适的解决方案,userInfo
参数
self.timer = [NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:@selector(fastTapCartBack:)
userInfo:@"FAST"
repeats:NO];
并在选择器方法中获取信息
-(void)fastTapCartBack:(NSTimer *)timer {
NSString *info = (NSString *)timer.userInfo;
}
是的,它是有效的,但我需要定义我方法的第二个参数。如何用指定的第二个参数定义我的选择器?@SaMiGiMiX你测试过我的答案吗?使用
NSInvocation
?是的,但在NSInvocation*调用上有错误和线程断点