NSCoding oddity
NSCoding oddity
- Subject: NSCoding oddity
- From: Bruce Sturgen <email@hidden>
- Date: Tue, 6 Nov 2001 13:01:12 -0600
I have an object model like:
Composite
Leaf
Branch
Where Branch contains 0..* Composite instances.
All three implement NSCoding. All three implement
initWithCoder:(NSCoder*)coder and encodeWithCoder:(NSCoder*)coder. The
implementations look like:
// Composite:
-(void)encodeWithCoder:(NSCoder*)coder {
[coder encodeObject:[self name]];
}
-(id)initWithCoder:(NSCoder*)coder {
[super init];
[self setName:[coder decodeObject]];
return self;
}
//Leaf:
-(void)encodeWithCoder:(NSCoder*)coder {
[super encodeWithCoder:coder];
[coder encodeObject:[self data]];
}
-(id)initWithCoder:(NSCoder*)coder {
[super initWithCoder:coder];
[self set
Data:[coder decodeObject]];
return self;
}
// Branch:
-(void)encodeWithCoder:(NSCoder*)coder {
[super encodeWithCoder:coder];
[children encodeWithCoder:coder];
}
-(id)initWithCoder:(NSCoder*)coder {
[super initWithCoder:coder];
children = [[coder decodeObject] retain];
[[self children]
makeObjectsPerformSelector:@selector(setParent:)
withObject:self];
return self;
}
I have a controller object that points to a Composite as the root. The
root is referenced as a Composite*, but for new documents, it is
actually a Branch*.
Upon performing an Archive, everything appears fine. Upon loading I get
the following error:
2001-11-06 12:45:39.274 myApp[874] An uncaught exception was raised
2001-11-06 12:45:39.276 myApp[874] *** file inconsistency: read 'i',
expecting '@'
2001-11-06 12:45:39.276 myApp[874] *** Uncaught exception:
<NSArchiverArchiveInconsistency> *** file inconsistency: read 'i',
expecting '@'
Now for the SQOD (Stupid Question of the Day): what the heck am I doing
wrong here? I'm invoking the super class NSCoding first in all cases. As
there something inherently stupid I'm doing here? I've stared at this
code for a while and cannot see what the problem is. I've checked for
the really dumb stuff like not declaring the <NSCoding> protocol and
ensuring that Branch and Leaf both subclass Composite and making sure
that the superclass is called in the correct spot.
TIA for any help.
_________________________________
Bruce E. Sturgen
Think Different!
ICQ: 120302101 AIM: bsturg3n
Surrealism: You have two giraffes and the government forces you to take
harmonica lessons.