1

I'm trying to complete the CS193P Course independently. I am on assignment 2 of the course and part of the assignment asks for me to do the following:

"Change the computed instance variable displayValue to be an Optional Double rather than a Double"

I was able to change displayValue to be an Optional Double, but now my UILabel which shows the displayValue now will display the optional value instead of the double value (which makes sense).

Example:

5 (then press enter) will display a value of Optional(5.0) in the UILabel.

Here is what I tried:

I determined that result and displayValue! will return a double.

I tried changing display.text = result to display!.text! = result but that did not fix the issue.

Here is a snippet of my code, I think you shouldn't need any more but please comment if you think I should show something else!

P.S. the name of the display is display

@IBAction func enter() {
    if let result = brain.pushOperand(displayValue!) {
        displayValue = result
    } else {
        displayValue = 0
    }
}

var displayValue: Double? {
    get {
        return NSNumberFormatter().numberFromString(display.text!)!.doubleValue
    }
    set{
        display!.text! = "\(newValue)"     //display is the UILabel
    }
Sami
  • 579
  • 5
  • 25
  • 1
    The problem is that `newValue` is an optional that must be unwrapped when you create the label. `display.text = "\(newValue!)"`. Of course, that will crash if `displayValue` gets set to `nil`, so you should consider how you want to handle that. – vacawama Aug 10 '15 at 17:39
  • Yes--I was planning on using an "if let" to handle that case of nil. I just wanted to figure out how to get it to display correctly first. EDIT: Sorry--Just saw that you added an exclamation to "\newValue", that fixed my issue. Thanks for the advice – Sami Aug 10 '15 at 17:52
  • @vacawama any suggestions on how to handle the case of `nil`? I keep crashing :(. The professor in the video originally would have the `displayValue` equal zero in the case of `nil` but he said that was a bad way of doing it because it really should equal `nil` – Sami Aug 11 '15 at 18:27
  • I've watched earlier versions of those classes, just not the latest Swift ones. Prof. H is quite good. What do you want to happen in the case of nil. Set display to empty string? "" – vacawama Aug 11 '15 at 18:59

1 Answers1

3

I believe the display is supposed to be blank when, for example, the stack cannot be evaluated to give a result.

So you can just use

var displayValue: Double? {
    get { return NSNumberFormatter().numberFromString(display.text!)?.doubleValue }
    set { if newValue != nil { display.text = "\(newValue!)" }
            else { display.text = " " }
    }
}

And then for "enter"

@IBAction func enter() {
    if let dV = displayValue {
        brain.pushOperand(dV)
        displayValue = brain.evaluate()
    }
    userIsInTheMiddleOfTypingANumber = false
}

This solution worked for me. I am working through this course independently too. It is tricky and I have to listen to the lectures LOTS of times :-)

Jacki

Jacki
  • 46
  • 4