Swift
class ViewController: UIViewController {
var timer = NSTimer()
override func viewDidLoad() {
NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector(self.timerMethod()), userInfo: nil, repeats: true)
}
func timerMethod() {
print("Timer method called")
}
func endTimer() {
timer.invalidate()
}
}
Swift 3
class ViewController: UIViewController {
var timer = Timer()
override func viewDidLoad() {
Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(self.timerMethod()), userInfo: nil, repeats: true)
}
func timerMethod() {
print("Timer method called")
}
func endTimer() {
timer.invalidate()
}
}
Must be invalidated manually if desired.
Swift
NSTimer.scheduledTimerWithTimeInterval(3.0, target: self, selector: Selector(self.timerMethod()), userInfo: nil, repeats: false)
Swift 3
Timer.scheduledTimer(timeInterval: 3.0, target: self, selector: #selector(self.timerMethod()), userInfo: nil, repeats: false)
Timer will be fired once, 3 seconds after time of execution. Will be invalidated automatically, once fired.