在Objective-C(iPhone)中将字符串保存到文件中 [英] Saving a string into file in Objective-C (iPhone)

查看:180
本文介绍了在Objective-C(iPhone)中将字符串保存到文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎偶然发现了从字符串中保存xml文件的问题(这是在iPhone上完成的)
文件本身存在并包含在项目中(因此在工作区内),以及所有指示我从传递后的代码片段中获取没有任何错误的模拟器并在iPhone上失败(错误513),但在任何一种情况下都不保存文件!

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;

}

任何帮助将不胜感激,
-A

Any help would be appreciated, -A

推荐答案

您不应该写入应用程序包中包含的文件。在真正的iPhone上,你可能会被禁止这样做,因为这些文件是经过数字签名的。

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.

即使你可以修改打包的文件,它也不是一个好的地方。存储数据。从App Store升级或Xcode构建重新安装应用程序将使用原始文件覆盖该文件。

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.

而是将XML存储到Documents目录中。你可以得到这样的路径:

Instead, store your XML into the Documents directory. You can get the path like this:

NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
    NSUserDomainMask, YES); 
NSString* documentsDirectory = [paths objectAtIndex:0];     
NSString* leafname = [m_FileName stringByAppendingFormat: @".xml" ]; 
NSString* filenameStr = [documentsDirectory
    stringByAppendingPathComponent:leafname];

如果您的文件需要一些您不希望在代码中生成的初始状态,请让您的app在第一次需要时检查它是否存在于文档目录中,如果缺少,则从包中的模板中复制它。

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:

[[NSUserDefaults standardUserDefaults] setObject:foo forKey:FOO_KEY];

这篇关于在Objective-C(iPhone)中将字符串保存到文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆