Saving Data example 2 Saving data in an archive Archiving is used when you want to save all the information in an object. It is appropriate for an object that holds data. It will only work with objects where every instance variable is a: 1) scalar object like int, float, etc. 2) an object that conform to the NSCoding protocol (most predefined objects meet this criteria). I believe an object that mixes an NSData instance variable with other object classes cannot be saved this way. Instead save the data in a plist. /*********************************************************/ //in .h file of class which will save data by archiving: //add a key constant (a string) for the object to be archived #define kMyObjectX @"myObjectX" //add a key constant (a string) for each instance variable in ObjectX #define kObject1 @"object1" #define kObject2 @"object2" #define kObject3 @"object3" //etc. //add a constant for the data file name #define kFileName @"archive" //add the protocol and the protocol: @interface MyClass: ParentClass { /*********************************************************/ //in the .m file of the class which will save data by archiving: #pragma mark NSCoding methods //the encoding method -(void) encodeWithCoder:(NSCoder *)encoder { //notice the different method names depending on the object type //also, some methods use a key and some don't [encoder encodeObject:object1 forKey:kObject1]; [encoder encodeInt:object2 forKey:kObject2]; [encoder encodeObject:object3 forKey:kObject3]; } //the decoding method -(id)initWithCoder:(NSCoder *)decoder { //use next line if superclass conforms to NSCoding if(self = [super initWithCoder:decoder]) //use next line if superclass is NSObject or doesn't conform to NSCoding //if(self = [super init]) { self.object1 = [decoder decodeObjectForKey:kObject1]; self.object2 = [decoder decodeIntForKey:kObject2]; self.object3 = [decoder decodeObjectForKey:kObject3]; } return self; } /*********************************************************/ //to archive an object in the class //create instance of NSMutableData to hold the archived object NSMutableData *data = [[NSMutableData alloc] init]; //create an instance of NSKeyedArchiver NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data]; //archive the object //multiple objects can be archived by using multiple statements [archiver endcodeObject:myObjectX forKey:kMyObjectX]; //finish archiving [archiver finishEncoding]; //write to file BOOL success = [data writeToFile:[self dataFilePath] atomically:YES]; [archiver release]; [data release]; /*********************************************************/ //to unarchive an object in the class //create an instance of NSData from the archive file NSData *data = [[NSData alloc] initWithContentsOfFile:[self dataFilePath]]; //create an instance of NSKeyedArchiver NSKeyedArchiver *unarchiver = [[NSKeyedArchiver alloc] initForReadingWithData:data]; //decode and return an autoreleased object specified by the key self.object = [unarchiver decodeObjectForKey:kMyObjectX]; //finish archiving [unarchiver finishDecoding]; [unarchiver release]; [data release];