是否在Swift 3中弃用了setTimer?

Cal*_*ter 7 quick-nimble swift3

我正在尝试为Swift 3创建一个CocoaPod.因为CocoaPods使用NimbleQuick,而那些库还没有更新,我分配了repos并试图转换它们.

在Nimble项目中,有一个函数调用,签名为:

setTimer(start: DispatchTime, interval: UInt64, leeway: UInt64)
Run Code Online (Sandbox Code Playgroud)

编译说 Cannot invoke 'setTimer' with an argument list of type '(start: DispatchTime, interval: UInt64, leeway: UInt64)'

private let pollLeeway: UInt64 = NSEC_PER_MSEC
let interval = UInt64(pollInterval * Double(NSEC_PER_SEC))
asyncSource.setTimer(start: DispatchTime.now(), interval: interval, leeway: pollLeeway)
Run Code Online (Sandbox Code Playgroud)

自动完成显示所有setTimer方法都已弃用,但是从我发现它们不应该这样做.

有替代品吗?

小智 7

在Swift 3.0中你应该使用

let timer = DispatchSource.makeTimerSource(flags: DispatchSource.TimerFlags(rawValue: UInt(0)), queue: DispatchQueue.global(qos: DispatchQoS.QoSClass.default))
    timer.scheduleRepeating(deadline: DispatchTime.init(uptimeNanoseconds: UInt64(100000)), interval: DispatchTimeInterval.seconds(1), leeway: DispatchTimeInterval.seconds(0))
Run Code Online (Sandbox Code Playgroud)

相反,它对我有用


Dar*_*ren 0

在 Xcode 8 beta 1 中,该方法的签名是:

public func setTimer(start: DispatchTime, 
                  interval: DispatchTimeInterval, 
                    leeway: DispatchTimeInterval = default)
Run Code Online (Sandbox Code Playgroud)

如果您插入正确的DispatchTime参数DispatchTimeInterval,它将编译,您将看到一个弃用警告:

'setTimer(start:leeway:)' is deprecated: replaced by 
instance method 'DispatchSourceTimer.scheduleOneshot(deadline:leeway:)'
Run Code Online (Sandbox Code Playgroud)

与往常一样,按住命令并单击这些 Swift 类和方法将进入声明这些方法的 Swift 接口源。