美文网首页
Swift Return

Swift Return

作者: 幸运者_Lucky | 来源:发表于2018-05-04 10:23 被阅读11次

    The return type of functions that don’t explicitly specify a return type, that is, an empty tuple ().

    Declaration

    typealias Void = ()
    

    Discussion

    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"
    

    Reference: Void

    相关文章

      网友评论

          本文标题:Swift Return

          本文链接:https://www.haomeiwen.com/subject/krjlrftx.html