Realm()初始化返回nil

时间:2018-08-22 10:29:51

标签: ios swift realm

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)
    }

这是我的模型课。问题是当我尝试在领域中保存数据时 领域为零。 如果let realm = Realm()-在这里,realm为零。如果我从属性中删除@objc dynamic,则领域不再为零,但是在那种情况下,我将无法保存数据。有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

在课堂上,您必须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")
    }
}