Saturday, May 7, 2016

Timers

In this example when startTimer function is called it creates a timer that fires every second repeatedly. Every time the timer fires it makes a call to the notifyUiToUpdate function.
class Timer {
    
    private var _timer: NSTimer!

    func startTimer() {
        _timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, 
            selector: #selector(notifyUiToUpdate), userInfo: nil, repeats: true)
    }
    
    func stopTimer() {
        _timer.invalidate()
    }
    
    @objc func notifyUiToUpdate() {
        NSNotificationCenter.defaultCenter().postNotification(
            NSNotification(name: "updateView", object: nil))
    }
}


(Swift 2.2)

1 comment:

  1. I honestly don't know why I had to put @objc in front of my notifyUiUpdate() function. The editor made me add it.

    ReplyDelete

Note: Only a member of this blog may post a comment.

SwiftUI Search & Filter with Combine - Part 3 (iOS, Xcode 13, SwiftUI, 2...

In part 3 of the Searchable video series, I show you how to use Combine in #SwiftUI for the search and filter logic connected to the searcha...