Xcode 原生支持文档 cmets,在快速帮助中生成巧妙呈现的文档(无论是在 ⌥ 单击符号时的弹出窗口中,还是在快速帮助检查器 ⌥⌘2 kbd>)。
符号文档 cmets 现在基于丰富的 Playground cmets 使用的相同 Markdown syntax,因此您可以在 Playground 中执行的许多操作现在可以直接在源代码文档中使用。
有关语法的完整详细信息,请参阅Markup Formatting Reference。请注意,丰富的游乐场 cmets 和符号文档的语法之间存在一些差异;这些都在文档中指出(例如,块引号只能在游乐场中使用)。
下面是一个示例和当前适用于符号文档 cmets 的语法元素列表。
更新
Xcode 7 beta 4 ~ 添加了“- Throws: ...”作为顶级列表项,它出现在快速帮助中的参数和返回描述旁边。
Xcode 7 beta 1 ~ Swift 2 对语法的一些重大更改 - 文档 cmets 现在基于 Markdown(与 Playgrounds 相同)。
Xcode 6.3 (6D570) ~ 缩进文本现在被格式化为代码块,随后的缩进被嵌套。似乎不可能在这样的代码块中留下空白行 - 尝试这样做会导致文本被添加到包含任何字符的最后一行的末尾。
Xcode 6.3 beta ~现在可以使用反引号将内联代码添加到文档 cmets。
Swift 2 示例
/// 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:
///
/// 
///
/// - 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."
}
评论风格
///(内联)和/** */(块)样式 cmets 都支持生成文档 cmets。虽然我个人更喜欢 /** */ cmets 的视觉风格,但 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
代码块:
/// 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)
图片:
/// 
URL 可以是 Web URL(使用“http://”)或绝对文件路径 URL(我似乎无法使用相对文件路径)。
链接和图像的 URL 也可以与内联元素分开,以便将所有 URL 保存在一个可管理的位置:
/// A [link][1] an an ![image][2]
///
/// ...
///
/// [1]: http://www.example.com
/// [2]: http://www.example.com/image.jpg
关键词
除了 Markdown 格式,Xcode 还可以识别其他标记关键字以在快速帮助中突出显示。这些标记关键字大多采用- <keyword>:格式(parameter除外,它还包括冒号前的参数名称),其中关键字本身可以用大小写字符的任意组合编写。
符号部分关键字
以下关键字在帮助查看器中显示为突出部分,位于“说明”部分下方和“声明于”部分上方。包含后,它们的顺序固定如下所示,即使您可以按照您喜欢的任何顺序将它们包含在 cmets 中。
在Symbol Section Commands section of the Markup Formatting Reference 中查看完整记录的部分关键字列表及其预期用途。
/// - parameters:
/// - <#parameter name#>:
/// - <#parameter name#>:
/// - throws:
/// - returns:
或者,您可以这样编写每个参数:
/// - parameter <#parameter name#>:
符号描述字段关键字
以下关键字列表在帮助查看器的“说明”部分的正文中显示为粗体标题。它们将按照您编写它们的任何顺序出现,与“描述”部分的其余部分一样。
由 Erica Sadun 从this excellent blog article 转述的完整列表。另请参阅Symbol Description Field Commands section of the Markup Formatting Reference 中完整记录的关键字列表及其预期用途。
归因:
/// - author:
/// - authors:
/// - copyright:
/// - date:
可用性:
/// - since:
/// - version:
告诫:
/// - attention:
/// - important:
/// - note:
/// - remark:
/// - warning:
发展状况:
/// - bug:
/// - todo:
/// - experiment:
实施质量:
/// - complexity:
功能语义:
/// - precondition:
/// - postcondition:
/// - requires:
/// - invariant:
交叉引用:
/// - seealso:
导出文档
可以使用开源命令行实用程序 Jazzy 从内联文档生成 HTML 文档(旨在模仿 Apple 自己的文档)。
$ [sudo] gem install jazzy
$ jazzy
Running xcodebuild
Parsing ...
building site
jam out ♪♫ to your fresh new docs in `docs`
来自this NSHipster article的控制台示例