Search code examples
swiftcocoamodelnsdocument

Is NSDocument subclass a model object or not?


I am writing a multi-document application for macOS Swift. I'm starting with the Document app template provided by Xcode. It's unclear whether it’s an object model or not. Is NSDocument the model or is the data contained therein the model?


Solution

  • Apple says that a “document’s data is called the data model”:

    In the Cocoa document architecture, your document is represented by a custom subclass of NSDocument. The NSDocument class provides most of the behavior for managing your document. In your subclass, you override or fine-tune this behavior by providing custom code for reading and writing document data. A document’s data is called the data model. Each NSDocument has its own NSWindowController, which in turn creates an NSWindow object for displaying the document content.

    Because a document-based app handles multiple documents that are open at the same time, it uses NSDocumentController to manage them. Cocoa provides most of the infrastructure for managing your documents. With file coordination, version management, and conflict resolution among other documents, it provides the easiest path to using iCloud.

    enter image description here

    This NSDocument subclass imports Cocoa and is deeply entangled with UI layer code, so one would be hard-pressed to call a true model object (though it is a useful abstraction). Many of us strive to keep true “model” objects free of UI/OS dependencies.

    In short, the data managed by the NSDocument is more aptly considered the “model”.