O'Reilly logo

Beginning Mac OS® X Programming by Drew McCormack, Michael Trent

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

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

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