roh*_*mar 1 if-statement ios swift
我正在通过 swift文档学习 Swift 。在学习控制流语句时,在 if-else 部分语句中,我使用了文档中给出的这段代码,但它给出了一个错误,指出此语法是错误的。
let temperatureInCelsius = 25
let weatherAdvice = if temperatureInCelsius <= 0 {
"It's very cold. Consider wearing a scarf."
} else if temperatureInCelsius >= 30 {
"It's really warm. Don't forget to wear sunscreen."
} else {
"It's not that cold. Wear a T-shirt."
}
print(weatherAdvice)
Run Code Online (Sandbox Code Playgroud)
错误:
在此代码之后的文档中,又给出了一个代码,该代码给出了相同的错误,再加上一个错误“nil”需要上下文类型”
let weatherAdvice = if temperatureInCelsius <= 0 {
"It's very cold. Consider wearing a scarf."
} else if temperatureInCelsius >= 30 {
"It's really warm. Don't forget to wear sunscreen."
} else {
"It's not that cold. Wear a T-shirt."
}
print(weatherAdvice)
Run Code Online (Sandbox Code Playgroud)
这是Swift 5.9 的一项功能,您需要在 Xcode 14 或 Xcode 15 beta 中安装最新的 Swift 5.9 工具链。
否则按照传统方式进行
let temperatureInCelsius = 25
let weatherAdvice: String
if temperatureInCelsius <= 0 {
weatherAdvice = "It's very cold. Consider wearing a scarf."
} else if temperatureInCelsius >= 30 {
weatherAdvice = "It's really warm. Don't forget to wear sunscreen."
} else {
weatherAdvice = "It's not that cold. Wear a T-shirt."
}
print(weatherAdvice)
// Prints "It's not that cold. Wear a T-shirt."
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
68 次 |
| 最近记录: |