An error of "try! realm.write{}"


#1

I’m creating a quiz app and I’m using Realm for answers.

I wrote this into the viewController.

 let realm = try! Realm()
        
        let accept = Realm()
            accept.id = 1
            accept.noun = "acceptance"
            accept.verb = "accept"
            accept.adj = "acceptable"
            accept.adv = "acceptably"
        
        try! realm.write{      //the error is " Value of type 'Realm' has no member 'write' ""
            realm.add(accept)
        }

Xcode version is 10.2.1.

What should I do? Could you help me?


#2

let accept = Realm()

I think you want to make a model, not a Realm on this.


#3

So, should I change “Realm()” to object class name?


#4

yeah, you should if that is what you want.


#5

Thank you. I changed to

 let realm = try! Word() //Word is object class name
        
        let accept = Word()
            accept.id = 1
            accept.noun = "acceptance"
            accept.verb = "accept"
            accept.adj = "acceptable"
            accept.adv = "acceptably"
        
        try! realm.write{
         realm.add(Word)
        }
        

but it still has same error.
Is there any other possibillities?


#6

As mentioned previously this is incorrect

let realm = try! Word()

it should be

let realm = try! Realm()

Show us what your Word class looks like please.

There’s actually some very solid getting started documentation you should review as well. Check out the Creating Objects section for some example code.


#7

You are completely wrong.

let realm = try! Realm()
        let accept = World()
            accept.id = 1
            accept.noun = "acceptance"
            accept.verb = "accept"
            accept.adj = "acceptable"
            accept.adv = "acceptably"

try! realm.write {
realm.add(accept)
}

first, you need to make a realm. Then you need to make an object that you store in the realm and third, you add the object to the realm.


#8

Thank you for telling.
I changed that then the error is disappeared!!