Newbie Q : Not-so-simple encoding&decoding example
Newbie Q : Not-so-simple encoding&decoding example
- Subject: Newbie Q : Not-so-simple encoding&decoding example
- From: email@hidden
- Date: Fri, 26 May 2006 11:16:49 +0200 (CEST)
- Importance: Normal
Hello all,
I've read many tutorials on the way to encode and decode objects in Cocoa,
but they all share one limitation : they only treat the simplest
"one class-one data type" case. When I tried to adapt it to the slightly more
complicated example below I failed. Can anyone please tell me what should
be corrected
in the encoding & decoding methods below ?
Ewan
Three classes : MyDocument, Class1, Class2
Here are the interfaces :
@interface Class1 : NSObject <NSCoding> {
NSMutableArray* array1;
NSMutableArray* array2;
}
@end
@interface Class2 : NSObject <NSCoding> {
NSMutableArray* array3;
NSMutableArray* array4;
Class1* class1;
}
@end
@interface MyDocument : NSDocument {
Class2* class2;
}
@end
and here are the coding methods :
In Class1.m
-- (id)initWithCoder:(NSCoder *)coder
{
if (self = [super init]) {
[array1 release];
[array2 release];
array1=[[coder decodeObject] retain];
array2=[[coder decodeObject] retain];
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)coder
{
[coder encodeObject:array1];
[coder encodeObject:array2];
}
In Class2.m
-- (id)initWithCoder:(NSCoder *)coder
{
if (self = [super init]) {
[array3 release];
[array4 release];
[class1 release];
array3=[[coder decodeObject] retain];
array4=[[coder decodeObject] retain];
class1=[[coder decodeObject] retain];
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)coder
{
[coder encodeObject:array3];
[coder encodeObject:array4];
[coder encodeObject:class1];
}
In MyDocument.m :
- (NSData *)dataRepresentationOfType:(NSString *)aType
{
return [NSArchiver archivedDataWithRootObject:class2];
}
- (BOOL)loadDataRepresentation:(NSData *)data ofType:(NSString *)aType
{
[class2 release];
class2 = [[NSUnarchiver unarchiveObjectWithData:data] retain];
return YES;
}
_______________________________________________
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