2016-11-16 67 views
1

我需要列出对象的LinkingObjects类型的所有属性。如何列出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; 
}] 

哪里有狗和猫的属性?

谢谢。

回答

0

LinkingObjects属性在computedProperties属性RLMObjectSchema中列出,该属性当前未在Swift版本的类中公开或存在。虽然有可能通过给定obj-c类的实例(通过.valueForKey("computedProperties"))得到私有属性,但这对于Swift不适用,并且在使用时没有任何好方法可以使用obj-c RLMObjectSchema Realm Swift。

There's an existing feature request to expose this

+0

谢谢。看来我们将不得不等待他们...... –

0

我找到了解决办法:

let computedProperties = Persons.sharedSchema()?.computedProperties