Re: Why does initWithCoder has other self-object?
Re: Why does initWithCoder has other self-object?
- Subject: Re: Why does initWithCoder has other self-object?
- From: Quincey Morris <email@hidden>
- Date: Thu, 18 Aug 2011 00:40:05 -0700
On Aug 17, 2011, at 04:34 , Arno Oesterheld wrote:
> I am a beginner with Xcode and Objective-C and stuck with a quite simple thing for over two days now. I hope you can help me.
>
> My Project is deployed for OS X 10.6, it uses Garbage Collection and I am using Xcode 4.0.1.
>
> I made a multi document application starting with the template provided by Xcode. I just have one class as subclass of NSDocument.
>
> For opening documents I use initWithCoder. The decoding mithin this method works fine - I get the values that were saved.
> But these values are "lost" when I would like to use them in an other method (of the same class).
>
> I assume that I make some mistakes with using the right comibation of init: / initWithCoder: / initWithContentsOfURL: etc.
> The self-object does always have a different adress in the initWithCoder method then in the other methods.
> I tried plenty of combinations of the above methods and even tried to call differend methods in the super class (NSDocument) within initWithCoder.
Well, it's hard to answer your questions very clearly, because you have some basic confusions here. You're going to have to go back and study the documentation some more, and possibly get hold of a Cocoa tutorial boom that leads you through some of the basics.
I think the central (but not only) point of confusion is that you seem to be trying to archive a NSDocument object. This isn't how it works. The NSDocument object (well, your subclass object) is a transient object, created when you open the document and destroyed when you close it.
The *contents* of your document, also known as your data model, is what is archived, usually to a file. It's an object or a hierarchy of objects of some *other* class than NSDocument.
So, it makes no sense to declare your NSDocument subclass as conforming to NSCoding.
I think what's happening now (very roughly, since you didn't show enough code to be sure of this) is that you're archiving a NSDocument object as your data model. When you re-open the document file, you unarchive the object, which contains the archived data (so there is the *appearance* that it did something right). Now you have 2 NSDocument objects -- the one that was created for you as a result of opening the document file, and the one that was unarchived. The second one isn't expecting to be created this way, and it kind of messes things up.
Try reading here:
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaDesignPatterns/CocoaDesignPatterns.html#//apple_ref/doc/uid/TP40002974-CH6-SW1
especially about the MVC pattern. The NSDocument object is a *controller*. Your archived data is a *model*.
You're also going to need to understand the information here:
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Documents/Concepts/OverviewDocArchitecture.html#//apple_ref/doc/uid/20000023-BAJBBBAH
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden