Summary: in my opinion, Swift documentation is not as good as it should be, especially for developers using Swift on non-Apple platforms. I believe we should do better if we want to see Swift become more popular in non-Apple-centric communities.
Specifically, I think the Swift open source project should host and maintain the official documentation for Swift, independent from Apple.
Current Swift documentation resources
The following resources comprise the official documentation for Swift:
-
The book The Swift Programming Language (TSPL) at docs.swift.org/swift-book, consisting of a short guided tour, a language guide and a language reference
-
The reference documentation for the standard library at developer.apple.com/documentation/swift
-
The reference documentation for the Core Libraries:
- Foundation: developer.apple.com/documentation/foundation
- Dispatch: developer.apple.com/documentation/dispatch
- XCTest: developer.apple.com/documentation/xctest
-
The reference documentation for the Swift Package Manager at docs.swift.org/package-manager
-
The collection of Swift Evolution proposals at apple.github.io/swift-evolution
-
The API design guidelines at swift.org/documentation/api-design-guidelines
Problems
Here are some of the problems I have with the documentation as it stands today:
The Swift Programming Language is not a good language reference
TSPL is a good book, but it's not very usable as a reference in my opinion:
-
The TSPL site has no search function. Quick, go to docs.swift.org/swift-book and look up the syntax for the
@available
attribute. Or how theif case let
syntax works. Or how you define a custom operator precedence group. Finding these things is difficult unless you know exactly where to look. -
The language reference section is often written in terms of grammar rules, which I guess is good for compiler developers, but doesn't really fit my mental model as a user of the language. For example, to find the syntax for a
switch
statement (assuming I know where to look), I have to click through this: -
TSPL pages are long and combine multiple topics in a single page. It's difficult to link to specific sections. (The "On this page" menu only has links for the top level, which is often not granular enough.)
The API documentation is Apple-centric
Unlike the Swift book, the standard library documentations is hosted at developer.apple.com/documentation/swift and not on swift.org. I imagine this does not inspire confidence in people evaluating Swift's maturity on non-Apple platforms.
(Side note: the standard library reference isn't even listed on swift.org/documentation. The standard library page on swift.org talks about where you can find the source code for the stdlib, but doesn't have a link to the documentation, either.)
Availability annotations in the standard library docs are presented in terms of Xcode versions. This is not helpful for developers on other platforms. For example, as a Swift developer on Linux who wants to use the Result
type, I have to know that an availability of "Xcode 10.2+" corresponds to Swift 5.0, which is not at all obvious.
SwiftDoc.org, a wonderful community website originally created by @nnnnnnnn and now maintained by @mattt, does a better job at this than the official documentation. But keeping a site like that up to date is a lot of work for a single person.
Core Libraries have no documentation for non-Apple platforms
The Core Libraries are owned and controlled by Apple (there's no community evolution process for Foundation), so it's more understandable that their documentation is hosted on developer.apple.com.
It is unfortunate, however, that the official documentation for Foundation, Dispatch, and XCTest is only concerned with the Darwin version of the libraries:
-
APIs that are not available on non-Darwin platforms are not marked as such. One recent example where this caused confusion:
URLSessionWebSocketTask
is not available on Linux. -
Subtleties like the split between Foundation and FoundationNetworking are not reflected in Apple's documentation.
-
API availability is listed in terms of Apple SDK versions, which is not accurate for other platforms. This can easily cause confusion. Examples:
-
corelibs-Foundation was aligned to macOS 10.15/iOS 13.0 in time for Swift 5.3, meaning that an API listed with "available in macOS 10.15" will not necessarily be available in Swift 5.1, even though that's the Swift version that came out at the same time.
-
I recently found out that the new
XCTIssue
API in XCTest is not part of Swift 5.3 on Linux, requiring me to rewrite some code I had just written.
-
-
Minor point: the Apple documentation uses Apple terminology. For example, XCTest is called a "framework", not a "library". (Not a big deal, but it's a symptom that shows where the documentation originated.)
The Implementation Status page in the corelibs-Foundation repository has more information on API availability for non-Apple platforms, but I don't think it's a natural place where people would look for it: people need a lot of background knowledge about the nature of Foundation to know that this is even a problem. Plus, I'm never sure how up to date the status page is. For example, it doesn't list URLSessionWebSocketTask
at all.
Proposed changes
Here are some changes I'd love to see:
-
Swift documentation should live in the Swift open source project, not at Apple. The standard library documentation should move to swift.org.
-
Availability annotations for standard library APIs should list the Swift compiler version, not (only) the Xcode version.
-
The official documentation at swift.org could set an example for other Swift libraries to follow on how to publish API documentation. Maybe we can take advantage of existing documentation generators created by members of the community, such as swift-doc or Jazzy.
-
The Core Libraries should get an official documentation for non-Apple platforms (either integrated into the current docs or hosted separately on swift.org).
-
In addition to API docs, the documentation at swift.org should include a searchable language reference that includes everything from keywords ("if", "switch", "func") to other syntax tokens ("//", "{", "<"). Examples:
-
It should be possible to search for the difference between
//
and///
. -
When searching for "[", it should list all places where Swift uses square brackets in its syntax: subscripts, array literals, dictionary literals, array type declarations, dictionary type declarations (did I forget anything?).
-
When searching for "<", the search results should list overloads of the
<
operator as well as a doc explaining the use of angle brackets in Swift's generics syntax.
Ideally, a beginner who sees Swift code for the first time should be able to find the meaning of each syntax token just by searching the documentation (and eventually by Option+clicking on a token in Xcode, but that's a different topic; for Apple folks: FB5462882/Radar 27471627, filed in 2016).
-
These changes would surely be a big undertaking, but I believe they would align well with several goals stated by @tkremenek in On the road to Swift 6, namely to create a better cross-platform story for Swift and to make writing Swift "a fantastic development experience".
What do you all think? I'd love to hear from folks at Apple if "decoupling" the Swift documentation from the Apple SDK documentation was considered in the past.