Check If UITextField Contains Value

The below code example checks if the UITextField value is empty and can be used when we need to make sure that all required fields contain values for example and if otherwise, display an error message.

The firstNameTextField is a UITextField added to the UIView on the page. I simply refer to the name of the UITextField object and use isEmpty to check if is true. The exclamation mark at the end “!” is used to unwrap the value.

If you are interested in video lessons on how to write Unit tests and UI tests to test your Swift mobile app, check out this page: Unit Testing Swift Mobile App

Check If UITextField isEmpty

if (firstNameTextField.text?.isEmpty)!
{
  // Display Alert dialog window if the First Name TextField is empty
}

For more Swift code examples and tutorials, please check the Swift Code Examples page on this website.


Leave a Reply

Your email address will not be published.