Modifying an Optional in Place

You can modify an optional in place. Add a call to appendContentsOf(_:) on upCaseErrorDescription.

Listing 8.10  Modifying in place

...
upCaseErrorDescription?.appendContentsOf(" PLEASE TRY AGAIN.")
upCaseErrorDescription

Modifying an optional in place can be extremely helpful. In this case, all we wanted to do was update a string inside of an optional. We did not need anything returned. If there was a value inside of the optional, then we wanted to add some text to the string. If there was no value, then we did not want to do anything.

This is exactly what modifying an optional in place does. The ? at the end of upCaseErrorDescription works similarly to optional chaining insofar as it exposes ...

Get Swift Programming: The Big Nerd Ranch Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.