Last active
March 6, 2019 16:59
-
-
Save IanKeen/9c501e1dedd1a81e5018 to your computer and use it in GitHub Desktop.
Simple NSTimer extension to support closures
This file contains 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
extension NSTimer { | |
private class TimerClosure { | |
private let closure: (NSTimer) -> Void | |
init(closure: (NSTimer) -> Void) { self.closure = closure } | |
@objc func selector(timer: NSTimer) { self.closure(timer) } | |
} | |
static func scheduledTimerWithTimeInterval(ti: NSTimeInterval, closure aClosure: (NSTimer) -> Void, userInfo: AnyObject?, repeats yesOrNo: Bool) -> NSTimer { | |
let wrapped = TimerClosure(closure: aClosure) | |
return NSTimer.scheduledTimerWithTimeInterval(ti, target: wrapped, selector: "selector:", userInfo: userInfo, repeats: yesOrNo) | |
} | |
static func scheduledTimerWithTimeInterval(ti: NSTimeInterval, userInfo: AnyObject?, repeats yesOrNo: Bool, closure aClosure: (NSTimer) -> Void) -> NSTimer { | |
let wrapped = TimerClosure(closure: aClosure) | |
return NSTimer.scheduledTimerWithTimeInterval(ti, target: wrapped, selector: "selector:", userInfo: userInfo, repeats: yesOrNo) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment