I'm following the book
IOS Programming : The Big Nerd Ranch Guide
And I'm in the part where you create a simple app that converts Fahrenheit to Celsius and the final Challenge is to prevent users from typing alphabetical characters, here is my try.
func textField(_ textField: UITextField,
shouldChangeCharactersIn range: NSRange,
replacementString string: String) -> Bool {
let allowedCharacters = CharacterSet.decimalDigits
let characterSet = CharacterSet(charactersIn: string)
return allowedCharacters.isSuperset(of: characterSet)
let existingTextHasDecimalSeparator = textField.text?.range(of: ".")
let replacementTextHasDecimalSeparator = string.range(of: ".")
if existingTextHasDecimalSeparator != nil,
replacementTextHasDecimalSeparator != nil{
return false
}else{
return true
}
}
It works but I get a warning that says 'Code after return will never be executed' is there a better way to do this to prevent this warning? Thank you in advance!