Using an NSTimer in Swift

前端 未结 10 1531
生来不讨喜
生来不讨喜 2020-12-05 06:19

In this scenario, timerFunc() is never called. What am I missing?

class AppDelegate: NSObject, NSApplicationDelegate {

    var myTimer: NSTimer? = nil

             


        
10条回答
  •  臣服心动
    2020-12-05 07:14

    Since this thread made me try to put the timer on a RunLoop myself (which solved my problem), I post my specific case as well - who knows maybe it helps somebody. My timer is created during app start up and initialisation of all the objects. My problem was that, while it did schedule the timer, it still never fired. My guess is, this was the case because scheduledTimerWithTimeInterval was putting the timer on a different RunLoop during startup of the App. If I just initialise the timer and then use NSRunLoop.mainRunLoop().addTimer(myTimer, forMode:NSDefaultRunLoopMode) instead, it works fine.

提交回复
热议问题