5

帮助!我遇到错误'表达式类型'(_,_.Stride)-> _'在没有更多上下文的情况下模棱两可'。有谁知道为什么会发生这种情况并有解决方案?我正在使用 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
}
4

1 回答 1

13

表达式.now()返回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)
于 2017-06-14T15:38:39.600 回答