Skip to content

Commit 916d247

Browse files
author
Itai Ferber
committed
Update Timer to match API exposed by Darwin Foundation
1 parent 65c983d commit 916d247

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

Foundation/NSTimer.swift

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ internal func __NSFireTimer(_ timer: CFRunLoopTimer?, info: UnsafeMutableRawPoin
1515
t._fire(t)
1616
}
1717

18-
open class Timer: NSObject {
18+
open class Timer : NSObject {
1919
typealias CFType = CFRunLoopTimer
2020

2121
internal var _cfObject: CFType {
@@ -52,6 +52,17 @@ open class Timer: NSObject {
5252
_timer = timer
5353
}
5454

55+
// !!! The interface as exposed by Darwin marks init(fire date: Date, interval: TimeInterval, repeats: Bool, block: @escaping (Timer) -> Swift.Void) with "convenience", but this constructor without.
56+
// !!! That doesn't make sense as init(fire date: Date, ...) is more general than this constructor, which can be implemented in terms of init(fire date: Date, ...).
57+
// !!! The convenience here has been switched around and deliberately does not match what is exposed by Darwin Foundation.
58+
/// Creates and returns a new Timer object initialized with the specified block object.
59+
/// - parameter: timeInterval The number of seconds between firings of the timer. If seconds is less than or equal to 0.0, this method chooses the nonnegative value of 0.1 milliseconds instead
60+
/// - parameter: repeats If YES, the timer will repeatedly reschedule itself until invalidated. If NO, the timer will be invalidated after it fires.
61+
/// - parameter: block The execution body of the timer; the timer itself is passed as the parameter to this block when executed to aid in avoiding cyclical references
62+
public convenience init(timeInterval interval: TimeInterval, repeats: Bool, block: @escaping (Timer) -> Swift.Void) {
63+
self.init(fire: Date(), interval: interval, repeats: repeats, block: block)
64+
}
65+
5566
/// Alternative API for timer creation with a block.
5667
/// - Experiment: This is a draft API currently under consideration for official import into Foundation as a suitable alternative to creation via selector
5768
/// - Note: Since this API is under consideration it may be either removed or revised in the near future
@@ -63,7 +74,7 @@ open class Timer: NSObject {
6374
}
6475

6576
open func fire() {
66-
if !valid {
77+
if !isValid {
6778
return
6879
}
6980
_fire(self)
@@ -98,7 +109,13 @@ open class Timer: NSObject {
98109
CFRunLoopTimerInvalidate(_timer!)
99110
}
100111

101-
open var valid: Bool {
112+
open var isValid: Bool {
102113
return CFRunLoopTimerIsValid(_timer!)
103114
}
115+
116+
// Timer's userInfo is meant to be read-only. The initializers that are exposed on Swift, however, do not take a custom userInfo, so it can never be set.
117+
// The default value should then be nil, and this is left as subclassable for potential consumers.
118+
open var userInfo: Any? {
119+
return nil
120+
}
104121
}

TestFoundation/TestNSTimer.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,13 @@ class TestNSTimer : XCTestCase {
7979
var flag = false
8080

8181
let dummyTimer = Timer.scheduledTimer(withTimeInterval: 0.01, repeats: true) { timer in
82-
XCTAssertTrue(timer.valid)
82+
XCTAssertTrue(timer.isValid)
8383
XCTAssertFalse(flag) // timer should tick only once
8484

8585
flag = true
8686

8787
timer.invalidate()
88-
XCTAssertFalse(timer.valid)
88+
XCTAssertFalse(timer.isValid)
8989
}
9090

9191
let runLoop = RunLoop.current()

0 commit comments

Comments
 (0)