cs193p – Project #2 Assignment #2 Task #4

By Mark Schellhase (Own work) [GFDL (http://www.gnu.org/copyleft/fdl.html) or CC BY-SA 3.0 (http://creativecommons.org/licenses/by-sa/3.0)], via Wikimedia Commons

Please note, this blog entry is from a previous course. You might want to check out the current one.

The extra credit item from last week to turn displayValue into a Double? (i.e, an Optional rather than a Double) is now required. displayValue should return nil whenever the contents of the display cannot be interpreted as a Double. Setting displayValue to nil should clear the display.

… done that, too. However, hint #1 asks to consider using optional chaining, which we did not up to now.

Assuming there is always a text string, and that we handle the internationalization problem elsewhere, we can reduce the getter of displayValue to a single line using optional chaining:

return NSNumberFormatter().numberFromString(display.text!)?.doubleValue

The complete code for the task #4 is available on GitHub.


Leave a Reply

Your email address will not be published.