//长时间不用的知识很容易忘,所以正如小学教体育的语文老师所说:“好记性不如烂笔头”//
存放在bundle目录下的文件只可以读,不可以写
//存放在sandbox下的可读可写
bundle目录下读取:
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"city" ofType:@"plist"];//获取文件路径方式一
NSString *plistPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"city.plist"];//获取文件路径方式二
NSDictionary *cityDic = [NSDictionary dictionaryWithContentsOfFile:plistPath];//获取数据
沙盒目录下读取:
//沙盒获取路径
NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [pathArray objectAtIndex:0];
//获取文件的完整路径
NSString *plistPath = [path stringByAppendingPathComponent:@"city.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if(![fileManager fileExistsAtPath:plistPath]) {
NSString *dataPath = [[NSBundle mainBundle]pathForResource:@"city" ofType:@"plist"];;
NSError *error;
//拷贝文件到沙盒的document下
if([fileManager copyItemAtPath:dataPath toPath:plistPath error:&error]) {
NSLog(@"copy success");
} else{
NSLog(@"%@",error);
}
}
NSMutableDictionary *dataDic = [[NSMutableDictionary alloc]initWithContentsOfFile:plistPath];
沙盒目录下写入:
NSMutableDictionary *dataDic = [[NSMutableDictionary alloc]initWithContentsOfFile:plistPath];
dataDic[@"beijing"] = @“首都”; //更改内容
[dataDic writeToFile:plistPath atomically:YES]; //写入