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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access