在Objective C中,我可以使用#pragma mark来标记符号导航器中的代码片段。由于这是一个C预处理器命令,所以在Swift中不可用。在Swift中有替代品吗,或者我必须使用丑陋的评论吗?


当前回答

Add a to-do item: Insert a comment with the prefix TODO:. For example: // TODO: [your to-do item]. Add a bug fix reminder: Insert a comment with the prefix FIXME:. For example: // FIXME: [your bug fix reminder]. Add a heading: Insert a comment with the prefix MARK:. For example: // MARK: [your section heading]. Add a separator line: To add a separator above an annotation, add a hyphen (-) before the comment portion of the annotation. For example: // MARK: - [your content]. To add a separator below an annotation, add a hyphen (-) after the comment portion of the annotation. For example: // MARK: [your content] -.

其他回答

有三个选项可以在Swift中添加#pragma_mark:

1) // MARK: -你的文字在这里-

2) // TODO: -你的文本在这里-

3) // FIXME: -你的文本在这里-

注意:用于添加分隔符

//# MARK: - Spinner Class Methods

在冒号和描述之间添加一行,以插入分隔线。这有助于更好地组织代码。上面的代码和截图使用了包含一行的MARK注释。

//#标记:-文本方法(LINE) //#标记:文本方法(NO LINE)

这只适用于MARK注释。

Xcode官方文档

苹果目前的官方文档部分为可见性注释代码引入了三个注释:TODO:, FIXME:和MARK:。

最新的Xcode版本(v14.2)支持另外两个注释(虽然没有出现在官方文档中):!!:和??

注意:! !:和??:被某些Xcode版本(如v10.0)不支持,原因未知。

示例截图1 - Xcode 14.2 + macOS 13.1 (Ventura)

示例截图2 - Xcode 10.1 + macOS 10.14.3 (Mojave)

专业程序员必须使用这个标签来获得好的代码。 这对团队合作也有好处。

// MARK: example Web Service start here
// TODO: example 1
// FIXME: Please change BASE url before live 

像这样的方法很容易找到

Xcode 8现在像下面这样处理它,在方法下拉菜单中显示如下: