beta - 二元运算符 '&&' 不能应用于两个 Bool 操作数

标签 beta swift2 xcode7

我最近将 Xcode 更新到了新的 7.0 beta。

我使用助手进行了迁移,但还有一些问题。

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 并捕获它们。

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

关于beta - 二元运算符 '&&' 不能应用于两个 Bool 操作数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30822070/

相关文章:

ios - 准备(对于segue : UIStoryboardSegue,发件人: AnyObject?)在swift 3.0/Xcode 8 b6中丢失

ios - Xcode 7 Beta 6,dyld ___NSArray0__ 崩溃

swift - 继续在 Swift 2.2 上显示错误,但可以成功构建

ios - 快速可选类型做什么

swift - 编译 Swift 资源卡在 Xcode 7.0.1 中

ios - 如何在 iOS 11 中以编程方式打开 pdf 标记

timeout - Sails v0.11.0-rc10 资源加载超时

ios - swift2.0,从 REST 调用返回信息(Alamofire)

ios - 在 for-in 循环中更改数组的名称

c++11 - Apple clang-703.0.29 如何在 C++1x 支持方面映射回 clang 版本?