NSDictionary+plist简单示例 .

来源:互联网 发布:java判断float整数 编辑:程序博客网 时间:2024/06/06 07:26

在iPhone/iPad工程里面,添加 File->Other->Property List,例如:test.plist,然后在其中添加3個项目(Key) Name, Date, Dept,并填充Value值。

以下为对此test.plist文件的一系列常用操作(myname,mydate,mydept为定义的变量):

[cpp] view plaincopyprint?
  1. NSString *path=[[NSBundle mainBundle] pathForResource:@"test" ofType:@"plist"];
  2. //从文件内容创建字典
  3. NSDictionary *dict=[NSDictionary dictionaryWithContentsOfFile:path];

  4. //从字典取出Key对应的Value
  5. self.myname.text=[dict valueForKey:@"Name"] ;
  6. NSLog(@"%@",[dict valueForKey:@"Name"]);
  7. self.mydate.text=[dict valueForKey:@"Date"] ;
  8. self.mydept.text=[dict valueForKey:@"Dept"] ;
  9. //改变Key对应的值
  10. [dict setValue:@"Jimmy" forKey:@"Name"];
  11. //将改变后的结果(字典)写入文件(filepath指定路径和文件名)
  12. [dict writeToFile:filepath atomically:YES];

原创粉丝点击