iOS Development: plist file operations for data storage

Source: Internet
Author: User

1.plist, full name propertylist, the attribute list file, which is a file used to store serialized objects. This kind of file is often used during iOS development. This property list file has a. plist extension, and is often called a plist file. The file is in XML format. The plist file stores data in the form of Key-value. It can be used to store user settings or to store information that needs to be used frequently and not changed frequently.

The operation on the plist file has been created, deleted, written and read. In these four operations, write and read are the more common operations.

2. Below I have one by one representations of these four operations.

First, how to create a plist file. The creation of a plist file can be created either by creating a new file in the program or by creating a file in the form of code in the program.

The first is to create the plist file by creating a new file and selecting the property list in the resource under iOS project in the popup window. Then click on the Testplistdemo.plist file, a root row appears, click on the root line, and then click on the right mouse button->add row or click on the plus sign after root to add a line. This line contains three attributes, key, type, value. Where key is the field property, type is the field and value is the field. The type also contains 7 types, two of which are arrays and dictionary, which are the form of an array and can contain many key-value under them.

and the other 5 kinds are boolean,data,string,date,number. These 5 types of data are the data to be included by the array and dictionary.

Create the plist file with code as follows:

Establish file Management

Nsfilemanager *FM = [Nsfilemanager Defaultmanager];

Locate the path where the documents file is located

Nsarray *path = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES);

Get the path to the first Documents folder

NSString *filepath = [path objectatindex:0];

Add the Testplist file

NSString *plistpath = [FilePath stringbyappendingpathcomponent:@ "Test.plist"];

Start creating a file

[FM Createfileatpath:plistpath Contents:nil Attributes:nil];

deleting files

[FM Removeitematpath:plistpath Error:nil];

Before writing the data, the data to be written is written to a dictionary, creating a dictionary:

Create a dictionary

Nsdictionary *dic = [nsdictionary dictionarywithobjectsandkeys:@ "Zhangsan", @ "1", @ "Lisi", @ "2", nil];

Write data to plist file

[DiC Writetofile:plistpath Atomically:yes];

Read the data in the plist in the following form:

To read the plist file, you first need to read the plist file into the dictionary

Nsdictionary *dic2 = [Nsdictionary Dictionarywithcontentsoffile:plistpath];

Print data

NSLog (@ "Key1 is%@", [Dic2 valueforkey:@ "1"]);

NSLog (@ "DIC is%@", dic2);

Http://www.linuxidc.com

For the array read and write in plist, the code is as follows:

Add the Testplist file

NSString *plistpaths = [FilePath stringbyappendingpathcomponent:@ "Tests.plist"];

Start creating a file

[FM createfileatpath:plistpaths Contents:nil Attributes:nil];

Create an array

Nsarray *arr = [[Nsarray alloc] initwithobjects:@ "1", @ "2", @ "3", @ "4", nil];

Write

[arr writetofile:plistpaths Atomically:yes];

Read

Nsarray *arr1 = [Nsarray arraywithcontentsoffile:plistpaths];

Print

NSLog (@ "Arr1is%@", arr1);

iOS Development: plist file operations for data storage

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.