当我使用我从App Store下载的映射模型在应用程序上测试迁移时,它会成功迁移,但如果我在迁移之前更改数据时尝试这样做,则会发生以下错误:
2015-10-22 10:50:27.761 CREW Emergency Preparedness [240:5906] *因未捕获的异常终止应用程序' NSRangeException',原因:' * - [__ NSArray0 objectAtIndex:]:索引0超出空NSArray的范围' ***第一次抛出调用堆栈: (0x1838d4f5c 0x1984cbf80 0x183850b60 0x10039b9e0 0x10039aa9c 0x1003ae214 0x1000eed48 0x100151274 0x1001517c0 0x188e2b610 0x188ee87e8 0x188ee86cc 0x188ee7920 0x188ee74e4 0x188ee710c 0x188ee7074 0x188e26ff0 0x18862df14 0x188628b20 0x1886289e0 0x18862807c 0x188627dd0 0x1886214bc 0x18388bc30 0x1838899d4 0x1837b8de0 0x188e980ac 0x188e92f44 0x1001273d0 0x198cf68b8) libc ++ abi.dylib:以NSException类型的未捕获异常终止
它崩溃的声明是:
try coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: [NSMigratePersistentStoresAutomaticallyOption:true])
如何完成此迁移?
我没有包含其余的代码,因为我刚刚将该语句插入到标准的appDelegate.swift代码中,但这里是:
lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator? = {
// The persistent store coordinator for the application. This implementation creates and return a coordinator, having added the store for the application to it. This property is optional since there are legitimate error conditions that could cause the creation of the store to fail.
// Create the coordinator and store
var coordinator: NSPersistentStoreCoordinator? = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("CREWData.sqlite")
var error: NSError? = nil
var failureReason = "There was an error creating or loading the application's saved data."
do {
try coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: [NSMigratePersistentStoresAutomaticallyOption:true, NSInferMappingModelAutomaticallyOption: true])
} catch var error1 as NSError {
error = error1
coordinator = nil
// Report any error we got.
var dict = [String: AnyObject]()
dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data"
dict[NSLocalizedFailureReasonErrorKey] = failureReason
dict[NSUnderlyingErrorKey] = error
error = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict)
// Replace this 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.
NSLog("Unresolved error \(error), \(error!.userInfo)")
abort()
} catch {
fatalError()
}
return coordinator
}()