表达式类型“(_,_.Stride)-> _”在没有更多上下文的情况下是模棱两可的。

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
}

1
尝试将 let offsetTime = 0 更改为 let offsetTime = 0.0 - rmaddy
@rmaddy 即使将 offsetTime 设置为 0.0,这个表达式仍然存在可读性模糊的上下文。也许更优雅的解决方案是:let offsetTime: TimeInterval = 0 - Zell B.
1个回答

13
表达式.now()返回的是结构体类型DispatchTimelet offsetTime = 0将变量初始化为Int类型。错误信息有误导性,实际上是类型不匹配。
尽管编译器可以推断出数字字面量的类型。
DispatchQueue.main.asyncAfter(deadline: .now() + 3)

最可靠的方法是使用带有关联值的 DispatchTimeInterval 类型,将 Int 字面量或变量添加到 DispatchTime 值中。
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(offsetTime)

and

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

有四种DispatchTimeInterval枚举类型

  • .seconds(Int)表示秒
  • .milliseconds(Int)表示毫秒
  • .microseconds(Int)表示微秒
  • .nanoseconds(Int)表示纳秒

诀窍在于,如果您想要0.5秒,只需做500毫秒。 - Lucas

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接