二元运算符“&&”不能应用于两个 Bool 操作数

Binary operator '&&' cannot be applied to two Bool operands

我最近将 Xcode 更新到新的 7.0 测试版。

我用助手完成了迁移,但还有一些问题。

func saveContext () {
    if let moc = self.managedObjectContext {
        var error: NSError? = nil
        if moc.hasChanges && !moc.save() {
            NSLog("Unresolved error \(error), \(error!.userInfo)")
            abort()
        }
    }
}

第 4 行有 4 个问题: 第一个是:

Binary operator '&&' cannot be applied to two Bool operands

第二个是:

Call can throw, but it is not marked with 'try' and the error is not handled

有人可以帮助我吗?

下面是一些应该可以解决问题的代码。请记住在 throw 语句之前加上 try 和 catch。

func saveContext () {
    if let moc = self.managedObjectContext {
        if moc.hasChanges  {
            do {
                try moc.save()
            } catch {
                NSLog("Unresolved error \(error)")
                abort()
            }
        }
    }
}