我將 Realm Swift 用于以下代碼:
class Item: Object {
@Persisted(primaryKey: true) var name = ""
@Persisted(originProperty: "items") var collection: LinkingObjects<SomeCollection>
}
class SomeCollection: Object {
@Persisted(primaryKey: true) var name = ""
let items = List<Item>()
}
在應用程式啟動時,當我初始化Realm我得到的實體時:
屬性 'SomeCollection.items' 宣告為鏈接物件屬性 'Item.collection' 的來源不存在” UserInfo={NSLocalizedDescription=Schema 驗證因以下錯誤而失敗:
有趣的是,如果我將行更改為LinkingObjects:
let collection = LinkingObjects(fromType: SomeCollection.self, property: "items")
然后它作業正常,但是,在將Item實體添加到items串列后SomeCollection,實體的collection屬性Item不顯示任何已鏈接SomeCollection的,我認為應該自動鏈接?
我在這里做錯了什么?
感謝幫助!
uj5u.com熱心網友回復:
這
let items = List<Item>()
需要是這個
@Persisted var items = List<Item>()
或者
@Persisted var items: List<Item>
取決于用例。
let語法是我們過去的做法。
這稱為逆關系,檔案中對此進行了更深入的介紹。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/477705.html
