121. Best code
@IBAction func operate(sender: UIButton) {
let operation = sender.currentTitle!
if userInTheMiddleOfTypingNumber {
enter()
}
switch operation {
case "×":
performOperation{
$1 * $0
}
case "÷":
performOperation{
$1 / $0
}
case "+":
performOperation{
$1 + $0
}
case "−":
performOperation{
$1 - $0
}
default:
break
}
}
122. 開根號
func performOperation2(operation:Double -> Double) {
if operandStack.count >= 1 {
displayValue = operation(operandStack.removeLast())
enter()
}
}
case "√":
performOperation { op1 in
sqrt(op1)
}
123. Objective-C does not support method overloading, you have to use a different
method name. When you inherited UIViewController you inherited NSObject and
made the class interopable to Obj-C. Swift on the other hand supports
overloading, that's why it works when you remove the inheritance
func performOperation(operation:Double -> Double) {
if operandStack.count >= 1 {
displayValue = operation(operandStack.removeLast())
enter()
}
}
func performOperation(operation:(Double, Double) -> Double) {
if operandStack.count >= 2 {
displayValue = operation(operandStack.removeLast(),
operandStack.removeLast() )
enter()
}
}