如何在Swift 3中记录函数的闭包参数的参数? [英] How do you document the parameters of a function's closure parameter in Swift 3?

查看:84
本文介绍了如何在Swift 3中记录函数的闭包参数的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Xcode 8 beta和Swift 3中,当您有一个将闭包作为参数的方法时,例如:

In Xcode 8 beta and Swift 3, when you have a method that takes a closure as a parameter, for example:

func foo(bar: (String) -> Void) {
    bar("Hello, world")
}

如何记录闭包需要的参数?例如,如果我这样写:

How do you document the parameters the closure takes? For example, if I wrote this:

/// Calls bar with "Hello, world"
/// - parameter bar: A closure to call
func foo(bar: (String) -> Void) {
    bar("Hello, world")
}

然后,快速帮助如下所示:

Then the quick help looks like this:

我想知道什么是语法,该语法使我可以编写一些文本来代替无描述".非常感谢!

I would like to know what the syntax is that will allow me to write some text to replace "No description." Many thanks!

推荐答案

据我所知,只有标记了闭包参数,您才能记录它们:

As far as I know, you can only document the closure parameters if you label them:

/// Calls bar with "Hello, world"
/// - parameter bar: A closure to call
/// - parameter theString: A string to use
func foo(bar: (theString: String) -> Void) {
    bar(theString: "Hello, world")
}

这不是理想的选择:它会强制您在调用闭包时使用参数标签,并且如果存在命名冲突,则似乎无法区分两者.

This is less than ideal: it forces you to use an argument label when you call the closure, and if there are naming conflicts, there seems no way to distinguish between the two.

编辑:正如@Arnaud所指出的,您可以使用_来防止在调用闭包时必须使用参数标签:

Edit: As @Arnaud pointed out, you can use _ to prevent having to use the parameter label when calling the closure:

/// Calls bar with "Hello, world"
/// - parameter bar: A closure to call
/// - parameter theString: A string to use
func foo(bar: (_ theString: String) -> Void) {
    bar("Hello, world")
}

实际上,这是Swift 3中唯一有效的方法,因为参数标签不再是类型系统的一部分(请参见SE-0111 ).

In fact, this is the only valid approach in Swift 3 because parameter labels are no longer part of the type system (see SE-0111).

这篇关于如何在Swift 3中记录函数的闭包参数的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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