在这种情况下,timerFunc()从未被调用。我缺少什么?
@H_403_1@class AppDelegate: NSObject,NSApplicationDelegate {
var myTimer: NSTimer? = nil
func timerFunc() {
println("timerFunc()")
}
func applicationDidFinishLaunching(aNotification: NSNotification?) {
myTimer = NSTimer(timeInterval: 5.0,target: self,selector:"timerFunc",userInfo: nil,repeats: true)
}
}
您可以创建一个自动将自己添加到runloop并开始启动的计划定时器:
@H_403_1@NSTimer.scheduledTimerWithTimeInterval(0.5,selector: "timerDidFire:",userInfo: userInfo,repeats: true)
或者,你可以保留你当前的代码,并添加定时器runloop当你准备好了:
@H_403_1@let myTimer = NSTimer(timeInterval: 0.5,repeats: true) NSRunLoop.currentRunLoop().addTimer(myTimer,forMode: NSRunLoopCommonModes)