Handling empty optional strings in Swift

String optionals in Swift are a little peculiar. The String? type sort of has two “invalid” values: nil and "". This can make it cumbersome to handle and ripe for bugs and unexpected behavior.

Take for example a UITextField ’s text property, which is a String?. We could write the following code to appropriately alert the user when the text field is left empty, while unwrapping the text string for later use:

if let title = textField.text { if title.isEmpty { // Alert: textField is empty! } // title String is now unwrapped, non-empty, and ready for use}

Any combination of guard let or optional chaining, like textField.text?.isEmpty ?? true could’ve been used here as well. Either way, the implementation gets messy, and you’ll be dealing with multiple and/or nested conditionals.

Extensions allow you to add new functionality to an existing class, structure, enumeration, or protocol type. With a where clause, you can restrict the extension to specific or conforming types.

So how can we apply this to handling the empty string value for a String?? Let’s define a new computed property nilIfEmpty in an extension restricted to optional Strings, which transforms an empty String to nil or returns the original String?.