4

I know there are tons of stack overflow pages out there that explain how to do this but everytime I take the code from here and put it in i get the same error and that error is value of "string?" has no member "text" Any ideas of a solid way that will work for checking if a textfield is empty in swift?

let userEmail = userEmailTextField.text;
// Check for empty fields
if (userEmail.text.isEmpty) {
    // Display alert message
    return;
}
4
  • The error points to incorrect use of the text property. Please show your code, otherwise it's hard to guess what's going on. Commented Jul 2, 2016 at 17:12
  • Just posted the code with the error Commented Jul 2, 2016 at 17:16
  • 1
    Your userEmail variable already contains the text of the userEmailTextField. No need to do another userEmail.text, just do userEmail.isEmpty Commented Jul 2, 2016 at 21:23
  • in Android you have a method 'editTextView.isBlankOrEmpty()'. When it comes to iOS ?! Commented Apr 30, 2020 at 5:16

6 Answers 6

37

This post is given a good answer (it's a pity it has no "accepted" mark). Use (self.field.text?.isEmpty ?? true).

Assume your textField is declared as:

    @IBOutlet weak var textField: UITextField!

You can check its emptiness with:

    if textField.text?.isEmpty ?? true {
        print("textField is empty")
    } else {
        print("textField has some text")
    }

To use the variables in your edited post:

    let userEmail = userEmailTextField.text;
    // Check for empty fields
    if userEmail?.isEmpty ?? true {
        // Display alert message
        return;
    }

or:

    // Check for empty fields
    if userEmailTextField.text?.isEmpty ?? true {
        // Display alert message
        return;
    }
Sign up to request clarification or add additional context in comments.

2 Comments

I put that exact code in and i still get the same error when i change "textfield" to "userEmail"
It seems you edited your post while I am writing. And userEmail in your edited post is a little bit misleading. I'll add some codes adapting your edited code.
5

The text property is an optional. So it can contains a String or nil.

If you want to treat nil as an empty String then just write

let isEmpty = (textField.text ?? "").isEmpty

4 Comments

I dont really understand what you mean by that, i just want userEmail to equal userEmailTextField
Im not sure what nil is and or how to use it
Why would i need to do anything to isEmpty anyways its a built in
@BestThereEverWas you need to understand some of the basic elements of Swift in order to understand the answers you get/got. Optionals is a fundamental aspect of Swift, and failure to understand them leads to the kinds of questions like you asked and the answers like you got. If you don't know what "nil" is, then you need to learn about it. Every Swift primer discusses them at length....
3

Alternatively you can also use:

Swift 3:

if (textField.text.characters.count > 0) {
   print("text field not empty")
} else {
   print("text field empty")
}

Swift 4.x and above:

if (textField.text.count > 0) {
       print("text field not empty")
    } else {
       print("text field empty")
    }

3 Comments

I like this solution. I think in Swift 5 it is if (nameField.text!.count > 0)
Yes, @Nrc that's true. I'll update my answer. Thanks.
Be aware that your solution do not cover empty spaces. I mean the textField could "look" empty, with no letters, but if the user writes an space (perhaps by accident) it will register as not empty
1

Give you an example picture and cover code.

@IBAction func save(_ sender: Any) {
    print("Saving...")
    //CHECK MANDATORY FIELDS
    checkMandatoryFields()
}

private func checkMandatoryFields(){

    //CHECK EMPTY FIELDS
    if let type = typeOutle.text, let name = nameOutlet.text, let address = addressOutlet.text, type.isEmpty || name.isEmpty || address.isEmpty {
        print("Mandatory fields are: ")
        errorDisplay(error: "Mandatory fields are: Type, Name, Address.")
        return
    }

    //CHECK SPACE ONLY FIELDS
}

enter image description here

Comments

0

Here's the correct answer for this.

textField.text = ""
if (textField.text.isEmpty) {
    print("Ooops, it's empty")
}

Comments

0

It was this check that helped me since it was necessary for me to send a request to the API, and it was necessary to send nill instead of "" if the textField is without text.

textField.text!.count > 0  ? textField.text : nil

Alternatively, you can check this way (but this option did not fit me):

if textField.text != nil {

} else {

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.