【问题标题】:NSInvalidArgument exception when using #selector swift3使用#selector swift3时出现NSInvalidArgument异常
【发布时间】:2016-11-19 12:46:40
【问题描述】:

当我运行函数时

func makeSpriteShoot(bullets bulletInfo:MHBulletInformation,player playerSprite:SKSpriteNode){
    print("Foo")
    let shootTimer = Timer.scheduledTimer(timeInterval: bulletInfo.frequency, target: true, selector: #selector(shootBullet), userInfo: nil, repeats: true)
}
func shootBullet(){
    player.shootBullet(angle: 90)//player is a instance of a subclass of SKSpriteNode
}

我得到以下异常+SIGABRT:

由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[__NSCFBoolean shootBullet]:无法识别的选择器已发送到实例...

上述两个函数都在 Swift3 中运行的 SKScene 的子类中。

值得注意的是,与 #selector(test) 不同,我没有收到任何编译时错误

【问题讨论】:

    标签: swift swift3 selector nstimer unrecognized-selector


    【解决方案1】:

    您错误地将target 设置为Boolean 值。

    target 是在计时器触发时将由 aSelector 指定的消息发送到的对象。计时器保持对目标的强引用,直到它(计时器)失效。

    因此,如果您安排 Timer 的类中存在方法,则只需将 target 设置为 self。

    let shootTimer = Timer.scheduledTimer(timeInterval: bulletInfo.frequency, target: self, selector: #selector(shootBullet), userInfo: nil, repeats: true)
    

    【讨论】:

      猜你喜欢
      • 2018-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-15
      • 2015-02-05
      • 2014-08-06
      • 1970-01-01
      相关资源
      最近更新 更多