Skip to content

Instantly share code, notes, and snippets.

@Baza207
Created February 20, 2017 23:32
Show Gist options
  • Save Baza207/8fedf7195158520539f20f6d3cec4527 to your computer and use it in GitHub Desktop.
Save Baza207/8fedf7195158520539f20f6d3cec4527 to your computer and use it in GitHub Desktop.
Timer extension to add basic block timers for pre-iOS 10.
public extension Timer {
@discardableResult
public class func scheduledTimer(withTimeInterval interval: TimeInterval, block: @escaping (Timer) -> Void) -> Timer {
let fireDate = interval + CFAbsoluteTimeGetCurrent()
let timer = CFRunLoopTimerCreateWithHandler(kCFAllocatorDefault, fireDate, 0, 0, 0) { (runLoopTimer) in
block(runLoopTimer!)
}!
CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer, CFRunLoopMode.commonModes)
return timer
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment