Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Chapter 4 bug in the section “Showing an Action Sheet”

In Beginning iPhone Development with Swift 2, Chapter 4, Showing an Action Sheet, part of the existing code is:

            let msg = self.nameField.text!.isEmpty
                        ? "You can breathe easy, everything went OK."
                        : "You can breathe easy, \(self.nameField.text),"
                            + " everything went OK."

That code results in the following alert, if the nameField text is "Jed":  You can breathe easy, Optional(“Jed”), everything went OK.

To solve the problem, forcibly unwrap the self.nameField.text value by adding an exclamation point. This is safe, because the test for it being nil has already been tried.

            let msg = self.nameField.text!.isEmpty
                        ? "You can breathe easy, everything went OK."
                        : "You can breathe easy, \(self.nameField.text!),"
                            + " everything went OK."

The changed code shows the following text in the alert:  You can breathe easy, Jed, everything went OK.

Comments

Sign In or Register to comment.