Instantiating Realm database for Swift 2.0 - best practice?

风流意气都作罢 提交于 2019-11-30 17:47:05

Unless you're actually going to handle the errors you receive, I highly recommend using try!.

Your second code snippet doesn't work because, if initializing the Realm fails, that realm variable is never assigned to, which is invalid. You could probably work around that by making the realm variable be of type Realm?.

Keep in mind that both Realm() and write can throw. That means both of them need try catch unless you use try!. Like this:

    do {
        let realm = try Realm()

        do {
            try realm.write {
                realm.add(myObject_1)
            }
        } catch let error {
            print(error)
        }

    } catch let error{

        print(error)
    }
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!