XML attribute list (plist) archive for data storage of iOS development

Source: Internet
Author: User
Tags xml attribute

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

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.