Swift 函数从其他函数返回 Void 返回类型

时间:2021-01-12 04:38:06

标签: swift return void

我今天遇到了类似这样的 Swift 代码。作者好久不见了。

func doSomething(string: String) {

    ...

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

    ...
}

func udpate(using: String) {

    //does some stuff

}

请注意 doSomething() 不返回值。 update() 也没有。至少不是明确的。正如文档中所说,不返回值的 Swift 函数实际上会返回值。他们返回虚空。

因此 return update(using: string) 是合法的语法。 update() 返回 VoiddoSomething() 也返回,因此编译器不会报错并运行。

这是我的问题。 return update(using: string) 是利用了一些方便的副作用还是与此完全相同?

update(using: string)
return

感谢您的考虑。

0 个答案:

没有答案