在快速closures中返回void和()之间的区别
这些closures有什么区别?
let closureA: () -> ()
和
let closureB: () -> Void
如果你看看标题,你会看到Void是()的typealias,
/// The empty tuple type. /// /// This is the default return type of functions for which no explicit /// return type is specified. typealias Void = ()
你可以validation使用这样的操场,
let a = Void() println(a) // prints ()
更新
如果你想在头文件中看到Void的声明,在上面的代码中,在swift的操作系统或xcode编辑器中input如下代码,
let a: Void = ()
然后,cmd +在上面的expression式中点击“Void”关键字,你会被带到头文件,在那里你可以看到Void的声明。
这个文件已经更新了更多的信息,就是这样,
/// The return type of functions that don't explicitly specify a return type; /// an empty tuple (ie, `()`). /// /// When declaring a function or method, you don't need to specify a return /// type if no value will be returned. However, the type of a function, /// method, or closure always includes a return type, which is `Void` if /// otherwise unspecified. /// /// Use `Void` or an empty tuple as the return type when declaring a /// closure, function, or method that doesn't return a value. /// /// // No return type declared: /// func logMessage(_ s: String) { /// print("Message: \(s)") /// } /// /// let logger: (String) -> Void = logMessage /// logger("This is a void function") /// // Prints "Message: This is a void function" public typealias Void = ()
相同。 这只是一个typealias,所以它的工作原理是一样的。
typealias Void = ()
听起来像埃里卡·萨登和苹果正试图坚持虚无: http : //ericasadun.com/2015/05/11/swift-vs-void/
根本没有区别
Void
是()的别名:
typealias Void = ()