Skip to content

Commit ad051f5

Browse files
committed
add timer into closure
1 parent 4114378 commit ad051f5

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

Src/SwiftyTimer.swift

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,18 @@
2424

2525
import Foundation
2626

27+
import Foundation
28+
2729
private class NSTimerActor {
28-
var block: () -> Void
30+
var block: (NSTimer!) -> Void
31+
var timer: NSTimer!
2932

30-
init(_ block: () -> Void) {
33+
init(_ block: (NSTimer!) -> Void) {
3134
self.block = block
3235
}
3336

3437
@objc func fire() {
35-
block()
38+
block(timer)
3639
}
3740
}
3841

@@ -44,32 +47,38 @@ extension NSTimer {
4447
/// **Note:** the timer won't fire until it's scheduled on the run loop.
4548
/// Use `NSTimer.after` to create and schedule a timer in one step.
4649

47-
public class func new(after interval: NSTimeInterval, _ block: () -> Void) -> NSTimer {
50+
public class func new(after interval: NSTimeInterval, _ block: (NSTimer!) -> Void) -> NSTimer {
51+
let timer: NSTimer!
4852
let actor = NSTimerActor(block)
49-
return self.init(timeInterval: interval, target: actor, selector: "fire", userInfo: nil, repeats: false)
53+
timer = self.init(timeInterval: interval, target: actor, selector: "fire", userInfo: nil, repeats: false)
54+
actor.timer = timer
55+
return timer
5056
}
5157

5258
/// Create a timer that will call `block` repeatedly in specified time intervals.
5359
///
5460
/// **Note:** the timer won't fire until it's scheduled on the run loop.
5561
/// Use `NSTimer.every` to create and schedule a timer in one step.
5662

57-
public class func new(every interval: NSTimeInterval, _ block: () -> Void) -> NSTimer {
63+
public class func new(every interval: NSTimeInterval, _ block: (NSTimer!) -> Void) -> NSTimer {
64+
let timer: NSTimer!
5865
let actor = NSTimerActor(block)
59-
return self.init(timeInterval: interval, target: actor, selector: "fire", userInfo: nil, repeats: true)
66+
timer = self.init(timeInterval: interval, target: actor, selector: "fire", userInfo: nil, repeats: true)
67+
actor.timer = timer
68+
return timer
6069
}
6170

6271
/// Create and schedule a timer that will call `block` once after the specified time.
6372

64-
public class func after(interval: NSTimeInterval, _ block: () -> Void) -> NSTimer {
73+
public class func after(interval: NSTimeInterval, _ block: (NSTimer!) -> Void) -> NSTimer {
6574
let timer = NSTimer.new(after: interval, block)
6675
timer.start()
6776
return timer
6877
}
6978

7079
/// Create and schedule a timer that will call `block` repeatedly in specified time intervals.
7180

72-
public class func every(interval: NSTimeInterval, _ block: () -> Void) -> NSTimer {
81+
public class func every(interval: NSTimeInterval, _ block: (NSTimer!) -> Void) -> NSTimer {
7382
let timer = NSTimer.new(every: interval, block)
7483
timer.start()
7584
return timer

0 commit comments

Comments
 (0)