Created
November 20, 2015 06:19
-
-
Save miguellara/5eafd3ca25511fe74bc5 to your computer and use it in GitHub Desktop.
NSTimer Block Extension
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
import Foundation | |
extension NSTimer { | |
class func scheduledTimerWithTimeInterval( | |
timeInterval: NSTimeInterval, userInfo: AnyObject?, repeats: Bool, block: (timer: NSTimer) -> Void) -> NSTimer | |
{ | |
let minion = TimerBlock(block: block) | |
// The `minion` will be retained by the `NSTimer` as its `target`, until the timer is invalidated. | |
return NSTimer.scheduledTimerWithTimeInterval( | |
timeInterval, target: minion, selector: Selector("timerDidFire:"), userInfo: userInfo, repeats: repeats) | |
} | |
} | |
final class TimerBlock: NSObject { | |
private let block: (timer: NSTimer) -> Void | |
init(block: (timer: NSTimer) -> Void) { | |
self.block = block | |
} | |
func timerDidFire(timer: NSTimer) { | |
block(timer: timer) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment