pco*_*ion 233 documentation-generation swift
许多语言都支持文档注释,允许生成器(如doxygenjavadoc或doxygen)通过解析相同的代码来生成代码文档.
Swift有这样的类型文档评论功能吗?
Stu*_*art 373
Xcode本身支持文档注释,在快速帮助中生成⌥智能渲染文档(在弹出符号时单击符号,以及快速帮助检查器中⌥⌘2).
符号文档注释现在基于丰富的游乐场注释使用的相同Markdown语法,因此您在游乐场中可以执行的许多操作现在可以直接在源代码文档中使用.
有关语法的完整详细信息,请参阅标记格式参考.请注意,丰富的游乐场评论和符号文档的语法之间存在一些差异; 这些都在文件中指出(例如块引用只能在游乐场中使用).
下面是一个示例和当前用于符号文档注释的语法元素的列表.
Xcode 7 beta 4~添加了" - Throws: ..."作为顶级列表项,与快速帮助中的参数和返回说明一起显示.
Xcode 7 beta 1~使用Swift 2对语法进行了一些重大更改 - 文档注释现在基于Markdown(与游乐场相同).
Xcode 6.3(6D570)〜缩进文本现在被格式化为代码块,后续缩进被嵌套.似乎不可能在这样的代码块中留下空白行 - 尝试这样做会导致文本被添加到最后一行的末尾,其中包含任何字符.
现在可以使用反引号将Xcode 6.3 beta~ Inline代码添加到文档注释中.
/// 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."
}
Run Code Online (Sandbox Code Playgroud)

这两个///(内置)和/** */(块)风格的注释都支持生产文档注释.虽然我个人更喜欢视觉风格的/** */评论,但Xcode的自动缩进可能会在复制/粘贴时破坏此评论样式的格式,因为它会删除前导空格.例如:
/**
See sample usage:
let x = method(blah)
*/
Run Code Online (Sandbox Code Playgroud)
粘贴时,代码块缩进将被删除,并且不再呈现为代码:
/**
See sample usage:
let x = method(blah)
*/
Run Code Online (Sandbox Code Playgroud)
出于这个原因,我通常会使用///,并将其用于本答案中的其余示例.
标题:
/// # My Heading
Run Code Online (Sandbox Code Playgroud)
要么
/// My Heading
/// ==========
Run Code Online (Sandbox Code Playgroud)
副标题:
/// ## My Subheading
Run Code Online (Sandbox Code Playgroud)
要么
/// My Subheading
/// -------------
Run Code Online (Sandbox Code Playgroud)
横向规则:
/// ---
Run Code Online (Sandbox Code Playgroud)
无序(项目符号)列表:
/// - An item
/// - Another item
Run Code Online (Sandbox Code Playgroud)
您也可以使用+或*作为无序列表,它必须是一致的.
有序(编号)列表:
/// 1. Item 1
/// 2. Item 2
/// 3. Item 3
Run Code Online (Sandbox Code Playgroud)
代码块:
/// for item in array {
/// print(item)
/// }
Run Code Online (Sandbox Code Playgroud)
需要至少四个空格的缩进.
重点(斜体):
/// Add like *this*, or like _this_.
Run Code Online (Sandbox Code Playgroud)
强(粗体):
/// You can **really** make text __strong__.
Run Code Online (Sandbox Code Playgroud)
请注意,您不能在同一元素上混合使用星号(*)和下划线(_).
内联代码:
/// Call `exampleMethod(_:)` to demonstrate inline code.
Run Code Online (Sandbox Code Playgroud)
链接:
/// [Link Text](https://en.wikipedia.org/wiki/Hyperlink)
Run Code Online (Sandbox Code Playgroud)
图片:
/// 
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
除了Markdown格式,Xcode还会识别其他标记关键字,以便在"快速帮助"中突出显示.这些标记关键字主要采用格式- <keyword>:(例外parameter,其中还包括冒号前的参数名称),其中关键字本身可以使用大写/小写字符的任意组合编写.
以下关键字在帮助查看器中显示为"描述"部分下方和"已申报"部分上方的突出部分.包含时,即使您可以在评论中以任何顺序包含它们,它们的顺序也会如下所示固定.
请参阅" 标记格式参考"的" 符号部分命令"部分中完整记录的部分关键字及其预期用途.
/// - parameters:
/// - <#parameter name#>:
/// - <#parameter name#>:
/// - throws:
/// - returns:
Run Code Online (Sandbox Code Playgroud)
或者,您可以这样编写每个参数:
/// - parameter <#parameter name#>:
Run Code Online (Sandbox Code Playgroud)
以下关键字列表在帮助查看器的"描述"部分的正文中显示为粗体标题.它们将以您编写的顺序出现,与"描述"部分的其余部分一样.
完整列表由Erica Sadun 撰写的这篇优秀博客文章解读.另请参阅标记格式参考的符号描述字段命令部分中完整记录的关键字列表及其预期用途.
归因:
/// - author:
/// - authors:
/// - copyright:
/// - date:
Run Code Online (Sandbox Code Playgroud)
可用性:
/// - since:
/// - version:
Run Code Online (Sandbox Code Playgroud)
告诫:
/// - attention:
/// - important:
/// - note:
/// - remark:
/// - warning:
Run Code Online (Sandbox Code Playgroud)
发展状况:
/// - bug:
/// - todo:
/// - experiment:
Run Code Online (Sandbox Code Playgroud)
实施质量:
/// - complexity:
Run Code Online (Sandbox Code Playgroud)
功能语义:
/// - precondition:
/// - postcondition:
/// - requires:
/// - invariant:
Run Code Online (Sandbox Code Playgroud)
交叉参考:
/// - seealso:
Run Code Online (Sandbox Code Playgroud)
HTML文档(旨在模仿Apple自己的文档)可以使用Jazzy(一种开源命令行实用程序)从内联文档生成.
$ [sudo] gem install jazzy
$ jazzy
Running xcodebuild
Parsing ...
building site
jam out ?? to your fresh new docs in `docs`
Run Code Online (Sandbox Code Playgroud)
从NSHipster这篇文章中获取的控制台示例
小智 58
以下是一些用于记录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";
}
}
Run Code Online (Sandbox Code Playgroud)
如上所述,您可以使用格式化数字列表,项目符号,参数和返回值文档在快速帮助中呈现上述内容.
这些都没有记录 - 提交雷达来帮助他们.
Log*_*nke 38
Xcode 8中的新功能,您可以选择这样的方法
func foo(bar: Int) -> String { ... }
Run Code Online (Sandbox Code Playgroud)
然后按command+ option+/或从Xcode的"编辑器"菜单中选择"结构" - "添加文档",它将为您生成以下注释模板:
/// <#Description#>
///
/// - parameter bar: <#bar description#>
///
/// - returns: <#return value description#>
Run Code Online (Sandbox Code Playgroud)
Jea*_*nan 27
Swift包含"///"注释处理(尽管可能还不是所有内容).
写下类似的东西:
/// Hey!
func bof(a: Int) {
}
Run Code Online (Sandbox Code Playgroud)
然后选择 - 点击func名称和voilà:)
是.基础常见(我用Obj-C等效的片段)
Objective-C的:
/**
@brief <#Short description - what it is doing#>
@discussion <#Description#>
@param <#paramName#> <#Description#>.
@return <#dataType#> <#Description#>.
*/
Run Code Online (Sandbox Code Playgroud)
迅速
/**
<#Short inline description - what it is doing#>
<#Description#>
:param: <#paramName#> <#Description#>.
:returns: <#dataType#> <#Description#>.
*/
Run Code Online (Sandbox Code Playgroud)
如果你只使用Swift,那么Jazzy值得一看.
https://github.com/realm/jazzy
我发现了一些有趣的东西,挖掘Xcode二进制文件.结尾的文件.swiftdoc.它肯定有文档,因为这些文件包含Swift UIKit/Foundation API的文档,不幸的是它似乎是专有的文件格式,用于Xcode的Documentation查看器.
| 归档时间: |
|
| 查看次数: |
52471 次 |
| 最近记录: |