我是 Swift 新手,正在尝试学习可选值的概念。我的问题是,在之前的代码上下文中,是否有必要将变量 'possibleIntegerValue' 设置为 'optional Int(Int?)' 或者可以省略?
符号并将 'possibleIntegerValue' 设置为 'Int' 类型?
如果我确实将类型从可选 Int 更改为 Int,它会产生什么样的影响?
let numberSymbol: Character = "三" // Simplified Chinese for the number 3
var possibleIntegerValue: Int?
switch numberSymbol {
case "1", "١", "一", "๑":
possibleIntegerValue = 1
case "2", "٢", "二", "๒":
possibleIntegerValue = 2
case "3", "٣", "三", "๓":
possibleIntegerValue = 3
case "4", "٤", "四", "๔":
possibleIntegerValue = 4
default:
break
}
if let integerValue = possibleIntegerValue {
print("The integer value of \(numberSymbol) is \(integerValue).")
} else {
print("An integer value could not be found for \(numberSymbol).")
}