文档注释在Xcode中有原生支持,可以生成智能渲染的文档,包括快速帮助(当使用⌥-点击符号时,在弹出窗口中)和快速帮助检查器 ⌥⌘2 中的文档。
符号文档注释现在基于与丰富的playground注释相同的Markdown语法,因此现在可以直接在源代码文档中使用playgrounds中的大部分功能。
有关语法的完整详细信息,请参见标记格式参考。请注意,丰富的playground注释和符号文档的语法存在一些差异;这些差异在文档中指出(例如,块引用只能在playgrounds中使用)。
下面是一个示例和目前适用于符号文档注释的语法元素列表。
Xcode 7 beta 4 ~ 添加了"- Throws: ...
"作为顶级列表项,与参数和返回描述一起显示在快速帮助中。
Xcode 7 beta 1 ~ Swift 2的语法发生了一些重大变化 - 文档注释现在基于Markdown(与playgrounds相同)。
Xcode 6.3(6D570)~ 缩进文本现在被格式化为代码块,并嵌套后续缩进。在这样的代码块中不可能留下空行 - 尝试这样做会导致文本被附加到最后一行上,其中包含任何字符。
Xcode 6.3 beta ~ 现在可以使用反引号将内联代码添加到文档注释中。
/// Text like this appears in "Description".
///
/// Leave a blank line to separate further text into paragraphs.
///
/// You can use bulleted lists (use `-`, `+` or `*`):
///
/// - Text can be _emphasised_
/// - Or **strong**
///
/// Or numbered lists:
///
/// 7. The numbers you use make no difference
/// 0. The list will still be ordered, starting from 1
/// 5. But be sensible and just use 1, 2, 3 etc…
///
/// ---
///
/// More Stuff
/// ==========
///
/// Code
/// ----
///
/// Use backticks for inline `code()`. Indentations of 4 spaces or more will create a code block, handy for example usage:
///
/// // Create an integer, and do nothing with it
/// let myInt = 42
/// doNothing(myInt)
///
/// // Also notice that code blocks scroll horizontally instead of wrapping.
///
/// Links & Images
/// --------------
///
/// Include [links](https://en.wikipedia.org/wiki/Hyperlink), and even images:
///
/// ![Swift Logo](/Users/Stuart/Downloads/swift.png "The logo for the Swift programming language")
///
/// - note: That "Note:" is written in bold.
/// - requires: A basic understanding of Markdown.
/// - seealso: `Error`, for a description of the errors that can be thrown.
///
/// - parameters:
/// - int: A pointless `Int` parameter.
/// - bool: This `Bool` isn't used, but its default value is `false` anyway…
/// - throws: A `BadLuck` error, if you're unlucky.
/// - returns: Nothing useful.
func doNothing(int: Int, bool: Bool = false) throws -> String {
if unlucky { throw Error.BadLuck }
return "Totally contrived."
}
支持使用///
(内联)和/** */
(块)样式的注释来生成文档注释。虽然我个人更喜欢/** */
注释的视觉样式,但是在复制/粘贴时,Xcode的自动缩进可能会破坏此注释样式的格式,因为它会删除前导空格。例如:
/**
See sample usage:
let x = method(blah)
*/
/**
See sample usage:
let x = method(blah)
*/
///
,并将在本答案的其余示例中使用它。
标题:
/// # My Heading
/// My Heading
/// ==========
副标题:
/// ## My Subheading
或者
/// My Subheading
/// -------------
水平分隔线:
/// ---
/// - An item
/// - Another item
+
或*
来创建无序列表,只需要保持一致即可。
有序(编号)列表:
/// 1. Item 1
/// 2. Item 2
/// 3. Item 3
Code blocks:
/// for item in array {
/// print(item)
/// }
强调(斜体):
/// Add like *this*, or like _this_.
强调(粗体):
/// You can **really** make text __strong__.
*
)和下划线(_
)。
行内代码:
/// Call `exampleMethod(_:)` to demonstrate inline code.
链接:
/// [Link Text](https://en.wikipedia.org/wiki/Hyperlink)
图片:
/// ![Alt Text](http://www.example.com/alt-image.jpg)
/// A [link][1] an an ![image][2]
///
/// ...
///
/// [1]: http://www.example.com
/// [2]: http://www.example.com/image.jpg
除了Markdown格式外,Xcode还识别其他标记关键词以在快速帮助中突出显示。这些标记关键词大多采用-<keyword>:
的格式(例外是parameter
,它还包括冒号前的参数名称),其中关键词本身可以写成任何大小写字母的组合。
以下关键词以突出的形式显示在帮助查看器中,位于“描述”部分下方,“声明于”部分上方。当包含这些关键词时,它们的顺序固定为如下所示,即使您可以按任何顺序在注释中包含它们。
请参阅标记格式参考中的符号部分命令章节,获取完整记录的关键词列表及其预期用途。
/// - parameters:
/// - <#parameter name#>:
/// - <#parameter name#>:
/// - throws:
/// - returns:
或者,您可以这样编写每个参数:
/// - parameter <#parameter name#>:
以下关键词列表以粗体标题形式显示在帮助查看器的“描述”部分的正文中。它们将按照您编写它们的顺序出现,就像“描述”部分的其余内容一样。
完整列表由Erica Sadun的这篇优秀博客文章改写而来。还可以查看标记格式参考中的符号描述字段命令部分中关键词及其预期用途的完全记录列表。
归属:
/// - author:
/// - authors:
/// - copyright:
/// - date:
可用性:
/// - since:
/// - version:
警告:
/// - attention:
/// - important:
/// - note:
/// - remark:
/// - warning:
开发状态:
/// - bug:
/// - todo:
/// - experiment:
实现品质:
/// - complexity:
功能语义:
/// - precondition:
/// - postcondition:
/// - requires:
/// - invariant:
/// - seealso:
可以使用开源命令行工具Jazzy从内联文档生成HTML文档(旨在模仿苹果的官方文档)。
$ [sudo] gem install jazzy
$ jazzy
Running xcodebuild
Parsing ...
building site
jam out ♪♫ to your fresh new docs in `docs`
控制台示例取自this NSHipster article
/// - todo: keyword
关键字。 - leonardomyOtherMethod(param1 :)
以获取扩展功能”。 - Ky -/// - Tag: otherMethod
和 [otherMethod](x-source-tag://otherMethod)
来实现。有关更多详细信息,请参见 我的答案。 - Clay Ellis以下是一些在Xcode 6中编写Swift代码时可用于文档化的方法。尽管存在一些错误和对冒号的敏感问题,但这比没有强。具体方法如下:
class Foo {
/// This method does things.
/// Here are the steps you should follow to use this method
///
/// 1. Prepare your thing
/// 2. Tell all your friends about the thing.
/// 3. Call this method to do the thing.
///
/// Here are some bullet points to remember
///
/// * Do it right
/// * Do it now
/// * Don't run with scissors (unless it's tuesday)
///
/// :param: name The name of the thing you want to do
/// :returns: a message telling you we did the thing
func doThing(name : String) -> String {
return "Did the \(name) thing";
}
}
///
)行。省略此操作会导致XCode(在撰写本文时为6.1.1)将参数帮助包含在函数描述的主体中。 - Robin MachargXcode 8 的新功能,您可以像这样选择一个方法
func foo(bar: Int) -> String { ... }
然后按下command
+ option
+ /
或从Xcode的“编辑器”菜单中选择"结构" - "添加文档",它将为您生成以下注释模板:
/// <#Description#>
///
/// - parameter bar: <#bar description#>
///
/// - returns: <#return value description#>
Swift包含“///”注释处理(虽然可能还不是全部)。
写出类似于这样的东西:
/// Hey!
func bof(a: Int) {
}
然后选项点击函数名称,voilà :)
我可以确认ShakenManChild提供了正确的解决方案。
请确保在描述下方有一个空行!
/**
@brief <#Short description - what it is doing#>
@discussion <#Description#>
@param <#paramName#> <#Description#>.
@return <#dataType#> <#Description#>.
*/
Swift
/**
<#Short inline description - what it is doing#>
<#Description#>
:param: <#paramName#> <#Description#>.
:returns: <#dataType#> <#Description#>.
*/
.swiftdoc
结尾。这些文件明显包含文档,因为它们包含Swift UIKit / Foundation API的文档,但不幸的是,它似乎是一种专有文件格式,只能在Xcode的文档查看器中使用。Swift 5.6+ 提供了丰富的 API 参考文档和交互式教程生成,使用 DocC ⇗。Swift-DocC 插件 ⇗ 提供了 Swift Package Manager 的命令,支持为 SwiftPM 库和可执行文件构建文档。
Package.swift
dependencies: [
// other dependencies
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
],
终端:
swift package generate-documentation
swift package plugin generate-documentation --help
博客:Swift.org
文档:已存档
示例代码
WWDC会议
语法手册
目前支持的关键元素和标题词。
/// This text appears in the top **Summary** section.
/// These top lines appears before the **Declaration** heading.
///
/// - Author: Firstname Lastname
/// - Version: 0.1
///
/// Leave a blank line to separate further text into paragraphs.
///
/// | key | value |
/// |-------|-------|
/// | abc | 123 |
///
/// Example code block:
///
/// ``` swift
/// // Create an integer, and do nothing with it
/// let myInt = 42
/// doNothingReally(myInt)
/// ```
///
/// Bulleted lists can use `-`, `+` or `*`:
///
/// - Text can be *emphasised*
/// - Or **strong**
/// - You can use backticks for `code(012)` 012
///
/// Lists can be numbered:
///
/// 1. The numbers you use make no difference
/// 2. The list will still be ordered
/// 3. But be sensible and just use 1, 2, 3 etc…
///
/// - See Also: [DocD Reference](https://www.swift.org/documentation/docc/)
/// - See Also: <https://www.swift.org/documentation/docc/>
///
/// - Date: 2023.05.12
/// - Copyright: @whomever
/// - Experiment: This may or may not work as expected.
/// - Invariant: `xyz` will not change during execution
/// - Attention: Look here!
/// - Note: Whether the weather be fine, or whether the weather be…
/// - Postcondition: `address` will be updated
/// - Precondition: `person` must be non-nil
/// - Remark: This is remarkable.
/// - Requires: `Contacts` framework
/// - Requires: macOS version 12 or better
/// - Since: The Beginning Of Time
/// - Todo: Just one more task
/// - Tip: Lorem ipsum
/// - Warning: warning, warning
///
/// - Important: This needs to be read.
///
/// - Throws: nothing
///
/// - Parameters:
/// - int: A pointless `Int` parameter.
/// - bool: This `Bool` isn't used, but its default value is `false` anyway…
///
/// - Returns: Nothing useful.
func doNothingReally(int: Int, bool: Bool = false) -> String {
return "Totally contrived."
}
See Also:
运行良好。然而,SeeAlso:
目前无法工作,需要等待支持遗留- SeeAlso: callout asides#461的一般发布。
// MARK:
注释也计划在未来的 Xcode 版本中推出。 - Leandros