Skytect Skytect -4 years ago 107
Swift Question

Expression type '(_, _.Stride) -> _' is ambiguous without more context

Help! I am encountering the error 'Expression type '(_, _.Stride) -> _' is ambiguous without more context'. Does anyone know why this is happening and a have solution to this? I am using Swift 4.

Code:

let offsetTime = 0
DispatchQueue.main.asyncAfter(deadline: .now() + offsetTime) { //Expression type '(_, _.Stride) -> _' is ambiguous without more context
self.currentTaskForUser.text = "Starting\n" + note + "in"
self.timerDown(from: 3, to: 1)
}
DispatchQueue.main.asyncAfter(deadline: .now() + offsetTime + 3) { //Expression type '(_, _.Stride) -> _' is ambiguous without more context
self.currentTaskForUser.text = note
let difficultyValue = Int(self.difficultyControl.titleForSegment(at: self.difficultyLevel.selectedSegmentIndex)!)!
self.timerUp(from: 1, to: difficultyValue)
self.offsetTime += 13
}

Answer Source

The expression .now() returns the type DispatchTime which is a struct.

let offsetTime = 0 initializes the variable as Int. The error is misleading, practically it's a type mismatch


Although the compiler can infer the type of an numeric literal

DispatchQueue.main.asyncAfter(deadline: .now() + 3)

the most reliable way to add an Int literal or variable to a DispatchTime value is a DispatchTimeInterval case with associated value.

DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(offsetTime)

and

DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(offsetTime) + .seconds(3))

There are four DispatchTimeInterval enumeration cases

  • .seconds(Int)
  • .milliseconds(Int)
  • .microseconds(Int)
  • .nanoseconds(Int)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download