表达式类型'(_,_。Stride) - > _'没有更多的背景是模棱两可的

时间:2017-06-14 14:33:25

标签: swift swift4

帮助!我遇到错误' Expression type'(_,_ .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
}

1 个答案:

答案 0 :(得分: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)