表达式类型 '(_, _.Stride) -> _' 在没有更多上下文的情况下不明确

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

求助!我遇到错误 'Expression type '(_, _.Stride) -> _' is ambiguous without more context'。有谁知道为什么会这样并且对此有解决方案?我正在使用 Swift 4.
代码:

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
}

表达式 .now() returns 类型 DispatchTime 是一个结构。

let offsetTime = 0 将变量初始化为 Int。该错误具有误导性,实际上是类型不匹配


尽管编译器可以推断出数字文字的类型

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

Int 文字或变量添加到 DispatchTime 值的最可靠方法是 DispatchTimeInterval 具有关联值的案例。

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

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

有四个DispatchTimeInterval枚举案例

  • .seconds(Int)
  • .milliseconds(Int)
  • .microseconds(Int)
  • .nanoseconds(Int)