所有重要的方法,接口,分類以及協議定義應該有伴隨的注釋來解釋它們的用途以及如何使用。更多的例子可以看 Google 代碼風格指南 [File and Declaration Comments](http://google-styleguide.googlecode.com/svn/trunk/objcguide.xml#File_Comments)。
簡而言之:有長的和短的兩種字符串文檔。
短文檔適用于單行的文件,包括注釋斜杠。它適合簡短的函數,特別是(但不僅僅是)非 public 的 API:
~~~
// Return a user-readable form of a Frobnozz, html-escaped.
~~~
文本應該用一個動詞 ("return") 而不是 "returns" 這樣的描述。
如果描述超出一行,你應該用長的字符串文檔: 一行斜杠和兩個星號來開始塊文檔 (/**, 之后是總結的一句話,可以用句號、問號或者感嘆號結尾,然后空一行,在和第一句話對齊寫下剩下的注釋,然后用一個 (*/)來結束。
~~~
/**
This comment serves to demonstrate the format of a docstring.
Note that the summary line is always at most one line long, and
after the opening block comment, and each line of text is preceded
by a single space.
*/
~~~
一個函數必須有一個字符串文檔,除非它符合下面的所有條件:
- 非公開
- 很短
- 顯而易見
字符串文檔應該描述函數的調用符號和語義,而不是它如何實現。