Re: Archiving, coding etc.
Re: Archiving, coding etc.
- Subject: Re: Archiving, coding etc.
- From: Raphael Sebbe <email@hidden>
- Date: Tue, 9 Oct 2001 13:13:11 +0200
On Tuesday, October 9, 2001, at 10:34 AM, Smith, Bradley wrote:
>
Hi everyone,
>
>
I'm having trouble loading my documents (and possibly saving them. Hard
>
to tell).
>
>
My document class implements loadDataRepresentation with code like
>
this: -
>
>
m_theModel = [NSUnarchiver unarchiveObjectWithData: data];
>
>
m_theModel is an instance of CModel, which is a class which derives from
>
CItem and contains an NSMutableArray of CClass(es), which also derive
>
from CItem. CItem has one data member - NSString *m_strName;
>
>
CItem conforms to the NSCoding protocol and implements the following : -
>
>
- (id)initWithCoder:(NSCoder *)coder
>
{
>
[self setName: [coder decodeObject]];
>
return self;
>
}
>
>
- (void)encodeWithCoder:(NSCoder *)coder
>
{
>
[coder encodeObject: [self name]];
>
return;
>
}
>
>
CClass does not override anything.
>
>
CModel has this: -
>
>
- (id)initWithCoder:(NSCoder *)coder
>
{
>
// Let CItem decode the name
>
self = [super initWithCoder: coder];
>
// Decode our array of classes
>
[m_theClasses initWithCoder: coder];
The problem is here, it should be:
m_theClasses = [[coder decodeObject] retain];
You only call init... on your superclass when unarchiving.
I believe you could probably have found it by setting a breakpoint on
-[NSException raise]. (just a little trick !)
>
return self;
>
}
>
>
- (void)encodeWithCoder:(NSCoder *)coder
>
{
>
// Encode the name
>
[super encodeWithCoder: coder];
>
// Encode our classes
>
[coder encodeObject: [self classes]];
>
return;
>
}
>
>
My document *seems* to save okay i.e. when I open it in BBEdit I see in
>
amongst the binary bits the names of my classes and the contents of the
>
m_strName strings and they seem to be in the order I would expect to
>
have
>
encoded them.
You can also use HexEditor.app ;-). You can find it on Stepwise's
Softrak.
Raphael