• 微软原版系统

  • 一键重装系统

  • 纯净系统

  • 在线技术客服

魔法猪系统重装大师 一键在线制作启动 U 盘 PE 系统 用一键重装的魔法拯救失去灵魂的系统
当前位置:首页 > 教程 > 电脑教程

iOS开发读取plist文件、iphone中plist文件的读写存

时间:2015年04月02日 15:30:47    来源:魔法猪系统重装大师官网    人气:15104

在Xcode中建立一个iOS项目后,会自己产生一个.plist文件,点击时会看见它显示的是类似于excel表格:

但是,如果打开方式选择Source Code,你会看见它其实是一个xml文件。

我们会做一个小例子,在这个例子中我们自己建立一个plist文件并填入数据,然后运行时读取这个plist文件,并将数据填写在界面上。

首先要知道读取plist文件的方法,一般来说,使用代码

NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"listFileName" ofType:@"plist"];
NSArray *array = [[NSArray alloc] initWithContentsOfFile:plistPath];
NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:plistPath];

已经足够了,此时可以使用NSLog例程查看array和dictionary的内容。不过,有时候受plist文件内容的限制,array内容可能为空。

其实,用dictionary就已经足够了,在下面的例子里我们也只用dictionary。

1、运行Xcode4.2,新建一个Single View Application,名称为ReadPlistFile,其他设置如下图:

2、新建我们自己的plist文件:

File —> New —> New File,选择Mac OS X下的Property List

文件名为 customInfo,Group选择Supporting Files。

3、单击新建的customInfo.plist,我们添加数据,如下图:

注意,Type一项的类型,选择的是Dictionary,以Source Code打开,显示如下:

<?xml version="1.0" encoding="UTF-8"?>



	Student
	
		Name
		Yang
		Sex
		Male
		Num
		SX_010
	
	Mentor
	
		Name
		Gu
		Sex
		Male
	

4、为视图添加控件:

单击BIDViewController.xib,打开IB,拖几个控件上去,并设置好布局,如下图:

上图中所有的控件都是Label,并设置了字体大小。

5、接下来就是映射呗,把五个灰色的Label都映射到BIDViewController.h文件中,类型都死OutLet,名称依次是stuName,stuSex,stuNum,mtName,mtSex。

6、单击BIDViewController.m,在viewDidLoad方法中的[super viewDidLoad]之后添加如下代码:

//首先读取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、运行,查看效果:



iphone:plist的读写存代码示例

01- (void)viewDidLoad {
02 
03//读取plist
04NSString *path = [[NSBundle mainBundle] pathForResource:@”demo” ofType:@”plist”];
05NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
06NSLog(@”%d”,[data count]);
07 
08//添加一项内容
09[data setObject:@"content" forKey:@"item4"];
10 
11//获取应用程序沙盒的Documents目录
12NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
13path = [paths objectAtIndex:0];
14 
15//得到完整的文件名
16NSString *filename=[path stringByAppendingPathComponent:@"test.plist"];
17 
18[data writeToFile:filename atomically:YES];
19[data release];
20[super viewDidLoad];
21}
iOS,开发,读取,plist,文件,、,iphone,中,
栏目:电脑教程 阅读:1000 2023/12/27
Win7教程 更多>>
U盘教程 更多>>
Win10教程 更多>>
魔法猪学院 更多>>

Copyright © 2015-2023 魔法猪 魔法猪系统重装大师

本站发布的系统仅为个人学习测试使用,请在下载后24小时内删除,不得用于任何商业用途,否则后果自负,请支持购买微软正版软件。

在线客服 查看微信 返回顶部