Eclipse和IntelliJ IDEA系的IDE都有自动生成文档注释的功能,Xcode虽然安装了VVDocument,可是仍然感受注释的功能不是很完善,因而今天整理了一下书写文档注释的一些用法。
首先要说的就是文档注释提取的工具:主要是介绍HeaderDoc和appleDoc
1.咱们日常长按option键同时鼠标点击,弹出的文档就是Xcode会自动使用HeaderDoc生成的。如图:
2.appleDoc只为Objective-C服务,能够在文档书写完成以后使用appledoc生成docSet,能生成和Apple一个风格的文档,以下所示:
而后要说的就是一些编写文档注释的规范了:(注意本文值讨论文档注释,仅仅做为解释说明的注释不会涉及)linux
共有如下几种:
行前注释:(功能较多,可用做对属性、方法、类的声明,一般用做对属性的声明)c++
///
行后注释:(通常对属性、成员变量的声明等)api
/**< /*!< ///< //!<
使用的方法以下:app
/// 姓名 @property (nonatomic, copy) NSString *name; @property (nonatomic, copy) NSString *phone; /**< 电话 */ @property (nonatomic, copy) NSString *address; /*!< 住址 */ @property (nonatomic, assign) float height; ///< 身高 @property (nonatomic, assign) NSUInteger age; //!< 年龄
经常使用如下几种:(常常用在类声明和方法声明以前)工具
/// line1 /// line2 /** * line1 * line2 */ /*! line1 * line2 */
示例以下:ui
/*! * Comment */ @interface Comment : NSObject /** * an exmaple * * @param obj input parameter */ - (void)commonMethod:(id)obj; /// exmaple 2 /// @param obj input parameter - (void)commonMethod2:(id)obj; @end
在写方法的文档注释的时候多用一些参数说明, 这时候会用到@param标签,除此以外还有其余标签this
标签 | Example | 含义 |
---|---|---|
@param | @param myValue The value to process. | 对方法的参数描述 |
@result | @result Returns 1 on success, 0 on failure. | 对返回值的描述 |
@return | @result Returns 1 on success, 0 on failure. | 和@result.相同 |
@templatefield | @templatefield base_type The base type to store in the linked list. | Each of the function’s template fields (C++). |
@throws | @throws bananas | 对抛出异常的描述 |
@var | @var myVar Description goes here |
对局部变量或方法的描述 |
标签 | Example | 含义 |
---|---|---|
@abstract | @abstract write the track to disk | 简短描述,不要超过1行 |
@apiuid | @apiuid //my_ref/doc/magic | 重写与这个注释绑定的 API UID (apple_ref),也就是重写这个注释的惟一标识, 使用不当会带来标识冲突等问题 |
@attribute @attributelist @attributeblock |
@attribute My Attribute Name Value goes here. | 添加一个自定义的不必定符合规则的tag |
@availability | @availability 10.3 and later | 适用版本描述 |
@brief | @brief write the track to disk | 简短描述 |
@discussion | @discussion This is what this function does. @some_other_tag | 详细描述 |
@indexgroup | @indexgroup Name of Group | 提供放在发布页面的组织信息,若是没有使用这个tag,会使用来自内部的class或者头文件的组织信息 |
@internal | @internal | 标记为内部文档,若是生成文档时在命令行设置了 --document-internal,这个文档才会被生成 |
@link | @link //apple_ref/c/func/function_name link text goes here @/link 或者 @link function_name link text goes here @/link 或者 @link function_name @/link |
插入一个API ref所在的连接 |
@namespace | @namespace BSD Kernel | 对所处的命名空间的说明 |
@see | @see apple_ref Title for link | 参数与@link相同 若是API reference已经在see或seealso中出现这个tag会被忽略 |
@seealso | @seealso apple_ref Title for link | 参数与@link相同 若是API reference已经在see或seealso中出现这个tag会被忽略 |
@textblock | @textblock My text goes here @/textblock | @textblock和@/textblock之间出现的tag全都是文档内容 |
@updated | @updated 2003-03-14 | 上次更新的时间 |
标签 | Example | 含义 |
---|---|---|
@author | @author Anon E. Mouse | 做者 |
@charset | @charset utf-8 | 生成HTML文件使用的编码,同@encoding |
@compilerflag | @compilerflag -lssl | 使用时须要添加的编译指令 |
@copyright | @copyright Apple | 版权,这个值会覆盖在配置文件中的值,同时不能被分为多行 |
@CFBundleIdentifier | @CFBundleIdentifier org.mklinux.driver.test | 所在的包名、程序的BundleID |
@encoding | @encoding utf-8 | 为生成的HTML文件设置编码 |
@flag | @flag -lssl The SSL Library |
参见@compilerflag |
@ignore | @ignore API_EXPORT | 告诉HeaderDoc删除指定的标记 |
@ignorefuncmacro | @ignorefuncmacro __P | 告诉HeaderDoc不要包含指定的方法宏 |
@language | @language c++ | 已经废弃的tag。指定当前的开发语言 |
@meta | @meta robots index,nofollow 或者 @meta http-equiv="refresh" content="0;http://www.apple.com" |
将要添加到每一个页面的meta tag,能够用这两种形式中的一种指定 @meta
|
@preprocinfo | @preprocinfo This header uses the DEBUG macro to enable additional debugging. | 描述与processor相关的宏(-DDEBUG, for example)指定以后的行为) |
@related | @related Sixth cousin of Kevin Bacon. | 指出与这个头文件关联的另外一个头文件,可使用多个@related标签 |
@unsorted | @unsorted | 指出你不但愿HeaderDoc帮你对头文件的内容排序 |
@version | @version 2.3.1 | 文档适用的版本 |
@whyinclude | @whyinclude Because it was there. | 指出你为何要包含一些头文件 |