iOS代码注释文档生成

本文介绍了如何在Swift中编写代码注释,包括Description、Parameters、Throws和Returns等部分的详细说明,以及如何使用特定关键字来增强注释。此外,还阐述了如何通过Apple的markdown支持生成代码文档,并导出这些文档。
摘要由CSDN通过智能技术生成

一、代码注释

参考https://useyourloaf.com/blog/swift-documentation-quick-guide/

Symbol Documentation
There are four sections, of which, only the description is always included:

  • Description
  • Parameters
  • Throws
  • Returns

Description:注释描述的写法


/**
 What does this function do? (这个部分就会自动生成Description的部分)
*/

func someFunction(name: String) -> Bool {
  return false
}

Parameters: 参数的写法

/**
 Another useful function
 - parameters:
   - alpha: Describe the alpha param
   - beta: Describe the beta param
*/
func doSomething(alpha: String, beta: String) {

}

或者

/**
 Another useful function
 - parameter alpha: Describe the alpha param
 - parameter beta: Describe the beta param
*/
func doSomething(alpha: String, beta: String) {}

Throws:抛异常的写法,只能出现一次

/// Some introductory test that describes the purpose
/// of the function.
/// - Throws: SomeError you might want to catch

func someFunction() throws -> Bool {

Returns:返回值的注释写法值能出现一次

/// Some introductory test that describes the purpose
/// of the function.
/// - Returns: true or false

func someFunction() throws -> Bool {}

如果需要一些高级的注释这里有一些关键字可供选择

关键字列表

- Attention: Watch out for this!
- Author: Tim Cook
- Authors:
  John Doe
  Mary Jones
- Bug: This may not work
- Complexity: O(log n) probably
- Copyright: 2016 Acme
- Date: Jan 1, 2016
- experiment: give it a try
- important: know this
- invariant: something
- Note: Blah blah blah
- Precondition: alpha should not be nil
- Postcondition: happy
- Remark: something else
- Requires: iOS 9
- seealso: something else
- Since: iOS 9
- Todo: make it faster
- Version: 1.0.0
- Warning: Don't do it

例如:


/**
     Another useful function  
     - Warning: ddddd (在生成的注释文档中会有一红色的备注框)
    */
    func doSomething(alpha: String, beta: String) {
        
    }


二、生成代码注释文档

苹果支持markdown的注释文档,生成步骤:

  • cmd+n 选择documentation下的 documentation Catalog
  • 然后 Product–>build documentation 或者 快捷键:shift+control+cmd+d
    这样就会生成注释文档,文档可以导出。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值