swiftwkinterfacegroup

Print to Console when WKInterfaceTimer reaches 0


I want to see that my program knows when the timer is done. I'm using WKInterfaceTimer in Xcode 7 Beta 3 (7A152u). The "Tick Tock" prints to the console while the counter is counting down. But when it reaches 0, "Timer Done" does not print.

    @IBOutlet var myTimer: WKInterfaceTimer!

    @IBAction func startButton() {
        myTimer.start()
        myTimer.setDate(NSDate(timeIntervalSinceNow: 4)) // Arbitrary 4 second coundown.

        // Impliment an alert.
        if myTimer == 0 {
            print("Timer Done")

        } else {
        print("Tick Tock")

    }
}

Solution

  • What you did is wrong. After you call setDate the timer start counting down. To get info what is actual state of the timer you should instantiate NSTimer and check when it fires.

    From Apple's Documentation:

    To know when the timer reaches 0, configure an NSTimer object with the same target date you used to set up the timer.