1
votes
class Ticket: Object {

    @objc dynamic var ticketId = ""
    @objc dynamic var ticketTypeCode = ""
    @objc dynamic var price = ""

    required init()
    {
        super.init()
    }

    required init(value: Any, schema: RLMSchema)
    {
        super.init(value: value, schema: schema)
    }

    required init(realm: RLMRealm, schema: RLMObjectSchema)
    {
        super.init(realm: realm, schema: schema)
    }

here's my model class. the problem is that when I try to save data in realm realm is nil. if let realm = Realm() - here realm is nil. if I remove @objc dynamic from properties realm is'n nil any more, but in that case I can't save data. Can someone help me?

1
Realm instantiation should be done in try block - Nishu_Priya

1 Answers

0
votes

In the class you have to initialize the realm.

func initialzeRealmData() {
    do {
        let realm = try Realm()
        let localSession = Ticket()
        localSession. ticketId =  //give ticketID 
        try realm.write {
            realm.add(localSession)
        }
    } catch {
        print("REALM ERROR: error in initializing realm")
    }
}