user10368914
user10368914

Reputation:

Guard Let statement not triggering even when values are nil

Here is a UIBarButtonItem I have:

@IBAction func doneButtonPressed(_ sender: UIBarButtonItem) {
    print("doneButton Pressed")
    // Guard statement ensures that all fields have been satisfied, so that the JumpSpotAnnotation can be made, and no values are nil
    guard let name = nameTextField.text,
        let estimatedHeight = estimatedHeightTextField.text,
        let locationDescription = descriptionTextView.text else {
            nameRequiredLabel.isHidden = false
            heightRequiredLabel.isHidden = false
            descriptionRequiredLabel.isHidden = false
            print("User did not put in all the required information.")
            return
           }

The code below it in the IBAction is irrelevant since this is a guard let problem. It won't trigger even when I literally set the values to nil. In my viewDidLoad I put:

    nameTextField.text = nil
    estimatedHeightTextField.text = nil
    descriptionTextView.text = nil

And when I press the button, without changing the values of these texts, the guard let statement still doesn't trigger, and the rest of the function below executes. Any ideas why? Thanks.

Upvotes: 1

Views: 756

Answers (2)

Leo Dabus
Leo Dabus

Reputation: 236360

UITextField text property default value is an emptyString. It will NEVER return nil even if you assign nil to it before checking its value. BTW UIKeyInput protocol has a property called hasText exactly for this purpose. If you would like also to avoid the user entering only whitespaces and new lines you can trim them before checking if it is empty. You can extend UITextInput and implement your own isEmpty property. This would cover for UITextField and UITextView with a single implementation:

extension UITextInput {
    var isEmpty: Bool {
        guard let textRange = self.textRange(from: beginningOfDocument, to: endOfDocument) else { return true }
        return text(in: textRange)?.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty == true
    }
}

let textField = UITextField()
textField.text = " \n "
textField.isEmpty   // true

let textView = UITextView()
textView.text = " \n a"
textView.isEmpty   // true

Upvotes: 0

JTODR
JTODR

Reputation: 338

If you are just checking that the text field is empty then you can do something like:

guard 
    let estimatedHeight = estimatedHeightTextField.text, 
    !estimatedHeight.isEmpty, 
    let locationDescription = descriptionTextView.text,
    !locationDescription.isEmpty 
    else {
        nameRequiredLabel.isHidden = false
        heightRequiredLabel.isHidden = false
        descriptionRequiredLabel.isHidden = false
        print("User did not put in all the required information.")
        return
    }

Checkout out this answer: https://stackoverflow.com/a/24102758/12761873

Upvotes: 2

Related Questions