early return from a void-func in Swift?

后端 未结 5 1879
情歌与酒
情歌与酒 2020-12-31 07:43

Can someone explain me the following behaviour in Swift?

func test() -> Bool {
    print(\"1 before return\")
    return false
    print(\"1 after return\         


        
5条回答
  •  猫巷女王i
    2020-12-31 07:53

    func noReturn() {...}
    

    is the same as

    func noReturn() -> (Void) {...} //or func noReturn() -> ()
    

    and since print(...) has the same signature it is ok to call return print(...) in a void function

提交回复
热议问题