【发布时间】:2012-10-25 12:00:16
【问题描述】:
我一直在尝试将 Core Data 添加到我现有的 iOS 项目中。我没有现有的 sql 数据库,但我创建了一个数据模型。我按照以下教程进行操作:http://wiresareobsolete.com/wordpress/2009/12/adding-core-data-existing-iphone-projects/
它会在以下代码中产生错误:
(NSPersistentStoreCoordinator *) persistentStoreCoordinator{
if (persistentStoreCoordinator != nil){
return persistentStoreCoordinator;
}
NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentDirectory]
stringByAppendingPathComponent: @"MyPOC.sqlite"]];
NSError *error = nil;
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc]
initWithManagedObjectModel:[self managedObjectModel]];
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error]){
NSLog(@"Unresolved error OH NO %@, %@", error, [error userInfo]);
}
return persistentStoreCoordinator;
}
我收到以下错误:
2012-10-25 13:52:29.156 MyPOC[1994:11603] Unresolved error OH NO Error
Domain=NSCocoaErrorDomain Code=512 "The operation couldn’t be completed.
(Cocoa error 512.)" UserInfo=0x8246240 {reason=Failed to create file; code = 2},
{reason = "Failed to create file; code = 2";}
我真的不知道它为什么会崩溃以及如何解决它。如果需要更多信息来帮助,请告诉我。
【问题讨论】:
-
检查此问题的已接受答案:stackoverflow.com/questions/6848394/…
-
您应该检查
storeUrl的值。错误代码很可能意味着您要在其中创建 SQLite 文件的目录不存在。