2014-09-03 54 views
0

我一直在爲Swift編寫一些教程。我遇到了一個TicTacToe教程,我嘗試使用Xcode 6 Beta 6進行編碼。當我檢查字典中的值時,出現以下錯誤:無法找到接受所提供參數的'& &'的過載。這是我的代碼。Swift錯誤:無法找到接受提供參數的'&&'的重載

var plays = [Int:Int]() 

var whoWon = ["I":0,"you":1] 
for (key,value) in whoWon { 
if ((plays[6] == value && plays[7] == value && plays[8] == value) || 
    (plays[3] == value && plays[4] == value && plays[5] == value) || 
    (plays[0] == value && plays[1] == value && plays[2] == value) || 
    (plays[6] == value && plays[3] == value && plays[0] == value) || 
    (plays[7] == value && plays[4] == value && plays[1] == value) || 
    (plays[8] == value && plays[5] == value && plays[2] == value) || 
    (plays[6] == value && plays[4] == value && plays[2] == value) || // error appears on this line 
    (plays[8] == value && plays[4] == value && plays[0] == value)) 
{ 
    userMessage.hidden = false 
    userMessage.text = "Looks like \(key) won!" 
} 
+0

如何將某些東西分解爲子表達式? – skyguy 2014-09-20 16:14:09

回答

0

如果你看一下在報告導航完整編譯器的輸出,那麼你將看到 消息

note: expression was too complex to be solved in reasonable time; consider breaking up the expression into distinct sub-expressions

它告訴你如何解決這個問題。

+0

謝謝。我忘了問題導航器選項卡。 – Renee 2014-09-03 22:17:00

+0

嘿@Martin R我正在使用這個相同的教程,我是一個初學者,你會如何將它分解成子表達式? – skyguy 2014-09-20 02:46:38

1

我在做同樣的教程。 在我看來有點不可思議,它在子表達式被打破了,但它的伎倆,當我讀到這裏: http://swiftlang.eu/community/34-xcode-6-beta-6-swift-can-t-handle-long-expressions/0

這可能是由於Xcode中的問題。

這裏的重寫功能:

func checkForWin() { 
    var whoWon = ["I": 0, "You": 1] 
    for (key, value) in whoWon { 
     var wonA = (plays[1] == value && plays[2] == value && plays[3] == value) 
     var wonB = (plays[4] == value && plays[5] == value && plays[6] == value) 
     var wonC = (plays[7] == value && plays[8] == value && plays[9] == value) 
     var wonD = (plays[1] == value && plays[4] == value && plays[7] == value) 
     var wonE = (plays[2] == value && plays[5] == value && plays[8] == value) 
     var wonF = (plays[3] == value && plays[6] == value && plays[9] == value) 
     var wonG = (plays[1] == value && plays[5] == value && plays[9] == value) 
     var wonH = (plays[3] == value && plays[5] == value && plays[7] == value) 

     if(wonA || wonB || wonC || wonD || wonE || wonF || wonG || wonH) { 
       userMessage.hidden = false 
       userMessage.text = "Looks like \(key) won!" 
       resetBtn.hidden = false 
       done = true 
     } 
    } 
} 
+1

爲什麼'var'而不是'let'? (I.E.,won *可能是常量,對嗎?...) – 2014-10-14 13:54:09

0

使用圓括號「分手錶達成不同的子表達式」。 This works in Xcode 6.4

if (((plays[1] == value) && (plays[2] == value) && (plays[3] == value)) || 
    ((plays[4] == value) && (plays[5] == value) && (plays[6] == value)) || 
    ((plays[7] == value) && (plays[8] == value) && (plays[9] == value)) || 
    ((plays[1] == value) && (plays[4] == value) && (plays[7] == value)) || 
    ((plays[2] == value) && (plays[5] == value) && (plays[8] == value)) || 
    ((plays[3] == value) && (plays[6] == value) && (plays[9] == value)) || 
    ((plays[1] == value) && (plays[5] == value) && (plays[9] == value)) || 
    ((plays[3] == value) && (plays[5] == value) && (plays[7] == value))) {...} 
相關問題