79

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!

Greg
  • 10,360
  • 6
  • 44
  • 67
Søren Mortensen
  • 1,663
  • 1
  • 11
  • 25

2 Answers2

96

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

Tim Vermeulen
  • 12,352
  • 9
  • 44
  • 63
  • 3
    This absolutely answers my question, thank you! I actually had no idea you could add argument labels to a closure! That's fantastic news. And to the contrary, I don't think that's less than ideal at all. Argument labels make code easier to understand imho, and I would rather have argument labels than be calling closures with entirely anonymous parameters. – Søren Mortensen Jul 30 '16 at 02:27
  • 1
    Naming conflicts could become a problem, though. – Søren Mortensen Jul 30 '16 at 02:27
  • I like argument labels for closures as well, but I'd rather not have them forced on me just for documentation purposes... :) – Tim Vermeulen Jul 30 '16 at 02:29
  • 2
    Yeah, I agree. It would be really nice if you could do something like `- parameter $0: Description`, perhaps indented further after `- parameter bar` – Søren Mortensen Jul 30 '16 at 02:30
  • 9
    Note that you can write `(_ theString: String)` in order to have a local parameter name for documentation purposes without having to use it at the call site. As a matter of fact, since Xcode 8 beta 6 you have to, or the compiler will complain with "Function types cannot have argument label". See [SE-0111](https://github.com/apple/swift-evolution/blob/master/proposals/0111-remove-arg-label-type-significance.md) and [this email](https://lists.swift.org/pipermail/swift-evolution-announce/2016-July/000233.html) for details. – Arnaud Aug 30 '16 at 11:48
  • @Arnaud Totally, I'll update my answer as soon as I can. – Tim Vermeulen Aug 30 '16 at 11:52
  • 3
    Although this allows the label to appear, I'm still seeing "No Description" in it's detail. Has anyone actually found a way around this? I actually want to show a proper description. – wuf810 Apr 07 '17 at 07:29
  • 1
    @wuf810 this worked for me, and hasn't broken recently as far as I know... which version of Swift are you using? – Søren Mortensen Apr 07 '17 at 11:03
  • 1
    Hey @SørenMortensen I'm on Swift 3. I have found this on Swift mailing list that indicates that that there is an issue with the parameters in the closure. https://bugs.swift.org/browse/SR-3693 What version are you using. Any chance you could paste in your documented function (or an example) so I can try and see if it's working for me. Like I said, I see the label names but the description shows as "No Description." – wuf810 Apr 07 '17 at 14:21
  • 6
    I'm having the same problem as @wuf810. Even though I label the closure parameter and add it's description, it keeps showing a square saying it has no description for that parameter. – David Cespedes Feb 07 '18 at 21:28
  • FWIW, I'm still seeing this as well in my project (Xcode 9.2). I might have to file a Radar. – Jeff C. Feb 07 '18 at 23:33
  • @DavidCespedes I have the exact same issue using Xcode 9.2 – Dávid Pásztor Feb 08 '18 at 22:04
  • 1
    @DávidPásztor I finally solved making sure I don't add more indentation to closure parameters. They have to have the same indentation as other parameters – David Cespedes Feb 13 '18 at 18:15
  • 1
    Seems to break if the `bar` parameter is changed to optional. – Nate Whittaker Feb 14 '19 at 20:05
8

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

AirXygène
  • 2,409
  • 15
  • 34