writeToFile atomically returns NO

11,845

Use writeToFile:options:error: instead of writeToFile:atomically: so you can log the error code.

NSError *error;
success = [plistData writeToFile:file options:0 error:&error];
if (!success) {
    NSLog(@"writeToFile failed with error %@", error);
}

UPDATE

Based on the error you pasted into your question, I guess you already have a directory named /var/mobile/Applications/4B60A704-BE00-4160-BFB4-AD89187FADD1/Library/Caches/StoryCache/readability: feed. You cannot overwrite a directory with a file. You need to rename or delete the directory first. You can use -[NSFileManager removeItemAtPath:error:] to delete a directory and all of its contents recursively.

Share:
11,845
adit
Author by

adit

Updated on September 15, 2022

Comments

  • adit
    adit over 1 year

    I have the following code:

    NSString *errorStr = nil;
    NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:dict
                                                                   format:NSPropertyListBinaryFormat_v1_0
                                                         errorDescription:&errorStr];
    if (errorStr) {
        // Darn.
        NSLog(@"saving dictionary error %@", errorStr);
    } else {
        // [plistData writeToFile:file atomically:YES];
        // (ankit): Changing this to NO for speed.
        success = [plistData writeToFile:file atomically:NO];
    }
    

    however this returns NO, for the following dictionary:

      {
        entries = (
                    {
                author = "Jada Wong";
                cellIdentifier = default;
                date = "2012-10-15 02:22:41 +0000";
                domain = Readability;
                feedUrl = "readability://feed";
                imagesAreOnPulsesubscriber = 0;
                order = 0;
                summary = "";
                templateUrl = "default_template.html";
                title = "We\U2019re Going To Try Out Dolce & Gabbana\U2019s Beauty Look This Weekend";
                url = "http://www.styleite.com/media/styledish-10122012/";
            }
        );
    }
    

    but it works for the following dict:

    {
        entries =     (
                    {
                author = "Colleen Taylor";
                cellIdentifier = default;
                date = "2012-10-15 01:00:05 +0000";
                domain = TechCrunch;
                domainUrl = "http://techcrunch.com";
                feedUrl = "http:/asdasdm/TechCrunch";
                images =             (
                    "http://asdshot-2012-10-14-at-5-20-43-pm.png"
                );
                imagesAreOnPulsesubscriber = 1;
                lastUpdated = 1350263095;
                order = 0;
                shortLink = "http://bete.me/s/ej2Tg";
                summary = "Bonobos, the men's clothing company, has gained lots of ground since it was founded in 2007 for its presence on the web as a mostly pure e-commerce p...";
                templateUrl = "default_template.html";
                title = "Inside Bonobos\U2019 New Palo Alto Digs, Where The Startup Known For E-Commerce Is Investing In Bricks & Mortar";
                url = "http://techcrunch.com/2012/10/14/bonobos-palo-alto-mike-hart/";
            }
        );
    }
    

    Any idea why? It worked for different dictionary The file path I am writing to is:

    /var/mobile/Applications/4B60A704-BE00-4160-BFB4-AD89187FADD1/Library/Caches/StoryCache/readability:  feed
    

    The error I got is:

    "The operation couldn\u2019t be completed. (Cocoa error 512.)" UserInfo=0xb41580 {NSFilePath=/var/mobile/Applications/4B60A704-BE00-4160-BFB4-AD89187FADD1/Library/Caches/StoryCache/readability:  feed, NSUnderlyingError=0xbb8930 "The operation couldn\u2019t be completed. Is a directory"}
    
  • adit
    adit over 11 years
    hmm..for some reason changing in the file path to /var/mobile/Applications/4B60A704-BE00-4160-BFB4-AD89187FADD‌​1/Library/Caches/Sto‌​ryCache/jesus worked.. any idea?
  • zaph
    zaph over 11 years
    Because the file path was invalid?
  • ThomasW
    ThomasW over 11 years
    @adit Getting the error message as Rob suggests will tell you what the problem was.
  • rob mayoff
    rob mayoff over 11 years
    @adit Uh... the power of christ compelled it? You might be too young to get that reference to The Exorcist. Anyway, get the error object and log it. If you can't figure out the problem from the error log, paste it into your question so we can look at it.
  • adit
    adit over 11 years
    I copy pasted the error message on my question, it's saying that it's a directory
  • zaph
    zaph over 11 years
    NSLog the file path each write and then figure out why the path is a dictionary.