iOS Learning plist reading and writing files

Source: Internet
Author: User

Dry iOS development time, often used to plist files. What file is it plist? It is the full name of the property list , which is a file that is used to store the serialized object. The property list file has a . plist extension. Therefore, it is commonly referred to as plist files. The file is in XML format.

Plist file Pass is often used to store user settings. can also be used to store bundles of 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 with what you 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, in the blank right -click, right-click Add row to add data, add a successful data, on this data, right-click on the value Type select Dictionary. Click the plus sign to add the data under this dictionary


Added value after key is added. Add 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 you need to save some of the configuration of the program. 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 you have obtained the plistdemo.plist data that you created manually. This data is appended with an entry that proves that the input was written.

How does it prove that the added content was 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 !





iOS Learning plist reading and writing files

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.