Bronze Solution

#1

EDIT: here is the new code… It is now working.

func textField(textField: UITextField,
            shouldChangeCharactersInRange range: NSRange,
            replacementString string: String) -> Bool {
            
        let existingTextHasDecimalSeparator = textField.text?.rangeOfString(".")
        let replacementTextHasDecimalSeparator = string.rangeOfString(".")
        
        let decimal = NSCharacterSet(charactersInString: "0123456789.")
        let isDecimalCharacter = string.rangeOfCharacterFromSet(decimal)
        
        if isDecimalCharacter != nil &&
           (existingTextHasDecimalSeparator == nil || replacementTextHasDecimalSeparator == nil) {
            return true
        } else {
            return false
        }
    }
#2

That’s not an acceptable question on any computer programming forum. Instead you need to:

  1. Post your code along with any errors.

If there are no errors, then post:

  1. Your code.
  2. Your expected results.
  3. The actual results.

But I’ll just mention that the variable name existingLetters is so poor that it could be misleading you. Or, maybe you don’t realize what the string parameter variable contains? From the docs:

[quote]string:
The replacement string for the specified range. During typing, this parameter normally contains only the single new character that was typed, but it may contain more characters if the user is pasting text. When the user deletes one or more characters, the replacement string is empty.[/quote]

I would explore the result of your conditional if someone types: “1” then “a”. Also, to debug your conditional, I would print out the values of each variable in the conditional immediately before the conditional executes.

#3

Yeah that was a terrible question. I was rushing off to work. I wrote it so I wouldn’t forget about it because I planned to come back to it tonight.

I see the problem now. Thanks for the tip! :smiley: :smiley: :smiley: