0

I have been working on some tutorials for Swift. I came across a TicTacToe tutorial that I'm trying to code using Xcode 6 Beta 6. I'm getting the following error when I check the values in a dictionary: Could not find an overload for '&&' that accepts the supplied arguments. Here's my code.

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!"
 }
Renee
  • 3
  • 1
  • 2

3 Answers3

1

I was doing the same tutorial. Seems to me a bit weird that it had to be broken up in subexpressions, but it did the trick, and as I read here: http://swiftlang.eu/community/34-xcode-6-beta-6-swift-can-t-handle-long-expressions/0

it might be due to a problem of Xcode.

Here's the rewritten function:

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
        }
    }
}
Michael van de Waeter
  • 1,473
  • 15
  • 29
0

If you look at the full compiler output in the Report Navigator then you will see the message

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

which tells you how to solve the problem.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • Thanks. I forgot about the Issue Navigator tab. – Renee Sep 03 '14 at 22:17
  • Hey @Martin R I am working with this same tutorial and I'm a beginner, how would you go about breaking it into sub expressions? – blue Sep 20 '14 at 02:46
0

Use the parenthesis to "breaking up the expression into distinct sub-expressions". 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))) {...}