Skip to content

Instantly share code, notes, and snippets.

@benjohnde
Last active July 22, 2016 13:32
Show Gist options
  • Save benjohnde/4a1c9a16546bcc0673a15582767c608f to your computer and use it in GitHub Desktop.
Save benjohnde/4a1c9a16546bcc0673a15582767c608f to your computer and use it in GitHub Desktop.
Timer and Blocks pre iOS 10
private class TimerActor {
var block: (Timer) -> Void
init(block: (Timer) -> Void) {
self.block = block
}
dynamic func fire(sender: Timer) {
block(sender)
}
}
private extension Selector {
static let fire = #selector(TimerActor.fire)
}
extension Timer {
class func schedule(timeInterval: TimeInterval, repeats: Bool, block: (Timer) -> Void) -> Timer {
let actor = TimerActor(block: block)
return Timer.scheduledTimer(timeInterval: timeInterval, target: actor, selector: .fire, userInfo: nil, repeats: repeats)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment