A tutorial on creating and reading plist files that store user settings in IOS applications _ios

Source: Internet
Author: User

When doing iOS development, often use to plist files, that plist file is what? It is the full name of the property list, which is a file for storing serialized objects. The property list file has a. plist extension, and is therefore 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 to read and write plist files.

1. Create Project Plistdemo
After the project is created, you can find the project's corresponding plist file, open as shown in the following illustration:

In the editor to display similar forms and tables, you can right-click on the plist, open in source mode, you can see the XML format of the plist file.

2, create plist files.
Press Command +n shortcut key to create, or file-> new-> New File, select the property List under Mac OS X

File name is Custominfo,group select supporting files.

3, click the new custominfo.plist, we add data, the following figure:

Notice that the type of a item, selected as dictionary, opens with source code, and appears 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, as shown below:

All the controls in the previous illustration are label and have the font size set.

5, the next is mapping chant, the five gray label are mapped to the BIDViewController.h file, the type is 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:

Copy Code code as follows:

First read the data in the Studentinfo.plist
NSString *plistpath = [[NSBundle mainbundle] pathforresource:@ "Custominfo" oftype:@ "plist"];
Nsdictionary *dictionary = [[Nsdictionary alloc] initwithcontentsoffile:plistpath];

Fill in the student information into 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 mentor information to a view
Tmpinfo = [Dictionary objectforkey: @ "Mentor"];
Self.mtName.text = [NSString stringwithformat:@ "%@", [Tmpinfo Objectforkey: @ "Name"]];
Self.mtSex.text = [NSString stringwithformat:@ "%@", [Tmpinfo Objectforkey: @ "Sex"]];

7, run, view the effect:

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.