iosswiftcs193p

Calculator in Swift Course crash


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
    }
}

}


Solution

  • 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:

    1. Click on the button that is causing the program to crash in the storyboard
    2. Go to the Show Connections Inspector (Blue arrow with a circle *See picture)
    3. Click the black x in the Connections inspector (it should show up like this in the picture, my connection is blurEffect).

    Show Connections Inspector

    1. Go back to the view controller and comment out or delete the reference to the old button connections (There should be a black dot by the function to say that there is a missing connection).
    2. Try relinking the function button back to the code and put the old code back in the function.

    Hope this helps! :)