的NSTimer引发异常

问题描述:

我目前正在努力实现连最简单的定时器到我的项目,因为每一个我试图给我这个错误日志:的NSTimer引发异常

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() 

正如你所看到的代码是相对简单的,但即使如此,我不知道是什么可能导致异常。我尝试过使用不同的选择器语法,并且处理所有事情,但我很困难。帮助将不胜感激。

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") 
    }