kzia
kzia

Reputation: 555

Reading and writing NSMutableDictionary to a plist file

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

Upvotes: 2

Views: 8299

Answers (3)

Ashok R
Ashok R

Reputation: 20766

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
}

Upvotes: 0

zbMax
zbMax

Reputation: 2818

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

Upvotes: 1

Ishank
Ishank

Reputation: 2926

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

Upvotes: 4

Related Questions