我知道目标c中(void)的含义,但我想知道这段代码的含义是什么:
(Void) -> (Void)
Run Code Online (Sandbox Code Playgroud)
在迅速.
Ami*_*ava 18
() -> ()只是意味着Void -> Void- 一个不接受任何参数且没有返回值的闭包.
在Swift中,Void是空元组的类型,().
typealias Void = ()
空元组类型.
这是未指定显式返回类型的函数的缺省返回类型.
举个例子
let what1: Void->Void = {}
Run Code Online (Sandbox Code Playgroud)
要么
let what2: Int->Int = { i in return i }
Run Code Online (Sandbox Code Playgroud)
都是具有不同类型的有效表达式.所以打印有类型()->() (aka Void->Void).严格来说,printThat有类型(() -> ()) -> () (aka (Void->Void)->Void
Void函数没有很多意义,因为Int函数等...... Swift中的每个函数都有一个类型,由函数的参数类型和返回类型组成.
最后,关于"void"函数,请注意这两个函数签名之间没有区别:
func myFunc(myVar: String) // implicitly returns _value_ '()' of _type_ ()
func myFunc(myVar: String) -> ()
Run Code Online (Sandbox Code Playgroud)
奇怪的是,你可以有一个可选的空元组类型,所以下面的函数与上面的两个不同:
func myFunc(myVar: String) -> ()? {
print(myVar)
return nil
}
var c = myFunc("Hello") /* side effect: prints 'Hello'
value: nil
type of c: ()? */
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6651 次 |
| 最近记录: |