Last active
July 22, 2016 13:32
-
-
Save benjohnde/4a1c9a16546bcc0673a15582767c608f to your computer and use it in GitHub Desktop.
Timer and Blocks pre iOS 10
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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