RealmSwift Cannot Find commitWriteTransaction() Compilation Error

Asked 2 years ago, Updated 2 years ago, 70 views

I have a question about how to use RealmSwift.

Run Environment
- OS El Capitan 10.11.1
- Xcode 7.1
- CocoaPods 0.96.2

What should I do if I want to put data in the following objects?

 class User:RLMObject{
    dynamic variable = 0
    dynamic var name=""

    override class func primaryKey()->String{
        return "id"
    }
}

let user=User()
user.id = "1"
user.name="aaaaa"

do{
    let realm=try!RLMealm.defaultRealm()
    realm.beginWriteTransaction()
    realm.addObject(user)
    realm.commitWriteTransaction()⇦ This method is missing and an error occurs.
    ...

swift realm

2022-09-30 20:12

1 Answers

I think the try is just missing, not missing the method. The commitWriteTransaction() method can fail, so throws is defined and must be called with try.

realm.beginWriteTransaction()
realm.addObject(user)
try realm.commitWriteTransaction()



By the way, if you are targeting iOS 8 or higher, RealmSwift is the safer and simpler code for Swift.

https://realm.io/jp/docs/swift/latest/


2022-09-30 20:12

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.