NSDocument -init vs. -initWithContentsOfFile:ofType: (re post)
NSDocument -init vs. -initWithContentsOfFile:ofType: (re post)
- Subject: NSDocument -init vs. -initWithContentsOfFile:ofType: (re post)
- From: Keith Renz <email@hidden>
- Date: Sun, 7 Sep 2003 19:01:45 -0400
In my NSDocument subclass, I would like to have separate
initializations for new (untitled) documents and documents opened from
file, i.e. mutually exclusive inits. What I want to do is not perform a
new document's initialization when opening from a file. This seems like
a difficult thing to do because my new document initialization is done
in the -init method. When I open a new document, all works well. But
when I open a document from file, -init is invoked, does the new
document initialization, and eventually -loadDataRepresentation:ofType:
is invoked, re-initializing most of the data. Seems like a waste. In
fact, in a different NSDocument subclass, it's more than just a waste,
but a problem.
The Programming Topic, Creating a Subclass of NSDocument, says:
-----
The initializers of NSDocument are another issue for subclassers. The
init method is the primary initializer, and it is invoked by the other
initializer initWithContentsOfFile:ofType:. The init method is directly
invoked when a new document is created; the
initWithContentsOfFile:ofType: method is directly invoked when a
document is opened. Therefore if you have any initializations that
apply only to documents that are opened, you should override
initWithContentsOfFile:ofType:; if you have general initializations,
you should, of course, override init. In both cases, be sure to invoke
super's implementation as the first thing.
-----
I really can't isolate the initializations using
initWithContentsOfFile:ofType: for documents from file because it
immediately invokes -init, assuming I invoke super as required, which
does the new document initialization. I can set a "has file" flag in
-initWithContentsOfFile:ofType: before invoking super, and check the
flag in -init before doing the new document initialization, but that's
clearly wrong due to the issues with self during initialization.
I feel like I'm missing something. Can someone shed some light on this?
Thanks,
Keith
_______________________________________________
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.