Can someone explain me the following behaviour in Swift?
func test() -> Bool {
print(\"1 before return\")
return false
print(\"1 after return\
func test2() is similar to func test2() -> Void
So your code gets treated as,
func test2() -> Void {
print("2 before return")
return print("2 after return")
}
Adding semicolon after print should fix it.
func test2() -> Void {
print("2 before return")
return; print("2 after return")
}
You can also see the error if you place value type after return line and you will understand more,
func test2() {
print("2 before return")
return
2
}
error: unexpected non-void return value in void function 2 ^