【iOS-Swif】 Call can throw, but it is not marked with 'try' and the error is not

54 阅读1分钟

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

因为没有使用try catch语句处理异常。解决方案有三种:

第一种:使用do / catch语句处理异常:

do {
    try context.save()
} catch {
            
}

第二种:如果你想调用一个被声明为可能抛出的函数,但是你知道它不会抛出异常,因为你会给它正确的输入,这个时候可以使用try! 也能解决问题 :

try! context.save()

第三种:在函数声明中使用 throws 将异常抛出,并在方法前加上try:

func foo(value: Bool) throws {
  try context.save()
}