iPhone pathForResource 与 BundlePath

发布于 2024-09-06 15:43:58 字数 624 浏览 1 评论 0原文

我想使用 pathForResource,但如果路径不存在,它看起来不会创建路径。因此,我尝试通过执行以下操作来手动创建一个文件:

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

我正在动态创建文件,因此我需要在构建并运行应用程序后访问它们。但它将项目放在一个唯一的 id 文件夹中,因此路径如下所示:

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

但每次我进行构建时,该唯一的 id 都会发生变化。创建每次都可以到达的路径(即使在模拟器中)的正确方法是什么?

谢谢。

更新:编辑了问题,希望对将来遇到这个问题的人有所帮助。

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

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"];

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

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)?

Thanks.

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

萌化 2024-09-13 15:43:58

按照您表达问题的方式,这就是您在构建之前读取已包含在捆绑包中的 plist 的方式:

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

如果您想访问每个应用程序所具有的目录,作为构建后创建的文件的唯一存储区域,你使用这个:

#define kFilename   @”data.plist”

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

return [documentsDirectory stringByAppendingPathComponent:kFilename];
}

然后你可以检查它并在这里做一些数据处理:

NSString *filePath = [self dataFilePath]; 

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

如果你购买/阅读开始iPhone 3开发,你会省去很多麻烦,特别是第11章,他在其中讨论了数据持久性(这就是这个例子的来源)。这是一本很棒的书。

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"];

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
}

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.

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