Xcode在构建时更改应用程序文件夹的位置 [英] Xcode changes location of applications folder on build
问题描述
我将.mp3存储在我的iOS应用程序文档目录中,并保存Core Data的路径。当我重建应用程序Xcode似乎将应用程序数据移动到不同的目录,从而使存储的路径无效。为什么会发生这种情况,以及关于保存文件路径的最佳做法是什么?
I am storing an .mp3 in my iOS apps documents directory and saving the path to Core Data. When I rebuild the app Xcode seems to move the application data to a different directory thus making the stored path invalid. Why is this happening and what are the best practices around saving file paths?
我正在使用Xcode 6.2,并且已成功检索过去的文件。
I am using Xcode 6.2 and have been successful retrieving the file in the past.
文件位置下载:
in ~/Library/Developer/CoreSimulator/Devices/82D1931C-590D-45A2-AB9B-8D1D4F2530C5/data/Containers/Data/Application/
$ find . -iname p612.mp3
./1C070BF8-0E2E-4EAC-99B6-C56E48675E6E/Documents/adf07cf85254e8f28f942f2d6fa704ae/p612.mp3
重建后文件的位置:
in ~/Library/Developer/CoreSimulator/Devices/82D1931C-590D-45A2-AB9B-8D1D4F2530C5/data/Containers/Data/Application/
$ find . -iname p612.mp3
./E6C7D0AF-E61C-4BDD-AF4B-68C445E2BB0D/Documents/adf07cf85254e8f28f942f2d6fa704ae/p612.mp3
推荐答案
由于 iOS 8.0 ,每次重建代码时,应用程序文件夹的名称(包含您的文档和库目录)变化。这是预期的行为。
Since iOS 8.0, every time you rebuild your code, the name of the application folder (which contains your documents and library directories) changes. This is the intended behaviour.
所以如果你想存储一个路径,你应该只存储文件名,然后将它与文档目录的位置即时组合。这样,它总是指向正确的位置。
So if you want to store a path, you should only store the filename, and then combine it with the location of the documents directory on the fly. This way, it will always point to the correct location.
文档目录的内容将保留,即使它所在的文件夹的名称将从build更改为build。
The contents of the documents directory will persist even though the name of the folder in which it resides will change from build to build.
// Returns the URL to the application's Documents directory.
- (NSURL *)applicationDocumentsDirectory
{
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
请参阅: 技术说明TN2406 。
See: Technical Note TN2406.
这篇关于Xcode在构建时更改应用程序文件夹的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!