[英]writeToFile atomically returns NO
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: 但是对于以下字典,它返回NO:
{
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: 但它适用于以下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"}
Use writeToFile:options:error:
instead of writeToFile:atomically:
so you can log the error code. 使用
writeToFile:options:error:
而不是writeToFile:atomically:
所以你可以记录错误代码。
NSError *error;
success = [plistData writeToFile:file options:0 error:&error];
if (!success) {
NSLog(@"writeToFile failed with error %@", error);
}
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
. 根据您粘贴到问题中的错误,我猜您已经有一个名为
/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. 您可以使用
-[NSFileManager removeItemAtPath:error:]
以递归方式删除目录及其所有内容。
I accidentally wrote NSURL.absoluteString as the argument to writeToFile:. 我不小心写了NSURL.absoluteString作为writeToFile的参数:。 Make sure you do NSURL.path not NSURL.asboluteString!!!!!
确保你做的NSURL.path不是NSURL.asboluteString !!!!!
The reason is "" and NSNull value of Dictionary is not allowed to save to file. 原因是“”和Dictionary的NSNull值不允许保存到文件。 In your case :"summary = "";"
在你的情况下:“summary =”“;” so, writetofail return NO.
所以,writetofail返回NO。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.