I'm trying to follow a course on Swift by Stanford on iTunes U but I got stuck when it came to creating the enter key. As soon as I press the enter key in the simulator the app crashes and gives me no description of why. Here's the code:
import UIKit
class ViewController: UIViewController {
@IBOutlet var display: UILabel!
var userIsInTheMiddleOfTypingNumber = false
@IBAction func appendDigit(sender: UIButton) {
let digit = sender.currentTitle!
if userIsInTheMiddleOfTypingNumber == false {
display.text = digit
userIsInTheMiddleOfTypingNumber = true
} else {
display.text = display.text! + digit
}
}
var operandStack = Array<Double>()
@IBAction func enter() {
userIsInTheMiddleOfTypingNumber = false
operandStack.append(displayValue)
print("operandStack = \(operandStack)")
}
var displayValue: Double {
get {
return NSNumberFormatter().numberFromString(display.text!)!.doubleValue
} set {
display.text = "\(newValue)"
userIsInTheMiddleOfTypingNumber = false
}
}
}
This was my first app that I worked on too! This happens to me all the time. What probably happened is you are getting a SIGABRT because the function from the button has been changed or has a bad reference. A simple way to fix this is like so:
Hope this helps! :)