//長(zhǎng)時(shí)間不用的知識(shí)很容易忘,所以正如小學(xué)教體育的語(yǔ)文老師所說:“好記性不如爛筆頭”//
存放在bundle目錄下的文件只可以讀,不可以寫
//存放在sandbox下的可讀可寫
bundle目錄下讀?。?br>
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"city" ofType:@"plist"];//獲取文件路徑方式一
NSString *plistPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"city.plist"];//獲取文件路徑方式二
NSDictionary *cityDic = [NSDictionary dictionaryWithContentsOfFile:plistPath];//獲取數(shù)據(jù)
沙盒目錄下讀?。?/p>
//沙盒獲取路徑
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"] = @“首都”; //更改內(nèi)容
[dataDic writeToFile:plistPath atomically:YES]; //寫入