错误:NSTimer选择器中的ARC禁止将Objective-C指针隐式转换为“SEL _Nonnull”

时间:2018-02-22 09:14:23

标签: ios objective-c nstimer

我有这样的方法:

-(void)fastTapCartBack:(NSString*)ActionType

我想在NSTimer中使用它作为选择器,如下所示:

self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:[self performSelector:@selector(fastTapCartBack:) withObject:@"FAST"] userInfo:nil repeats:NO]

但它有错误:

ARC禁止将Objective-C指针隐式转换为'SEL _Nonnull'

2 个答案:

答案 0 :(得分:1)

您正在传递方法调用[self performSelector:@selector(fastTapCartBack:) withObject:@"FAST"]作为选择器,Objective-C

不允许这样做

替换此

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];

答案 1 :(得分:1)

您无法在目标/操作模式中传递第二个参数。

但是在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;
}