Swift 3中setTimer已经被弃用了吗?

7
我正在尝试为Swift 3创建一个CocoaPod。由于CocoaPods使用NimbleQuick,而这些库还没有更新,所以我fork了这些repos并试图将它们转换。
在Nimble项目中有一个函数,其签名为:
setTimer(start: DispatchTime, interval: UInt64, leeway: UInt64)

编译器报错:无法使用类型为'(start: DispatchTime, interval: UInt64, leeway: UInt64)'的参数列表调用'setTimer'
private let pollLeeway: UInt64 = NSEC_PER_MSEC
let interval = UInt64(pollInterval * Double(NSEC_PER_SEC))
asyncSource.setTimer(start: DispatchTime.now(), interval: interval, leeway: pollLeeway)

自动完成显示所有的setTimer方法都被弃用了,但根据我所发现的,它们不应该被弃用。
有替代方法吗?
2个回答

8
在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))

我改为使用to,这对我起作用了。


0
在Xcode 8 beta 1中,该方法的签名为:
public func setTimer(start: DispatchTime, 
                  interval: DispatchTimeInterval, 
                    leeway: DispatchTimeInterval = default)

如果您插入正确的DispatchTimeDispatchTimeInterval参数,它将编译并显示一个弃用警告:
'setTimer(start:leeway:)' is deprecated: replaced by 
instance method 'DispatchSourceTimer.scheduleOneshot(deadline:leeway:)'

一如既往地,使用 command+ 点击这些 Swift 类和方法,您将进入声明这些方法的 Swift 接口源代码。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接