For the previous section, simply describe the archiving and archiving of a custom object: http://www.cnblogs.com/BeyondAverage0908/p/4597245.html
This section describes the archiving and archiving of multiple custom objects
The following code illustrates: Defines two classes of dog and cat, and uses the @property to expand the corresponding several properties (simple code, not stick to the source).
The following code section is the main archive and archive code: note the following two methods need to be implemented in the corresponding custom class:-(void) Encodewithcoder: (nscoder *) Acoder; Method,-(ID) Initwithcoder: (nscoder *) Adecoder; specific implementation details see previous blog content
#import "Dog.h"#import "Cat.h"intMainintargcConst Char*argv[]) {@autoreleasepool {//define a Dog objectDog *xiaohei =[[Dog alloc] init]; Xiaohei.name=@"Little Black"; Xiaohei.age= A; Cat*xiaomao =[[Cat alloc] init]; Xiaomao.name=@" Kitten"; Xiaomao.nickname=@"Jen"; Xiaomao.weight=2.03; //Archive//define a mutable data object (required) to store the custom object at encoding timeNsmutabledata *mutdate =[Nsmutabledata data]; Nskeyedarchiver*keyedarch =[[Nskeyedarchiver alloc] initforwritingwithmutabledata:mutdate]; //format code for dog and cat[Keyedarch Encodeobject:xiaohei Forkey:@"Xiaohei"]; [Keyedarch Encodeobject:xiaomao Forkey:@"Xiaomao"]; //End Encoding[Keyedarch finishencoding]; BOOL Flag= [Mutdate writetofile:@"/users/qianfeng/desktop/day22_oc12_ Protocol/day22_oc12_ custom Object archiving and solution archiving/dogandcat.txt"Atomically:yes]; if(flag) {NSLog (@"multiple Custom objects archived successfully"); }Else{NSLog (@"multiple custom object archiving failed"); } //Archive: Read an object from an archived fileNSData *data = [NSData datawithcontentsoffile:@"/users/qianfeng/desktop/day22_oc12_ Protocol/day22_oc12_ custom Object archiving and solution archiving/dogandcat.txt"]; Nskeyedunarchiver*keyedunarch =[[Nskeyedunarchiver alloc] initforreadingwithdata:data]; Dog*ndog = [Keyedunarch decodeobjectforkey:@"Xiaohei"]; Cat*ncat = [Keyedunarch decodeobjectforkey:@"Xiaomao"]; [Keyedunarch finishdecoding]; NSLog (@"Ndog =%@", Ndog); NSLog (@"ncat name =%@,nickname =%@,weight =%.2f", Ncat.name,ncat.nickname,ncat.weight); } return 0;}
oc-archiving and unpacking of multiple custom objects