1

在此处输入图像描述

我正在将 swift 1.2 转换为 swift 3.2 这里 init 显示错误任何建议

var context:NSManagedObjectContext
var psc:NSPersistentStoreCoordinator
var model:NSManagedObjectModel
var store:NSPersistentStore? = nil
var contextDict = [Int : NSManagedObjectContext]()
var results:NSBatchUpdateResult

init() {
    let bundle = Bundle.main
    let modelURL =
    bundle.url(forResource: "maildough", withExtension:"momd")
    model = NSManagedObjectModel(contentsOf: modelURL!)!
    psc = NSPersistentStoreCoordinator(managedObjectModel:model)
    context = NSManagedObjectContext(concurrencyType:.mainQueueConcurrencyType)
    context.persistentStoreCoordinator = psc
} ///getting error here

从初始化程序返回错误而不初始化所有存储的属性

4

1 回答 1

0

那你需要怎么做才能快速 3+

var errorHandler: (Error) -> Void = {_ in }

lazy var libraryDirectory: NSURL = {

    let urls = FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask)

    return urls[urls.count-1] as NSURL
}()


lazy var managedObjectModel: NSManagedObjectModel = {

    let modelURL = Bundle.main.url(forResource: "maildough", withExtension: "mom")
    return NSManagedObjectModel(contentsOf: modelURL!)!
}()


lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator = {
    let coordinator = NSPersistentStoreCoordinator(managedObjectModel:
        self.managedObjectModel)
    let url = 
self.libraryDirectory.appendingPathComponent("maildough.sqlite")
    do {
        try coordinator.addPersistentStore(ofType:
            NSSQLiteStoreType,
                                           configurationName: nil,
                                           at: url,
                                           options: [
                                            NSMigratePersistentStoresAutomaticallyOption: true,

 NSInferMappingModelAutomaticallyOption: true
            ]
        )
    } catch {
        // Report any error we got.
        NSLog("CoreData error \(error), \(error._userInfo)")
        self.errorHandler(error)
    }
    return coordinator
}()

lazy var managedObjectContext: NSManagedObjectContext = {
    let coordinator = self.persistentStoreCoordinator
    var mainManagedObjectContext = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
    mainManagedObjectContext.persistentStoreCoordinator = coordinator
    return mainManagedObjectContext
}()
于 2017-06-29T10:26:26.633 回答