[英]Error : Implicit conversion of an Objective-C pointer to 'SEL _Nonnull' is disallowed with ARC in Selector Of NSTimer
I have a method like this : 我有这样的方法:
-(void)fastTapCartBack:(NSString*)ActionType
And I want to use it as selector in NSTimer like this : 我想像这样在NSTimer中将其用作选择器:
self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:[self performSelector:@selector(fastTapCartBack:) withObject:@"FAST"] userInfo:nil repeats:NO]
But It have Error : 但是有错误:
Implicit conversion of an Objective-C pointer to 'SEL _Nonnull' is disallowed with ARC ARC不允许将Objective-C指针隐式转换为'SEL _Nonnull'
You are passing a method call [self performSelector:@selector(fastTapCartBack:) withObject:@"FAST"]
as selector, this is not allowed by Objective-C 您正在传递一个方法调用
[self performSelector:@selector(fastTapCartBack:) withObject:@"FAST"]
作为选择器,Objective-C不允许这样做
replace this 取代这个
self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:[self performSelector:@selector(fastTapCartBack:) withObject:@"FAST"] userInfo:nil repeats:NO];
By this 这样
You should use NSInvocation way 您应该使用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];
You cannot pass a second parameter in the target / action pattern. 您无法在目标/操作模式中传递第二个参数。
However in case of NSTimer
there is a very suitable solution, the userInfo
parameter 但是,对于
NSTimer
有一个非常合适的解决方案, userInfo
参数
self.timer = [NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:@selector(fastTapCartBack:)
userInfo:@"FAST"
repeats:NO];
and get the information in the selector method 并在选择器方法中获取信息
-(void)fastTapCartBack:(NSTimer *)timer {
NSString *info = (NSString *)timer.userInfo;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.