I am attempting to extend Foundation's Timer
class in Swift 3, adding a convenience initializer. But its call to the Foundation-provided initializer never returns.
The problem is illustrated in the following trivialized demo, which can be run as a Playground.
import Foundation
extension Timer {
convenience init(target: Any) {
print("Next statement never returns")
self.init(timeInterval: 1.0,
target: target,
selector: #selector(Target.fire),
userInfo: nil,
repeats: true)
print("This never executes")
}
}
class Target {
@objc func fire(_ timer: Timer) {
}
}
let target = Target()
let timer = Timer(target: target)
Console output:
Next statement never returns
To study further,
• I wrote similar code extending URLProtocol
(one of the only other Foundation classes with an instance initializer). Result: No problem.
• To eliminate the Objective-C stuff as a possible cause, I changed the wrapped initializer to init(timeInterval:repeats:block:)
method and provided a Swift closure. Result: Same problem.