Automatic Headers in Swift

Unlike Objective-C, Swift doesn’t have a concept of header files. In a lot of ways that’s great, because it eliminates a lot of unnecessary typing and repetition. However, I find myself missing headers because they were a great way to get an at-a-glance look at the public interface to a class. Consider the following Objective-C header:

The header defines a Message class with a number of properties, as well as a method for replying to the message and a class method for downloading all messages. It’s easy to tell from looking at the header what functionality it exposes to other classes without needing to worry about how it’s implemented. The header also hides any private variables that other classes don’t need to know about and don’t have access to.

In contrast, here’s the same class re-written in Swift:

The Swift class intermingles the interface and the implementation, as well as public and private properties and functions. We can see all the guts of what the class does under the hood, even though that’s not relevant to someone who just wants to use the class. Moreover, it’s conceivable that some of these functions would be long. (I’ve omitted an actual implementation here for the sake of this example.) That means a lot of scrolling and scanning through code just to see what functions are available.

In order to make this easier, I propose that Apple add a new view to the Assistant Editor in Xcode. This view would display an on-the-fly public “header” for the Swift file in the main editor pane. The header would only contain public (and maybe internal?) methods, as well as function signatures. Something like the following:

As you can see, it’s very similar to an Objective-C header. However, we didn’t actually have to write it manually, and there’s no actual file associated with the header. It’s just a view of the class generated by Xcode for convenience. This view would be uneditable, and would be automatically updated by Xcode as the Swift code changes. In practice, this would be a lot like developing in Objective-C using the “Counterparts” mode in the Assistant Editor to display header and implementation side-by-side. In short, the “header view” would give developers all the benefits of headers without actually having to write them.

UPDATE 9/6/14: I’ve filed a radar requesting this enhancement. Please consider duping!