且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

上下文?.save(nil)出现错误

更新时间:2023-11-06 18:55:16

你得到的错误是 context 变量不是可选的,所以是没用的。

You get that error as your context variable is not optional so the ? is useless.

也是swift 2引入了 do-catch 构造,以允许高级错误处理,就像使用尝试其他语言一样-catch ,所以带有错误参数的函数,例如 save() NSManagedObjectContext 已更改并丢失了错误参数并将错误报告为异常;所以你应该这样做

Also swift 2 introduced the do-catch construct to allow advanced error handling as you would do in other languages with try-catch, so functions with an error parameter such as save() of NSManagedObjectContext changed and have lost the error parameter and report errors as exceptions; so you should do

do {
    try context.save()
} catch let error {
    // Handle error stored in *error* here
}

如果你不喜欢我想处理你可以做的错误

If you don't want to handle the error you can do

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