Skip to content

Instantly share code, notes, and snippets.

@jtbandes
Created January 23, 2016 02:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jtbandes/89bdb8a745b9f65d60a1 to your computer and use it in GitHub Desktop.
Save jtbandes/89bdb8a745b9f65d60a1 to your computer and use it in GitHub Desktop.
extension NSTimer
{
private class Trampoline
{
typealias Handler = @convention(block) (NSTimer) -> Void
@objc static func handleTimer(timer: NSTimer) {
let handler = unsafeBitCast(timer.userInfo, Handler.self)
handler(timer)
}
}
static func scheduledTimerWithTimeInterval(interval: NSTimeInterval, repeats: Bool, usingBlock block: @convention(block) (NSTimer) -> Void) -> NSTimer
{
return scheduledTimerWithTimeInterval(interval, target: Trampoline.self, selector: "handleTimer:", userInfo: unsafeBitCast(block, AnyObject.self), repeats: repeats)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment