Re: Keyed archiver
Re: Keyed archiver
- Subject: Re: Keyed archiver
- From: "Gerriet M. Denkmann" <email@hidden>
- Date: Sun, 21 Nov 2004 13:53:09 +0100
On 21.11.2004, at 13:00, Jonathan Jackel wrote:
On Nov 21, 2004, at 4:47 AM, Gerriet M. Denkmann wrote:
I have an object, which I want to archive.
- (void)encodeWithCoder:(NSCoder *)coder
{
[ coder encodeObject: myString forKey: @"myString" ];
}
- (id)initWithCoder:(NSCoder *)decoder
{
myString = [ decoder decodeObjectForKey: @"myString" ];
NSLog("@ myString: \"%@\");
return self ;
}
When myString is @"$noll" everything works just fine.
But if myString is @"$null" (note the changed vowel) the unarchive
string is nil.
Four things:
1. Always call super in your init method. Always.
Yes, you are right. Now my initWithCoder: starts with:
self = [ super init ];
if ( self == nil ) return nil ;
2. Your NSLog statement looks wrong. There's no parameter and no
closing quote mark (the escape sequence at the end doesn't count).
And the first @ should go outside the first quote mark.
Right again; as you sure have already realized, this statement has
never been compiled - it was just typed in Mail. And I have to
apologize for the sloppy typing.
It should have been:
NSLog(@"myString = %@", myString);
3. Use accessors to set ivars. Otherwise your ivars will be
deallocated prematurely. This could be your problem.
Well, as I log the value immediately after decodeObjectForKey: there is
not much chance for premature deallocation.
4. I think it is a good practice to use constant strings, defined
outside the implementation, as keys. That way you will get a compiler
warning if you type them incorrectly. For example:
I agree that this is a good practice; but alas, it has nothing to do
with my problem.
Try it that way and see if you still have problems.
I did and I still have.
I can archive and restore a string with the value "$noll" but I cannot
do the same with "$null".
Kind regards
Gerriet.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden