表达式类型'(_,_.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 
} 
+0

尝试将'let offsetTime = 0'更改为'let offsetTime = 0.0'。 – rmaddy

+0

@rmaddy即使将'offsetTime'设置为0.0,也会以某种方式工作,但该表达式仍具有可读性不明确的上下文。也许更优雅的解决方案是这样的:'让offsetTime:TimeInterval = 0' –

表达式.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的答案也有效,也有更深入的解释。