Chapter 5. Working with Documents on macOS

Now that we’ve done the groundwork for the macOS application, we can start adding the features that power it. Here’s where we’ll actually be doing some programming with Swift.

Because most of the functionality of the app, along with the user interface, comes from the Document class that was automatically provided when we first created the project in Xcode, we’ll be spending most of our time working with Document and enhancing its features to meet our needs. We’ll be adding support for storing text inside our note document format, creating a user interface to show that text and making sure the app can save and open note files.

Along the way, we’ll talk about how documents work on macOS, how to build applications that work with the document system to help users get their work done, and how Swift fits into all of this.

The NSDocument Class

In macOS, documents are represented by the NSDocument class. When you create a new type of document, you subclass this class, inheriting from it, and add the properties and methods that are specific to your situation. For example, later in this chapter, we’ll be adding properties that store the text of the note. If you need a refresher about subclassing and inheritance, refer back to “Inheritance”.

Note

iOS has a similar class, called UIDocument. We’ll be looking at UIDocument in lots of detail in Chapter 9.

The NSDocument class and its many related classes form a framework that allows you to focus on ...

Get Learning Swift, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.