使用#selector swift3 时出现 NSInvalidArgument 异常
NSInvalidArgument exception when using #selector swift3
当我运行函数
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:
terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFBoolean shootBullet]: unrecognized selector sent to instance ...
以上两个函数都在 Swift3 中 SKScene 运行ning 的子类中。
值得注意的是,与 #selector(test)
不同,我没有遇到任何编译时错误
您在将 target
设置为 Boolean
值时犯了错误。
target
is the object to which to send the message specified by aSelector when the timer fires. The timer maintains a strong reference to target until it (the timer) is invalidated.
因此,如果 class 中存在您安排 Timer
.
的方法,则只需将 target
设置为 self
let shootTimer = Timer.scheduledTimer(timeInterval: bulletInfo.frequency, target: self, selector: #selector(shootBullet), userInfo: nil, repeats: true)
当我运行函数
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:
terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFBoolean shootBullet]: unrecognized selector sent to instance ...
以上两个函数都在 Swift3 中 SKScene 运行ning 的子类中。
值得注意的是,与 #selector(test)
您在将 target
设置为 Boolean
值时犯了错误。
target
is the object to which to send the message specified by aSelector when the timer fires. The timer maintains a strong reference to target until it (the timer) is invalidated.
因此,如果 class 中存在您安排 Timer
.
target
设置为 self
let shootTimer = Timer.scheduledTimer(timeInterval: bulletInfo.frequency, target: self, selector: #selector(shootBullet), userInfo: nil, repeats: true)