views:

132

answers:

3

I don't understand how the term "Document" is used in objective-C and cocoa. When I build an Application, why do I have an "Document"? That makes no sense to me. I could have an Application like an Image Editor, wherein I can open like 100 Images at same time. So every Image is a Document and has its own Nib file? Can someone explain that term in a way that's humanly understandable? Best would be a lot of examples, I think.

+1  A: 

Document-based, in terms of the Xcode templates, essentially means an NSDocument subclass. NSDocument and NSDocumentController provide an abstraction that makes it simpler to support multiple documents.

You are correct, you can hand-code it without NSDocument just fine. However, NSDocument does make it a bit easier.

NilObject
Okay, so every Nib file is made for one type of "Document" (ie. an word document, an image to edit, or a new calculator instance)?
Thanks
Not exactly. Each Nib just represents a group of related user interface functionality. It's up to you to decide whether or not that means "a document" or not :)
NilObject
A nib is an archive of objects. Generally, those objects are views and/or windows along with some NSControllers—as NilObject says, a user interface. So any controller object (incl. a document, which Apple calls a “model-controller”) that has a user interface can load that user interface from a nib.
Peter Hosey
+4  A: 

From my ancient but still mostly relevant Cocoa NSFAQ (Not-so FAQ):

Q18 Why would I want a Document subclass?

A18 Because you have a central data model which needs an object to manage it.

In more detail, don't always think Document==File. The conceptual Document in most Mac applications may indeed map to a single file on disk but is also often a central object (Model in classic MVC design).

If you're using a database, the Document might manage the database connection and even end up saved as a file persisting that connection.

For your photo library, the Document might be singular, just containing some settings, or you may have multiple Documents corresponding to different layouts and filters as to which photos are visible.

There's no reason why you can't have more than one Document or even hundreds open if you want to go that way - the Document is a way to provide data to views and you can have hundreds of instances if you want.

Andy Dent
A: 

If you ask Apple What is a document? they'll tell you.

Graham Lee