Documentation Archive Developer
Search
PATH  Documentation > WebObjects 4.5 > WebObjects Developer's Guide


Table of Contents Previous Section

Archiving Custom Objects in Other Applications

Custom classes that can't take advantage of an EOEditingContext for archiving must take a different approach. These classes still must conform to the NSCoding protocol and implement its encodeWithCoder: and initWithCoder: methods; however, you must implement them differently. In encodeWithCoder:, you use the coder argument provided to encode the object's instance variables. In initWithCoder:, the object uses the decoder provided to initialize itself.

For example:

- encodeWithCoder:(NSCoder *)coder {
[coder encodeObject:carID];
[coder encodeObject:colorID];
[coder encodeObject:colorPicture];
[coder encodeObject:packagesIDs];
[coder encodeObject:downPayment];
[coder encodeObject:leaseTerm];
}

- initWithCoder:(NSCoder *)coder {
self = [super init];
carID = [[coder decodeObject] retain];
colorID = [[coder decodeObject] retain];
colorPicture = [[coder decodeObject] retain];
packagesIDs = [[coder decodeObject] retain];
downPayment = [[coder decodeObject] retain];
leaseTerm = [[coder decodeObject] retain];
car = nil;
return self;
}
The Java version of the same example looks like this:

public void encodeWithCoder(NSCoder coder) {
coder.encodeObject(leaseTerm);
coder.encodeObject(downPayment);
// The custom Car object that contains all other info about the car.
coder.encodeObject(car);
}

public ShoppingCart(NSCoder coder) {
super();
leaseTerm = coder.decodeObject();
downPayment = coder.decodeObject();
Car aCar = (Car)coder.decodeObject();
setCar(aCar);
}
For more information on archiving, see the class specifications for NSCoding, NSCoder, NSArchiver, and NSUnarchiver in the Foundation Framework Reference.

Table of Contents Next Section