表达式类型'(_,_.Stride) - > _'在没有更多上下文的情况下不明确
问题描述:
帮助!我遇到错误“表达式类型”(_,_.Stride) - > _'在没有更多上下文的情况下不明确“。有谁知道为什么会发生这种情况,并有解决方案吗?我使用雨燕4
代码:表达式类型'(_,_.Stride) - > _'在没有更多上下文的情况下不明确
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
}
答
表达式.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)
答
@ rmaddy的回答为我工作:
尝试改变让offsetTime = 0让offsetTime = 0.0。
@ vadian的答案也有效,也有更深入的解释。
尝试将'let offsetTime = 0'更改为'let offsetTime = 0.0'。 – rmaddy
@rmaddy即使将'offsetTime'设置为0.0,也会以某种方式工作,但该表达式仍具有可读性不明确的上下文。也许更优雅的解决方案是这样的:'让offsetTime:TimeInterval = 0' –