Online Book Reader

Home Category

Cocoa Programming for Mac OS X - Aaron Hillegass [54]

By Root 884 0
to recreate the graph of objects from the stream of bytes, you will unarchive it. When your application starts up, it unarchives the objects from the NIB file that was created by the compiler.

Although objects have both instance variables and methods, only the instance variables and the name of the class go into the archive. In other words, only data, not code, goes into the archive. As a result, if one application archives an object and another application unarchives the same object, both applications must have the code for the class as linked in. In the NIB file, for example, you have used classes like NSWindow and NSButton from the AppKit framework. If you do not link your application against the AppKit framework, it will be unable to create the instances of NSWindow and NSButton that it finds in the NIB file.

There was once a shampoo ad that said, “I told two friends, and they told two friends, and they told two friends, and so on, and so on, and so on.” The implication was that as long as you told your friends about the shampoo, everyone who matters would eventually wind up using the shampoo. Object archiving works in much the same way. You archive a root object, it archives the objects to which it is attached, they archive the objects to which they are attached, and so on, and so on, and so on. Eventually, every object that matters will be in the archive.

Archiving involves two steps. First, you need to teach your objects how to archive themselves. Second, you need to cause the archiving to occur.

The Objective-C language has a construct called a protocol, which is identical to the Java construct called an interface. That is, a protocol is a list of method declarations. When you create a class that implements a protocol, it promises to implement all the methods declared in the protocol.

NSCoder and NSCoding


One protocol is called NSCoding. If your class implements NSCoding, it promises to implement the following methods:

- (id)initWithCoder:(NSCoder *)coder;

- (void)encodeWithCoder:(NSCoder *)coder;

An NSCoder is an abstraction of a stream of bytes. You can write your data to a coder or read your data from a coder. The initWithCoder: method in your object will read data from the coder and save that data to its instance variables. The encodeWithCoder: method in your object will read its instance variables and write those values to the coder. In this chapter, you will implement both methods in your Person class.

NSCoder is an abstract class. You won’t ever create instances of an abstract class. Instead, an abstract class has some capabilities that are intended to be inherited by subclasses. You will create instances of the concrete subclasses. Namely, you will use NSKeyedUnarchiver to read objects from a stream of data, and you will use NSKeyedArchiver to write objects to the stream of data.

Encoding


NSCoder has many methods, but most programmers find themselves using just a few of them repeatedly. Here are the methods most commonly used when you are encoding data onto the coder:

- (void)encodeObject:(id)anObject forKey:(NSString *)aKey

This method writes anObject to the coder and associates it with the key aKey. This will cause anObject’s encodeWithCoder: method to be called (and they told two friends, and they told two friends...).

For each of the common C primitive types (such as int and float), NSCoder has an encode method:

- (void)encodeBool:(BOOL)boolv forKey:(NSString *)key

- (void)encodeDouble:(double)realv forKey:(NSString *)key

- (void)encodeFloat:(float)realv forKey:(NSString *)key

- (void)encodeInt:(int)intv forKey:(NSString *)key

To add encoding to your Person class, add the following method to Person.m:

- (void)encodeWithCoder:(NSCoder *)coder

{

[coder encodeObject:personName forKey:@"personName"];

[coder encodeFloat:expectedRaise forKey:@"expectedRaise"];

}

If you looked at the documentation for NSString, you would see that it implements the NSCoding protocol. Thus, the personName knows how to encode itself.

All the commonly

Return Main Page Previous Page Next Page

®Online Book Reader