我为一个简单的计算器应用程序遇到了这些代码行。
func processOperation (operation: Operation) {
if currentOperation != Operation.Empty {
if runningNumber != "" {
rightValStr = runningNumber
runningNumber = ""
if currentOperation == Operation.Multiply {
result = "\(Double(leftValStr)! * Double(rightValStr)!)"
} else if currentOperation == Operation.Divide {
result = "\(Double(leftValStr)! / Double(rightValStr)!)"
} else if currentOperation == Operation.Subtract {
result = "\(Double(leftValStr)! - Double(rightValStr)!)"
} else if currentOperation == Operation.Add {
result = "\(Double(leftValStr)! + Double(rightValStr)!)"
}
leftValStr
被声明为var leftValStr = ""
rightValStr
也被声明为var rightValStr =""
我想知道使用“!”的目的是什么 在\(Double(leftValStr)! / Double(rightValStr)!)
是为了?据我所知,“!” 用于展开可选。leftValStr 和 rightValStr 这里没有声明为 Optional 那么为什么我们必须解开它们呢?