美文网首页
NSMutableDictionary写文件失败解决

NSMutableDictionary写文件失败解决

作者: 勤_c661 | 来源:发表于2019-11-21 15:46 被阅读0次

    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];

    相关文章

      网友评论

          本文标题:NSMutableDictionary写文件失败解决

          本文链接:https://www.haomeiwen.com/subject/rkwzkqtx.html