简体   繁体   中英

Return void func from func in Swift

I'm new in Swift. I'm trying to return void(in Java terminology) func from another func in Swift, but I get strange output:

func returnVoidFunc() -> (() -> ()) {
    func innerOne() {
        print("Hello from inside")
    }
    return innerOne
}

var initFunc = returnVoidFunc()
print(initFunc())

output is:

Hello from inside
()

Why I've got () and how to return void func correct?

returnVoidFunc correctly returns a () ->() function, initFunc is a reference to that function.

The reason you see () printed is because you are evaluating initFunc , and since it returns () , and that's what's printed.

In Swift, by the way, () is basically the same as Void .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM