我刚刚下载了新的 Xcode 7.0 beta 并从 Swift 1.2 迁移到了 Swift 2。迁移显然没有改变整个代码,实际上方法 saveContext() 很好,直到为该行抛出 2 个错误:
if moc.hasChanges && !moc.save() {
二元运算符 '&&' 不能应用于两个 Bool 操作数
和
call 可以抛出,但是没有标记 '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()
}
}
}
关于如何让它工作的任何想法?