在将核心数据添加到项目时无法添加sql文件

Ste*_*lis 6 sqlite core-data ios

我试图将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;
}
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

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";}
Run Code Online (Sandbox Code Playgroud)

我真的不知道它崩溃的原因以及如何解决它.如果需要更多信息,请告诉我.

Cha*_*ska 16

我正在按照几乎相同的教程并收到相同的错误消息.我想出了问题所在.

就我而言,无法创建文件,因为storeURL路径不正确并指向不存在的文件夹.

在我的帮助方法[self applicationDocumentDirectory](你没有在你的问题中提供)我输入示例代码就像在教程中一样:

- (NSString *) applicationDocumentsDirectory
{
    return [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES) lastObject];
}
Run Code Online (Sandbox Code Playgroud)

它产生了URL file:///Users/xxxxxx/Library/Application%20Support/iPhone%20Simulator/7.0.3/Applications/A6FDD8DB-475B-4C9B-B995-28CCE068DF82/库/文档/

在那里有一个愚蠢的代码完成拼写错误,它会生成一个无效的路径.你能看见它吗?

它是NSSearchPathDirectory输入参数的枚举NSDocumentationDirectory,它应该是NSDocumentDirectory.

正确的代码和URL是:

- (NSString *) applicationDocumentsDirectory
{
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject;
}
Run Code Online (Sandbox Code Playgroud)

网址:file:///Users/xxxxxx/Library/Application%20Support/iPhone%20Simulator/7.0.3/Applications/A6FDD8DB-475B-4C9B-B995-28CCE068DF82/文件/