How do you tell if a string is empty in Swift? That depends on what you mean by “empty”. You might mean a string with zero length, or maybe also an optional string that is nil. What about a “blank” string that only contains whitespace. Let’s see how to test for each of those conditions with Swift.
Using isEmpty
A Swift String
is a collection of characters and the Collection
protocol already has a test for an empty collection:
1 | var isEmpty: Bool { get } |
We have access to the source code for Collection.swift
in the standard library so we can see what this does:
1 | public var isEmpty: Bool { |
If the startIndex
and endIndex
of the collection are the same the collection is empty. Using this for a String
:
1 | "Hello".isEmpty // false |
Note: Use isEmpty rather than comparing count to zero which requires iterating over the entire string:
1 | // Don't do this to test for empty |
What about whitespace?
Sometimes I want to test not only for an empty string but for a blank string. For example, I want a test that also returns true for each of these strings:
1 | " " // space |
I’ve seen people do this by first trimming whitespace from the string and then testing for empty. With Swift 5, we can make use of [0221-character-properties][0221-character-properties] to directly test for whitespace. We could write the test like this:
1 | func isBlank(_ string: String) -> Bool { |
That works but a simpler way to test all elements in a sequence is to use allSatisfy. Rewriting as an extension of String:
1 | extension String { |
This is looking promising:
1 | "Hello".isBlank // false |
What about optional strings?
We can extend the solution to allow for optional strings. Here’s an extension to Optional where the wrapped element is a String:
1 | extension Optional where Wrapped == String { |
Using optional chaining with a default value we return true if the optional string is nil else we test the String as before. We can now also write:
1 | var title: String? = nil |
Testing for a “blank” string iterates over the string so don’t use it when isEmpty is all you need.
Happy coding!
[Source: useyourloaf]