iosswiftxcodereturnxcode10.1

Xcode 10.1 "return" statement doesn't stop function execution


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!


Solution

  • 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")
    }