'在 'try' 表达式 [warning] in swift 3 中没有调用抛出函数

'no calls to throwing functions occur within 'try' expression [warning] in swift 3

我试图从核心数据中删除和反对,我在 try catch 中尝试了它,如下所示。

do {
   try self.managedContext.deleteObject(self.productList[indexPath.row])
} catch let error as NSError {
        print("something wrong with the delete : \(error.userInfo)")
}

它说 'no calls to throwing functions occur within 'try' expression'catch block is unreachable because no errors are throw in 'do' block。下图给你更多的想法。

这是为什么。我不知道。如何解决这个问题。希望你的帮助。

deleteObject 方法没有抛出。删除 Do-Catch 块,警告将消失。该警告似乎不言自明。

确保您在 try throws 之后调用的方法。

示例:

func someThrowingFunction() throws -> Int {
    // ...
}

let x = try? someThrowingFunction()

let y: Int?
do {
    y = try someThrowingFunction()
} catch {
    y = nil
}

更多相关信息请点击此处:ErrorHandling