[OC learning-27] object archiving and archive-an example of standard data and Custom Data

Source: Internet
Author: User

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

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.