Reading and writing NSMutableDictionary to a plist file

只谈情不闲聊 提交于 2019-12-23 23:38:20

问题


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");
}

回答1:


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



回答2:


You have to specify a complete path to save data :

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



回答3:


IN SWIFT 3.0

To get photoCache.plist file path which is in the document directory.

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
    }

To check whether the file exists or not.

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


来源:https://stackoverflow.com/questions/17191697/reading-and-writing-nsmutabledictionary-to-a-plist-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!