Read and write examples of plist files in Apple iOS development

Source: Internet
Author: User
Tags sublime text

Preface: When doing iOS development, often use the plist file, then, what is plist file? Here, I do a simple introduction and use, it is the full name is: Property list, the attribute listing file, which is used to store the serialized object of the file. The property list file has a. plist extension, so it is often referred to as a plist file and the file is in XML format.
plist files are typically used to store user settings, or to store bundled information, and to store additional information.

Below, I will create a project to learn to read and write plist files.

1. Create Project Demo

After the project is created, you can find the project's corresponding plist file, open as shown in the following illustration:

In the editor to display similar forms and tables, you can right-click on the plist, the source mode to open, you can see the plist file XML format, such as the following, with sublime text open

2, create plist files.

File-> new-> New File, or press command +n shortcut key to create, select the property List under Ios–>resource

Open the Plistdemo file, in the blank out right, right click Add Row added data, add a successful data, on this data on the right to see the value type selection dictionary. The point plus sign adds the data under this dictionary:

Add value after the key is added, add cell phone number and age

After the creation is complete, check with source code to see that the plist file is like this:

<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE plist Public "-//apple//dtd plist 1.0//en" "Http://www.apple.com/DTDs/PropertyList-1.0.dtd" >
<plist version= "1.0" >
<dict>
<key>jack</key>
<dict>
<key>phone_num</key>
<string>13801111111</string>
<key>age</key>
<string>22</string>
</dict>
<key>tom</key>
<dict>
<key>phone_num</key>
<string>13901111111</string>
<key>age</key>
<string>36</string>
</dict>
</dict>
</plist>

3, read the plist file data

Now the file creation is successful, how to read it, the implementation code is as follows:

-(void) viewdidload
{
[Super Viewdidload];
Read plist
NSString *plistpath = [[NSBundle mainbundle] pathforresource:@ "Plistdemo" oftype:@ "plist"];
Nsmutabledictionary *data = [[Nsmutabledictionary alloc] initwithcontentsoffile:plistpath];
NSLog (@ "%@", data);//print data directly.
}

Print out the results:

PLISTDEMO[6822:F803] {
Jack = {
age = 22;
"Phone_num" = 13801111111;
};
Tom = {
Age = 36;
"Phone_num" = 13901111111;
};
}
This will read out the data.

4. Create and write plist files

In the development process, sometimes need to save some of the configuration of the program, or game data, and so on. You need to write plist data at this time.
The written plist file is generated in the sandbox directory of the corresponding program.
The code that reads the plist data is then added to the code that writes the data,

-(void) viewdidload
{
[Super Viewdidload];
Read plist
NSString *plistpath = [[NSBundle mainbundle] pathforresource:@ "Plistdemo" oftype:@ "plist"];
Nsmutabledictionary *data = [[Nsmutabledictionary alloc] initwithcontentsoffile:plistpath];
NSLog (@ "%@", data);
Add a piece of content
[Data setobject:@ "add some content" forkey:@ "C_key"];
Get the documents directory for the application sandbox
Nsarray *paths=nssearchpathfordirectoriesindomains (Nsdocumentdirectory,nsuserdomainmask,yes);
NSString *plistpath1 = [Paths objectatindex:0];
Get the full file name
NSString *filename=[plistpath1 stringbyappendingpathcomponent:@ "Test.plist"];
Input Write
[Data writetofile:filename Atomically:yes];
So how do I prove my data is written? Read it, look.
Nsmutabledictionary *data1 = [[Nsmutabledictionary alloc] initwithcontentsoffile:filename];
NSLog (@ "%@", data1);
}


After acquiring the plistdemo.plist data that you have created manually, add a piece of content behind the data to prove that the input was written.
How do I prove that the added content is written? Here is the print result:

The above is my summary of recent days, there are incisive, of course, there are omissions, in the next work, I will try to be careful as comprehensively as possible, further strengthen the effectiveness of the work, and effectively exert their limited wisdom and hard working spirit, for the company's technical level and product quality of the steady climb to do all the contribution.

Related Article

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.