I\'m wondering what the best practice for instantiating a Realm database is for Swift 2. One of the major differences between Realm for Swift 1.2 and Swift 2 is that the Rea
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)
}