Group Group Group Group Group Group Group Group Group Forums

Swift 2 Tutorial Part 2: A Simple iOS App

In the second part of this Swift 2 tutorial series, learn how to make a simple iOS app with Storyboards: a tip calculator.

This is a companion discussion topic for the original entry at = Double((totalTextField.text! as NSString).doubleValue)

Here you need to convert a String to a Double. This is a bit of a hack to do this; hopefully there will be an easier way in a future update to Swift.

Here is a “swift 2” way:

@IBAction func calculateTapped(sender: AnyObject) { guard let text = totalTextField.text, let val = Double(text) else { resultsTextView.text = "Invalid total"; return } = val // ... }

Update Needed: In Step 8 of the section “Creating your Views”, you may wish to update the screenshot to reflect the presence of the stack views icon.

Most importantly, the reference to “the third button in the lower right” needs to change to “the fourth button in the lower right”

Converting a String to a Double in Swift 2:

Yep, this caught me out!