Swift三元语法错误

Swift三元语法错误,swift,ternary-operator,Swift,Ternary Operator,我以前一直用Objective-C编程,我是Swift新手。这个Xcode错误让我很困惑 func renderBufferAreaBAUp(yOffset: CGFloat, amount: CGFloat, ifLeft: Bool) { var topViewIndexForIndexAdjust = ifLeft?leftTopIndex:rightTopIndex } 在这一行中,我打算使用三元。leftTopIndex和rightTopIndex都是Int

我以前一直用Objective-C编程,我是Swift新手。这个Xcode错误让我很困惑

func renderBufferAreaBAUp(yOffset: CGFloat, amount: CGFloat, ifLeft: Bool)
{     
        var topViewIndexForIndexAdjust = ifLeft?leftTopIndex:rightTopIndex
}
在这一行中,我打算使用三元。leftTopIndex和rightTopIndex都是Int类型。但是Xcode在这一行给了我这些

一行中的连续语句必须用“;”分隔 期望表达式


谁能告诉我这是什么意思?谢谢。

Swift错误消息通常是晦涩难懂的,没有帮助。您真正的问题是Swift在
周围需要一些空间

var topViewIndexForIndexAdjust = ifLeft ? leftTopIndex : rightTopIndex

必须使用空格分隔操作数和运算符:

var topViewIndexForIndexAdjust = ifLeft ? leftTopIndex : rightTopIndex
                                       ^ ^            ^ ^
Swift在这方面非常严格——即使是这一行显然正确的代码:

let val =12

生成编译错误。

三元运算符的隐藏的Swift错误消息

错误1

incrementValue = incrementValue == 0?1:incrementValue //errors saying add ";", consecutive statements must be separated by ";"
错误2

incrementValue = incrementValue = 0 ? 1 : incrementValue //error - Assigning a variable to itself
正确的一个是-带空格和==

incrementValue = incrementValue == 0 ? 1 : incrementValue //worked