Bronze Challenge messy solution

Hi @jgegrm here is my solution if it helps.

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    
    let existingTextHasDecimalSeparator = textField.text?.range(of: ".")
    let replacementTextHasDecimalSeparator = string.range(of: ".")
    let isAlphaneumeric = string.rangeOfCharacter(from: CharacterSet.letters)
    
    if ((existingTextHasDecimalSeparator != nil && replacementTextHasDecimalSeparator != nil) || isAlphaneumeric != nil) {
        return false
    } else {
        return true
    }
}

I wrote an explanation here that might help others understand the code.

1 Like