[英]How to list LinkingObjects properties in Realm?
我需要列出一個對象的 LinkingObjects 類型的所有屬性。
class Dogs: Object {
dynamic var name: String = ""
dynamic var age: Int = 0
dynamic var owner: Persons?
}
class Cats: Object {
dynamic var name: String = ""
dynamic var age: Int = 0
dynamic var owner: Persons?
}
class Persons: Object {
dynamic var name: String = ""
dynamic var address: String = ""
let dogs = LinkingObjects(fromType: Dogs.self, property: "owner")
let cats = LinkingObjects(fromType: Cats.self, property: "owner")
}
ObjectSchema 正確返回架構:
let person = Persons()
let schema = person.objectSchema
print(schema)
結果:
Persons {
name {
type = string;
objectClassName = (null);
linkOriginPropertyName = (null);
indexed = NO;
isPrimary = NO;
optional = NO;
}
address {
type = string;
objectClassName = (null);
linkOriginPropertyName = (null);
indexed = NO;
isPrimary = NO;
optional = NO;
}
dogs {
type = linking objects;
objectClassName = Dogs;
linkOriginPropertyName = owner;
indexed = NO;
isPrimary = NO;
optional = NO;
}
cats {
type = linking objects;
objectClassName = Cats;
linkOriginPropertyName = owner;
indexed = NO;
isPrimary = NO;
optional = NO;
}
}
但是,objectSchema.properties 不返回 LinkingObjects 屬性。
let properties = schema.properties
print(properties)
返回:
[name {
type = string;
objectClassName = (null);
linkOriginPropertyName = (null);
indexed = NO;
isPrimary = NO;
optional = NO;
}, address {
type = string;
objectClassName = (null);
linkOriginPropertyName = (null);
indexed = NO;
isPrimary = NO;
optional = NO;
}]
狗和貓的屬性在哪里?
謝謝。
我找到了解決方案:
let computedProperties = Persons.sharedSchema()?.computedProperties
該LinkingObjects性能列在computedProperties
財產RLMObjectSchema
,這是目前尚未公開或者存在的類的斯威夫特版本。 雖然給定 obj-c 類的實例(通過.valueForKey("computedProperties")
)可以訪問私有屬性,但這不適用於 Swift 並且沒有任何好的方法可以訪問使用 Realm Swift 時的 obj-c RLMObjectSchema
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.