Objective-C magic path [19-Archive], objective-c19-Archive

Source: Internet
Author: User
Tags xml attribute

Objective-C magic path [19-Archive], objective-c19-Archive

Master haomeng is devoted to his contribution and respects the author's Labor achievements. Do not repost them.

If the article is helpful to you, you are welcome to donate to the author, support haomeng master, the amount of donation is free, focusing on your mind ^_^

I want to donate: Click to donate

Cocos2d-X source code download: point I send


In Objective-C, archiving is a process,

You can save one or more objects in a certain format to restore them later.

Similar to Java serialization and deserialization.


Applications on Mac OS X use the XML Attribute list (or plists)

Store data such as default parameter selection, application settings, and configuration information.


Use the PropertyList Editor program to create an attribute list.

You can use the NSPropertyListSerialization class to write or read attribute lists in files and port them between different platforms.


Archive methods generally include:

1) Use the XML Attribute list for archiving. (If possible, try to use the XML Attribute list in the Program)

2) use NSKeyedArchiver for archiving.

3) Use NSData to create a custom file.


To archive objects that are not currently listed, you must inform the system of How to archive (or encode) Your objects and how to archive (or decode) them.

According to the <NSCoding> protocol, add encodeWithCoder: method and initWithCoder: Method to the class definition.


Each time the archive program wants to encode an object based on the specified class, it will call the encodeWithCoder: method,

This method tells the archive program how to archive.

Similarly, every time an object is decoded from a specified class, the initWIthCoder: method is called.


Generally, the encoding method should specify how to archive each instance variable in the object to be saved.


It is easy to recover data from an archive file: the work is only opposite to that of an archive file.

First, you need to allocate a data space as before.

Second, read the data in the archive file into the data space.

Then, you need to create an NSKeydUnarchiver object and notify it to decode data from the specified space.

You must call the decoding method to extract and decode the archived object. After that, you can send a finishDecoding message to the NSKeyedUnarchiver object.


Use the archive program to copy objects:

You can use the archive function of Foundation to create deep copy of objects.

Related Article

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.