Error : Implicit conversion of an Objective-C pointer to 'SEL _Nonnull' is disallowed with ARC in Selector Of NSTimer

Error : Implicit conversion of an Objective-C pointer to 'SEL _Nonnull' is disallowed with ARC in Selector Of 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'

您正在将方法调用 [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];

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

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