如何在 iPhone 应用程序中获取文件路径 [英] How to get file path in iPhone app

查看:53
本文介绍了如何在 iPhone 应用程序中获取文件路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用中访问我的文件时遇到问题.

I have a problem accessing my files in my app.

我目前正在使用

//Directly from TileMap example from WWDC2010
NSString *tileDirectory = [[[NSBundle mainBundle] resourcePath] 
                           stringByAppendingPathComponent:@"Tiles"];

访问我的 MKOverlay 的磁贴.这给了我这个目录

to access my tiles for my MKOverlay. This gives me this directory

/Users/xxxx/Library/Application Support/iPhone Simulator/4.2/Applications/9D62025C-C53B-472C-8309-xxxx/xxxx.app/Tiles

/Users/xxxx/Library/Application Support/iPhone Simulator/4.2/Applications/9D62025C-C53B-472C-8309-xxxx/xxxx.app/Tiles

x 仅出于隐私原因

我的磁贴位于我的应用程序根目录中名为 Tiles 的文件夹中,该文件夹位于 Xcode 中名为 Tiles 的组中,该组直接位于 资源组.

I have my tiles in a folder called Tiles in the root of my application which is in Xcode in a group called Tiles which is in directly in the Resources group.

当我运行我的应用程序时,我收到一个简单的错误,说它在生成的目录中找不到我的磁贴(上面的一个引用)如果我替换那段代码并使其成功:

When I run my app, I get a simple error saying that it could not find my tiles at the generated directory (the one quote above) If I replace that piece of code and make it:

NSString *tileDirectory = @"/Users/xxxx/Documents/xxxx/Tiles";

然后我的应用程序运行良好.这显然是因为它在我的 Mac 上的直接位置找到了我的磁贴.这对于测试来说很好,但我需要它在我的 iPhone/iPad 上运行.

Then my app works fine. This is obviously because it finds my tiles in its direct location on my Mac. This is fine for testing, but I need it to work on my iPhone/iPad.

出现此问题的原因可能是:

This problem might be occurring due to:

  • 生成的目录不正确.
  • 磁贴图像未包含在构建的 .app 文件中.

无论如何,我都不知道该怎么做才能解决它.

Either way, I have no clue of what to do to solve it.

我该如何解决这个问题?

How can I solve this problem?

我把那段代码改成:

NSString *tileDirectory = [[NSBundle mainBundle] resourcePath];

现在它可以在模拟器中运行,因为所有文件都在应用程序根文件夹中,我不要求它进入另一个名为Tiles"的目录.

Now it works in simulator, because all files are in the apps root folder and I don't ask for it to enter another directory called "Tiles".

这在模拟器上运行没有错误,但是在我的 iPhone 上它给出了原始错误(只是一个不同的文件路径,但也以 /xxxx.app

This runs with no error on the simulator, but when on my iPhone it gives the original error (just a different file path but also ending with /xxxx.app

如何确保我的应用程序文件中有一个目录,例如 xxxx.app/Tiles - TileMap 可以做到这一点.

How can I ensure a directory in my app file such as xxxx.app/Tiles - TileMap does this.

推荐答案

请记住,您在 xcode 中创建的文件夹/组",黄色的那些不会在您的 iPhone 应用程序中反映为真正的文件夹.他们只是在那里构建您的 XCode 项目.您可以根据需要嵌套任意数量的黄色组,但它们仍然仅用于在 XCode 中组织代码的目的.

Remember that the "folders/groups" you make in xcode, those which are yellowish are not reflected as real folders in your iPhone app. They are just there to structure your XCode project. You can nest as many yellow group as you want and they still only serve the purpose of organizing code in XCode.

在 XCode 之外创建一个文件夹,然后将其拖过来,然后在弹出窗口中选择为任何添加的文件夹创建文件夹引用"而不是为任何添加的文件夹创建组".

Make a folder outside of XCode then drag it over, and select "Create folder references for any added folders" instead of "Create groups for any added folders" in the popup.

这篇关于如何在 iPhone 应用程序中获取文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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