繁体   English   中英

如何在 Realm 中列出 LinkingObjects 属性?

[英]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

有一个现有的功能请求来公开 this

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM