我正在努力将最简单的计时器实现到我的项目中,因为我尝试的每一个都给了我这个错误日志:
2015-12-19 14:23:47.164 Reanimate[14879:898570] -[Reanimate.helicut
timerFunc]: unrecognized selector sent to instance 0x7ff95d29d3f0
2015-12-19 14:23:47.169 Reanimate[14879:898570] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Reanimate.helicut timerFunc]: unrecognized selector sent to instance 0x7ff95d29d3f0'
*** First throw call stack:
(
0 CoreFoundation 0x0000000105dfef65 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x0000000107e88deb objc_exception_throw + 48
2 CoreFoundation 0x0000000105e0758d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x0000000105d54f7a ___forwarding___ + 970
4 CoreFoundation 0x0000000105d54b28 _CF_forwarding_prep_0 + 120
5 Foundation 0x0000000106408671 __NSFireTimer + 83
6 CoreFoundation 0x0000000105d5f364 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 20
7 CoreFoundation 0x0000000105d5ef11 __CFRunLoopDoTimer + 1089
8 CoreFoundation 0x0000000105d208b1 __CFRunLoopRun + 1937
9 CoreFoundation 0x0000000105d1fe98 CFRunLoopRunSpecific + 488
10 GraphicsServices 0x000000010bfc7ad2 GSEventRunModal + 161
11 UIKit 0x0000000106a11676 UIApplicationMain + 171
12 Reanimate 0x0000000105be62dd main + 109
13 libdyld.dylib 0x0000000108a1892d start + 1
14 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
以下是代码本身:
import Foundation
import SpriteKit
import UIKit
var heli: SKSpriteNode?
var helibg1: SKSpriteNode?
var helibg2: SKSpriteNode?
weak var myTypeWriter: SKLabelNode?
var text = String()
var text2 = String()
var text3 = String()
var text4 = String()
var texty = String()
var myCounter = 0
class helicut: SKScene{
var timer = NSTimer()
override func didMoveToView(view: SKView) {
heli = self.childNodeWithName("heli") as? SKSpriteNode
helibg1 = self.childNodeWithName("helibg1") as? SKSpriteNode
helibg2 = self.childNodeWithName("helibg2") as? SKSpriteNode
myTypeWriter = self.childNodeWithName("type") as? SKLabelNode
func timerr() {
NSTimer.scheduledTimerWithTimeInterval(2.0, target: self, selector: "timerFunc", userInfo: nil, repeats: false)
}
func timerFunc() {
print("it worked")
}
timerr()
正如您所看到的,代码相对简单,但即便如此,我也不知道可能导致异常的原因。我尝试过使用不同的选择器语法,并且几乎所有东西都搞乱了,但我很困惑。非常感谢帮助。
答案 0 :(得分:3)
将timerr()
和timerFunc()
放在didMoveToView()
之外。
匹配selector
的函数必须在类的顶层定义。
class helicut: SKScene {
var timer = NSTimer()
override func didMoveToView(view: SKView) {
heli = self.childNodeWithName("heli") as? SKSpriteNode
helibg1 = self.childNodeWithName("helibg1") as? SKSpriteNode
helibg2 = self.childNodeWithName("helibg2") as? SKSpriteNode
myTypeWriter = self.childNodeWithName("type") as? SKLabelNode
timerr()
}
func timerr() {
NSTimer.scheduledTimerWithTimeInterval(2.0, target: self, selector: "timerFunc", userInfo: nil, repeats: false)
}
func timerFunc() {
print("it worked")
}