【发布时间】:2015-06-09 10:06:11
【问题描述】:
我刚刚下载了新的 Xcode 7.0 测试版并从 Swift 1.2 迁移到 Swift 2。迁移显然没有改变整个代码,实际上方法 saveContext() 很好,直到为该行抛出 2 个错误:
if moc.hasChanges && !moc.save() {
二元运算符 '&&' 不能应用于两个 Bool 操作数
和
调用可以抛出,但是没有标记'try'并且错误没有处理
方法如下:
// MARK: - Core Data Saving support
func saveContext () {
if let moc = self.managedObjectContext {
var error: NSError? = nil
if moc.hasChanges && !moc.save() {
// Replace this implementation 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()
}
}
}
关于如何让它工作的任何想法?
【问题讨论】: