【问题标题】:Expression type '(_, _.Stride) -> _' is ambiguous without more context表达式类型 '(_, _.Stride) -> _' 在没有更多上下文的情况下是模棱两可的
【发布时间】:2017-06-14 14:33:25
【问题描述】:

帮助!我遇到错误'表达式类型'(_,_.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
}

【问题讨论】:

  • 尝试将let offsetTime = 0 更改为let offsetTime = 0.0
  • @rmaddy 即使将offsetTime 设置为 0.0 也会以某种方式起作用,但这个表达式仍然具有可读性模糊的上下文。也许一个更优雅的解决方案将是这个:let offsetTime : TimeInterval = 0

标签: swift swift4


【解决方案1】:

表达式.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)

【讨论】:

  • 诀窍是如果你想做 0.5 秒,就做 500 毫秒
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-11-23
  • 2021-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多