首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >writeToFile原子地返回NO

writeToFile原子地返回NO
EN

Stack Overflow用户
提问于 2012-10-15 10:14:37
回答 3查看 11.8K关注 0票数 5

我有以下代码:

代码语言:javascript
复制
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];
}

但是,对于以下字典,这将返回NO:

代码语言:javascript
复制
  {
    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/";
        }
    );
}

但它适用于下面的字典:

代码语言:javascript
复制
{
    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/";
        }
    );
}

知道为什么吗?它适用于不同的字典,我要写入的文件路径是:

代码语言:javascript
复制
/var/mobile/Applications/4B60A704-BE00-4160-BFB4-AD89187FADD1/Library/Caches/StoryCache/readability:  feed

我得到的错误是:

代码语言:javascript
复制
"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"}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-10-15 10:26:27

使用writeToFile:options:error:而不是writeToFile:atomically:,这样就可以记录错误代码。

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

更新

根据您粘贴到问题中的错误,我猜您已经有一个名为/var/mobile/Applications/4B60A704-BE00-4160-BFB4-AD89187FADD1/Library/Caches/StoryCache/readability: feed的目录。您不能用文件覆盖目录。您需要先重命名或删除目录。您可以使用-[NSFileManager removeItemAtPath:error:]递归删除目录及其所有内容。

票数 21
EN

Stack Overflow用户

发布于 2017-01-01 06:12:49

我不小心把NSURL.absoluteString写成writeToFile:的参数。确保你做NSURL.path而不是NSURL.asboluteString!

票数 0
EN

Stack Overflow用户

发布于 2018-01-31 14:59:00

原因是"“,不允许字典的NSNull值保存到文件。在你的例子中:"summary = "";“所以,writetofail返回NO。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12888445

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档