没有åˆå§‹åŒ–所有存储属性的错误从åˆå§‹åŒ–程åºè¿”回

时间:2017-06-29 10:05:50

标签: ios swift xcode swift3 swift3.2

enter image description here

我将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

在ä¸åˆå§‹åŒ–所有存储属性的情况下从åˆå§‹åŒ–程åºè¿”回错误

1 个答案:

答案 0 :(得分:0)

你需è¦å¦‚何为swift 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
}()