DocC is Apple’s new software for constructing stunning, interactive, and native-feeling documentation proper out of your code, and it integrates easily into each Xcode and the net.
On this article I’m going to stroll you thru the basics of utilizing it in two alternative ways: constructing documentation for a framework, and including articles that present extra depth. Alongside the best way, you’ll see some locations the place DocC works brilliantly, and likewise some locations the place it doesn’t actually work like I’d anticipate in any respect. And that’s okay – it’s solely the preliminary launch, in spite of everything.
Earlier than I begin, although, there are some fundamentals:
- It’s essential to have Xcode 13 put in to be able to have entry to DocC.
- It helps solely Swift at the moment. They’ve already obtained requests so as to add Goal-C assist, and in the long run I hope they’ll add assist for different languages too.
- In case you have been curious, DocC is brief for “Documentation Compiler”, and it’s pronounced with two equally harassed syllables – “Doc-See” versus “doxy”.
- Xcode is full of a lot of main new options – it’s one other actual stand out yr, and I hope the staff are feeling actually proud. Yow will discover out extra in my article What’s new in Xcode 13.
Please remember I’m utilizing Xcode 13 beta 1, so it’s all early days at this level – I anticipate DocC to get polished up loads earlier than the ultimate launch later this yr.
Sponsor Hacking with Swift and reach the world’s largest Swift community!
First, what DoC is not
The very first thing I did when making an attempt out DocC is what I anticipate most app builders would do: I opened up certainly one of my initiatives to see what DocC would make of it. In my case, that was Unwrap – it’s a great dimension of venture with varied dependencies, and matches the type of factor many app builders can be working with.
If you wish to do that in your personal app venture, simply go to the Product menu and select Construct Documentation. It would take a minute or two to finish, however when it’s completed you’ll see Apple’s Developer Documentation window seem with its outcomes – your app’s documentation, dwelling proper alongside Apple’s personal frameworks.
Besides it isn’t, at the least not in the best way I had hoped. You see, at the moment DocC helps solely frameworks and packages, which implies it picked out solely the exterior packages utilized by my app versus all of the app code itself. It is a little bit of a disgrace, to be sincere, as a result of the overwhelming majority of us work on precise app initiatives, and when somebody new joins a staff it will be nice to present them some clear, readable documentation to assist on-ramp them.
When you’re on this state of affairs, I’d recommend seeking to break your venture down into smaller packages – that ought to permit DocC to scan and doc them appropriately.
My subsequent try was with my Sitrep venture, which is a package deal. I felt positive this may work a lot better, however once more hit issues: DocC will solely scan for issues which are obtainable externally to the package deal, so most of Sitrep is ignored. This isn’t a bug, however somewhat one other disconnect between my head and the best way DocC works – it’s very tightly centered on people who find themselves utilizing a package deal or framework, versus folks constructing that package deal or framework, or certainly an app.
When you ever used one thing like Jazzy to generate your documentation, it had the identical public
limitation by default, however at the least there you might ask it to show inner values for if you truly wished it.
Working with Markdown documentation
As soon as I had found out what DocC wasn’t, it was a lot simpler to discover a venture that might undoubtedly work properly: SwiftGD, my open-source picture manipulation package deal that allows you to create graphics with server-side Swift.
Though this package deal does have some inner performance, it’s principally uncovered to the world as public API and that’s precisely what DocC is in search of. And out of the field – simply going to Product > Construct Documentation – we truly get one thing fairly good:
- All of the lessons, structs, and enums are listed, together with their summaries.
- Choosing any of them will present
- You possibly can open up any of them within the navigator to disclose their properties and strategies
- A number of clickable hyperlinks between strategies and properties
In truth, the documentation is just about indistinguishable from Apple’s personal developer documentation – even all the way down to exhibiting “No overview obtainable” when one thing is lacking.
All this works out of the field due to documentation feedback, that are Xcode feedback written in a particular format: both utilizing triple slash, ///
, or by beginning with /**
.
Markdown feedback are not the identical because the common inline feedback you add to specific code, so it’s worthwhile to use them in another way:
- Common feedback,
//
and/*
, are used to mark what your code is making an attempt to do, including explainers for individuals who wish to perceive or modify that piece of code. - Documentation feedback,
///
and/**
, are used to mark how your code ought to be used, explaining what its parameters are, what it returns, and extra.
Xcode has handled documentation remark specifically even earlier than DocC – they get pulled out and proven within the Fast Assist pane, in addition to within the autocomplete popup.
One of the simplest ways to get began with Markdown documentation is to pick a operate you wish to doc, press Shift+Cmd+A to carry up the code actions balloon, then choose Add Documentation. When you’re like me and like keyboard shortcuts, use Choose+Cmd+/ to get the identical end result.
This shortcut is especially helpful earlier than features, as a result of it provides details about parameters and return sort mechanically. However when you’re inside a documentation remark, all of the Markdown you’d anticipate is on the market to you:
Place textual content in `backticks` to mark items of code.
DocC additionally helps ``doubleBackticks`` to create hyperlinks to different elements of your documentation, and paths corresponding to ``SomeClass/someProperty`` to be extra particular.
* Write bullets by beginning with an asterisk then an area.
* Indent your asterisks to create sublists
1. You possibly can write numbered listed by beginning with 1.
1. Subsequent gadgets might be numbered increased if you'd like, however you may as well quantity them 1. and Xcode will renumber them for you.
# Headings begin with a # image
When you want subheadings, use ##, ###, and so forth.
For hyperlinks, [place your text in brackets](and your hyperlink in parentheses).
Write *one asterisk* round phrases to make them italic
Write **two asterisks** round phrases to make them daring
Write ***three asterisks*** round phrases to make them daring and italic on the similar time
That’s all simply common Markdown syntax, however there are some particular issues that each Xcode and DocC search for, and for those who add these it’s going to actually enrich the documentation you present.
For instance, the Returns
key phrase enables you to specify what worth the caller can anticipate again when the operate runs efficiently. This isn’t simply the info sort – Xcode can determine that out for itself – however as an alternative what’s in the worth. So, for instance you would possibly say this:
- Returns: A string containing a date formatted as RFC-822
There’s additionally the Parameter
key phrase, which helps you to specify the identify of a parameter and describe what it comprises. You possibly can embody as many Parameter
strains as you’ve gotten parameters, simply itemizing them every out together with what they really are:
- Parameter album: The identify of a Taylor Swift album
- Parameter monitor: The monitor quantity to load
Once more, Xcode can work out the kind of the parameter, so that you’re simply documenting what it represents.
There’s additionally the Throws
key phrase, the place you may present a comma-separated listing of the error sorts that may be thrown by the operate:
- Throws: LoadError.networkFailed, LoadError.writeFailed
There are others, however in apply one of the crucial necessary items of textual content you’ll use is the road break – actually simply urgent return. It is because the primary strains of your remark are used because the abstract in your operate, however if you go away one clear line break adopted by extra textual content that subsequent textual content will get used because the detailed dialogue of your code.
Including a Documentation Catalog
To date we’ve simply relied on plain documentation feedback to get good outcomes, however DocC lets us go loads additional by including customized articles that actually carry our documentation to life.
You see, every little thing now we have thus far makes for a great reference, which is ideal for the instances you wish to examine a particular sort, property, or technique. Nevertheless it’s much less good at truly introducing what our framework is making an attempt to do – offering an introductory reference, explaining how the elements match collectively, or stating notably necessary ideas.
We will repair that by including a documentation catalog to our venture, which lets us present customized documentation that sits outdoors of our foremost codebase.
To do that in your personal venture, right-click in your package deal’s listing within the venture navigator, then select New File. When you scroll all the way down to the Documentation part you’ll see Documentation Catalog – choose that and click on Subsequent, and Xcode will create the catalog for you instantly.
Xcode will create the documentation catalog for you, referred to as Documentation. Inside there it’s going to create a single Markdown file, additionally considerably unhelpfully referred to as Documentation, in addition to a Sources listing the place you may place belongings corresponding to screenshots.
To get began, attempt altering that preliminary Markdown doc in order that it represents the homepage in your entire package deal. To try this, change the road on the very high to your package deal identify, much like this:
# ``SwiftGD``
You possibly can then go forward and write free Markdown beneath, however fastidiously – you get management over your abstract, overview, and all of the subjects you wish to listing, however you must comply with a selected format:
- Depart the “Matters” title alone. When you change its identify the output will look bizarre, and for those who attempt to add one other second-level heading it gained’t work.
- The third-level headings – “Group” by default – turn out to be the left-hand-aligned headings that sit subsequent to stuff you wish to pick.
- Contained in the third-level headings you may place bulleted hyperlinks to articles you wish to point out. Don’t try to place something apart from hyperlinks to different issues in there, as a result of it gained’t work.
By way of what you would possibly put into the third-level headings, you may both hyperlink to sorts into your package deal, or hyperlink to different documentation articles you’ve written.
Necessary: Bear in mind, hyperlinks are like common code Markdown besides use double backticks.
For instance, in my SwiftGD venture I would wish to pick some particular sorts which are necessary like this:
- ``Colour``
- ``Rectangle``
Or I would wish to level out particular properties on a sort, like this:
- ``Rectangle/level``
- ``Rectangle/dimension``
You would possibly discover how Xcode is sensible sufficient to do code completion right here, which is absolutely useful when working with strategies since you simply sort them out in full, full with parameter names:
- ``Picture/applyInterpolation(enabled:currentSize:newSize:)``
At build-time these hyperlinks get expanded to full documentation references that click on by way of to the total web page, and likewise offering any abstract textual content beneath if doable.
When you’ve made your modifications, rebuild your documentation to see the way it seems to be – all being properly it ought to all be there.
Necessary: When you discover your modifications aren’t seen, you would possibly discover it’s worthwhile to improve the swift-tools-version
in your Bundle.swift file. SwiftGD was set to five.0 and so didn’t work properly with DocC, however bumping that as much as 5.5 solved the issue.
Earlier than we transfer on, I wish to present you ways sensible DocC is with regard to photographs, as a result of similar to utilizing an asset catalog it takes a variety of the trouble away so long as you assist it alongside.
So as to add a picture, first give it a reputation that clarifies what it’s for. Apple recommends utilizing @2x photographs throughout the board, so on the very least it’s worthwhile to present [email protected] Nonetheless, if you’d like one picture for mild mode and one other for darkish, you may present the darkish variant by placing “~darkish” earlier than the @2x to make [email protected]
These sorts of magic filenames have been frequent in iOS growth earlier than we had entry to asset catalogs, and actually I’d wish to see them right here too – managing belongings will get tough in non-trivial venture, and any instruments that may assist are welcome.
Anyway, to embed that picture in an article, use customary Markdown picture syntax:

In that small snippet, discover first how we don’t want so as to add picture variant information to the filename – simply plain previous hws.png is okay – and likewise that it’s necessary so as to add an outline to VoiceOver can describe the picture contents.
When you go forward and construct your documentation once more, you must see the image in place. In beta 1, plainly the change between mild and darkish mode isn’t fairly flawless – you would possibly discover it’s worthwhile to change again and ahead between articles to get the picture to replace.
The place these articles turn out to be much more helpful is if you begin including different articles to your doc catalog, as a result of you may add as many extra articles as you wish to your catalog and hyperlink them collectively.
To do this out, right-click in your documentation catalog within the venture navigator and add a brand new file. You’ll see there may be each an Article File and an Extension File, however it is a bit complicated as a result of so far as I can see they each make the identical factor – they each make a Markdown file with no particular construct choices.
The one precise distinction is the template that’s generated, as a result of paperwork with a sort within the top-level header are thought-about to be documentation particularly for that sort. Consider it a bit like a Swift extension: you might put all of your code within the authentic sort, but it surely’s usually simpler and extra logical to separate issues up. In documentation phrases, you might put very lengthy documentation feedback into your code for those who wished, however these extension recordsdata imply you don’t must as a result of you may transfer them over to pure Markdown as an alternative.
For now, go forward and select Article File, which lets us generate one other free-text article. Similar to our authentic article this could include all of the Markdown you need, together with hyperlinks to different articles and API you wish to point out.
The identify you give to this new article issues, as a result of it impacts the way you reference it elsewhere. You should utilize areas to get extra pure names if you’d like; they’ll simply get changed with dashes at construct time.
Anyway, now that now we have a second article you may go forward and add all of the content material you wish to it, however extra importantly you may hyperlink to that article elsewhere in your documentation. That is completed utilizing a particular hyperlink sort:
<doc:New-Article>
Once more, Xcode goes to autocomplete this for you, and it’ll even verify for those who add a damaged hyperlink accidentally – you’ll get a warning proper there in your Markdown file. (PS: Xcode people: might this be an error? Thanks!)
Distributing your work
All this time we’ve been pushing our work instantly into Xcode’s documentation viewer, however that’s simply short-term – as quickly as you shut Xcode it’s going to disappear from there. It’s useful whilst you’re working to know that all of it will get swept away as quickly as you stop, however clearly sooner or later you’ll have one thing you truly wish to share with the broader world.
You possibly can if you’d like construct your documentation package deal mechanically utilizing xcodebuild
, however you may as well right-click on it instantly contained in the documentation viewer and select Export. When you do that you simply’ll get a “.doccarchive” package deal – it’s nothing fancy, only a listing with must learn your documentation in Xcode or on the net.
So, you might now zip that up and mail it to somebody for evaluation, or if you wish to publish it on-line. Now, for those who’re something like me you’re most likely conscious that Python has a built-in internet server module that’s actually useful for fast testing – you’d run python3 -m http.server within the listing.
Nonetheless, that doesn’t work: the package deal they generate requires a really particular server configuration. It must be served on the web site root, with particular redirects in place to be able to work. Actually, if Apple hadn’t offered the right Apache htaccess directions I wouldn’t have managed to get it working.
On your reference as a lot as mine, right here they’re:
RewriteEngine On
RewriteRule ^(documentation|tutorials)/.*$ YourPackage.doccarchive/index.html [L]
RewriteRule ^(css|js|information|photographs|downloads|favicon.ico|favicon.svg|img|theme-settings.json|movies)/.*$ YourPackage.doccarchive/$0 [L]
Upon getting that in a htaccess file in your internet server, clearly adjusted in order that YourPackage.doccarchive matches your filename, you must be capable of copy throughout the documentation archive and have every little thing roughly work.
To attempt it out, I arrange http://docc.hackingwithswift.com – it gained’t truly work as a hyperlink, as a result of it’s an area internet server working on my Mac utilizing MAMP. Though the archive must be served from the web site root, it doesn’t take over the remainder of your website – you continue to must serve the remainder of the pages your self, and it’ll simply handle these corresponding to http://docc.hackingwithswift.com/documentation/SwiftGD in my case.
The tip end result seems to be principally similar to Apple’s personal hosted documentation, except mild and darkish mode controls displayed on the backside. On the one hand that’s nice, as a result of it’s a well-recognized atmosphere to lots of people, however then again I feel there can be some loud calls for personalisation choices.
As issues stand, the generated documentation is successfully a sealed field – the Apache rewrite rule references a theme-settings.json file, which recommend some customization goes to come back, however I don’t know to what extent. I can think about larger firms wanting their company branding built-in, or their common website navigation.
The online pages are additionally very heavy on the JavaScript and actually I’m unsure why – the reference documentation and articles are easy beasts, and if DocC might flatten them to plain HTML then I think about the rewrite guidelines would simply go away. On the similar time, Apple’s personal documentation system is totally inaccessible with JavaScript turned off, so I don’t maintain out an excessive amount of hope right here.
The place subsequent?
It’s value reiterating what I stated at first: that is solely beta 1 of Xcode 13, so there’s an excessive amount of time for issues to evolve earlier than we hit a remaining launch, and even then it’s just the start – I anticipate DocC to proceed to evolve and develop sooner or later as extra options are added.
For instance, for those who question xcodebuild on the command line it seems to be like experimental documentation protection is someplace in there, which might give DocC the flexibility to report again how a lot of our code is totally documented.
We’re additionally but to see how DocC adapts to different programming languages, not least Goal-C. Apple people on Twitter and the developer boards have stated it’s a precedence, which might go an extended approach to serving to DocC obtain broader adoption.
There are already instruments on the market that do comparable duties, however none are fairly so built-in. I spoke to JP Simard, who created the broadly in style Jazzy software again in 2015 in addition to many others – it does the same job to DocC, besides is open supply, community-driven, helps Goal-C, and works with personal symbols.
As JP identified, the power of DocC is that it goes past simply documentation technology:
“I’m actually comfortable to see Apple offering a code documentation technology software, and it’s extra than simply the software – it’s the viewer UI, it’s Xcode integration with the Construct Documentation command, Fast Assist integration, and it’s a CLI too. The output has a static HTML website you may host your self and consider in an internet browser, however even higher all the info that’s proven within the wealthy UI can be accessible in a machine-readable JSON format so present documentation technology instruments like Jazzy and SwiftDoc might simply add assist for ingesting that information.”
One attention-grabbing benefit that DocC has is its skill to generate interactive tutorials similar to these Apple used for SwiftUI. Though they aren’t one thing I can see myself utilizing simply but, I’ll definitely be watching to see if the group reveals curiosity on this format.
Apple has introduced that DocC can be open sourced by the tip of the yr, and I’m anticipating uptake to be fairly brisk. Heck, there’s already a project in place to assist cloud-based DocC archives, taking away all the trouble of internet hosting fully.
DocC was a extremely nice shock this yr, and the staff have completed a great job of constructing actually tight integration with the remainder of Xcode. I’m assured the few hiccups I’m hitting proper now will get ironed out earlier than launch, and we’d even see a number of further little options sneak in too.
Are you planning to maneuver over to DocC, or is it lacking a characteristic you’re counting on? Let me know on Twitter @twostraws!
Sponsor Hacking with Swift and reach the world’s largest Swift community!