在Swift 2.0中,do {} catch不会处理从这里抛出的错误 [英] Errors thrown from here are not handled for do { } catch in Swift 2.0

查看:133
本文介绍了在Swift 2.0中,do {} catch不会处理从这里抛出的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我更新swift 2.0之后,我遇到了一个错误,就像下面的图片那样{try} catch。

After i update swift 2.0 i got an error with do { try } catch like an image below.

我该如何解决这个问题?
谢谢!

How can i fix this? Thanks!

推荐答案

错误告诉您封闭式捕获并非详尽无遗。这是因为自动生成的 catch 块只捕获 NSError 对象,编译器无法分辨是否有些其他 ErrorType 将被抛出。

The error is telling you that the enclosing catch is not exhaustive. This is because the auto-generated catch block is only catching NSError objects, and the compiler can't tell whether some other ErrorType will be thrown.

如果你确定不会抛出其他错误,你可以添加另一个default catch block:

If you're sure no other errors will be thrown, you can add another default catch block:

do {
    objects = try managedObjectContext?.executeFetchRequest(request)
} catch let error1 as NSError {
    error = error1
    objects = nil
} catch {
    // Catch any other errors 
}

这篇关于在Swift 2.0中,do {} catch不会处理从这里抛出的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆