EventGet 50% off your ticket to MongoDB.local NYC on May 2. Use code Web50!Learn more >>
MongoDB Developer
Realm
plus
Sign in to follow topics
MongoDB Developer Centerchevron-right
Developer Topicschevron-right
Productschevron-right
Realmchevron-right

Document our Realm-Powered Swift Frameworks using DocC

Diego Freniche6 min read • Published Sep 22, 2021 • Updated May 13, 2022
RealmSwift
Facebook Icontwitter iconlinkedin icon
Rate this article
star-empty
star-empty
star-empty
star-empty
star-empty

Introduction

In the previous post of this series we added Realm to a really simple Binary Tree library. The idea was to create a Package that, using Realm, allowed you to define binary trees and store them locally.
Now we have that library, but how will anyone know how to use it? We can write a manual, a blog post or FAQs, but luckily Xcode allowed us to add Documentation Comments since forever. And in WWDC 21 Apple announced the new Documentation Compiler, DocC, which takes all our documentation comments and creates a nice, powerful documentation site for our libraries and frameworks.
Let’s try it documenting our library!

Documentation Comments

Comments are part of any language. You use regular comments to explain some specially complicated piece of code, to leave a reason about why some code was constructed in a certain way or just to organise a really big file or function (By the way, if this happens, split it, it’s better). These comments start with // for single line comments or with /* for block comments.
And please, please please don't use comments for things like
For example, these are line comments from Package.swift:
While this is a regular block comment:
We’ve had Documentation Comments in Swift (and Objective C) since forever. This is how they look:
These are similar in syntax, although have two major differences:
  • You can write Markup in documentation comments and Xcode will render it
  • These comments explain what something is and how it’s used, not how it’s coded.
Documentation comments are perfect to explain what that class does, or how to use this function. If you can’t put it in plain words, probably you don’t understand what they do and need to think about it a bit more. Also, having clearly stated what a function receives as parameters, what returns, edge cases and possible side effects helps you a lot while writing unit tests. Is simple to test something that you’ve just written how it should be used, what behaviour will exhibit and which values should return.

DocC

Apple announced the Documentation Compiler, DocC, during WWDC21. This new tool, integrated with Xcode 13 allows us to generate a Documentation bundle that can be shared, with beautiful web pages containing all our symbols (classes, structs, functions, etc.)
With DocC we can generate documentation for our libraries and frameworks. It won’t work for Apps, as the idea of these comments is to explain how to use a piece of code and that works perfectly with libraries.
DocC allows for much more than just generating a web site from our code. It can host tutorials, and any pages we want to add. Let’s try it!

Generating Documentation with DocC

First, grab the code for the Realm Binary Tree library from this repository. In order to do that, run the following commands from a Terminal:
If you want to follow along and make these changes, just checkout the tag initial-state with git checkout initial-state.
Then open the project by double clicking on the Package.swift file. Once Xcode ends getting all necessary dependencies (Realm-Swift is the main one) we can generate the documentation clicking in the menu option Product > Build Documentation or the associated keyboard shortcut ⌃⇧⌘D. This will open the Documentation Browser with our library’s documentation in it.
Xcode’s Documentation Browser showing the BinaryTree Framework documentation.
As we can see, all of our public symbols (in this case the BinaryTree class and TreeTraversable protocol are there, with their documentation comments nicely showing. This is how it looks for TreeTraversable::mapInOrder(tree:closure:)
Xcode Documentation Browser showing details for the mapInOrder function

Adding an About Section and Articles

This is nice, but Xcode 13 now allows us to create a new type of file: a Documentation Catalog. This can host Articles, Tutorials and Images. Let’s start by selecting the Sources > BinaryTree folder and typing ⌘N to add a new File. Then scroll down to the Documentation section and select Documentation Catalog. Give it the name BinaryTree.docc. We can rename this resource later as any other file/group in Xcode. We want a name that identifies it clearly when we create an exported documentation package.
Xcode “Choose a template for your new file” window opened with “Documentation Catalog” selected
Let’s start by renaming the Documentation.md file into BinaryTree.md. As this has the same name as our Doc Package, everything we put inside this file will appear in the Documentation node of the Framework itself.
We can add images to our Documentation Catalog simply by dragging them into Resources. Then, we can reference those images using the usual Markdown syntax ![](/image-name.png). This is how our framework’s main page look like now:
Documentation Browser showing the BinaryTree main page with an image of a tree, in pixel art style
Inside this documentation package we can add Articles. Articles are just Markdown pages where we can explain a subject in written longform. Select the Documentation Package BinaryTree.docc and add a new file, using ⌘N. Choose Article File from Documentation. A new Markdown file will be created. Now write your awesome content to explain how your library works, some concepts you need to know before using it, etc.

Tutorials

Tutorials are step by step instructions on how to use your library or framework. Here you can explain, for example, how to initialize a class that needs several parameters injected when calling the init method, or how a certain threading problem can be handled.
In our case, we want to explain how we can create a Tree, and how we can traverse it.
Tutorial main page showing a header with a pixel art tree image and two Articles at the bottom.
So first we need a Tutorial File. Go to your Tutorials folder and create a new File. Select Documentation > Tutorial File. A Tutorial file describes the steps in a tutorial, so while you scroll through it related code appears, as you can see here in action.
Animated GIF showing how as you scroll down steps, new code appears to the right of the Article
We need two things: our code snippets and the tutorial file. The tutorial file looks like this:
As you can see, we have a first @Tutorial(time: 5) line where we put the estimated time to complete this tutorial. Then some introduction text and images, and one @Section. We can create as many sections as we need. When the documentation is rendered they’ll correspond to a new page of the tutorial and can be selected from a dropdown picker. As a tutorial is a step-by-step explanation, we now add each and every step, that will have some text that will tell you what the code will do and the code itself you need to enter.
That code is stored in Resources > code as regular Swift files. So if you have 5 steps you’ll need five files. Each step will show what’s in the associated snippet file, so to make it appear as you advance one step should include the previous step’s code. My approach to code snippets is to do it backwards: first I write the final snippet with the complete sample code, then I duplicate it as many times as steps I have in this tutorial, finally I delete code in each file as needed.

Recap

In this post we’ve seen how to add developer documentation to our code, how to generate a DocC package including sample code and tutorials.
This will help us explain to others how to use our code, how to test it, its limitations and a better understanding of our own code. Explaining how something works is the quickest way to master it.
In the next post we’ll have a look at how we can host this package online!
If you have any questions or comments on this post (or anything else Realm-related), then please raise them on our [community forum(https://www.mongodb.com/community/forums/c/realm-sdks/58). To keep up with the latest Realm news, follow @realm on Twitter and join the Realm global community.

Reference Materials

Sample Repo

Apple DocC documentation

WWDC21 Videos


Facebook Icontwitter iconlinkedin icon
Rate this article
star-empty
star-empty
star-empty
star-empty
star-empty
Related
Tutorial

Build an Infinite Runner Game with Unity and the Realm Unity SDK


Feb 03, 2023 | 12 min read
Tutorial

Building a Mobile Chat App Using Realm – Integrating Realm into Your App


Apr 06, 2023 | 20 min read
Tutorial

How to Write Integration Tests for MongoDB Atlas Functions


Aug 26, 2022 | 5 min read
Tutorial

Start Implementing Google Auth With MongoDB Realm in Your Android App


Mar 13, 2024 | 9 min read
Table of Contents