2013-06-19 79 views
2

我试图保存NSMutableDictionaryapplicationDidEnterBackgroundAppDelegate.mplist文件。在保存之后立即尝试检查文件是否存在并读回,但找不到该文件。读写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"); 
} 
+0

photoCacheFilename应complate路径不只是文件名.. – bhawesh

+0

我试过的完整路径,如以下,但仍是同样的结果:的NSString * photoCacheFilename = [@ “〜/文档/ photoCache.plist” stringByExpandingTildeInPath]; – kzia

+0

正如他在[问题] [1]的回答中那样获得完整路径。 [1]:http://stackoverflow.com/questions/9433598/write-into-plist-file-using-nsdictionary-object – keen

回答

4

你没有指定Documents目录的正确路径

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 
+0

查看我在编辑中的问题。 – kzia

+0

嗯,你能检查'writeToFile'是否返回true或false吗? –

+0

'writeToFile'失败。它看起来像我试图写的字典包含不允许写入plist文件的数据类型。谢谢您的帮助。 – kzia

1

您必须指定一个完整的路径来保存数据:

NSString *myFile = @"myFile.plist"; 
NSArray *filePaths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [filePaths objectAtIndex:0]; 
NSString *path = [documentsDirectory stringByAppendingPathComponent:myFile]; 
[photoDict writeToFile:path atomically:YES]; 
+0

在该问题中查看我的编辑。 – kzia

+0

对我来说似乎是正确的...尝试实例化您的可变字典而不检查文件的存在,在调试区域放置一个断点并检查其值。 – zbMax

+0

'writeToFile'失败。它看起来像我试图写的字典包含不允许写入plist文件的数据类型。谢谢您的帮助。 – kzia

0

斯威夫特3.0

获取文档目录中的photoCache.plist文件路径。

func getPath() -> String { 
     let plistFileName = "photoCache.plist" 
     let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) 
     let documentPath = paths[0] as NSString 
     let plistPath = documentPath.appendingPathComponent(plistFileName) 
     return plistPath 
    } 

要检查文件是否存在。

let plistPath = self.getPath() 
if FileManager.default.fileExists(atPath: plistPath) { 
    //File Exists 
}