Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

swift

swift4

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
}
like image 961
skytect Avatar asked Jun 14 '17 14:06

skytect


1 Answers

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)
like image 167
vadian Avatar answered Oct 06 '22 20:10

vadian