mercredi 3 juin 2020

maxLength for switch text field

I have several text fields, each with a different number of maximum characters. How can I change the if branch to enum and use switch?

 //if -> switch
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        let newLength = (textField.text ?? "").count + string.count - range.length

        if(textField == textFieldA) {
            return newLength <= 6
        }
        if(textField == textFieldB) {
            return newLength <= 7
        }
        if(textField == textFieldC) {
            return newLength <= 8
        }
        return true
    }

Aucun commentaire:

Enregistrer un commentaire