Bronze Challenge

#1

This solution uses the letterCharacterSet method of NSCharacter set:

[code]func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
let existingTextHasDecimalSeparator = textField.text?.rangeOfString(".")
let replacementTextHasDecimalSeparator = string.rangeOfString(".")
let abc = NSCharacterSet.letterCharacterSet()
let replacementTextHasLetter = string.rangeOfCharacterFromSet(abc)

        if replacementTextHasLetter != nil {
            return false
        }
    
    if existingTextHasDecimalSeparator != nil && replacementTextHasDecimalSeparator != nil {
        return false
    }
    else {
    return true
    }
}

[/code]

#2

Clear, simple & works great. Thanks! :smiley:

#3

Just curious why all the “let” statements. For instance, why not just write:

if ((string.rangeOfCharacterFromSet(NSCharacterSet.letterCharacterSet())) != nil

Why create “abc” and “replacementTextHasLetter”?