我正在将NSTableView与可可粉结合使用。
当我尝试将其保存到Realm时,与我的表绑定的Array引起了问题。
错误:错误无法将具有观察者的对象添加到领域
是否存在将Realm与NSTableview可可绑定变量一起使用的解决方法?
class Dog: Object {
@objc dynamic var owner: Person?
@objc dynamic var name = ""
var age = RealmOptional<Int>()
let owners = LinkingObjects(fromType: Person.self, property: "dogs") // inverse
}
class Person: Object {
@objc dynamic var name = ""
@objc dynamic var birthdate = Date(timeIntervalSince1970: 1)
let dogs = List<Dog>() // to-many
}
。
let realm = try! Realm()
@objc dynamic var dogsArray = [Dog]() // Cocoa binding with my NSTableView
override func viewDidLoad() {
super.viewDidLoad()
let dog1 = Dog(value: [nil, "Fido", 5])
let dog2 = Dog(value: [nil, "Pluto", 8])
dogsArray = [dog1,dog2]
let person = Person(value: ["Alex", Date()])
person.dogs.append(objectsIn: dogsArray)
try! realm.write { realm.add(person) }
}