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