2

The return statement in Xcode 10.1 is not honoured by debugger,

For eg.,

    func doSomething() {

        print("Task A")
        return

        print("Task B")
    }

This prints

Task A
Task B //This is not expected to be printed as we have a `return` before this line 

Can someone help me!

Saif
  • 2,678
  • 2
  • 22
  • 38
  • That should not happen. Try a clean and rebuild. Sometimes Xcode loses its mind and does not rebuild your source files after you make changes. – Duncan C Mar 21 '19 at 13:12
  • 2
    @DuncanC: It *does* happen (see the linked-to Q&A). Swift executes the statement `return print("Task B")`, which prints the string and returns void. – Whether that should be considered a bug or not is a different story. Inserting a semicolon helps. – Martin R Mar 21 '19 at 13:14
  • @MartinR, well it's definitely not a bug, just a natural quirk of non-mandatory-semicolon syntax. – user28434'mstep Mar 21 '19 at 13:28

1 Answers1

8

Because expression after return is treated as argument of return.
So your code understood by compiler as:

func doSomething() {
    print("Task A")
    return print("Task B")
}

To prevent it you can use semicolon to explicitly separate this expressions.
Like that:

func doSomething() {
    print("Task A")
    return;
    print("Task B")
}
ManWithBear
  • 2,787
  • 15
  • 27