IOS---Object archive

Source: Internet
Author: User
Tags list of attributes

Original: http://blog.sina.com.cn/s/blog_7124765801015imx.html

The data persistence methods provided by iOS are: SQLite, CoreData, attribute list, Nsuserdefault, Object archive.

Here is a brief introduction to the object archive:

An object archive is an object archive that is saved as a file to disk (also known as serialization, persistence), and is used when reading the file's save path to read the contents of the file (also known as a pickup, deserialization),

(the file archived by the object is confidential and the contents of the file cannot be viewed on disk, and the list of attributes is clear and viewable).

There are two ways to archive objects: 1: Archiving objects in the foundation 2: Customizing object archiving

1. Simple Object Archiving

Use of two classes: Nskeyedarichiver, Nskeyedunarchiver

NSString *homedirectory = Nshomedirectory (); Get root directory

NSString HomePath = [homedirectory stringbyappendingpathcomponent:@ "custom filenames, such as Test.archiver"];

Nsarray *array = @[@ "abc", @ "123", @12];

Bool flag = [Nskeyedarichiver archiverootobject:array Tofile:homepath];

if (flag) {

NSLog (@ "Archive success!) ");

}

Read the contents of the archive file:

Nsarray *array = [Nskeyedunarchiver Unarchiveobjectwithfile:homepath];

NSLog (@ "%@", array);

This makes it easy to archive and document the Nsarray object.

But this kind of archiving method has a disadvantage, that is, a file can only save an object, if there are multiple objects to be saved, then there is not more than n files, this is not very suitable, so there is the following method of archiving.

2. Custom content Archiving

Archive:

Using NSData instances as archived storage data

Add archived content---Use key-value pairs

Complete archiving

Solution Archive:

Read files from disk, generate NSData instances

Based on NSData instances and initial solution archive instances

Archive, access value based on key

NSString *homedirectory = Nshomedirectory (); Get root directory

NSString HomePath = [homedirectory stringbyappendingpathcomponent:@ "custom filenames, such as Test.archiver"];

Nsmutabledata *data = [[Nsmutabledata alloc] init];

Nskeyedarchiver *archiver = [[Nskeyedarchiver alloc] initforwritingwithmutabledata:data];

[Archiver encodefloat:50 forkey:@ "age"];

[Archiver encodeobject:@ "Jack" forkey:@ "name"];

[Archiver finishencoding]; End Add object to data

[Archiver release];

[Data Writetofile:homepath atomically:yes];//writes data to a file to be saved on disk

Ndata *content= [NSData Datawithconenteoffile:homepath];

Nskeyedunarchiver *unarchiver = [[Nskeyedunarchiver alloc] initforreadingwithdata:content];

float age = [unarchiver decodefloatforkey:@ ' age '];

NSString *name = [unarchiver decodeobjectforkey:@ "name"];

Well, that's it, the use of a custom archive and archive.

IOS---Object 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.