Saturday, March 28, 2015

Overview

When you want to run something every x amount of time, you use an NSTimer. It is like the setInterval or setTimeout function from Javascript. You can find more information on NSTimers here.

Content

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
class func scheduledTimerWithInterval(
    _ seconds: NSTimeInterval,
    target: AnyObject,
    selector: Selector(String),
    userInfo: AnyObject?,
    repeats: Bool
)
*/

let timer = NSTimer.scheduledTimerWithInterval(
    2.0,
    target: self,
    selector: "fire:", // the : means "call with an argument"
    userInfo: nil,
    repeats: true
)

func fire(timer: NSTimer) {
    if imDoneWithThisTimer {
        timer.invalidate()
    }
}

Every 2 seconds, the method fire(NSTimer) will be invoked in self


Random Posts