I'm stuck trying to add Core Data to my existing iOS project. I don't have an existing sql database but I created a data model. I followed the following tutorial: http://wiresareobsolete.com/wordpress/2009/12/adding-core-data-existing-iphone-projects/
It produces an error at the following code:
(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;
}
I get the following error:
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";}
I really have no idea why it crashes and how I can resolve it. If more information is needed to help please let me know.
storeUrl. The error code most probably means that the directory in which you want to create the SQLite file does not exist.