Core data CoreData“;验证存储“的url”时出错;
我在应用程序中遇到了一个问题,CoreData在模拟器中正常工作,但在设备上没有 我收到一封信Core data CoreData“;验证存储“的url”时出错;,core-data,ios,Core Data,Ios,我在应用程序中遇到了一个问题,CoreData在模拟器中正常工作,但在设备上没有 我收到一封信 2010-09-30 12:45:07.500 CoreDataTutorial_iOS[130:307] Unresolved error Error Domain=NSCocoaErrorDomain Code=513 "The operation couldn’t be completed. (Cocoa error 513.)" UserInfo=0x1412a0 {NSUnderlyingE
2010-09-30 12:45:07.500 CoreDataTutorial_iOS[130:307] Unresolved error Error Domain=NSCocoaErrorDomain Code=513 "The operation couldn’t be completed. (Cocoa error 513.)" UserInfo=0x1412a0 {NSUnderlyingException=Error validating url for store}, {
NSUnderlyingException = "Error validating url for store";
我在此函数中调用PersistentStoreCoordinator(抛出上述错误):
我正在“objc_exception_throw”上设置一个断点,以查看aStoreURL是什么,它是:
file://localhost/var/mobile/Applications/BE9A2982-BDC3-405D-A201-FB78E9E0790B/Documentscorebase.sqlite
我注意到它本身并没有在“/文档”之后添加最后的“/”。
当我以这种方式创建URL时
NSURL *aStoreURL = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingFormat:@"/corebase.sqlite"]];
它似乎起了作用,或者至少通过了那个部分。
这个函数不应该附加那个部分本身吗
-(NSString*) applicationDocumentsDirectory
{
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
}
它在模拟器中运行良好,正确的做法是什么
NSURL *aStoreURL = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory]
stringByAppendingFormat: @"corebase.sqlite"]];
应该是
NSURL *aStoreURL = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory]
stringByAppendingPathComponent: @"corebase.sqlite"]];
stringByAppending*PathComponent*而不是stringByAppending*Format*。
这个可爱的小虫子是自动完成带给你的:-)
为什么它在模拟器中工作?我猜是因为你可以在硬盘上的任何地方创建文件。因此,模拟器在应用程序目录中创建了Documentscorebase.sqlite。你应该检查一下它是否在那里
在iphone上,您仅限于Documents目录,不允许在任何地方创建文件
NSURL *aStoreURL = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory]
stringByAppendingPathComponent: @"corebase.sqlite"]];