Was setTimer deprecated in Swift 3?
Asked Answered
D

2

7

I am trying to create a CocoaPod for Swift 3. Because CocoaPods uses Nimble and Quick, and those libraries haven't been updated yet, I forked the repos and am trying to convert them.

In the Nimble project there is a function called with the signature of:

setTimer(start: DispatchTime, interval: UInt64, leeway: UInt64)

The compiler says 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)

The auto-complete shows all the setTimer methods are deprecated, but from what I found they shouldn't be.

Is there a replacement?

Diastyle answered 1/7, 2016 at 14:34 Comment(0)
T
8

In Swift 3.0 you should use

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 instead, and it's work for me

Treachery answered 28/9, 2016 at 6:46 Comment(0)
C
0

In Xcode 8 beta 1, the signature of that method is:

public func setTimer(start: DispatchTime, 
                  interval: DispatchTimeInterval, 
                    leeway: DispatchTimeInterval = default)

If you plug in the correct DispatchTime and DispatchTimeInterval parameters it will compile and you will see a deprecation warning:

'setTimer(start:leeway:)' is deprecated: replaced by 
instance method 'DispatchSourceTimer.scheduleOneshot(deadline:leeway:)'

As always, command+clicking on these Swift classes and methods will get you into the Swift interface sources where these methods are declared.

Cammie answered 1/7, 2016 at 14:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.