2017-06-14 20 views
3

幫助!我遇到錯誤「表達式類型」(_,_.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' –

回答

8

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

@ rmaddy的回答爲我工作:
嘗試改變讓offsetTime = 0讓offsetTime = 0.0。

@ vadian的答案也有效,也有更深入的解釋。

相關問題