Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Xcode Swift app keeps crashing and I am not sure why?

I am following a project to make a calculator and when I run it it crashes where there was no problem before.

import UIKit

class ViewController: UIViewController {
    // Instance Variable (Property)
    // This is actuall an optional (even tho it does not look like one.
    // The "!" below means that we do not have to put it after "display" everytime we write it. Implicitly Unwrapped Optional.
    @IBOutlet weak var display: UILabel!

    var userIsInTheMiddleOfTypingANumber = false


    @IBAction func appendDigit(sender: UIButton) {
        // Local variable aming the digit equal the number on the button.
        // The ! will unwrap the optionals, ie. the button values.
        let digit = sender.currentTitle!


        if userIsInTheMiddleOfTypingANumber {
            // display.text is an optional that can be a string. It must be unwrapped in order for it to be added to another string.
            display.text = display.text! + digit
        } else {
            display.text = digit
            userIsInTheMiddleOfTypingANumber = true
        }
    }



    @IBAction func operate(sender: UIButton) {
        let operation = sender.currentTitle!

        // Anytimee an operator is pressed.
        if userIsInTheMiddleOfTypingANumber{
            enter()
        }

        // Switch absolutely requires everycase is covered.
        switch operation {
        case "ร—": performOperation({ (op1: Double, op2: Double) -> Double in return op1 * op2
            })
//            case "รท":
//            case "+":
//            case "โˆ’":
            default: break
        }
    }



    func performOperation(operation: (Double, Double) -> Double) {
        if operandStack.count >= 2 {
            displayValue = operation(operandStack.removeLast(), operandStack.removeLast())
            enter()
        }
    }



    // Initializer.
    // The <> indicate what is in the array. When a button is pressed it is stored in an Array (see console when app is running).
    var operandStack = Array<Double>()

    @IBAction func enter() {
        userIsInTheMiddleOfTypingANumber = false
        operandStack.append(displayValue)
        println("operandStack = \(operandStack)")
    }

    // Computed Properties
    var displayValue: Double {
        get {
          return NSNumberFormatter().numberFromString(display.text!)!.doubleValue
        }
        set {
            // Converting "newValue" into a String.
            display.text = "\(newValue)"
            userIsInTheMiddleOfTypingANumber = false
        }
    }

}

It keeps crashing at the line:

   if operandStack.count >= 2 {

Any ideas?

1 Answer

Jayden Spring
Jayden Spring
8,625 Points

Because there is no argument named operandStack in scope, nor is there one declared in the ViewController.

If you have declared it somewhere else, we need to see what the object or type of operandStack is to help you.