【问题标题】:Compiling an XML file into a binary将 XML 文件编译为二进制文件
【发布时间】:2009-04-23 02:02:18
【问题描述】:

我想在 iPhone 应用程序中解析和 XML 文件,而不用去磁盘。 我可以这样做吗?如何?我在一个使用 MSVC 的应用程序中包含了一个 TXT 帮助文件。

我将 XML 文件放在项目中名为 Resources 的文件夹/组中。
我在 proj 目录中有 XML 文件。
我右键单击资源文件夹并选择添加 -> 现有文件
我右键单击 XML 文件并选择 GetInfo

我尝试更改路径类型 {Absolute, Relative to Project 等}
当我使用时,我的程序在模拟器上运行良好:

NSString * const DG_XmlRecipeFile = @"/Users/appleuser/Cocoa/iHungry6/Recipes.xml";

在我看来它也应该适用于:

NSString * const DG_XmlRecipeFile = @"Recipes.xml";

如果我正确设置了路径类型。它没有。

我是第一个计时器。感谢您阅读本文,马克

【问题讨论】:

    标签: iphone objective-c file resources


    【解决方案1】:

    Xcode 将项目资源复制到应用程序包。您可以按如下方式访问捆绑包中的文件:

    NSString *DG_XmlRecipeFile = [[NSBundle mainBundle] pathForResource:@"Recipes" ofType:@".xml"];
    

    包中的文件是只读的。如果要修改文件,则需要将其复制到可以修改的位置。您应用的 Documents 目录非常适合此操作。

    NSArray  *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docsDirectory = [[paths objectAtIndex:0] retain];
    NSString *newFilePath = [docsDirectory stringByAppendingPathComponent:@"Recipes.xml"];    
    NSError  *error = nil;
    
    [[NSFileManager defaultManager] copyItemAtPath:DG_XmlRecipeFile toPath:newFilePath error:&error];
    

    【讨论】:

    • 这很棒。谢谢你的时间。很高兴有你们在场。
    • 您的回答将我引向以下内容: NSString *DG_XmlRecipeFile = [[NSBundle mainBundle] pathForResource:@"DG_Recipes" ofType:@"xml"];您不仅解决了我的问题,还让我更好地了解了 iPhone 处理与平台无关的路径,同时尝试访问 Project Resources Group 文件夹中引用的文件的副本。我非常欠你的债。非常感谢,马克
    【解决方案2】:

    我不认为您所指的路径类型是生成的应用程序包中资源的路径。这是在 .proj 文件中引用文件的方式。

    【讨论】:

    • 感谢您的回答。感谢您抽出宝贵时间。
    猜你喜欢
    • 1970-01-01
    • 2017-02-20
    • 2011-01-09
    • 1970-01-01
    • 2018-11-08
    • 2013-01-03
    • 2017-07-29
    • 2011-03-19
    • 1970-01-01
    相关资源
    最近更新 更多