Swift 选择器 - 无法识别的选择器发送到实例
Swift selector - unrecognized selector sent to instance
我有一个功能:
func runRockRotation(rockSprite: SKSpriteNode){
startRockRotationAnimation(rockSprite, isRock: true)
}
当我这样称呼它时:
runRockRotation(rock)
它可以工作,但我似乎无法将它放入 NSTimer 选择器中。
var timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "runRockRotation:", userInfo: rock, repeats: false)
看了很多论坛,试过这个:
var timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "runRockRotation()", userInfo: rock, repeats: false)
var timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "runRockRotation(_)", userInfo: rock, repeats: false)
var timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "runRockRotation", userInfo: rock, repeats: false)
此外,尝试过不使用 rock,使用 nil,但似乎没有任何效果。
每次我得到:
2015-04-10 15:49:03.830 Meh[1640:218030] -[__NSCFTimer runAction:completion:]: unrecognized selector sent to instance 0x174166840
2015-04-10 15:49:03.832 Meh[1640:218030] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFTimer runAction:completion:]: unrecognized selector sent to instance 0x174166840'
如何在带有参数的选择器中调用我的函数?我知道如何在 Objective C 中做到这一点,但似乎无法在 Swift 中做到这一点。我们将不胜感激。
查看 scheduledTimerWithTimeInterval
的文档
你会看到
The selector should have the following signature: timerFireMethod:
(including a colon to indicate that the method takes an argument).
The
timer passes itself as the argument, thus the method would adopt the
following pattern:
- (void)timerFireMethod:(NSTimer *)timer
您的函数与此模式不匹配,因此找不到合适的选择器。
使用类似 -
func runRockRotationForTimer(_ timer: NSTimer){
self.runRockRotation(timer.userInfo as? SKSpriteNode)
timer.invalidate();
}
并使用
安排
var timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "runRockRotationForTimer:", userInfo: rock, repeats: false)
还有助于确保目标对象(在您的情况下为 self)是 NSObject
的子类
var timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: <THIS MUST BE NSOBJECT SUBCLASS>, selector: "runRockRotation:", userInfo: rock, repeats: false)
我有一个功能:
func runRockRotation(rockSprite: SKSpriteNode){
startRockRotationAnimation(rockSprite, isRock: true)
}
当我这样称呼它时:
runRockRotation(rock)
它可以工作,但我似乎无法将它放入 NSTimer 选择器中。
var timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "runRockRotation:", userInfo: rock, repeats: false)
看了很多论坛,试过这个:
var timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "runRockRotation()", userInfo: rock, repeats: false)
var timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "runRockRotation(_)", userInfo: rock, repeats: false)
var timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "runRockRotation", userInfo: rock, repeats: false)
此外,尝试过不使用 rock,使用 nil,但似乎没有任何效果。
每次我得到:
2015-04-10 15:49:03.830 Meh[1640:218030] -[__NSCFTimer runAction:completion:]: unrecognized selector sent to instance 0x174166840
2015-04-10 15:49:03.832 Meh[1640:218030] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFTimer runAction:completion:]: unrecognized selector sent to instance 0x174166840'
如何在带有参数的选择器中调用我的函数?我知道如何在 Objective C 中做到这一点,但似乎无法在 Swift 中做到这一点。我们将不胜感激。
查看 scheduledTimerWithTimeInterval
你会看到
The selector should have the following signature:
timerFireMethod:
(including a colon to indicate that the method takes an argument).The timer passes itself as the argument, thus the method would adopt the following pattern:
- (void)timerFireMethod:(NSTimer *)timer
您的函数与此模式不匹配,因此找不到合适的选择器。
使用类似 -
func runRockRotationForTimer(_ timer: NSTimer){
self.runRockRotation(timer.userInfo as? SKSpriteNode)
timer.invalidate();
}
并使用
安排var timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "runRockRotationForTimer:", userInfo: rock, repeats: false)
还有助于确保目标对象(在您的情况下为 self)是 NSObject
的子类var timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: <THIS MUST BE NSOBJECT SUBCLASS>, selector: "runRockRotation:", userInfo: rock, repeats: false)