-2

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!

OmarAguinaga
  • 707
  • 1
  • 8
  • 17
  • yes: dont put code after the `return` statement. Have you tried inputting 1.5 for example and see if that actually works? – luk2302 May 31 '17 at 16:59
  • That's good you learning iOS by book. For better knowledge, its better if you prefer Apple doc's bcz nothg better then that and main thing new something changed IOS you first got in apple doc rather than any other website and book. – dahiya_boy May 31 '17 at 17:04
  • I second Rashwan's answer. Also good to note you can change they keyboard type of the text field to only show the number pad or decimal pad. :) – karnett May 31 '17 at 17:10
  • @KimArnett changing keyboard type will not work in case of iPad – Maddy May 31 '17 at 18:33
  • @Maddy Yes, thanks for that clarification. – karnett May 31 '17 at 18:54
  • @karnett Never rely on a specific keyboard. Users can use external keyboards and users can copy and paste any text into a text field. A specific keyboard is useful but you must always properly filter valid text in the `shouldChangeCharactersIn` method regardless of the chosen keyboard. – rmaddy Jun 02 '17 at 01:01

2 Answers2

1

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

}
Maddy
  • 1,660
  • 11
  • 24
  • 1
    The code in this answer still has issues because it still allows people to enter a string such as `1.2.3.4.5`. – rmaddy Jun 02 '17 at 00:59
  • Yes @rmaddy that's the problem, everyone is saying that I should remove the part below the return statement but that part is useful as well. My question is how do I keep both and remove the warning. – OmarAguinaga Jun 07 '17 at 18:30
0

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.

Rashwan L
  • 38,237
  • 7
  • 103
  • 107