Return true, shouldn't this be gone?

In the code example, there is a:

return true

after the print lines. Shouldn’t this be gone, in the finished version? How else will the remaining lines ever be reached?

Great textbook, one of the best.

  1. Can a function that is defined to return a Bool return nothing?

  2. Can you change the function definition so that it doesn’t return anything?

  3. What does returning true(or false) mean?

There are lot’s of code examples in the book. Would it kill you to actually post the code or specify a page number?

Sorry for not posting the example. I’m using a Kindle cloud reader, no page numbers, location 2301 0f 11899, and recovering from shoulder surgery, so 1 hand typing. Hoping I could skate…
here it is:

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

print("Current text: \(textField.text)")
print("Replacement text: \(string)")

return true // <= this return,  wouldn't it end the func here,  unconditionally?

let existingTextHasDecimalSeparator = textField.text?.rangeOfString(".")
let replacementTextHasDecimalSeparator = string.rangeOfString(".")

if existingTextHasDecimalSeparator != nil &&        return false
else {
    return true


Okay, all valid excuses. :slight_smile:

My questions pertained to a different code listing. In the book, for the code listing you posted, all of the following lines:

[code]print(“Current text: (textField.text)”)
print(“Replacement text: (string)”)

return true[/code]

are crossed out.

You are correct. I’m surprised Xcode did not warn that the following code was unreachable.

There must not be a good strike through font in the Kindle web reader.Thanks.