我创建了一个Core Data应用程序,并希望将file.sqlite保存到与默认目录不同的目录中。我偶然发现了这个SO thread,然后发布了this SO question,项目似乎正在完成,但我的file.sqlite没有保存在我指定的位置。我创建了一个方法,并在AppDelegate.m文件中添加了一些代码,但是storeURL变量仍然将file.sqlite保存在默认的"Documents目录“中。如何调用我在AppDelegate.m文件中创建的方法来指定storeURL变量的正确位置?
我是不是该改变一下,
NSURL *storeURL = [NSURL fileURLWithPath: [docPath stringByAppendingPathComponent:@"Accounts.sqlite"]];至
NSURL *storeURL = [NSURL fileURLWithPath: [documentsDirectoryPath stringByAppendingPathComponent:@"Accounts.sqlite"]];这也会破坏iOS模拟器的支持吗?
发布于 2012-08-13 01:35:41
通过保留模拟器的默认文档路径,然后为设备设置不同的文档路径,我解决了这个问题。下面的代码在我的AppDelegate.m文件中
// add conditional code for simulator and iDevice
#if TARGET_IPHONE_SIMULATOR
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docPath = [documentPaths objectAtIndex:0];
NSURL *storeURL = [NSURL fileURLWithPath: [docPath stringByAppendingPathComponent:@"Accounts.sqlite"]];
#else
// jailbroken path - /var/mobile/Library/KegCop/
NSString *docPath = self.documentsDirectoryPath;
NSURL *storeURL = [NSURL fileURLWithPath: [docPath stringByAppendingPathComponent:@"Accounts.sqlite"]];
#endifhttps://stackoverflow.com/questions/11836775
复制相似问题