8.2. Working with Documents

The AppKit framework includes classes to help manage documents of information in a Cocoa program. When you use these classes, AppKit will handle your documents' files and windows for you. You need to focus on your documents' data and how you connect it to your user interface.

AppKit's document support can be used in two different ways, depending on how sophisticated your interface is or what kind of hurry you are in. In both cases you begin by creating your own custom subclass of NSDocument. The NSDocument class provides basic behavior for a document, including reading from and writing to files, tracking open windows, managing document changes and undo, printing, and so on.

Your NSDocument subclass can manage its own windows directly. In this case your NSDocument subclass will contain model code for storing document data in memory and working with files, as well as controller information such as populating the user interface, and updating the model when the interface changes (and vice versa).

Using NSDocument in this way has a few advantages for people new to Cocoa programming. Primarily it simplifies your application code by keeping all of your document knowledge in one class. It's easy to get started working with document-based applications using this method. Also, for very small projects with only a few objects, it may not make sense to burden the application with a more sophisticated design. On the other hand, more complicated classes may suffer ...

Get Beginning Mac OS® X Programming 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.