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?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Caleb Kleveter
  • 11,170
  • 8
  • 62
  • 92

2 Answers2

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

KingCQ
  • 81
  • 1
  • 2
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.

Darren
  • 25,520
  • 5
  • 61
  • 71