1 , overview
"Archive" means to persist the storage of data. The plist file is an XML-formatted file that expands to the name plist. If the object is NSString, Nsdictionary, Nsarray, NSData, NSNumber, and so on, you can use the writetofile:atomically: method to write the object directly to the property list file (the plist file). the special attention is plist The file cannot store the object.
IOS common file storage methods are:
XML attribute list (plist) archive
Preference (preference setting)
Nskeyedarchiver Archive (nscoding)
SQLite3
Core Data
2 , archiving nsdictionary
Archive a Nsdictionary object to a list of plist properties:
First, encapsulate the data into a dictionary:
Nsmutabledictionary *dict = [Nsmutabledictionary dictionary];
[Dict setobject:@ "hen" forkey:@ "name"];
[Dict setobject:@ "15013141314" forkey:@ "phone"];
[Dict setobject:@ "forkey:@" "Age"];
Second, get the full path to the documents/stu.plist in the sandbox and persist the dictionary to the documents/stu.plist file according to the path:
[Dict Writetofile:path Atomically:yes];//yes represents secure storage
After successfully writing to the documents directory, such as:
Open with a text editor with the contents of the file such as:
Open the properties file with Xcode such as:
The above is written from the nsdictionary to the plist file, or it can be written from Nsarray to the plist file, for example:
1. Get the Sandbox root path
NSString *home = Nshomedirectory ();
2. Stitching into document Path
NSString *docpath =
[Home stringbyappendingpathcomponent:@ "Documents"];
3. New data
Nsarray *data = @[@ "Jack", @10, @ "FFDSF"];
4. The path of stitching into a specific file
NSString *filepath =
[DocPath stringbyappendingpathcomponent:@ "Data.plist"];
5. Writing data
[Data Writetofile:filepath Atomically:yes];
3 , Recovery nsdictionary
Reads the contents of the documents/stu.plist, instantiates the Nsdictionary
Nsdictionary *dict = [nsdictionary dictionarywithcontentsoffile:p Ath];
NSLog (@ "name:%@", [dict objectforkey:@ "name"]);
NSLog (@ "phone:%@", [dict objectforkey:@ "Phone"]);
NSLog (@ "age:%@", [Dict objectforkey:@ "Age"]);
Print the following information:
Above is read to Nsdictionary, corresponding to, we can also read to Nsarray, for example:
1. Get the Sandbox root path
NSString *home = Nshomedirectory ();
2.document Path
NSString *docpath =
[Home stringbyappendingpathcomponent:@ "Documents"];
3. File path
NSString *filepath =
[DocPath stringbyappendingpathcomponent:@ "Data.plist"];
4. Reading data
Nsarray *data = [Nsarray Arraywithcontentsoffile:filepath];
4 , nsdictionary the storage and reading process
XML attribute list (plist) archive for data storage of iOS development