核心数据:ManagedObjectContext为Null

时间:2016-09-15 23:05:56

标签: swift core-data

在Swift中,我正在尝试保存到Core数据并收到此错误:

" + entityForName:nil不是用于搜索实体名称的合法NSPersistentStoreCoordinator"

查看以前的SO答案,似乎managedObjectContext为null。

我不明白为什么它是空的。我把它放在IBAction里面然后再把它传入。

查看控制器

var appDelegate: AppDelegate!
var managedContext: NSManagedObjectContext!

*IBAction* {
        appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        managedContext = NSManagedObjectContext(concurrencyType: .MainQueueConcurrencyType)

        let est = EstablishmentCoreData(context: managedContext)          
}

EstablishmentCoreData.swift

class EstablishmentCoreData: NSManagedObject {
    // Insert code here to add functionality to your managed object subclass
    init (context: NSManagedObjectContext) {        
        if let ent = NSEntityDescription.entityForName("EstablishmentCoreData", inManagedObjectContext: context) {
            super.init(entity: ent, insertIntoManagedObjectContext: context)
            self.title = "Title"          
        }
        else {
            fatalError("ERROR")
        }
    }
}

EstablishmentCoreData + CoreDataProperties.swift

import Foundation
import CoreData

extension EstablishmentCoreData {
    @NSManaged var title: String?
}

0 个答案:

没有答案