IPhoneApplication DevelopmentArchiving NSCoderThe tutorial is the content to be introduced in this article. When an object-oriented program is running, it generally creates a complex object relationship diagram, and it is often necessary to express such a complex object relationship diagram as word throttling. this process is calledArchiving10.1,
This byte stream can be transmitted over the network or written to a file. for example, if we create and save a nib file, Interface Builder writes the object to the nib file, which is such an arching process. For Java, this process is called serialization ).
The process of restoring the object relationship diagram from the byte stream is called unarchive. for example, when the program is started, the unarchive object in the nib file contains the member variables and methods. however, only the member variables and class names will be archive. in other words, data will be archive, but the code will not. therefore, if the program A archive object, and the program B unarchive object. therefore, both program A and program B must ensure that the code connected to the class is included. for example, in the nib file, you use the NSWindow and NSButton objects of the Appkit framework. if our program is not connected to the Appkit framework, we cannot generate the NSWindow and NSButton objects because archive only contains data but does not have code.
There was an advertisement for shampoo: "I told two of my friends, and they each told their two friends, that's one-to-ten and ten-to-one .. "The implication is that you told your friends that all of them started to use this shampoo. the object archiving works in a similar way. you archiving a root object. it archiving its own associated objects, and those associated objects will also archiving their own associated objects, and so on, and all related objects will be archiving.
Archiving is completed in two steps. 1. We need to inform our object of How to archive. 2. We need to stimulate the archiving action
Objective-CThe language has a mechanism called protocol, just like interfaces in java. A protocol declares a series of methods. however, if your class implements a protocol, it is scheduled. Your class needs to implement all the methods declared in protocol.
NSCoder and NSCoding
NSCoding is a protocol. If your class implements NSCoding, You need to implement these methods.
- - (id)initWithCoder:(NSCoder *)coder;
- - (void)encodeWithCoder:(NSCoder *)coder;
NSCoder is the abstract class of archivie byte stream. we can write data to a coder or read the data we write from the coder. our object method initWithCoder: reads data from a coder and assigns the data to the member variable. method encodeWithCoder: Write the value of the member variable to the coder. in this chapter, we will implement these two methods in the Person class.
NSCoder is an abstract class, which is not directly used to create objects. instead, we use subclasses inherited from it. that is, we use the NSKeyedUnarchiver class to read data from the byte stream, and the NSKeyedArchiver class to write the object to the byte stream.
Encoding
NSCoder contains many methods, but most people will find that only a few of them will be used. Below are some common methods used to archivie data.
- - (void)encodeObject:(id)anObject forKey:(NSString *)aKey
This method writes the anObject object to the coder, and associate it with the aKey [the next time you use the aKey to read the anObject from the coder] This will be called by the anObject method encodeWithCodr (remember the above shampoo advertisement. this is how it is passed on)
Use the following method for the basic type of C (such as int float). NSCoder
- - (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
Add the encoing Method to the Person class.
- - (void)encodeWithCoder:(NSCoder *)coder
- {
- [super encodeWithCoder:coder];
- [coder encodeObject:personName forKey:@"personName"];
- [coder encodeFloat:expectedRaise forKey:@"expectedRaise"];
- }
The encodeWithCoder of the parent class is called here, so that the parent class has the opportunity to write its own variables into the coder. therefore, the class in the class inheritance tree only writes its member variables to the coder and does not contain the member variables of the parent class.
Decoding
From the decoding data in the coder, we use these methods
- - (id)decodeObjectForKey:(NSString *)aKey
- - (BOOL)decodeBoolForKey:(NSString *)key
- - (double)decodeDoubleForKey:(NSString *)key
- - (float)decodeFloatForKey:(NSString *)key
- - (int)decodeIntForKey:(NSString *)key
If for some reason, the byte stream does not have data associated with the aKey, we will get 0. for example, if the object does not associate the key foo with a float data and write it to the coder, the coder returns 0.0 if the foo key is used to read the float data. if key foo is associated with an object data [written using the encodeWithCoder method], the coder returns nil
Add decoding to Person class
- - (id)initWithCoder:(NSCoder *)coder
- {
- [super init];
- personName = [[coder decodeObjectForKey:@"personName"] retain];
- expectedRaise = [coder decodeFloatForKey:@"expectedRaise"];
- return self;
- }
We didn't call the initWithCoder of the parent class, because NSObject didn't implement it. If the parent class of the Person class implements the NSCoding protocol, this method should be written like this
- - (id)initWithCoder:(NSCoder *)coder
- {
- [super initWithCoder:coder];
- personName = [[coder decodeObjectForKey:@"personName"] retain];
- expectedRaise = [coder decodeFloatForKey:@"expectedRaise"];
- return self;
- }
You can say, "in Chapter 3rd, the designated initializer completes all the init work and calls the designated initializer of the parent class. That is to say, other initializer methods of the class call the designated initializer, the Person class has the designated initializer-init. can this newly added initializer method not call the init method? "Yes, you are right. initWithCoer: It is a special case of this rule.
Okay. We have implemented the NSCoding protocol. Now let the Person class implement NSCoding protocol. Let's edit the Person. h file.
- @interface Person : NSObject <NSCoding> {
Compile our project now. You can also run the program to see. Although the Person class can encode itself. But we have no place to let it do so. So the program looks unchanged.