0
我正在製作一個最基本的計算器,從斯坦福在線課程中學習。我只做了一個「乘法」按鈕,以便在數組「operandStack」中乘以兩個值,但是每次程序崩潰時,我都不知道爲什麼。發送到實例的快速無法識別的選擇器
import UIKit
class ViewController: UIViewController
{
@IBOutlet weak var display: UILabel!
var userIsInTheMiddleOfTypingANumber:Bool = 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 "×":
if operandStack.count >= 2 {
displayValue = operandStack.removeLast() * operandStack.removeLast()
enter()
}
default: break
}
}
var operandStack = Array<Double>()
@IBAction func enter() {
userIsInTheMiddleOfTypingANumber = false
operandStack.append(displayValue)
println("operandStack = \(operandStack)")
}
var displayValue: Double {
get {
return NSNumberFormatter().numberFromString(display.text!)!.doubleValue
}
set {
display.text = "\(newValue)"
userIsInTheMiddleOfTypingANumber = false
}
}
}
你可以發佈崩潰描述嗎?你的代碼中有很多不安全的解包,因此很難找出問題的根源。 – 2015-03-13 14:04:24
的輸出是這樣的:operandStack = [6.0] operandStack = [6.0,5.0] 2015-03-14 08:56:50.334計算器[2640:62426] - [Calculator.ViewController操作]:無法識別的選擇器發送到實例0x7fedc161c8a0 2015-03-14 08:56:50.339計算器[2640:62426] ***由於未捕獲的異常'NSInvalidArgumentException',原因:' - [Calculator.ViewController操作]:無法識別的選擇器發送到實例0x7fedc161c8a0' – 2015-03-13 21:58:52