Prevent user from entering text

156 Views Asked by At

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!

2

There are 2 best solutions below

2
On

The warning,

'Code after return will never be executed

is itself self explanatory,The compiler has done analysis on your code and is telling you that any content below return allowedCharacters.isSuperset(of: characterSet) will never be executed.

As you said that: 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?

Ans: In order to remove this warning just remove the entire code below this statement return allowedCharacters.isSuperset(of: characterSet), the warning will be removed.

If you are looking for the better approach i would recommend the below approach, there would be better approach than this but it's not in my knowledge.

BTW,

To implement the functionality i.e. Prevent user from entering text. You can simply change the keyboard type,

textField.keyboardType = .decimalPad // this only works in iPhone not in iPad

As changing keyboard type only works in iPhone but not in iPad it's better to implement the characters allowed in textfield, in textfield delegate method shouldChangeCharactersIn as below,

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

let inverseSet = NSCharacterSet(charactersIn:".0123456789").inverted // characters you want to allow in textfield

let components = string.components(separatedBy: inverseSet)

let filtered = components.joined(separator: "")

return string == filtered

}
0
On

The error message is saying:

Code after return will never be executed

Which basically means that whenever you add a return statement no code after that statement will be executed.

The warning is from this row:

return allowedCharacters.isSuperset(of: characterSet)

I don´t think you need the code below that because in that function I bet you only want to return the allowed characters otherwise return false.