Realm Swift:如何捕获RLMException? [英] Realm Swift: how to catch RLMException?

查看:110
本文介绍了Realm Swift:如何捕获RLMException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为我做得正确.

let realm = try! Realm()
do {
    try realm.write {
        realm.add(myObject)
    }
} catch {
    print("something went wrong!")
}

但是我仍然崩溃,而不是那个打印语句.我对避免异常不感兴趣(在这种情况下,我是通过添加具有现有主键的对象故意造成的),但是我希望能够捕获该异常并防止崩溃.这可能吗?如果可以,怎么办?

But I'm still getting a crash instead of that print statement. I'm not interested in avoiding the exception (in this case I caused it deliberately by adding an object with an existing primary key) but I want to be able to catch it and prevent a crash no matter what. Is this possible, and if so, how?

推荐答案

Realm Swift仅针对被认为是程序员错误的东西抛出Objective-C异常.这些异常不是在运行时捕获和处理的,因为它们表示必须修复的程序错误.

Realm Swift throws Objective-C exceptions only for things that are considered to be programmer error. These exceptions are not intended to be caught and handled at runtime as they're indicative of an error in the program that must be fixed.

这篇关于Realm Swift:如何捕获RLMException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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