NSMutableDictionary写文件失败解决
下列代码保存文件失败:
NSString *plistPath = [[NSBundle mainBundle]pathForResource:@"JsEtag" ofType:@"plist"];
NSMutableDictionary *dataDic = [[NSMutableDictionary alloc]initWithContentsOfFile:plistPath];
dataDic[fileName] = Etag;
BOOL flag = [dataDic writeToFile:plistPathatomically:YES];
解决方法:
由于路径访问不对,应用使用NSSearchPathForDirectoriesInDomains获取路径,代码如下:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString*documentsDirectory = [pathsobjectAtIndex:0];
NSString *plistPath = [documentsDirectorystringByAppendingPathComponent:@"JsEtag.plist"];
NSMutableDictionary *dataDic = [[NSMutableDictionary alloc] initWithContentsOfFile: plistPath];
if(!dataDic) {
dataDic = [[NSMutableDictionaryalloc]init];
}
//保存Etag
dataDic[fileName] = Etag;
BOOLflag = [dataDicwriteToFile:plistPathatomically:YES];
网友评论