读取NSMutableDictionary并将其写入plist文件 [英] Reading and writing NSMutableDictionary to a plist file

查看:101
本文介绍了读取NSMutableDictionary并将其写入plist文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将AppDelegate.mapplicationDidEnterBackground中的NSMutableDictionary保存到plist文件中.保存后,我立即尝试检查该文件是否存在并读回,但找不到该文件.

I am trying to save NSMutableDictionary in applicationDidEnterBackground of AppDelegate.m an to a plist file. Immediately after saving it, I try to check if the file exists and read it back, but the file is not found.

NSString *photoCacheFilename = @"photoCache.plist";
[photoDict writeToFile:photoCacheFilename atomically:YES];
NSLog(@"File name: %@", photoCacheFilename);
BOOL isFile = [[NSFileManager defaultManager] fileExistsAtPath:photoCacheFilename];
if(isFile)
{
    NSLog (@"File found");
    NSMutableDictionary *newDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:photoCacheFilename];
}
else
{
    NSLog (@"File not found");
}

我按照一些用户的建议修改了代码,但是仍然找不到该文件.我不确定是否要正确检查文件的存在.

I modified the code as suggested by some users, but still the file is not found. I am not sure if I am checking for the existence of file correctly.

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *photoCacheFilename = [documentsPath stringByAppendingPathComponent:@"photoCache.plist"];

[photoDict writeToFile:photoCacheFilename atomically:YES];

BOOL isFile = [[NSFileManager defaultManager] fileExistsAtPath:photoCacheFilename];
if(isFile)
{
    NSLog (@"File found");
    NSMutableDictionary *newDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:photoCacheFilename];
}
else
{
    NSLog (@"File not found");
}

推荐答案

您未指定Documents目录的正确路径

You are not specifying the correct path of the Documents directory

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
NSString *documentsPath = [paths objectAtIndex:0];

NSString *photoCacheFilename = [documentsPath stringByAppendingPathComponent:@"photoCache.plist"]; // Correct path to Documents Dir in the App Sand box

[photoDict writeToFile:photoCacheFilename atomically:YES]; //Write

这篇关于读取NSMutableDictionary并将其写入plist文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆