在快速闭包中返回Void和()之间的区别 [英] Difference between returning Void and () in a swift closure

查看:173
本文介绍了在快速闭包中返回Void和()之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这些关闭之间有什么区别?

Whats the difference between these closures?

let closureA: () -> ()

let closureB: () -> Void


推荐答案

如果查看标题,你会看到Void是()的类型,

If you look into headers, you will see that Void is typealias for (),

/// The empty tuple type.
///
/// This is the default return type of functions for which no explicit
/// return type is specified.
typealias Void = ()

您可以使用这样的游乐场验证,

You can verify that using playground like this,

let a  = Void()
println(a) // prints  ()

更新

如果您希望查看声明在头文件中无效,在上面的代码中,在swift playground或xcode编辑器中输入如下代码,

If you wish to see the declaration of Void in header file, in the above code, type the code below in swift playground or xcode editor like so,

  let a: Void  =  ()

然后,cmd +点击上面表达式中的Void关键字,你将被带到头文件,你可以在其中实际看到Void的声明。

Then, cmd + click on the "Void" keyword in above expression, you will be taken to the header file where you can actually see the declaration for Void.

该文件已更新,有更多信息,如下所示,

The document has been updated with more information which is like this,


/// The return type of functions that don't explicitly specify a return type;
/// an empty tuple (i.e., `()`).
///
/// 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 = ()


这篇关于在快速闭包中返回Void和()之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆