Read and write iOS learning plist files

Source: Internet
Author: User

When doing iOS development, what is the plist file that is often used to plist files? Its full name is: Property list, the attribute listing file, which is a file used to store serialized objects. The property list file has a . plist extension , and is often referred to as a plist file. The file is in XML format.

Plist file Pass is often used to store user settings and can also be used to store bundled information


We create a project to learn how to read and write plist files.


1. Create Project Plistdemo


After the project is created, you can find the appropriate plist file for the project, open for example to see:


In the editor to display similar and tabular form, you can right-click on the plist, the source code to open, you can see the XML format of the plist file.



2. Create a plist file.


Press Command +n shortcut key creation, or file-> new-> New File, select the property List under Mac OS X



Create a plist file name called Plistdemo.



Open the Plistdemo file, right-click the blank, right-click Add row to add? data, add? After a successful data, right-click on this data to see value Type select Dictionary. Click the plus sign to join? The data under this dictionary


Join to finish key and join later? Value, add the phone number and age


After creation, using source code to see the plist file is 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 data from the plist file


now that the file has been created successfully, how to read it, implementation code such as the following:
-(void) viewdidload{    [Super Viewdidload];    Read Plist    nsstring *plistpath = [[NSBundle mainbundle] pathforresource:@ "Plistdemo" oftype:@ "plist"];    Nsmutabledictionary *data = [[Nsmutabledictionary alloc] initwithcontentsoffile:plistpath];    NSLog (@ "%@", "data");//Direct printing. }

Print out the results:

PLISTDEMO[6822:F803] {    Jack =     {age        = $;        " Phone_num "= 13801111111;    };    Tom =     {Age        = $;        " Phone_num "= 13901111111;}    ;}

This will read the data out.


4. Create and write plist files

In the development process, sometimes it is necessary to save some of the program configuration, or game data and so on. The plist data needs to be written.

The written plist file is generated in the appropriate program's sandbox folder.

Then read the code of the Plist data, add the code to write 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 content [data setobject:@ "add some content" forkey:@ "C_key"]; Get the Documents folder 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]; How does that prove my data is written?    Read it and see nsmutabledictionary *data1 = [[Nsmutabledictionary alloc] initwithcontentsoffile:filename];        NSLog (@ "%@", data1); Do any additional setup after loading the view, typically from a nib.}  

after acquiring the plistdemo.plist data that you have created manually, add a piece of content to the data to prove that the input was written.

How does it prove that the added content is written? The following are the printing results:



Code Address: Https://github.com/schelling/YcDemo/tree/master/PlistDemo

Copyright statement: This article by http://blog.csdn.net/totogo2010/Original, welcome reprint share. Please respect the author Labor, reproduced when the statement and the author of the blog link, thank you !





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.