我正在尝试Swift初学者建造计算器的项目。我目前正在尝试将基本运算符添加到计算器中。
然而,我不知道我做了什么,但突然间,所有基本操作员都从主控制器中消失了
但@IBAction func operator()左边的小+符号仍然表示存在基本运算符,但它无处可寻。
但是当我在Simulator中运行程序时,基本操作符会重新出现
代码:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var display: UILabel!
var userIsInTheMiddleOfTypingANumber = false
@IBAction func appendDigit(sender: UIButton) {
let digit = sender.currentTitle!
if userIsInTheMiddleOfTypingANumber {
display.text = display.text! + digit
} else {
display.text = digit
userIsInTheMiddleOfTypingANumber = true
}
}
@IBAction func operate(sender: UIButton) {
let operation = sender.currentTitle!
if userIsInTheMiddleOfTypingANumber {
enter()
}
switch operation {
case "×":performOperation {$0 * $1}
case "÷":performOperation {$1 / $0}
case "+":performOperation {$0 + $1}
case "−":performOperation {$1 - $0}
case "√":performOperation {sqrt($0)}
default: break
}
}
func performOperation(operation: (Double, Double) -> Double){
if operandStack.count >= 2{
displayValue = operation(operandStack.removeLast(), operandStack.removeLast())
enter()
}
}
private func performOperation(operation: Double -> Double){
if operandStack.count >= 1{
displayValue = operation(operandStack.removeLast())
enter()
}
}
var operandStack = Array<Double>()
@IBAction func enter() {
userIsInTheMiddleOfTypingANumber = false
operandStack.append(displayValue)
print("operantStack = \(operandStack)")
}
var displayValue: Double {
get {
return NSNumberFormatter().numberFromString(display.text!)!.doubleValue
}
set {
display.text = "\(newValue)"
userIsInTheMiddleOfTypingANumber = false
}
}
}
答案 0 :(得分:1)
对于您选择的当前尺寸类,将禁用这些按钮。如果您转到Utilities栏(右侧)并选择Attributes选项卡(中间的那个),请一直滚动到底部,确保选中第一个Installed框。享受斯坦福系列的乐趣。 See attached
答案 1 :(得分:1)