SwiftUI Swift Xcode 文档标记教程

Swift 文档基础

我发现 Swift 有趣的一件事是文档标记不是 IDE 的功能,而是编译器本身的功能。通过浏览源代码,我们可以看到 Swift 支持的所有内容:

MARKUP_SIMPLE_FIELD(AttentionField, attention, Attention)
MARKUP_SIMPLE_FIELD(AuthorField, author, Author)
MARKUP_SIMPLE_FIELD(AuthorsField, authors, Authors)
MARKUP_SIMPLE_FIELD(BugField, bug, Bug)
MARKUP_SIMPLE_FIELD(Complexity, complexity, Complexity)
MARKUP_SIMPLE_FIELD(CopyrightField, copyright, Copyright)
MARKUP_SIMPLE_FIELD(DateField, date, Date)
MARKUP_SIMPLE_FIELD(ExperimentField, experiment, Experiment)
MARKUP_SIMPLE_FIELD(ImportantField, important, Important)
MARKUP_SIMPLE_FIELD(InvariantField, invariant, Invariant)
MARKUP_SIMPLE_FIELD(LocalizationKeyField, localizationkey, LocalizationKey)
MARKUP_SIMPLE_FIELD(MutatingvariantField, mutatingvariant, Mutatingvariant)
MARKUP_SIMPLE_FIELD(NonmutatingvariantField, nonmutatingvariant, Nonmutatingvariant)
MARKUP_SIMPLE_FIELD(NoteField, note, Note)
MARKUP_SIMPLE_FIELD(PostconditionField, postcondition, Postcondition)
MARKUP_SIMPLE_FIELD(PreconditionField, precondition, Precondition)
MARKUP_SIMPLE_FIELD(RemarkField, remark, Remark)
MARKUP_SIMPLE_FIELD(RemarksField, remarks, Remarks)
MARKUP_SIMPLE_FIELD(ReturnsField, returns, Returns)
MARKUP_SIMPLE_FIELD(ThrowsField, throws, Throws)
MARKUP_SIMPLE_FIELD(RequiresField, requires, Requires)
MARKUP_SIMPLE_FIELD(SeeField, seealso, See)
MARKUP_SIMPLE_FIELD(SinceField, since, Since)
MARKUP_SIMPLE_FIELD(TagField, tag, Tag)
MARKUP_SIMPLE_FIELD(TODOField, todo, TODO)
MARKUP_SIMPLE_FIELD(VersionField, version, Version)
MARKUP_SIMPLE_FIELD(WarningField, warning, Warning)
MARKUP_SIMPLE_FIELD(KeywordField, keyword, Keyword)
MARKUP_SIMPLE_FIELD(RecommendedField, recommended, Recommended)
MARKUP_SIMPLE_FIELD(RecommendedoverField, recommendedover, Recommendedover)

(这里没有提到的是Parameters字段,处理方式略有不同)。

您可以通过添加破折号后跟标记的标识符来将这些字段中的任何一个附加到方法。当你这样做时会发生什么将取决于 Xcode,但快速查看功能支持大多数这些字段:

/// Combines this with another condition publisher.
///
/// I'm just adding random information for an article. Carry on!
///
/// - Attention: This code is really cool.
/// Here's why I think you should use this:
///  * item 1
///  * item 2
///    * item 2.1
/// * item 3
///
/// - Complexity: O(1)
/// - Parameters:
///   - publisher: The other publisher.
/// - Returns: A combined publisher that has the condition of both publishers.
func ...

在这里插入图片描述

Xcode 特定功能

尽管大部分工作都在 Swift 中完成,但 Xcode 也有自己的一些功能。您现在可能已经知道 Xcode 可以呈现 Markdown,在我看来,它最有趣的用途可能是添加指向文档的链接:

/// Combines this with another condition publisher.
///
/// - Bug:[IOS-10395](https://myjira.com/ios/10395)

在这里插入图片描述

但是 Xcode 也有一些样式实用程序。我特别喜欢你可以创建水平规则:

An example of using a *horizontal rule*

This text is above the horizontal rule

*  *  *  *  *

And this is below

在这里插入图片描述

但不幸的是,Xcode 最酷的文档功能仅限于 Playgrounds。在 Playground 中,链接标记甚至可以用于显示视频:

//: Watch the WWDC session introducing the San Francisco Font
//: ! [San Francisco font introduction ] ( new-fonts.mp4 )
``

![在这里插入图片描述](https://img-blog.csdnimg.cn/5dadb284b72c4a4eb6d5f21316bcd4ed.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2lDbG91ZEVuZA==,size_16,color_FFFFFF,t_70)

Xcode 还声称支持标题标记以显示更大的文本,但它似乎不适用于最新的 Xcode 版本。

/*
An example of using a character underline for a
heading*

This is a Heading 1

*/

![在这里插入图片描述](https://img-blog.csdnimg.cn/3c9a77955d684096942b998176a39170.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2lDbG91ZEVuZA==,size_16,color_FFFFFF,t_70)

# Xcode 13 / DocC 引入的更改
借助 Apple 在 Xcode 13 中发布的新文档页面生成工具,您现在可以引用代码。您已经可以使用块``swift为块编写代码块,但它实际上并没有链接到引用:

/// Merges two `Publishers` of `Condition<T>`.
/// “`swift
/// let publisher = CurrentValueSubject<Condition<Int>, Never>(.satisfied(1))
/// let publisherB = CurrentValueSubject<Condition<Int>, Never>(.satisfied(2))
/// let combined = publisher.combineCondition(publisherB)
/// “`



![在这里插入图片描述](https://img-blog.csdnimg.cn/3c3094a2d72f4e439026db370e983e1a.png)

***
# 精品教程推荐
- [《CSDN SwiftUI源码大全》](https://www.openswiftui.com/?gr_redirect=csdn-swiftui%e6%ba%90%e7%a0%81%e5%a4%a7%e5%85%a8)

- [《小专栏 SwiftUI教程》](https://www.openswiftui.com/?gr_redirect=%e5%b0%8f%e4%b8%93%e6%a0%8f-swiftui%e6%95%99%e7%a8%8b)

***
# 加入我们一起学习SwiftUI
QQ:3365059189
SwiftUI技术交流QQ群:518696470
教程网站:[www.openswiftui.com](https://www.openswiftui.com)

发表回复