97

I have a few questions about Swift documentation comments:

  1. Is there a way to make a Related declarations section like some of the Apple documentation has? For example, when I Option+Click the tablewView(_:heightForRowAtIndexPath:) method, it links me to three other related methods within the generated documentation.

  2. Is there any warning tag in Swift? I know Objective-C allowed me to do @warning and get a bolded warning in the generated documentation. However, :warning: does nothing in Swift's documentation comments, so I was curious if there was another way.

  3. Is there a way to make my documentation into an HTML file that is a similar format as the Apple Documentation? I know in other IDEs, such as Eclipse, I can generate HTML documentation for my code. Does XCode have this?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
ad121
  • 2,268
  • 1
  • 18
  • 24
  • possible duplicate of [Does Swift have documentation comments or tools?](http://stackoverflow.com/questions/24047991/does-swift-have-documentation-comments-or-tools) – Stuart Aug 11 '15 at 21:21
  • 2
    Swift comments has change over the years, using `CMD+OPTION+/` shortcut Xcode will add the template for the all the documentation needed (including return value, params, etc). It can be an extension, class, method, etc. This way you guarantee using the official document format/style. – rgkobashi Oct 16 '19 at 04:18
  • See Apple’s description of this feature, [Writing symbol documentation in your source files](https://developer.apple.com/documentation/xcode/writing-symbol-documentation-in-your-source-files). – Rob Dec 07 '22 at 15:45

6 Answers6

215

This answer was last revised for Swift 5.7 and Xcode 14.x.


DocC is Apple's documentation compiler that takes comments (plus additional resources) and produces rich documentation that can be viewed in Xcode or hosted on web.

Writing Documentation

Type /// or /** */ to begin a documentation comment and then use DocC's special dialect of Markdown to write the content. This dialect supports many keywords like - Parameters: for describing function arguments or - Returns: for describing return values.

Note how the > Warning: keyword was recognized as an aside and automatically emphasized. DocC supports multiple other aside types like Note, Tip and Important.

/// Produce a greeting string for the given `subject`.
///
/// ```
/// print(hello("world")) // "Hello, world!"
/// ```
///
/// > Warning: The returned greeting is not localized. To
/// > produce a localized string, use ``localizedHello(_:)``
/// > instead.
///
/// - Parameters:
///     - subject: The subject to be welcomed.
///
/// - Returns: A greeting for the given `subject`.
func hello(_ subject: String) -> String {
    return "Hello, \(subject)!"
}

Linking to Symbols

DocC will automatically link (and auto-complete!) symbols wrapped in double backticks ``. You can link to related symbols in the same type or other types in the same module.

Note that linking is limited only to public symbols and only to one module. As of today, there's no way to type e.g. ``UIView`` and have DocC automatically link it to UIKit's documentation.

Generating Webpages

DocC supports exporting your documentation into webpages. First, you need to compile your documentation by choosing Product → Build Documentation. Once the documentation is built, export its archive by clicking the More button. The archive will contain the entire documentation webpage that you can then host on your server.

The above process is a bit complicated, so there are many tools that can help you automate it. Apple offers swift-docc-plugin that you can add to your Swift package or Xcode project and configure it to run on every build. You can automate this process on CI as well.

Further Reading

I recommend reading the following guides to learn more about DocC:

akashivskyy
  • 44,342
  • 16
  • 106
  • 116
  • @akashivskyy If this is added in a dynamic swift framework, it is removed in the public interface. So the projects using the framework cannot view these documentation. Any idea how to fix that? – adev Jan 09 '18 at 20:56
  • @adev I cannot reproduce what you wrote. I added a precompiled .framework to my project, cmd-clicked a symbol and all the original doc comments are available. – akashivskyy Jan 10 '18 at 12:09
  • @akashivskyy That is strange. I wonder if carthage is stripping something when it adds my framework to application. – adev Jan 10 '18 at 20:22
  • @adev I used Carthage as well for my above test. – akashivskyy Jan 11 '18 at 12:09
  • The link in the answer now states this at the top of the page. "IMPORTANT This document is no longer being updated. For the latest information about Apple SDKs, visit the documentation website." Anyone aware where this is being kept up to date? I've had no luck searching for it at the link provided by apple: https://developer.apple.com/documentation – Michael Welch Jul 28 '18 at 20:39
61

Xcode 7.0 beta 4

The notation has been changed (:param: does not work anymore ...)

/// Creates the string representation of the poo with requested size.
///
/// - warning: Be carefull! Poos can hurt.
/// - parameter size: requested size of the poo
/// - returns: string representation of the poo
func makePoo(size: String) -> String
{
    return "Ouch. This is \(size) poo!"
}

And it looks like this:

PopUp with documentation

You can use either /// or /** */

stevo.mit
  • 4,681
  • 4
  • 37
  • 47
  • 2
    This answer must come up – Saranjith Oct 20 '17 at 06:27
  • If this is added in a dynamic swift framework, it is removed in the public interface. So the projects using the framework cannot view these documentation. Any idea how to fix that? – adev Jan 09 '18 at 20:57
44

For those who want to add this as code snippet. Swift 5, XCode 11.3+

This is an add on to : Yogendra Singh's Answer in this thread

/**
<#Summay text for documentation#>

- parameter <#parameterName#>: <#Description#>.
- returns: <#Return values#>
- warning: <#Warning if any#>


 # Notes: #
 1. <#Notes if any#>

 # Example #
```
 // <#Example code if any#>
```

*/

Copy and paste the above code in Xcode. Select the code and then Right click.

enter image description here

Save the code snippet and give the completion name as documentation.

enter image description here

Now if we start typing documentation, the snippet will be shown in the code completion.

enter image description here

abhimuralidharan
  • 5,752
  • 5
  • 46
  • 70
  • Echoing my comment to @YogendraSingh, I've found that seemingly no matter what you do Swift will automatically add an empty section called `Discussion` when a documentation comment is shown contextually--you can even see this empty `Discussion` section in the screenshot he supplied of the contextual pop-up matching the documentation comments he provided. To make matters worse, I've not been able to find out how you actually define the `Discussion` section in a documentation comment. If at all possible, could you amend your code snippet to include a way to do this? – Le Mot Juiced Mar 13 '20 at 18:02
  • @YogendraSingh has given his answer, unfortunately when I tried to apply it to a code snippet myself it didn't work and I'm not sure why. If you could update yours in response to his it would be very helpful. Of course we're all busy and I understand if it's a low priority. – Le Mot Juiced Mar 19 '20 at 14:51
  • 1
    This is a bit late, but check that you have typed 'documentation' in the Completion field (3rd line) – Jia Cheng Sun Jan 29 '22 at 04:50
10

Use the following notation for documentation comments.

/**
 This method sum two double numbers and returns.

 Here is the discussion. This methods adds two double and return the optional Double.


 - parameter number1: First Double Number.
 - parameter number2: Second Double Number.
 - returns: The sum of two double numbers.

 # Notes: #
 1. Parameters must be **double** type
 2. Handle return type because it is optional.

 # Example #
```
 if let sum = self.add(number1: 23, number2: 34) {
  print(sum)
 }
 ```
*/


func add(number1: Double, number2: Double) -> Double? {
    return number1 + number2
}

enter image description here

Yogendra Singh
  • 2,063
  • 25
  • 20
  • If you look at your picture of the results of the documentation comment, you can see right under the `Declaration` there's a section called `Discussion` that is totally empty. I have found that Swift almost always adds this empty `Discussion` area, and I've found it close to impossible to find out how to define the `Discussion` area inside the comments. If you could amend your example to demonstrate that, I think it would help a lot of people avoid the error. – Le Mot Juiced Mar 13 '20 at 17:59
  • 2
    After Summary leave one line and then whatever you write will be the discussion without using any special characters like (- etc). I have edited my answer please have a look. – Yogendra Singh Mar 19 '20 at 11:15
8

Keyboard shortcut in Xcode

option + cmd + /
Chase McElroy
  • 383
  • 3
  • 8
5

(3) To generate your documentation in HTML (or even generate docsets), I strongly recommend jazzy which was built for that purpose.

Even if it's still WIP, it works really well and generate documentation with similar presentation to the Apple documentation

AliSoftware
  • 32,623
  • 6
  • 82
  • 77