Archiving of System objects I will not introduce, this is not complicated, see for yourself will be.
I'm here to focus on archiving of custom objects.
Sample.h file
9 #import<Foundation/Foundation.h>Ten One @interfaceSample:nsobject<nscoding> { A -nsstring*name; - intMagicNumber; the floatshoesize; -Nsmutablearray *subthingies; - } - +@property (copy) nsstring*name; -@propertyintMagicNumber; +@propertyfloatshoesize; A@property (retain) Nsmutablearray *subthingies; at - --(ID) Initwithname: (NSString *) n magicnumber: (int) m Shoesize: (float) SS; - - @end
SAMPLE.M file
9 #import "Sample.h"Ten One @implementationSample A - @synthesizename; - @synthesizeMagicNumber; the @synthesizeshoesize; - @synthesizesubthingies; - --(ID) Initwithname: (NSString *) n magicnumber: (int) m Shoesize: (float) SS + { - if(self=[Super init]) + { ASelf.name =N; atSelf.magicnumber =m; -Self.shoesize =SS; -Self.subthingies =[Nsmutablearray array]; - } - return(self); - } in --(void) Dealloc to { + [name release]; - [subthingies release]; the [Super Dealloc]; * } $ Panax Notoginseng //encode an object (that is, serialize) --(void) Encodewithcoder: (Nscoder *) Acoder the { +[Acoder encodeobject:name Forkey:@"name"]; A[Acoder encodeint:magicnumber Forkey:@"MagicNumber"]; the[Acoder encodefloat:shoesize Forkey:@"shoesize"]; +[Acoder encodeobject:subthingies Forkey:@"subthingies"]; - } $ $ //decoding an object (deserialization) --(ID) Initwithcoder: (Nscoder *) Adecoder - { the if(self=[Super init]) - {WuyiSelf.name = [Adecoder decodeobjectforkey:@"name"]; theSelf.magicnumber = [Adecoder decodeintforkey:@"MagicNumber"]; -Self.shoesize = [Adecoder decodefloatforkey:@"shoesize"]; WuSelf.subthingies = [Adecoder decodeobjectforkey:@"subthingies"]; - } About return(self); $ - } - - A-(nsstring*) Description + { theNSString *description = [NSString stringWithFormat:@"%@:%d/%.1f%@", name,magicnumber,shoesize,subthingies]; - return(description); $ } the the @end
Using templates
NSString *path = [NSString stringWithFormat:@"%@/documents/archive.dat", Nshomedirectory ()]; Sample*S1 = [[Sample alloc] Initwithname:@"thing1"MagicNumber: theShoesize:10.5]; [S1.subthingies AddObject:@"1"]; [S1.subthingies AddObject:@"2"]; //Serialization ofNSData *data1 = [Nskeyedarchiver archiveddatawithrootobject:s1];//after serializing the S1, save to NSData[S1 release]; [Data1 Writetofile:path Atomically:yes];//persist into physical files//deserializationNSData *data2 = [NSData Datawithcontentsoffile:path];//Read FileSample *s2 = [Nskeyedunarchiver unarchiveobjectwithdata:data2];//deserializationNSLog (@"%@", S2);
If it is an array of objects of this type, serialization is also very simple, only the array must be serialized
1Sample *S1 = [[Sample alloc] Initwithname:@"thing1"MagicNumber: theShoesize:10.5]; 2[S1.subthingies AddObject:@"1"]; 3[S1.subthingies AddObject:@"2"]; 4 5Sample *S2 = [[Sample alloc] Initwithname:@"thing2"MagicNumber: AShoesize:22.2]; 6[S2.subthingies AddObject:@" A"]; 7[S2.subthingies AddObject:@" A"]; 8 9Nsarray *array =[Nsarray arraywithobjects:s1, S2, nil];Ten [S1 release]; One [S2 release]; A -NSString *path = [NSString stringWithFormat:@"%@/documents/archive.dat", Nshomedirectory ()]; - //Serialization of theNSData *data1 =[Nskeyedarchiver Archiveddatawithrootobject:array]; -[Data1 Writetofile:path Atomically:yes];//persist into physical files - //Serialization of houses -NSData *data2 = [NSData Datawithcontentsoffile:path];//Read File +Nsarray *array2 = [Nskeyedunarchiver unarchiveobjectwithdata:data2];//deserialization -NSLog (@"%@", array2);