@@ -31,23 +31,29 @@ extension Timer {
3131
3232 private struct AssociatedKeys {
3333 static var repeatCounterAddress = " repeat_counter_address "
34- static var numberOfRepeatsAddress = " number_of_repeats_address "
34+ // static var isTimerRunning = "is_timer_running_address "
3535 }
3636
37+ private var isRunning : Bool {
38+ get {
39+ return isValid //objc_getAssociatedObject(self, &AssociatedKeys.isTimerRunning) as? Bool ?? false
40+ }
41+ }
42+
43+
3744 private var repeatCounter : Int {
3845 get {
3946 return objc_getAssociatedObject ( self , & AssociatedKeys . repeatCounterAddress ) as? Int ?? 0
4047 }
4148 set {
42-
4349 objc_setAssociatedObject ( self ,
4450 & AssociatedKeys . repeatCounterAddress ,
4551 newValue ,
46- . OBJC_ASSOCIATION_RETAIN )
52+ . OBJC_ASSOCIATION_RETAIN_NONATOMIC )
4753 }
4854 }
4955
50- public class func every ( _ interval : TimeInterval , `for` times : Int , _ block : @escaping ( Timer ) -> Void ) -> Timer {
56+ public class func every ( _ interval : TimeInterval , `for` times : Int , _ block : @escaping ( Timer , Int ) -> Void ) -> Timer {
5157
5258 let timer = Timer . new ( every : interval , for : times , block )
5359 timer . start ( )
@@ -91,7 +97,7 @@ extension Timer {
9197 /// - Note: The `new` class function is a workaround for a crashing bug when using convenience initializers (rdar://18720947)
9298
9399 public class func new ( after interval : TimeInterval , _ block : @escaping ( ) -> Void ) -> Timer {
94- return CFRunLoopTimerCreateWithHandler ( kCFAllocatorDefault , CFAbsoluteTimeGetCurrent ( ) + interval , 0 , 0 , 0 ) { _ in
100+ return CFRunLoopTimerCreateWithHandler ( kCFAllocatorDefault , CFAbsoluteTimeGetCurrent ( ) + interval , 0 , 0 , 0 ) { timer in
95101 block ( )
96102 }
97103 }
@@ -134,7 +140,7 @@ extension Timer {
134140 /// Use `NSTimer.every` to create and schedule a timer in one step.
135141 /// - Note: The `new` class function is a workaround for a crashing bug when using convenience initializers (rdar://18720947)
136142
137- @ nonobjc public class func new ( every interval : TimeInterval , `for` times : Int , _ block : @escaping ( Timer ) -> Void ) -> Timer {
143+ @ nonobjc public class func new ( every interval : TimeInterval , `for` times : Int , _ block : @escaping ( Timer , Int ) -> Void ) -> Timer {
138144 var timer : Timer !
139145
140146 timer = CFRunLoopTimerCreateWithHandler ( kCFAllocatorDefault , CFAbsoluteTimeGetCurrent ( ) + interval , interval , 0 , 0 ) { _ in
@@ -148,7 +154,7 @@ extension Timer {
148154 }
149155 }
150156
151- block ( timer )
157+ block ( timer , timer . repeatCounter )
152158 }
153159 return timer
154160 }
@@ -175,6 +181,7 @@ extension Timer {
175181 runLoop . add ( self , forMode : mode )
176182 }
177183 }
184+
178185}
179186
180187// MARK: - Time extensions
@@ -197,4 +204,3 @@ public extension Double {
197204 public var days : TimeInterval { return self * 3600 * 24 }
198205}
199206
200-
0 commit comments