您的位置:首页 > 移动开发 > IOS开发

IOS复习Plist文件的读取和写入

2013-08-09 17:57 537 查看
和前面一样,新疆项目,

与前面不同的是,需要新建一个Plist文件,方法是command+n快捷键



如上面,选择Resource的Property List,点击Next



如上图,文件名plistdemo





如上图,左边资源管理器有一个plistdemo.plist的文件,打开,如上图

添加数据直接右键add row或者点击上图红框中的加,(注意前面三角形)

如果三角形朝下是建立下级,三角形往右是建立同级数据

建好后,右键文件名open as点击 source code将会出现下面xml文件

<?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>New item</key>
<string></string>
<key>yang</key>
<dict>
<key>phone_num</key>
<string>18000000000</string>
<key>age</key>
<string>21</string>
<key>chen</key>
<dict>
<key>phone_num</key>
<string>18000000001</string>
<key>age</key>
<string>22</string>
</dict>
</dict>
</plist>

与前面几个项目一样

打开chenViewController.m

添加代码如下

- (void)viewDidLoad
{
[superviewDidLoad];
//读取plist
NSString *plistPath = [[NSBundlemainBundle] pathForResource:@"plistdemo"ofType:@"plist"];
NSMutableDictionary *data = [[NSMutableDictionaryalloc] initWithContentsOfFile:plistPath];
NSLog(@"%@", data);

//添加一项内容
[data setObject:@"add some content"forKey:@"c_key"];
//获取应用程序沙盒的Documents目录
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *plistPath1 = [paths objectAtIndex:0];
//得到完整的文件名
NSString *filename=[plistPath1 stringByAppendingPathComponent:@"test.plist"];
//输入写入
[data writeToFile:filename atomically:YES];
//那怎么证明我的数据写入了呢?读出来看看
NSMutableDictionary *data1 = [[NSMutableDictionaryalloc] initWithContentsOfFile:filename];
NSLog(@"%@", data1);
//还没有判断写入失败情况,这个需要思考
}
点击运行,控制台输出如下,上面是读取的,下面是写入数据后重新输出的,但是打开plist文件看不到新添加的数据,因为这个是我们动态添加的(具体原因我也不知道,学习需要多查询啊)



//外面找的代码,思考数据显示到text上
//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:

//数据显示到text参考
NSString *plistPath = [[NSBundlemainBundle] pathForResource:@"testInfo"ofType:@"plist"];
NSMutableDictionary *data=[[NSMutableDictionaryalloc] initWithContentsOfFile:plistPath];
// 打印出plist文件
NSLog(@"%@",data);
//读取学生字典的内容到StudentInfo中qu
NSMutableDictionary *StudentInfo = [data objectForKey:@"Student"];
sNameTextField.text = [NSString stringWithFormat:@"%@",[StudentInfo objectForKey:@"Name"]];
sAgeTextField.text = [NSString stringWithFormat:@"%@",[StudentInfo objectForKey:@"Age"]];
sSexTextField.text = [NSString stringWithFormat:@"%@",[StudentInfo objectForKey:@"Sex"]];

NSMutableDictionary *teacherInfo = [data objectForKey:@"teacher"];
//在teacher字典中,把键为Name的值赋给tNameTextField的text上
tNameTextField.text = [NSString stringWithFormat:@"%@",[teacherInfo objectForKey:@"Name"]];
tSexTextField.text = [NSString stringWithFormat:@"%@",[teacherInfo objectForKey:@"Sex"]];

上面这些代码是输出数据到textfield上面

图像文件等下次介绍。

2013年8月7日,10:37,东南大学无锡分校桃园3宿舍106室
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: