Q: How to trigger code anytime a UItextfield is empty I would like to have code for anytime a UItextfield is empty. Not just on the viewdidload but all the time. I tried putting something like if textField.isEmpty == true in the editing changed action although the issue I was having is if you type more than 5 characters and then hold down backspace the code doesn't get triggered. Any ideas for what to do?

A: Hi you need to subscribe on editing changed 
How to check if the field is empty?
let textField = UITextField()
textField.addTarget(self, action: #selector(textChanged), for: .editingChanged)
@objc func textChanged () {
    if textField.text == "" || textField.text == nil {
        print("IS EMPTY")
    } else {
        print("NON EMPTY")
    }
}

How to set max length to UITextField
class ViewController: UIViewController, UITextFieldDelegate {
    
    override func viewDidLoad() {
        let textField = UITextField()
        textField.delegate = self
    }
        
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        guard let textFieldText = textField.text,
            let rangeOfTextToReplace = Range(range, in: textFieldText) else {
                return false
        }
        let substringToReplace = textFieldText[rangeOfTextToReplace]
        let count = textFieldText.count - substringToReplace.count + string.count
        return count <= 5
    }
    
}

