Read and write iOS learning plist files

Source: Internet
Author: User

This article reprinted to http://blog.csdn.net/totogo2010/article/details/7634185 Iosdictionaryencodingcommandlistfile

When doing iOS development, often use to the plist file, what is the plist file? 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 files are typically used to store user settings or 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 corresponding plist file for the project, which opens as shown:

In the editor to display similar to the form of a table, you can right-click on the plist, open with source code, 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 the plist file named Plistdemo.

Open the Plistdemo file, right-click in the blank, right-click Add row, and add data, after adding a successful data, right-click on this data to see Value type Select Dictionary. Click the plus sign to add the data under this dictionary

Add value after adding key, add phone number and age

After creation, use source code to see what the plist file looks like:

[CPP]View Plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <! DOCTYPE plist public "-//apple//dtd plist 1.0//en" "Http://www.apple.com/DTDs/PropertyList-1.0.dtd" >
  3. <plist version="1.0" >
  4. <dict>
  5. <key>jack</key>
  6. <dict>
  7. <key>phone_num</key>
  8. <string>13801111111</string>
  9. <key>age</key>
  10. <string>22</string>
  11. </dict>
  12. <key>tom</key>
  13. <dict>
  14. <key>phone_num</key>
  15. <string>13901111111</string>
  16. <key>age</key>
  17. <string>36</string>
  18. </dict>
  19. </dict>
  20. </plist>

3. Read the data from the plist file

Now that the file has been created successfully, how to read it, the implementation code is as follows: [CPP]View Plaincopy < param name= "wmode" value= "Transparent" >
    1. -(void) Viewdidload
    2. {
    3. [Super Viewdidload];
    4. //Read plist
    5. NSString *plistpath = [[NSBundle mainbundle] pathforresource:@"Plistdemo" oftype:@"plist"];
    6. Nsmutabledictionary *data = [[Nsmutabledictionary alloc] initwithcontentsoffile:plistpath];
    7. NSLog (@"%@", data);   Print data directly.
    8. }
Print out the results:

[CPP]View Plaincopy
    1. PLISTDEMO[6822:F803] {
    2. Jack = {
    3. age = 22;
    4. "Phone_num" = 13801111111;
    5. };
    6. Tom = {
    7. Age = 36;
    8. "Phone_num" = 13901111111;
    9. };
    10. }

This will read the data out.

4. Create and write plist files

In the development process, sometimes you need to save some 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 corresponding program's sandbox directory.

Then read the code of the Plist data, add the code to write the data,

[CPP]View Plaincopy
  1. <strong>-(void) Viewdidload
  2. {
  3. [Super Viewdidload];
  4. //Read plist
  5. NSString *plistpath = [[NSBundle mainbundle] pathforresource:@"Plistdemo" oftype:@"plist"];
  6. Nsmutabledictionary *data = [[Nsmutabledictionary alloc] initwithcontentsoffile:plistpath];
  7. NSLog (@"%@", data);
  8. //Add a piece of content
  9. [Data setobject:@"add some content" forkey:@"C_key"];
  10. //Get the documents directory for the application sandbox
  11. Nsarray *paths=nssearchpathfordirectoriesindomains (Nsdocumentdirectory,nsuserdomainmask,yes);
  12. NSString *plistpath1 = [Paths objectatindex:0];
  13. //Get the full file name
  14. NSString *filename=[plistpath1 stringbyappendingpathcomponent:@"Test.plist"];
  15. //Input Write
  16. [Data writetofile:filename Atomically:yes];
  17. //How does it prove that my data is written? Read it and see.
  18. Nsmutabledictionary *data1 = [[Nsmutabledictionary alloc] initwithcontentsoffile:filename];
  19. NSLog (@"%@", data1);
  20. additional setup after loading the view, typically from a nib.
  21. }
  22. </strong>

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 was written? Here is the result of the printout:



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!

Read and write iOS learning plist 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.