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, 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
Add value after adding key, 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 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 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 !
Read and write iOS learning plist files