【问题标题】:Error : Implicit conversion of an Objective-C pointer to 'SEL _Nonnull' is disallowed with ARC in Selector Of NSTimer错误:在 NSTimer 的选择器中不允许使用 ARC 将 Objective-C 指针隐式转换为“SEL _Nonnull”
【发布时间】:2018-02-22 09:14:23
【问题描述】:

我有这样的方法:

-(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”

【问题讨论】:

    标签: ios objective-c nstimer


    【解决方案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;
    }
    

    【讨论】:

      【解决方案2】:

      你正在传递一个方法调用 [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];
      

      【讨论】:

      • 是的,它有效,但我需要定义我的方法的第二个参数。如何用指定的第二个参数定义我的选择器?
      • @SaMiGiMiX 你测试我的答案了吗?使用NSInvocation?
      • 是的,但是在 NSInvocation * 调用上有错误和线程断点
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-15
      • 2012-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多