xcode 如何在 Swift 3 中记录函数闭包参数的参数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38669725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 09:05:35  来源:igfitidea点击:

How do you document the parameters of a function's closure parameter in Swift 3?

swiftxcodemarkup

提问by S?ren Mortensen

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

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

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:

然后快速帮助看起来像这样:

foo(bar:) Quick Help

foo(bar:) 快速帮助

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

我想知道允许我写一些文本来替换“无描述”的语法是什么。非常感谢!

回答by Tim Vermeulen

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.

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

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

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

/// 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")
}

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 中唯一有效的方法,因为参数标签不再是类型系统的一部分(参见SE-0111)。