I want to be able to schedule three small events in the future without having to write a function for each. How can I do this using NSTimer
? I understand block
It's pretty easy, but it isn't included in Apple framework, not yet at least.
You can write a block-based wrapper for NSTimer
yourself, e.g. using GCD, or you can use existing 3rd-party libraries like this one: https://github.com/jivadevoe/NSTimer-Blocks.
I have created a category on NSTimer witch makes it possible to use it with blocks.
https://github.com/mBrissman/NSTimer-Block
You can actually call:
NSTimer.scheduledTimerWithTimeInterval(ti: NSTimeInterval,
target: AnyObject,
selector: #Selector,
userInfo: AnyObject?,
repeats: Bool)
Use it like this:
NSTimer.scheduledTimerWithTimeInterval(1,
target: NSBlockOperation(block: {...}),
selector: #selector(NSOperation.main),
userInfo: nil,
repeats: true)