Using an NSTimer in Swift

2019-01-13 20:00发布

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

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)
    }
}

标签: swift nstimer
10条回答
祖国的老花朵
2楼-- · 2019-01-13 20:26

With swift3, you can run it with,

var timer: Timer?
func startTimer() {

    if timer == nil {
        timer = Timer.scheduledTimer(timeInterval: 3, target: self, selector: #selector(self.loop), userInfo: nil, repeats: true)
    }
}

func stopTimer() {
    if timer != nil {
        timer?.invalidate()
        timer = nil
    }
}

func loop() {
    //do something
}
查看更多
女痞
3楼-- · 2019-01-13 20:29

You can create a scheduled timer which automatically adds itself to the runloop and starts firing:

NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: "timerDidFire:", userInfo: userInfo, repeats: true)

Or, you can keep your current code, and add the timer to the runloop when you're ready for it:

let myTimer = NSTimer(timeInterval: 0.5, target: self, selector: "timerDidFire:", userInfo: nil, repeats: true)
NSRunLoop.currentRunLoop().addTimer(myTimer, forMode: NSRunLoopCommonModes)
查看更多
何必那么认真
4楼-- · 2019-01-13 20:32

As Drewag and Ryan pointed out, you need to create a scheduled timer (or schedule it yourself) It's easiest to create it scheduled already with:

myTimer = NSTimer.scheduledTimerWithTimeInterval(5.0, target: self, selector: "timerFunc:", userInfo: nil, repeats: true)

You also need to change your definition of timerFunc (and the associated selector) to take an argument and end with a ':'

func timerFunc(timer:NSTimer!) {
    ...
}
查看更多
我欲成王,谁敢阻挡
5楼-- · 2019-01-13 20:33

NSTimer's are not scheduled automatically unless you use NSTimer.scheduledTimerWithTimeInterval:

myTimer = NSTimer.scheduledTimerWithTimeInterval(5.0, target: self, selector: "timerFunc", userInfo: nil, repeats: true)
查看更多
女痞
6楼-- · 2019-01-13 20:36

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.

查看更多
Emotional °昔
7楼-- · 2019-01-13 20:36

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) { }
查看更多
登录 后发表回答