Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 290
Søren Mortensen Avatar asked Jul 30 '16 02:07

Søren Mortensen


People also ask

How do you pass a closure as a parameter?

A closure is said to escape a function when the closure is passed as an argument to the function, but is called after the function returns. When you declare a function that takes a closure as one of its parameters, you can write @escaping before the parameter's type to indicate that the closure is allowed to escape.

How do you read Swift closures?

Understanding closure syntax in Swift For closures, we must always write down the return type even when the closure doesn't return anything. Instead of -> Void or "returns Void ", this type specifies -> () or "returns empty tuple". In Swift, Void is a type alias for an empty tuple.


2 Answers

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:

/// 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).

like image 171
Tim Vermeulen Avatar answered Sep 18 '22 20:09

Tim Vermeulen


This seems to be broken for quite some time. Here is an example with XCode 11.6, where you can see that :

1 ) the parameters are documented as explained in @Tim Vermeulen answer enter image description here

2 ) nevertheless, the "no description" table appears in the help pop-over window enter image description here

3 ) BUT the text appears correctly in the quick help window enter image description here

I guess we need to wait (hope) for Apple to fix this.

A slight improvement, though. Instead of writing "Parameter" at each line, use the following syntax :

- Parameters:   - name1: description   - name2: description 

(indentation seems to be important)

Then you'll get enter image description here

But it doesn't work everywhere the function is called...

like image 33
AirXygène Avatar answered Sep 18 '22 20:09

AirXygène