我试图保存NSMutableDictionary
在applicationDidEnterBackground
的AppDelegate.m
和plist
文件。在保存之后立即尝试检查文件是否存在并读回,但找不到该文件。读写NSMutableDictionary到plist文件
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");
}
我修改了一些用户建议的代码,但仍找不到该文件。我不确定我是否正确检查文件的存在。
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");
}
photoCacheFilename应complate路径不只是文件名.. – bhawesh
我试过的完整路径,如以下,但仍是同样的结果:的NSString * photoCacheFilename = [@ “〜/文档/ photoCache.plist” stringByExpandingTildeInPath]; – kzia
正如他在[问题] [1]的回答中那样获得完整路径。 [1]:http://stackoverflow.com/questions/9433598/write-into-plist-file-using-nsdictionary-object – keen