在Swift中使用NSTimer

前端之家收集整理的这篇文章主要介绍了在Swift中使用NSTimer前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在这种情况下,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)

猜你在找的Swift相关文章