如何在Realm中列出LinkingObjects属性?

时间:2016-11-16 14:56:47

标签: swift 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;
}]

狗和猫的财产在哪里?

感谢。

2 个答案:

答案 0 :(得分:1)

我找到了解决方案:

let computedProperties = Persons.sharedSchema()?.computedProperties

答案 1 :(得分:0)

LinkingObjects属性列在computedProperties的{​​{1}}属性中,该属性当前不公开或存在于Swift版本的类中。虽然在给定obj-c类的实例(通过RLMObjectSchema)的情况下可以访问私有财产,但是它不会在Swift上运行,并且没有任何内容使用Realm Swift时,有必要访问obj-c .valueForKey("computedProperties")

There's an existing feature request to expose this