Swift 三元语法错误
Swift ternary syntax error
我以前一直在 Objective-C 编程,我是 Swift 的新手。这个错误Xcode让我真的很困惑。
func renderBufferAreaBAUp(yOffset: CGFloat, amount: CGFloat, ifLeft: Bool)
{
var topViewIndexForIndexAdjust = ifLeft?leftTopIndex:rightTopIndex
}
这一行我打算使用三元。 leftTopIndex 和 rightTopIndex 都是 Int 类型。但是 Xcode 给了我这条特定行上的那些,
一行中的连续语句必须用';'分隔
预期表达
谁能告诉我这些是什么意思?谢谢
Swift 错误消息通常含糊不清且没有帮助。你真正的问题是 Swift 在 ?
和 :
周围需要一些 space。
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
正确的一个是 - WITH SPACES 和 WITH ==
incrementValue = incrementValue == 0 ? 1 : incrementValue //worked
return episodes == nil ? .movie : .tvShow
我以前一直在 Objective-C 编程,我是 Swift 的新手。这个错误Xcode让我真的很困惑。
func renderBufferAreaBAUp(yOffset: CGFloat, amount: CGFloat, ifLeft: Bool)
{
var topViewIndexForIndexAdjust = ifLeft?leftTopIndex:rightTopIndex
}
这一行我打算使用三元。 leftTopIndex 和 rightTopIndex 都是 Int 类型。但是 Xcode 给了我这条特定行上的那些,
一行中的连续语句必须用';'分隔 预期表达
谁能告诉我这些是什么意思?谢谢
Swift 错误消息通常含糊不清且没有帮助。你真正的问题是 Swift 在 ?
和 :
周围需要一些 space。
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
正确的一个是 - WITH SPACES 和 WITH ==
incrementValue = incrementValue == 0 ? 1 : incrementValue //worked
return episodes == nil ? .movie : .tvShow