在iPhone/iPad工程里面,添加 File->Other->Property List,例如:test.plist,然后在其中添加3個项目(Key) Name, Date, Dept,并填充Value值。
以下为对此test.plist文件的一系列常用操作(myname,mydate,mydept为定义的变量):
NSString *path=[[NSBundle mainBundle] pathForResource:@"test" ofType:@"plist"];
//从文件内容创建字典
NSDictionary *dict=[NSDictionary dictionaryWithContentsOfFile:path];
//从字典取出Key对应的Value
self.myname.text=[dict valueForKey:@"Name"] ;
NSLog(@"%@",[dict valueForKey:@"Name"]);
self.mydate.text=[dict valueForKey:@"Date"] ;
self.mydept.text=[dict valueForKey:@"Dept"] ;
//改变Key对应的值
[dict setValue:@"Jimmy" forKey:@"Name"];
//将改变后的结果(字典)写入文件(filepath指定路径和文件名)
[dict writeToFile:filepath atomically:YES];