Using an NSTimer in Swift

前端 未结 10 1515
生来不讨喜
生来不讨喜 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:00

    To do it with the method the OP suggests, you need to add it to a run loop:

    myTimer = NSTimer(timeInterval: 5.0, target: self, selector:"timerFunc", userInfo: nil, repeats: true)
    NSRunLoop.mainRunLoop().addTimer(myTimer, forMode:NSDefaultRunLoopMode)
    

    The documentation also says that the target should take an argument, but it works without it.

    func timerFireMethod(timer: NSTimer) { }
    

提交回复
热议问题