IOS learning-plist file read/write

Source: Internet
Author: User

 

During iOS development, plist files are often used. What is a plist file? Its full name is Property List, which is an attribute List file used to store serialized objects. The property list file extension is. PlistIs usually called a plist file. The file is in xml format.

 

Plist files are usually 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 a project Plistdemo

 

After the project is created, you can find the plist file corresponding to the project and open it, as shown in:


The editor displays a format similar to a table. You can right-click the plist file and open it in source code mode to see the xml format of the plist file.

 

 

2. Create a plist file.

 

 

Press the command + N shortcut key to create, or File-> New File, and select the Property List under Mac OS X.


Create a plist file named plistdemo.

 

 

 

Open the plistdemo file, right-click it in the blank area, right-click Add row to Add data, right-click a piece of data, right-click the data, and select value Type to Dictionary. Click the plus sign to add the data under this Dictionary.


After the key is added, add the Value and the mobile phone number and age.

 

After the creation is complete, use source code to view the plist file as follows:

 

<?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 data from plist files

 

Now that the file is successfully created, how can I 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 .}
The output is as follows:

 

PlistDemo[6822:f803] {    jack =     {        age = 22;        "phone_num" = 13801111111;    };    tom =     {        age = 36;        "phone_num" = 13901111111;    };}

 

In this way, the data is read.

 

 

4. Create and write plist files

In the development process, you sometimes need to save some program configurations or game data. In this case, Plist data needs to be written.

The written plist file is generated in the sandbox directory of the corresponding program.

Then the code for reading plist data is added with the code for writing 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"]; // obtain the Documents directory of the application sandbox NSArray * paths = NSSearchPathForDirectoriesInDomains (NSDocume NtDirectory, NSUserDomainMask, YES); NSString * plistPath1 = [paths objectAtIndex: 0]; // obtain the complete file name NSString * filename = [plistPath1 stringByAppendingPathComponent: @ "test. plist "]; // input and write [data writeToFile: filename atomically: YES]; // How can I prove that 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 obtaining the manually created plistdemo. plist data, add a content to the data to prove that the input is written.

 

 

How can I prove that the added content has been written? The following is the result:


 

Address: https://github.com/schelling/YcDemo/tree/master/PlistDemo

Copyright Disclaimer: This article will be published at http://blog.csdn.net/totogo2010/, and you will be welcomed to enjoy the transfer and sharing. Please respect the work of the author. Keep this note and the author's blog link when reprinting. Thank you!

 

 

 

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.