Skip to content

Instantly share code, notes, and snippets.

@miguellara
Created November 20, 2015 06:19
Show Gist options
  • Save miguellara/5eafd3ca25511fe74bc5 to your computer and use it in GitHub Desktop.
Save miguellara/5eafd3ca25511fe74bc5 to your computer and use it in GitHub Desktop.
NSTimer Block Extension
import Foundation
extension NSTimer {
class func scheduledTimerWithTimeInterval(
timeInterval: NSTimeInterval, userInfo: AnyObject?, repeats: Bool, block: (timer: NSTimer) -> Void) -> NSTimer
{
let minion = TimerBlock(block: block)
// The `minion` will be retained by the `NSTimer` as its `target`, until the timer is invalidated.
return NSTimer.scheduledTimerWithTimeInterval(
timeInterval, target: minion, selector: Selector("timerDidFire:"), userInfo: userInfo, repeats: repeats)
}
}
final class TimerBlock: NSObject {
private let block: (timer: NSTimer) -> Void
init(block: (timer: NSTimer) -> Void) {
self.block = block
}
func timerDidFire(timer: NSTimer) {
block(timer: timer)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment