无法识别的select器发送到实例NSTimer Swift

我正在尝试开发一款包含简单秒表function的应用程序。 我正在使用Xcode 6和Swift语言。 这是FirstViewController中的代码

@IBAction func Stopwatch (Sender:UIButton) { var startTime = NSTimeInterval() func updateTime(timer:NSTimer) { //Find Current TIme var currentTime = NSDate.timeIntervalSinceReferenceDate() //Find the difference between current time and start time var elapsedTime :NSTimeInterval = currentTime - startTime //calculate the minutes in elapsed time. let minutes = UInt(elapsedTime / 60.0) elapsedTime -= (NSTimeInterval(minutes) * 60) //calculate the seconds in elapsed time. let seconds = UInt(elapsedTime) elapsedTime -= NSTimeInterval(seconds) //find out the fraction of milliseconds to be displayed. let hours = UInt(elapsedTime * 100) let strMinutes = minutes > 9 ? String(minutes):String(minutes) let strSeconds = seconds > 9 ? String(seconds):String(seconds) let strHours = hours > 9 ? String(hours):String(hours) //assign text to Label elapsedTimeLabel.text = "You have slept for \(strHours)" } //Timer var timer = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: Selector("updateTime:"), userInfo: nil, repeats: true) startTime = NSDate.timeIntervalSinceReferenceDate() } } 

每当我运行应用程序(在iOS模拟器内),当按下button(执行秒表function),应用程序崩溃。 错误总是读取

 2014-11-23 11:44:53.617 Sleep[9630:644637] -[Sleep.FirstViewController updateTime:]: unrecognized selector sent to instance 0x7ff6d9676e80 2014-11-23 11:44:53.622 Sleep[9630:644637] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Sleep.FirstViewController updateTime:]: unrecognized selector sent to instance 0x7ff6d9676e80' *** First throw call stack: 

我不知道为什么发生这种情况,我对iOS编程很新,所以请帮助!

让updateTime成为一个类的方法。
如果它是在一个纯Swift类中,你需要用@objc来声明该方法的声明:

 class A { @objc func updateTime(timer:NSTimer) { } } 

您已经将updateTime定义为Stopwatch的本地函数,因此它不在其范围之外。 您应该在课堂级别(与Stopwatch相同)移动它以便访问。

我注意到,但有一些本地variables应该可能是实例属性。

另一种可能性:确保你的select器函数不会throw ,因为这也会给你一个无法识别的select器exception:

 func mySelector(timer: NSTimer) throws { }