1. Archiving
Nsarray * array = @ [@ "ARCHIVE", @ "", @ ""];
// 1. Create an archive object
Nsmutabledata * Data = [nsmutabledatadata];
Nskeyedarchiver * archiver = [[nskeyedarchiveralloc] initforwritingwithmutabledata: Data];
// 2. Archive
[Archiver encodeobject: primary key: @ "array"];
// 3. the archive is terminated. If the archive is terminated, the archive is invalid.
[Archiver finishencoding];
[Array release];
[Archiver release];
// 4. Obtain the path of the written file. No file is automatically created.
Nsstring * documentpath =
[Nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, yes) firstobject];
Nsstring * newfiel = [documentpathstringbyappendingpathcomponent: @ "bb.txt"];
// 5. Write Data to the file
Bool issucceed = [datawritetofile: [selfgetfiel] atomically: Yes];
Ii. Archive operations
// 1. initialize nsmutabledata Based on the file path
Nsmutabledata * Data = [nsmutabledatadatawithcontentsoffile:/users/lanouhn/library/Application Support/iPhone simulator/7.1/applications/300befd5-1f93-47a3-a1b3-936195faa53f];
// 2. Create an archive class
Nskeyedunarchiver * unarchiver = [[nskeyedunarchiveralloc] initforreadingwithdata: Data];
// 3. Archive
Nsarray * array = [unarchiverdecodeobjectforkey: @ "array"];
// 4. End the archive
[Unarchiver finishdecoding];
[Unarchiver release];
3. Note
1. If you want to archive a class object, you must enable the class to implement the nscoding protocol.
2. When the object is a string, array, Dictionary, or set, the semantic attribute of the attribute is declared as copy.
3. When assigning values to instance variables after defining attributes, use the setter method.
Example
@ Interface person: nsobject <nscoding]>
@ Property (nonatomic, copy) nsstring * Name
Archive and archive