@@ -32,9 +32,8 @@ extension NSTimer {
3232 /// - Note: The `new` class function is a workaround for a crashing bug when using convenience initializers (rdar://18720947)
3333
3434 public class func new ( after interval : NSTimeInterval , _ block : ( ) -> Void ) -> NSTimer {
35- return CFRunLoopTimerCreateWithHandler ( kCFAllocatorDefault , CFAbsoluteTimeGetCurrent ( ) + interval , 0 , 0 , 0 ) { _ in
36- block ( )
37- }
35+ let actor = Actor { _ in block ( ) }
36+ return self . init ( timeInterval : interval , target : actor , selector : #selector( Actor . fire ) , userInfo : nil , repeats : false )
3837 }
3938
4039 /// Create a timer that will call `block` repeatedly in specified time intervals.
@@ -44,9 +43,8 @@ extension NSTimer {
4443 /// - Note: The `new` class function is a workaround for a crashing bug when using convenience initializers (rdar://18720947)
4544
4645 public class func new ( every interval : NSTimeInterval , _ block : ( ) -> Void ) -> NSTimer {
47- return CFRunLoopTimerCreateWithHandler ( kCFAllocatorDefault , CFAbsoluteTimeGetCurrent ( ) + interval , interval , 0 , 0 ) { _ in
48- block ( )
49- }
46+ let actor = Actor { _ in block ( ) }
47+ return self . init ( timeInterval : interval , target : actor , selector : #selector( Actor . fire ) , userInfo : nil , repeats : true )
5048 }
5149
5250 /// Create a timer that will call `block` repeatedly in specified time intervals.
@@ -57,11 +55,8 @@ extension NSTimer {
5755 /// - Note: The `new` class function is a workaround for a crashing bug when using convenience initializers (rdar://18720947)
5856
5957 @ 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
58+ let actor = Actor ( block )
59+ return self . init ( timeInterval : interval , target : actor , selector : #selector( Actor . fire ) , userInfo : nil , repeats : true )
6560 }
6661
6762 /// Create and schedule a timer that will call `block` once after the specified time.
@@ -101,6 +96,20 @@ extension NSTimer {
10196 runLoop . addTimer ( self , forMode : mode )
10297 }
10398 }
99+
100+ // MARK: - Internals
101+
102+ private class Actor {
103+ var block : NSTimer -> Void
104+
105+ init ( _ block : NSTimer -> Void ) {
106+ self . block = block
107+ }
108+
109+ @ objc func fire ( timer : NSTimer ) {
110+ block ( timer )
111+ }
112+ }
104113}
105114
106115// MARK: - Time extensions
0 commit comments