Documentation Archive Developer
Search
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.

You can see implementations of encodeWithCoder: and initWithCoder: in the DodgeDemo application, in the class ShoppingCart.

	- 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 DodgeDemo's ShoppingCart implements these methods instead:

	public void encodeWithCoder(Coder coder) {
		coder.encodeObject(leaseTerm);
		coder.encodeObject(downPayment);
		// DodgeDemoJava defines a custom Car object that contains all
		// info about the car.
		coder.encodeObject(car);
	}

	public ShoppingCart(Coder 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