Object archiving? The object data is stored as files to encrypt the data (that is, the data is not explicitly displayed in the document) and stored permanently.
If you want to use it, you can restore it from the file.
(1) Standard Data
// Main. M file # import <Foundation/Foundation. h> int main (INT argc, const char * argv []) {@ autoreleasepool {// archive an array // create a file path nsstring * homepath = nshomedirectory (); nsstring * filepath = [homepath stringbyappendingpathcomponent: @ "test. archive "]; // The suffix can also be. arc // create an array nsarray * [email protected] [@ "111", @ "222", @ "333"]; bool success = [nskeyedarchiver archiverootobject: arr1 tofile: filepath]; If (SUCCESS) {nslog (@ "success");} // restore data from the archive file nsarray * arr2 = [nskeyedunarchiver unarchiveobjectwithfile: filepath]; nslog (@ "% @", arr2);} return 0 ;}
An object is created as follows:
Archive is to output the results of the array:
( 111, 222, 333)
(2) Custom Data Archiving: create a data object data and then write the data to the file (but an archive object is created first, use the Data Writing Method of the archive object to write the object to the data object ). Archive is to extract the file (actually the data object data) in the form of data, then archive it, and then call the object value using the key value.
// Main. M file # import <Foundation/Foundation. h> int main (INT argc, const char * argv []) {@ autoreleasepool {// create a file path nsstring * homepath = nshomedirectory (); nsstring * filepath = [homepath stringbyappendingpathcomponent: @ "custom. arc "]; nsmutabledata * data1 = [nsmutabledata data]; // create a data object nskeyedarchiver * arc1 = [[nskeyedarchiver alloc] initforwritingwithmutabledata: data1]; // create an archive object with a data object [arc1 encodeobject: @ "Jack" forkey: @ "name"]; // write the data to the archive file, that is, [arc1 encodefloat: 50 forkey: @ "weight"]; [arc1 finishencoding]; // ends Data Writing [data1 writetofile: filepath atomically: Yes]; // write the data object to the archive file. // use the data object first. Then, nsdata * data2 = [nsdata datawithcontentsoffile: filepath]. // use this data object to create an unarchive object nskeyedunarchiver * unarc1 = [[nskeyedunarchiver alloc] initforreadingwithdata: data2]; // use the archive object to extract the float weight1 = [unarc1 decodefloatforkey: @ "weight"]; nsstring * name1 = [unarc1 decodeobjectforkey: @ "name"]; nslog (@ "%. 0f, % @ ", weight1, name1);} return 0 ;}
File archiving:
Output result of archive:
50,jack
[OC learning-27] object archiving and archive-an example of standard data and Custom Data