Archive and archive

Source: Internet
Author: User
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

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.