0

我一直在为 Swift 编写一些教程。我遇到了一个井字游戏教程,我正在尝试使用 Xcode 6 Beta 6 进行编码。当我检查字典中的值时出现以下错误:找不到接受提供的参数的“&&”的重载。这是我的代码。

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!"
 }
4

3 回答 3

1

我正在做同样的教程。在我看来它必须在子表达式中分解有点奇怪,但它成功了,正如我在这里读到的:http: //swiftlang.eu/community/34-xcode-6-beta-6-swift-无法处理长表达式/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
        }
    }
}
于 2014-10-14T13:38:07.903 回答
0

如果您在 Report Navigator 中查看完整的编译器输出,您将看到消息

注意:表达式过于复杂,无法在合理时间内解决;考虑将表达式分解为不同的子表达式

它告诉你如何解决问题。

于 2014-09-03T21:47:34.213 回答
0

使用括号“将表达式分解为不同的子表达式”。这适用于 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))) {...}
于 2015-07-11T07:45:07.343 回答