cs193p – Project #2 Assignment #2 Task #4

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.

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

cs193p – Project #2 Assignment #2 Task #3

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

Your UI should always be in sync with your Model (the CalculatorBrain).

… meaning we should reflect changes in the model in the UI … we will do that, too.

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

cs193p – Project #2 Assignment #2 Task #1

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

All of the changes to the Calculator made in lecture must be applied to your Assignment 1. Get this fully functioning before proceeding to the rest of the Required Tasks. And, as last week, type the changes in, do not copy/paste from anywhere.

… done …

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

cs193p – Lecture #5 – Objective-C Compatibility, Property List, Views

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

Objective-C Compatibility

Lecture #5 starts with theory on how Swift was built to be compatible with Objective-C and thus provides numerous seamless bridging methods:

  • NSString bridges to String
  • NSArray bridges to Array<AnyObject>
  • NSDictionary bridges to Dictionary<NSObject, AnyObject>
  • Int, Float, Double, Bool bridge to NSNumer – but not the other way around – use instead intValue, floatValue, doubleValue and boolValue

Additionally, it is possible to implicitly cast using as.

Continue reading “cs193p – Lecture #5 – Objective-C Compatibility, Property List, Views”

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

cs193p – Project #1 Assignment #1 Internationalization

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

When running on a real device, which region is set to something having not a “.” as comma symbol, the app crashes. Strangely it does not crash on the simulator.

One way to cope with this behavior is to force the number formatter to use the US locale all the time:

           if let displayText = display.text {
                let numberFormatter = NSNumberFormatter()
                numberFormatter.locale = NSLocale(localeIdentifier: "en_US")
                if let displayNumber = numberFormatter.numberFromString(displayText) {
                    return displayNumber.doubleValue
                }
            }

Continue reading “cs193p – Project #1 Assignment #1 Internationalization”

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail