@@ -36,6 +36,7 @@ extension NSTimer {
3636 block ( )
3737 }
3838 }
39+
3940 /// Create a timer that will call `block` repeatedly in specified time intervals.
4041 ///
4142 /// - Note: The timer won't fire until it's scheduled on the run loop.
@@ -47,7 +48,21 @@ extension NSTimer {
4748 block ( )
4849 }
4950 }
50-
51+
52+ /// Create a timer that will call `block` repeatedly in specified time intervals.
53+ /// (This variant also passes the timer instance to the block)
54+ ///
55+ /// - Note: The timer won't fire until it's scheduled on the run loop.
56+ /// Use `NSTimer.after` to create and schedule a timer in one step.
57+ /// - Note: The `new` class function is a workaround for a crashing bug when using convenience initializers (rdar://18720947)
58+
59+ @nonobjc public class func new( every interval: NSTimeInterval , _ block: NSTimer -> Void ) -> NSTimer {
60+ var timer : NSTimer !
61+ timer = CFRunLoopTimerCreateWithHandler ( kCFAllocatorDefault, CFAbsoluteTimeGetCurrent ( ) + interval, interval, 0 , 0 ) { _ in
62+ block ( timer)
63+ }
64+ return timer
65+ }
5166
5267 /// Create and schedule a timer that will call `block` once after the specified time.
5368
@@ -65,6 +80,15 @@ extension NSTimer {
6580 return timer
6681 }
6782
83+ /// Create and schedule a timer that will call `block` repeatedly in specified time intervals.
84+ /// (This variant also passes the timer instance to the block)
85+
86+ @nonobjc public class func every( interval: NSTimeInterval , _ block: NSTimer -> Void ) -> NSTimer {
87+ let timer = NSTimer . new ( every: interval, block)
88+ timer. start ( )
89+ return timer
90+ }
91+
6892 /// Schedule this timer on the run loop
6993 ///
7094 /// By default, the timer is scheduled on the current run loop for the default mode.
0 commit comments