Søren Mortensen Søren Mortensen - 1 year ago 127
Swift Question

How do you document the parameters of a function's closure parameter in 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:

foo(bar:) Quick Help

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

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download