Re: Documents & Controllers
Re: Documents & Controllers
- Subject: Re: Documents & Controllers
- From: publiclook <email@hidden>
- Date: Sun, 10 Aug 2003 16:26:08 -0400
On Sunday, August 10, 2003, at 01:34 PM, Seth Willits wrote:
For the longest time, before I really dived in to Cocoa, I could never
understand how to create a multi document application. I had seen the
very simple examples, and the others which showed how to create them
*without* NSDocument and the others, but I had never seen one that was
small enough to be readable and used the provided document classes.
Then I dived in. Once I realized what First Responder was all about,
it made a whole lot of sense and I wrote a whole lot of code. Yet I
find myself stuck when going to implement file reading and saving. I
looked in Cocoa Programming, but all it has is a large chapter on
*not* using NSDocument &
On the contrary, according to the index for "Cocoa Programming", in
addition to a chapter on *not* using NSDocument, Chapter 8 has a
description of using the documents classes starting on page 213.
Chapter 18 includes an example that uses the document classes.
Perhaps I am not understanding your question, but when I create a new
Project Builder project using the "Cocoa Document-Based Application"
template, Project Builder provides me with a class called MyDocument,
and that class already has the following two methods defined as well as
others:
- (NSData *)dataRepresentationOfType:(NSString *)aType
{
// Insert code here to write your document from the given data.
You can also choose to override -fileWrapperRepresentationOfType: or
-writeToFile:ofType: instead.
return nil;
}
- (BOOL)loadDataRepresentation:(NSData *)data ofType:(NSString *)aType
{
// Insert code here to read your document from the given data. You
can also choose to override -loadFileWrapperRepresentation:ofType: or
-readFromFile:ofType: instead.
return YES;
}
As long as you can provide an instance of NSData that contains your
document's data (usually model objects) you can implement
-dataRepresentationOfType:. As long as you can restore your document's
data from a provided NSData instance, you can implement
-loadDataRepresentation:ofType:. All of that seems so simple to me
that I am not surprised if you don't find lengthy explanations anywhere.
If you don't want to store your document's data in an NSData instance
even just for loading and saving, the helpful comments in the template
file tell you that you can use -fileWrapperRepresentationOfType: or
-writeToFile:ofType: along with -loadFileWrapperRepresentation:ofType:
or -readFromFile:ofType: instead of -dataRepresentationOfType: and
-loadDataRepresentation:ofType:.
Are there any problems so far ? I am sure we can clear up any
misunderstandings or uncertainties if you ask a slightly more specific
question about what you don't understand from the template code.
The HTMLEditor tutorial at Stepwise
http://www.stepwise.com/Articles/HTMLEditorX/index.html walks you
through using the document classes. O'Reilly has a nice tutorial at
http://www.macdevcenter.com/pub/a/mac/2001/06/01/cocoa.html. Apple
provides a FAQ for document based application questions at
http://developer.apple.com/documentation/Cocoa/Conceptual/Documents/
Tasks/FAQ.html. Start here:
http://developer.apple.com/documentation/Cocoa/Conceptual/Documents/
Tasks/ImplementingDocApp.html.
The following document contains an introduction to the document
architecture:
conferences.oreilly.com/presentations/ macosx02/crawford_docarch.pdf
A simple Google search also shows many resources that address your
question.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.