Check if Swift text field contains non-whitespace

This answer was last revised for Swift 5.2 and iOS 13.5 SDK.


You can trim whitespace characters from your string and check if it’s empty:

if !textField1.text.trimmingCharacters(in: .whitespaces).isEmpty {
    // string contains non-whitespace characters
}

You can also use .whitespacesAndNewlines to remove newline characters too.

Leave a Comment