lucgian84
lucgian84

Reputation: 843

Null while reading plist file from documents directory

In my app I store some informations in a plist file. I generated this plist with this code:

- (void)saveArrayToFile {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *fileName = [documentsDirectory stringByAppendingPathComponent:@"objects.plist"];
    [self.reportArray writeToFile:fileName atomically:YES];
}

With the application iExplorer I looked inside the app I created to find the file I created, now I need to read my file back. I found on the web several solution that allow me to read this plist file, so I tried this solution:

- (void)loadObjectFromFile {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];
    NSString *path = [documentsDir stringByAppendingString:@"objects.plist"];
    NSArray *temp = [NSArray arrayWithContentsOfFile:path];
    NSLog(@"%@", temp);
}

But if I look to the console in Xcode, I can't see the log of the content of my plist file, exactly i get this:

2013-10-17 10:29:34.586 MyApp[233:60b] (null)

I paste here the content of my plist file so maybe you can help me to read back the information stored in it, the content of plist file are:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
    <dict>
        <key>desc</key>
        <string>Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda.</string>
        <key>name</key>
        <string>Dadi rossi</string>
        <key>price</key>
        <string>5 €</string>
    </dict>
</array>
</plist>

What's wrong with my solution? Why I can't read the content of my plist file? Thank you!

Upvotes: 1

Views: 593

Answers (1)

Dipen Panchasara
Dipen Panchasara

Reputation: 13600

Your mistake is here in first line of reading

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);

you are reading data from NSDocumentationDirectory while you are storing data in NSDocumentDirectory. Just replace it with following line.

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

Second Mistake here Appending String instead of PathComponent Replace

NSString *path = [documentsDir stringByAppendingString:@"objects.plist"];

with

NSString *fileName = [documentsDirectory stringByAppendingPathComponent:@"objects.plist"];

Upvotes: 2

Related Questions