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