Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Core data 基于页面的应用程序中的核心数据支持_Core Data_Swift_Xcode6 - Fatal编程技术网

Core data 基于页面的应用程序中的核心数据支持

Core data 基于页面的应用程序中的核心数据支持,core-data,swift,xcode6,Core Data,Swift,Xcode6,我创建了一个基于页面的应用程序,默认情况下不支持核心数据(创建项目时不显示该选项)。所以,我所做的是创建第二个项目,不管它是什么类型,都支持核心数据,并复制&通过核心数据支持的AppDelegate代码。这是: func saveContext () { var error: NSError? = nil let managedObjectContext = self.managedObjectContext if managedObjectContext != nil

我创建了一个基于页面的应用程序,默认情况下不支持核心数据(创建项目时不显示该选项)。所以,我所做的是创建第二个项目,不管它是什么类型,都支持核心数据,并复制&通过核心数据支持的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方法,而AppDelegate方法又会引发异常:

 fatal error: unexpectedly found nil while unwrapping an Optional value

在managedObjectModel getter方法的return语句中引发异常。检查代码,在我看来,复制和粘贴代码是不够的,因为它似乎缺少一些资源(URL指向某个地方)。那么我如何创建这个资源呢?我还需要做些什么来添加核心数据支持吗?

复制代码是不够的。核心数据需要一个数据模型,告诉它如何配置数据。它基本上相当于SQL模式的核心数据,只是核心数据放在单独的文件中。这就是URL所指的——它试图找到一个名为
Wink.mom
的文件来表示模型。但是,如果项目中没有模型文件,则该文件不存在


您需要向项目中添加一个与此URL对应的文件。使用Xcode的新文件向导添加核心数据模型文件。为它指定一个与您尝试加载的URL匹配的名称。然后编辑数据模型文件以创建一些实体类型,这样您就可以使用模式了。

我有一个这样的文件,但它被称为模型。当你提到Wink.momd时,我意识到了这个问题;不过,我应该注意到这一点。将代码更改为参考Model.momd已工作!非常感谢。