iPhone pathForResource与bundlePath [英] iPhone pathForResource vs bundlePath

查看:44
本文介绍了iPhone pathForResource与bundlePath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用pathForResource,但是如果不存在路径,它似乎不会创建路径.因此,我尝试通过执行以下操作手动创建一个:

I want to use pathForResource, but it doesn't look like it will create the path if one doesn't exist. Therefore I'm trying to create one manually by doing the following:

 NSString *path = [NSString stringWithFormat:@"%@/%@.plist",[[NSBundle mainBundle] bundlePath],@"myFileName"];

我正在动态创建文件,因此我需要在 Build and Run 应用程序后访问它们.但这会将项目放在唯一的id文件夹中,因此路径如下所示:

I'm creating files dynamically, so I need to access them after I have Build and Run the application. But it puts the project in a unique id folder so the path comes out to something like:

/Users/RyanJM/Library/Application Support/iPhone Simulator/3.0/Applications/80986747-37FD-49F3-9BA8-41A42AF7A4CB/MyApp.app/myFileName.plist

但是每次我进行构建时,唯一ID都会更改.创建我每次都能到达的路径的正确方法是什么(即使在模拟器中)?

But that unique id changes every time I do a build. What is the proper way to create a path that I can get to every time (even in the Simulator)?

谢谢.

更新:修改了问题,希望以后可以帮助遇到此问题的任何人.

Update: edited the question, hopefully to help anyone who comes across it in the future.

更新:IWasRobbed回答了创建路径URL的正确方法.但是我能找到的最好的答案是来自

Update: IWasRobbed answered the proper way to get create a path URL. But the the best answer I've been able to find is from Brad Parks. Though, I do wish there was a cleaner way.

推荐答案

用您表达问题的方式,这就是您在构建之前如何阅读捆绑软件中包含的plist的方法:

With the way you phrased your question, this is how you read a plist that has been included in the bundle before build:

NSString *propertyListPath = [[NSBundle mainBundle] pathForResource:SomeString ofType:@"plist"];

如果要访问每个应用程序所具有的目录作为AFTER构建后创建的文件的唯一存储区,请使用以下方法:

If you want to access the directories that each app has as a unique storage area for a file that you create AFTER build, you use this:

#define kFilename   @"data.plist"

- (NSString *)dataFilePath { 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; 

return [documentsDirectory stringByAppendingPathComponent:kFilename];
}

然后您可以在此处检查并进行一些数据处理:

Then you can check for it and do some data handling here:

NSString *filePath = [self dataFilePath]; 

if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
// do something with data here
}

如果您购买/阅读了 iPhone 3开发入门,尤其是第11章,他在书中介绍了数据持久性(这就是本例的来源),则可以为您省去很多麻烦.这是一本很棒的书.

You would save yourself a lot of trouble if you bought/read Beginning iPhone 3 Development specifically chapter 11 where he goes over data persistence (which is where this example came from). It's a great book.

这篇关于iPhone pathForResource与bundlePath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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