帮助!我遇到错误' 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
}
答案 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)