我将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
在ä¸åˆå§‹åŒ–所有å˜å‚¨å±žæ€§çš„情况下从åˆå§‹åŒ–程åºè¿”回错误
ç”案 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
}()