在 Objective-C (iPhone) 中将字符串保存到文件中
我似乎偶然发现了一个关于从字符串保存 xml 文件的问题(这是在 iPhone 上完成的) 该文件本身存在并包含在项目中(因此在工作区中),并且我从后面的代码片段中获得的所有指示都在模拟器上通过,没有任何错误,但在 iPhone 上失败(错误 513),但在任何一种情况下,文件没有保存!
{
Hits = config->Hits;
NSString* filenameStr = [m_FileName stringByAppendingFormat: @".xml" ];
NSString* pData = [self getDataString]; // write xml format - checked out ok
NSError *error;
/* option 2 - does not work as well
NSBundle *mainBundle = [NSBundle mainBundle];
NSURL *xmlURL = [NSURL fileURLWithPath:[mainBundle pathForResource: m_FileName ofType: @"xml"]];
if(![pData writeToURL: xmlURL atomically: true encoding:NSUTF8StringEncoding error:&error])
{
NSLog(@"Houston - we have a problem %s@\n",[error localizedFailureReason]);
return false;
}
*/
if(![pData writeToFile: filenameStr atomically: FALSE encoding:NSUTF8StringEncoding error:&error])
{
NSLog(@"Houston - we have a problem %s@\n",[error localizedFailureReason]);
return false;
}
return true;
}
任何帮助,将不胜感激, -A
I seem to have stumbled over a problem regarding saving an xml file from a string (this is done on the iPhone)
The file itself exists and included in the project (hence within the workspace), and all indications I get from the code snippet which follows passes without any errors on the emulator and fail on the iPhone (error 513), but in either case the file is not saved!
{
Hits = config->Hits;
NSString* filenameStr = [m_FileName stringByAppendingFormat: @".xml" ];
NSString* pData = [self getDataString]; // write xml format - checked out ok
NSError *error;
/* option 2 - does not work as well
NSBundle *mainBundle = [NSBundle mainBundle];
NSURL *xmlURL = [NSURL fileURLWithPath:[mainBundle pathForResource: m_FileName ofType: @"xml"]];
if(![pData writeToURL: xmlURL atomically: true encoding:NSUTF8StringEncoding error:&error])
{
NSLog(@"Houston - we have a problem %s@\n",[error localizedFailureReason]);
return false;
}
*/
if(![pData writeToFile: filenameStr atomically: FALSE encoding:NSUTF8StringEncoding error:&error])
{
NSLog(@"Houston - we have a problem %s@\n",[error localizedFailureReason]);
return false;
}
return true;
}
Any help would be appreciated,
-A
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不应写入应用程序包中包含的文件。 在真正的 iPhone 上,您可能无法执行此操作,因为这些文件经过数字签名。
即使可以修改打包文件,它也不是存储数据的好地方。 从 App Store 升级或 Xcode 版本重新安装应用程序将会用原始文件覆盖该文件。
相反,请将 XML 存储到 Documents 目录中。 您可以获取如下路径:
如果您的文件需要一些您不想在代码中生成的初始状态,请让您的应用程序在第一次需要时检查它是否存在于文档目录中,如果存在缺少,请从包中的模板中复制它。
存储结构化数据的另一种方法是使用用户默认值。 例如:
You should not write to files included in the application package. On a real iPhone, you may be prevented from doing this because these files are digitally signed.
Even if you can modify a packaged file, it is not a good place to store data. Re-installing the application from an App Store upgrade or an Xcode build will overwrite the file with the original.
Instead, store your XML into the Documents directory. You can get the path like this:
If your file needs some initial state that you don't want to generate in your code, have your app check that it is present in the documents directory the first time it is needed and, if it is missing, copy it from the template in the package.
An alternative to storing structured data is to use user defaults. For example: