Graphics Programs Reference
In-Depth Information
In
BNRItem.m
, implement
encodeWithCoder:
to add the instance variables to the
container.
- (void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:itemName forKey:@"itemName"];
[aCoder encodeObject:serialNumber forKey:@"serialNumber"];
[aCoder encodeObject:dateCreated forKey:@"dateCreated"];
[aCoder encodeObject:imageKey forKey:@"imageKey"];
[aCoder encodeInt:valueInDollars forKey:@"valueInDollars"];
}
Notice that pointers to objects are encoded with
encodeObject:forKey:
, but
valueInDollars
is encoded with
encodeInt:forKey:
. Check the documentation
for
NSCoder
to see all of the types you can encode. Regardless of the type of the en-
coded value, there is always a key, which is a string that identifies which instance variable
is being encoded. By convention, this key is the name of the instance variable being en-
coded.
When an object is encoded, that object is sent
encodeWithCoder:
. When an object is
sent
encodeWithCoder:
, it encodes its instance variables in the same way - by send-
process where objects encode other objects.
Figure 14.1 Encoding an object