When you build an iOS project in Xcode, you will generate a. plist file yourself, and when clicked, you will see that it shows an Excel-like table:
However, if you open the way to select Source Code, you will see that it is actually an XML file.
We will do a small example, in this case we build a plist file and fill in the data, then read the plist file at runtime and fill in the data on the interface.
The first thing to know is how to read the Plist file, in general, using code
NSString *plistpath = [[NSBundle mainbundle] pathforresource:@ "Listfilename" oftype:@ "plist"]; Nsarray *array = [[Nsarray alloc] initwithcontentsoffile:plistpath]; Nsdictionary *dictionary = [[Nsdictionary alloc] initwithcontentsoffile:plistpath];
is sufficient, you can use the NSLog routine to view the contents of the array and dictionary. However, sometimes the contents of the Plist file are limited, and the array contents may be empty.
In fact, the use of dictionary is enough, in the following example we will only use dictionary.
1, run Xcode4.2, create a new single View application, named Readplistfile, other settings such as:
2. Create our own plist file:
File-> new-> New File, select the property List under Mac OS X
The file name is Custominfo,group select supporting files.
3, click New Custominfo.plist, we add data, such as:
Note that type one of the types, selected by dictionary, opens with source code, which is shown 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>student</key><dict><key>name</key><string >Yang</string><key>Sex</key><string>Male</string><key>Num</key> <string>sx_010</string></dict><key>mentor</key><dict><key>name</ Key><string>gu</string><key>sex</key><string>male</string></dict> </dict></plist>
4. Add controls for the view:
Click Bidviewcontroller.xib, open IB, drag several controls up, and set the layout, such as:
All the controls in the are label, and the font size is set.
5, Next is the mapping Bai, the five gray labels are mapped to the BIDViewController.h file, the type is dead outlet, the name is stuname,stusex,stunum,mtname,mtsex in turn.
6. Click Bidviewcontroller.m, and add the following code after [Super Viewdidload] in the Viewdidload method:
First read the data in studentinfo.plist nsstring *plistpath = [[NSBundle mainbundle] pathforresource:@ "Custominfo" ofType:@ "plist "]; Nsdictionary *dictionary = [[Nsdictionary alloc] initwithcontentsoffile:plistpath]; Fill in the student information in the view nsdictionary *tmpinfo = [Dictionary objectforkey: @ "Student"];self.stuname.text = [nsstring stringwithformat:@ "%@", [Tmpinfo Objectforkey: @ "Name"]];self.stusex.text = [nsstring stringwithformat:@ "%@", [ Tmpinfo Objectforkey: @ "Sex"]];self.stunum.text = [nsstring stringwithformat:@ "%@", [Tmpinfo Objectforkey: @ "Num"]]; Write tutor information to view tmpinfo = [Dictionary objectforkey: @ "Mentor"];self.mtname.text = [nsstring stringwithformat:@ "%@", [ Tmpinfo Objectforkey: @ "Name"]];self.mtsex.text = [nsstring stringwithformat:@ "%@", [Tmpinfo Objectforkey: @ "Sex"]];
7, run, see the effect:
Sample read and write memory code for Iphone:plist
- (void) Viewdidload {02 03//Read PlistNSString *path =[[NSBundle Mainbundle] pathforresource:@ "demo" oftype:@ "Plist"];Nsmutabledictionary *data =[[Nsmutabledictionary alloc] initwithcontentsoffile:path];NSLog (@ "%d ", [Data Count]);07 08//add a piece of content[Data setobject:@ "content" forkey:@ "Item4"];10 11//get the documents directory for the application sandboxNsarray *paths=Nssearchpathfordirectoriesindomains (nsdocumentdirectory,nsuserdomainmask,yes);Path = [Paths objectatindex:0];14 15//get the full file nameNSString *filename=[path stringbyappendingpathcomponent:@ "Test.plist"];17 18[Data writetofile:filename atomically:yes];19[Data release];20 [SuperViewdidload];21}