我被困在以下问题集上。有什么想法吗?
var hasFuel == "true"
if hasFuel == "true" {
print("Let's go for a drive")
} else if hasFuel != "true" {
print("We're not going anywhere!")
}
答案 0 :(得分:2)
这是赋值运算符与相等运算符的错误。
代码无法编译
'=='不是前缀一元运算符
您可能想分配该值
var hasFuel = "true"
更好
var hasFuel = true
这避免了第二个if
if hasFuel == true {
print("Let's go for a drive")
} else {
print("We're not going anywhere!")
}
或更短
if hasFuel {
print("Let's go for a drive")
} else {
print("We're not going anywhere!")
}