如何使用 Swift 文档注释

关于 Swift 文档评论,我有几个问题:

  1. 有没有办法像苹果的一些文档一样制作一个相关声明部分?例如,当我使用 Option + ClicktablewView(_:heightForRowAtIndexPath:)方法时,它将我链接到生成的文档中的其他三个相关方法。

  2. 斯威夫特有什么警告标签吗?我知道 Objective-C 允许我执行 @warning并在生成的文档中获得粗体警告。但是,:warning:在 Swift 的文档注释中什么都不做,所以我很好奇是否还有其他方法。

  3. 有没有办法将我的文档转换成与苹果文档类似的 HTML 文件格式?我知道在其他 IDE 中,比如 Eclipse,我可以为我的代码生成 HTML 文档。XCode 有这个吗?

54516 次浏览

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:

(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

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 /** */

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

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

Keyboard shortcut in Xcode

option + cmd + /