Swift 1.2 结果为'?'必须后跟调用、成员查找或下标
Swift 1.2 Results to '?' must be followed by a call, member lookup, or subscript
更新到 Swift 1.2 / Xcode 6.3 导致了以下错误:
了解 1.1 和 1.2 之间发生的变化的人可以帮助解决这里发生的问题吗?所有帮助表示赞赏!感谢您阅读到这里!
在Swift 1.2 中,在变量后跟?
展开它是非法的。 ?
用在 可选链 中,然后必须跟在方法调用、成员查找(即 属性)或下标作为错误信息说。
在您添加的评论中:
If I remove the "?" the code complies ok but what happens when a node
doesn't have a name?
将 String?
与文字 String
值进行比较是完全有效的,而无需先展开变量。如果可选的是 nil
,那么 nil
不等于任何文字 String
所以 if
将简单地失败。
在 Swift 游乐场中尝试以下操作:
var str: String? = nil
if str == "hello" {
println("it is hello")
} else {
println("not hello") // prints "not hello"
}
// Here we reassign str, but it is still a String?
str = "hello"
if str == "hello" {
println("it is hello") // prints "it is hello"
} else {
println("not hello")
}
所以它是完全安全的,只需将 paintedNode.name
与 "paintedArea"
进行比较,如果节点没有名称,则 paintedNode.name
将是 nil
并且 if
将失败,就像它有不同的名称一样。
更新到 Swift 1.2 / Xcode 6.3 导致了以下错误:
了解 1.1 和 1.2 之间发生的变化的人可以帮助解决这里发生的问题吗?所有帮助表示赞赏!感谢您阅读到这里!
在Swift 1.2 中,在变量后跟?
展开它是非法的。 ?
用在 可选链 中,然后必须跟在方法调用、成员查找(即 属性)或下标作为错误信息说。
在您添加的评论中:
If I remove the "?" the code complies ok but what happens when a node doesn't have a name?
将 String?
与文字 String
值进行比较是完全有效的,而无需先展开变量。如果可选的是 nil
,那么 nil
不等于任何文字 String
所以 if
将简单地失败。
在 Swift 游乐场中尝试以下操作:
var str: String? = nil
if str == "hello" {
println("it is hello")
} else {
println("not hello") // prints "not hello"
}
// Here we reassign str, but it is still a String?
str = "hello"
if str == "hello" {
println("it is hello") // prints "it is hello"
} else {
println("not hello")
}
所以它是完全安全的,只需将 paintedNode.name
与 "paintedArea"
进行比较,如果节点没有名称,则 paintedNode.name
将是 nil
并且 if
将失败,就像它有不同的名称一样。