gpt4 book ai didi

ios - 在快速关闭中返回 Void 和 () 之间的区别

转载 作者:IT王子 更新时间:2023-10-29 05:18:52 25 4
gpt4 key购买 nike

这些闭包有什么区别?

let closureA: () -> ()

let closureB: () -> Void

最佳答案

如果查看标题,您会发现 Void 是 () 的类型别名,

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

您可以像这样使用 playground 来验证,

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

更新

如果你想在头文件中看到 Void 的声明,在上面的代码中,像这样在 swift playground 或 xcode 编辑器中输入下面的代码,

  let a: Void  =  ()

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

文档已更新,包含更多信息,如下所示,

/// 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 = ()

关于ios - 在快速关闭中返回 Void 和 () 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27422453/

25 4 0
文章推荐: swift - Swift 是否有一个隐式的对象初始化器,就像在 C# 中一样?
文章推荐: javascript - 使用 Typescript 延迟加载模式
文章推荐: javascript - Angular : use a variable into
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com