O'Reilly logo

Learning Swift, 2nd Edition by Jon Manning, Tim Nugent, Paris Buttfield-Addison

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required