Archive anti-archiving

Source: Internet
Author: User


Archive anti-Archive (troublesome method)
/*
Archive
Create a person instance object
Person *person1=[[person Alloc]init];
[Email protected] "BBB";
[Email protected] "39";

Person *person2=[[person Alloc]init];
[Email protected] "AAA";
[Email protected] "18";


Archive used by NSData
Nsmutabledata *person1data=[nsmutabledata data];

Create an archive tool
Nskeyedarchiver *achiver=[[nskeyedarchiver Alloc]initforwritingwithmutabledata:person1data];

Archive (macro definition required)
[Achiver Encodeobject:person1 Forkey:kperson1];
[Achiver Encodeobject:person2 Forkey:kperson2];


Complete the conversion
[Achiver finishencoding];

Get Documents Path
NSString *documentspath=nssearchpathfordirectoriesindomains (NSDocumentDirectory, NSUserDomainMask, YES) [0];
Stitching file path
NSString *personpath=[documentspath stringbyappendingstring:@ "/Liu Jie Xxoo"];

Write
[Person1data Writetofile:personpath Atomically:yes];
NSLog (@ "documents:%@", Documentspath);






Anti-archiving
Obtaining data from a file path
NSData *undata=[nsdata Datawithcontentsoffile:personpath];

Create an anti-archiving tool
Nskeyedunarchiver *unarchiver=[[nskeyedunarchiver Alloc]initforreadingwithdata:undata];

For anti-archiving
Person *p1=[unarchiver Decodeobjectforkey:kperson1];
Person *p2=[unarchiver Decodeobjectforkey:kperson2];

End Anti-archive
[Unarchiver finishdecoding];
NSLog (@ "name:%@", p1.name);
NSLog (@ "age:%@", p1.age);
NSLog (@ "name:%@", p2.name);
NSLog (@ "age:%@", p2.age);
*/



Archive anti-archiving (simple method)

Get Documents Path
NSString *docpath=nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) [0];

Stitching file path
NSString *filepath=[docpath stringbyappendingstring:@ "/personarray.plist"];

/*
Only one object can be archived
Person *pn1=[[person Alloc]init];
[Email protected] "Wangxiao";
[Email protected] "20";

Archive
[Nskeyedarchiver ARCHIVEROOTOBJECT:PN1 Tofile:filepath];

Anti-archiving
Person *p1=[nskeyedunarchiver Unarchiveobjectwithfile:filepath];
NSLog (@ "name:%@", p1.name);
NSLog (@ "age:%@", pn1.age);
*/


Person *pn1=[[person Alloc]init];
[Email protected] "Tom";
[Email protected] "12";

Person *pn2=[[person Alloc]init];
[Email protected] "Kim";
[Email protected] "25";


Nsarray *[email protected][pn1,pn2];//put two data into the array



Anti-archiving
Nsarray *a=[nskeyedunarchiver Unarchiveobjectwithfile:filepath];

NSLog (@ "name:%@", [a[0] name]);
NSLog (@ "age:%@", [a[0] age]);

NSLog (@ "name:%@", [a[1] name]);
NSLog (@ "age:%@", [a[1] age]);








Archive anti-archive

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.