源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

iOS应用中存储用户设置的plist文件的创建与读写教程

  • 时间:2022-06-02 05:45 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:iOS应用中存储用户设置的plist文件的创建与读写教程
    在做iOS开发时,经常用到到plist文件,  那plist文件是什么呢? 它全名是:Property List,属性列表文件,它是一种用来存储串行化后的对象的文件。属性列表文件的扩展名为.plist ,因此通常被称为 plist文件。文件是xml格式的。 Plist文件通常用于储存用户设置,也可以用于存储捆绑的信息 我们创建一个项目来学习plist文件的读写。 1、创建项目Plistdemo 项目创建之后可以找到项目对应的plist文件,打开如下图所示: [img]http://files.jb51.net/file_images/article/201604/201641890318772.png?20163189345[/img] 在编辑器中显示类似与表格的形式,可以在plist上右键,用源码方式打开,就能看到plist文件的xml格式了。 2、创建plist文件。 按command +N快捷键创建,或者File —> New —> New File,选择Mac OS X下的Property List [img]http://files.jb51.net/file_images/article/201604/201641890447160.jpg?20163189457[/img] 文件名为 customInfo,Group选择Supporting Files。 3、单击新建的customInfo.plist,我们添加数据,如下图: [img]http://files.jb51.net/file_images/article/201604/201641890508538.png?20163189525[/img] 注意,Type一项的类型,选择的是Dictionary,以Source Code打开,显示如下:
<?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、为视图添加控件: 单击BIDViewController.xib,打开IB,拖几个控件上去,并设置好布局,如下图: [img]http://files.jb51.net/file_images/article/201604/201641890555870.png?2016318964[/img] 上图中所有的控件都是Label,并设置了字体大小。 5、接下来就是映射呗,把五个灰色的Label都映射到BIDViewController.h文件中,类型都是OutLet,名称依次是stuName,stuSex,stuNum,mtName,mtSex。 6、单击BIDViewController.m,在viewDidLoad方法中的[super viewDidLoad]之后添加如下代码:
[u]复制代码[/u] 代码如下:
//首先读取studentInfo.plist中的数据 NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"customInfo" ofType:@"plist"]; NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:plistPath];     //将学生信息填入视图 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"]];     //将导师信息写入视图 tmpInfo = [dictionary objectForKey: @"Mentor"]; self.mtName.text = [NSString stringWithFormat:@"%@", [tmpInfo objectForKey: @"Name"]]; self.mtSex.text = [NSString stringWithFormat:@"%@", [tmpInfo objectForKey: @"Sex"]];
7、运行,查看效果: [img]http://files.jb51.net/file_images/article/201604/201641890616043.png?20163189625[/img]
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部