我有两个班级:公司和员工。两者都继承了Realm Object 类。
class Company:Object {
var name:String = ""
var employee:Employee?
override static func primaryKey() -> String? {
return "name"
}
}
class Employee:Object {
var name:String = ""
var age:Int = 0
override static func primaryKey() -> String? {
return "name"
}
}
填充对象
var emp = Employee()
emp.name = "Sachin"
emp.age = 35
var comp = Company()
comp.name = "BCCI"
comp.employee = emp
println("Before: \(comp.employee)")
var realm = Realm()
realm.write {
println("Before Add: \(comp.employee)")
realm.add(comp, update: true)
println("In Block: \(comp.employee)")
}
println("After: \(comp.employee)")
结果:
之前:员工{ name = Sachin; 年龄= 35岁; }
之前添加:员工{ name = Sachin; 年龄= 35岁; }
区块:零 之后:零
问题:
在realm.add()
操作之后,为什么公司的员工财产对象为零?有什么想法吗?
答案 0 :(得分:5)
对于所有Realm Swift属性(ToString()
除外),您需要将属性声明为List
。将模型定义更改为以下内容应该有所帮助!
dynamic