在 Objective-C (iPhone) 中将字符串保存到文件中

发布于 2024-07-30 20:47:12 字数 1040 浏览 3 评论 0原文

我似乎偶然发现了一个关于从字符串保存 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼睛会笑 2024-08-06 20:47:12

您不应写入应用程序包中包含的文件。 在真正的 iPhone 上,您可能无法执行此操作,因为这些文件经过数字签名。

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

相反,请将 XML 存储到 Documents 目录中。 您可以获取如下路径:

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

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

存储结构化数据的另一种方法是使用用户默认值。 例如:

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

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:

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

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文