iPhone pathForResource 与 BundlePath
我想使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
按照您表达问题的方式,这就是您在构建之前读取已包含在捆绑包中的 plist 的方式:
如果您想访问每个应用程序所具有的目录,作为构建后创建的文件的唯一存储区域,你使用这个:
然后你可以检查它并在这里做一些数据处理:
如果你购买/阅读
开始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:
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:
Then you can check for it and do some data handling 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.