oc-archiving and unpacking of multiple custom objects

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.