【发布时间】:2014-08-13 17:02:58
【问题描述】:
在这种情况下,永远不会调用 timerFunc()。我错过了什么?
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)
}
}
【问题讨论】:
-
如果你使用定时器的
init你必须You must add the new timer to a run loop, using addTimer:forMode:。这是文档描述中的第二句话。否则使用scheduledTimerWithTimeInterval,这可能就是您要查找的内容。 -
为什么考虑到几个人提供了不同的答案而投反对票?
-
不能肯定地告诉你,但可能是因为答案并不难找到,正如我之前指出的那样。它在文档中是正确的。如果您让
option点击您的方法,您将在 5 秒内找到解决方案,甚至无需离开 Xcode。