gpt4 book ai didi

swift - 使用 #selector swift3 时出现 NSInvalidArgument 异常

转载 作者:行者123 更新时间:2023-11-30 12:53:25 25 4
gpt4 key购买 nike

当我运行该函数时

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 的子类中。

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

最佳答案

您在使用 Boolean 值设置 target 时犯了错误。

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.

因此,如果您调度 Timer 的类中存在方法,只需将 target 设置为 self 即可。

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

关于swift - 使用 #selector swift3 时出现 NSInvalidArgument 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40689232/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com