Bronze Challenge. Disallow Alphabetic Characters


#1

Hello,
This is my solution.
What do you think about?

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

Thanks in advance.


#2

improved

        func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
            return string.rangeOfCharacter(from: CharacterSet.letters) == nil