Original posts: http://blog.csdn.net/totogo2010/article/details/7634185
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 (right-plist file) to find out what the plist file looks like:
[CPP]View Plaincopy
- <?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, the implementation code is as follows:
[CPP]View Plaincopy
- -(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:
[CPP]View Plaincopy
- PLISTDEMO[6822:F803] {
- Jack = {
- age = 22;
- "Phone_num" = 13801111111;
- };
- Tom = {
- Age = 36;
- "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 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
- <strong>-(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];
- //How does it prove that my data is written? Read it and see.
- Nsmutabledictionary *data1 = [[Nsmutabledictionary alloc] initwithcontentsoffile:filename];
- NSLog (@"%@", data1);
- additional setup after loading the view, typically from a nib.
- }
- </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
Read and write IOS plist files