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

查看:356
本文介绍了如何在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的一个名为<$ c的组中$ c> 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 file。

  • The generated directory is incorrect.
  • The tile images aren't getting included in the builded .app file.

无论哪种方式,我都不知道如何解决它。

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

我该如何解决这个问题?

How can I solve this problem?

我将这段代码更改为:

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

现在它在模拟器中工作,因为所有文件都在apps根文件夹中,我不问它进入另一个名为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天全站免登陆