首页 > 解决方案 > Swift function returns Void return type from other function

问题描述

I came across Swift code something like this today. The author is long gone.

func doSomething(string: String) {

    ...

    if doMore(string: String) != nil {
        return update(using: string)
    }

    ...
}

func udpate(using: String) {

    //does some stuff

}

Notice that doSomething() does not return a value. Nor does update(). At least not explicitly. As it says in the docs, Swift functions that don't return a value actually do. They return Void.

Therefore return update(using: string) is legal syntax. update() returns Void and so does doSomething() so the compiler doesn't complain and it runs.

Here's my question. Is return update(using: string) exploiting some handy side effect or is it exactly the same as this?

update(using: string)
return

Thanks for your consideration.

标签: swiftreturnvoid

解决方案


推荐阅读