Object serialization: Difference between revisions

Content added Content deleted
No edit summary
Line 1,124: Line 1,124:
int numberOfLegs;
int numberOfLegs;
}
}
- (id) initWithName: (NSString*)name andLegs: (NSInteger)legs;
- (instancetype) initWithName: (NSString*)name andLegs: (NSInteger)legs;
- (void) dump;
- (void) dump;
// the following allows "(de)archiving" of the object
- (void) encodeWithCoder: (NSCoder*)coder;
- (id) initWithCoder: (NSCoder*)coder;
@end
@end


@implementation Animal
@implementation Animal
- (id) initWithName: (NSString*)name andLegs: (NSInteger)legs
- (instancetype) initWithName: (NSString*)name andLegs: (NSInteger)legs
{
{
if ((self = [super init])) {
if ((self = [super init])) {
Line 1,139: Line 1,136:
}
}
return self;
return self;
}
- (void) dealloc
{
[animalName release];
[super dealloc];
}
}
- (void) dump
- (void) dump
Line 1,170: Line 1,162:
NSMutableArray *eatenList;
NSMutableArray *eatenList;
}
}
- (id) initWithName: (NSString*)name hasFur: (BOOL)fur;
- (instancetype) initWithName: (NSString*)name hasFur: (BOOL)fur;
- (void) addEatenThing: (NSString*)thing;
- (void) addEatenThing: (NSString*)thing;
- (void) dump;
// for archiving / dearchiving:
- (void) encodeWithCoder: (NSCoder*)coder;
- (id) initWithCoder: (NSCoder*)coder;
@end
@end


@implementation Mammal
@implementation Mammal
- (id) init
- (instancetype) init
{
{
if ((self = [super init])) {
if ((self = [super init])) {
Line 1,187: Line 1,175:
return self;
return self;
}
}
- (id) initWithName: (NSString*)name hasFur: (BOOL)fur
- (instancetype) initWithName: (NSString*)name hasFur: (BOOL)fur
{
{
if ((self = [super initWithName: name andLegs: 4])) {
if ((self = [super initWithName: name andLegs: 4])) {
Line 1,198: Line 1,186:
{
{
[eatenList addObject: thing];
[eatenList addObject: thing];
}
- (void) dealloc
{
[eatenList release];
[super dealloc];
}
}
- (void) dump
- (void) dump
Line 1,208: Line 1,191:
[super dump];
[super dump];
NSLog(@"has fur? %@", (hasFur) ? @"yes" : @"no" );
NSLog(@"has fur? %@", (hasFur) ? @"yes" : @"no" );
// fast enum not implemented yet in gcc 4.3, at least
// without a patch that it seems to exist...
NSEnumerator *en = [eatenList objectEnumerator];
id element;
NSLog(@"it has eaten %d things:", [eatenList count]);
NSLog(@"it has eaten %d things:", [eatenList count]);
for ( id element in eatenList )
while( (element = [en nextObject]) != nil )
NSLog(@"it has eaten a %@", element);
NSLog(@"it has eaten a %@", element);
NSLog(@"end of eaten things list");
NSLog(@"end of eaten things list");
Line 1,228: Line 1,207:
if ((self = [super initWithCoder: coder])) {
if ((self = [super initWithCoder: coder])) {
hasFur = [coder decodeBoolForKey: @"Mammal.hasFur"];
hasFur = [coder decodeBoolForKey: @"Mammal.hasFur"];
eatenList = [[coder decodeObjectForKey: @"Mammal.eaten"] retain];
eatenList = [coder decodeObjectForKey: @"Mammal.eaten"];
}
}
return self;
return self;
Line 1,237: Line 1,216:
int main()
int main()
{
{
@autoreleasepool {
Mammal *aMammal;
Animal *anAnimal;
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// let us create a fantasy animal
// let us create a fantasy animal
anAnimal = [[Animal alloc]
Animal *anAnimal = [[Animal alloc]
initWithName: @"Eptohippos"
initWithName: @"Eptohippos"
andLegs: 7
andLegs: 7
];
];
// for some reason an Eptohippos is not an horse with 7 legs,
// for some reason an Eptohippos is not an horse with 7 legs,
// and it is not a mammal, of course...
// and it is not a mammal, of course...


// let us create a fantasy mammal (which is an animal too)
// let us create a fantasy mammal (which is an animal too)
aMammal = [[Mammal alloc]
Mammal *aMammal = [[Mammal alloc]
initWithName: @"Mammaluc"
initWithName: @"Mammaluc"
hasFur: YES
hasFur: YES
];
];
// let us add some eaten stuff...
// let us add some eaten stuff...
[aMammal addEatenThing: @"lamb"];
[aMammal addEatenThing: @"lamb"];
[aMammal addEatenThing: @"table"];
[aMammal addEatenThing: @"table"];
[aMammal addEatenThing: @"web page"];
[aMammal addEatenThing: @"web page"];


// dump anAnimal
// dump anAnimal
NSLog(@"----- original Animal -----");
NSLog(@"----- original Animal -----");
[anAnimal dump];
[anAnimal dump];


// dump aMammal...
// dump aMammal...
NSLog(@"----- original Mammal -----");
NSLog(@"----- original Mammal -----");
[aMammal dump];
[aMammal dump];


// now let us store the objects...
// now let us store the objects...
NSMutableData *data = [[NSMutableData alloc] init];
NSMutableData *data = [[NSMutableData alloc] init];
NSKeyedArchiver *arch = [[NSKeyedArchiver alloc]
NSKeyedArchiver *arch = [[NSKeyedArchiver alloc]
initForWritingWithMutableData: data];
initForWritingWithMutableData: data];
[arch encodeObject: anAnimal forKey: @"Eptohippos"];
[arch encodeObject: anAnimal forKey: @"Eptohippos"];
[arch encodeObject: aMammal forKey: @"Mammaluc"];
[arch encodeObject: aMammal forKey: @"Mammaluc"];
[arch finishEncoding];
[anAnimal release];
[data writeToFile: @"objects.dat" atomically: YES];
[aMammal release];
[arch finishEncoding];
[arch release];
[data writeToFile: @"objects.dat" atomically: YES];
[data release];


// now we want to retrieve the saved objects...
// now we want to retrieve the saved objects...
NSData *ldata = [[NSData alloc]
NSData *ldata = [[NSData alloc]
initWithContentsOfFile: @"objects.dat"];
initWithContentsOfFile: @"objects.dat"];
NSKeyedUnarchived *darch = [[NSKeyedUnarchiver alloc]
NSKeyedUnarchived *darch = [[NSKeyedUnarchiver alloc]
initForReadingWithData: ldata];
initForReadingWithData: ldata];
Animal *archivedAnimal = [darch decodeObjectForKey: @"Eptohippos"];
Animal *archivedAnimal = [darch decodeObjectForKey: @"Eptohippos"];
Mammal *archivedMammal = [darch decodeObjectForKey: @"Mammaluc"];
Mammal *archivedMammal = [darch decodeObjectForKey: @"Mammaluc"];
[darch finishDecoding];
[darch finishDecoding];
[ldata release];
[darch release];


// now let's dump/print the objects...
// now let's dump/print the objects...
NSLog(@"\n");
NSLog(@"\n");
NSLog(@"----- the archived Animal -----");
NSLog(@"----- the archived Animal -----");
[archivedAnimal dump];
[archivedAnimal dump];
NSLog(@"----- the archived Mammal -----");
NSLog(@"----- the archived Mammal -----");
[archivedMammal dump];
[archivedMammal dump];


}
[pool release];
return EXIT_SUCCESS;
return EXIT_SUCCESS;
}</lang>
}</lang>