我创建了一个基于页面的应用程序,该应用程序默认情况下不支持核心数据(创建项目时不显示该选项)。因此,我要做的是创建一个支持核心数据的任何类型的第二个项目,并复制并粘贴AppDelegate代码以支持核心数据。这里是:
func saveContext () {
var error: NSError? = nil
let managedObjectContext = self.managedObjectContext
if managedObjectContext != nil {
if managedObjectContext.hasChanges && !managedObjectContext.save(&error) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
//println("Unresolved error \(error), \(error.userInfo)")
abort()
}
}
}
// #pragma mark - Core Data stack
// Returns the managed object context for the application.
// If the context doesn't already exist, it is created and bound to the persistent store coordinator for the application.
var managedObjectContext: NSManagedObjectContext {
if !_managedObjectContext {
let coordinator = self.persistentStoreCoordinator
if coordinator != nil {
_managedObjectContext = NSManagedObjectContext()
_managedObjectContext!.persistentStoreCoordinator = coordinator
}
}
return _managedObjectContext!
}
var _managedObjectContext: NSManagedObjectContext? = nil
// Returns the managed object model for the application.
// If the model doesn't already exist, it is created from the application's model.
var managedObjectModel: NSManagedObjectModel {
if !_managedObjectModel {
let modelURL = NSBundle.mainBundle().URLForResource("Wink", withExtension: "momd")
_managedObjectModel = NSManagedObjectModel(contentsOfURL: modelURL)
}
return _managedObjectModel!
}
var _managedObjectModel: NSManagedObjectModel? = nil
// Returns the persistent store coordinator for the application.
// If the coordinator doesn't already exist, it is created and the application's store added to it.
var persistentStoreCoordinator: NSPersistentStoreCoordinator {
if !_persistentStoreCoordinator {
let storeURL = self.applicationDocumentsDirectory.URLByAppendingPathComponent("Wink.sqlite")
var error: NSError? = nil
_persistentStoreCoordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
if _persistentStoreCoordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: storeURL, options: nil, error: &error) == nil {
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
Typical reasons for an error here include:
* The persistent store is not accessible;
* The schema for the persistent store is incompatible with current managed object model.
Check the error message to determine what the actual problem was.
If the persistent store is not accessible, there is typically something wrong with the file path. Often, a file URL is pointing into the application's resources directory instead of a writeable directory.
If you encounter schema incompatibility errors during development, you can reduce their frequency by:
* Simply deleting the existing store:
NSFileManager.defaultManager().removeItemAtURL(storeURL, error: nil)
* Performing automatic lightweight migration by passing the following dictionary as the options parameter:
[NSMigratePersistentStoresAutomaticallyOption: true, NSInferMappingModelAutomaticallyOption: true}
Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details.
*/
//println("Unresolved error \(error), \(error.userInfo)")
abort();
}
}
return _persistentStoreCoordinator!
}
var _persistentStoreCoordinator: NSPersistentStoreCoordinator? = nil
我没有做任何改变。
现在,我想实例化一个NSManagedObject:
var appDelegate: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate;
var entity = NSEntityDescription.entityForName(ProfileDataEntityIdentifier, inManagedObjectContext: appDelegate.managedObjectContext);
var profileData = ProfileData(entity: entity, insertIntoManagedObjectContext: appDelegate.managedObjectContext);
当然,该方法调用AppDelegate方法,从而引发异常:
fatal error: unexpectedly found nil while unwrapping an Optional value
在return语句上的managedObjectModel getter方法中引发异常。在检查代码时,在我看来复制和粘贴代码是不够的,因为它似乎缺少了一些资源(URL指向某处)。那么如何创建此资源?我是否需要做其他任何事情以增加对核心数据的支持?
最佳答案
复制代码是不够的。核心数据需要一个数据模型,该模型告诉它如何配置数据。它基本上等同于SQL模式的Core Data,不同之处在于Core Data将其放在单独的文件中。这就是URL所指的-它试图找到一个表示模型的名为Wink.mom
的文件。但是,如果您的项目中没有模型文件,则该文件不存在。
您需要将一个文件添加到与该URL对应的文件。使用Xcode的新文件向导添加Core Data模型文件。给它一个名称,该名称与您要加载的URL相匹配。然后编辑数据模型文件以创建一些实体类型,以便您具有可使用的架构。
关于core-data - 基于页面的应用程序中的核心数据支持,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24657346/