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.
(Swift 2.2)
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)
I honestly don't know why I had to put @objc in front of my notifyUiUpdate() function. The editor made me add it.
ReplyDelete