【问题标题】:Realm RLMObject attributesForProperty领域 RLMObject attributesForProperty
【发布时间】:2015-02-16 11:23:35
【问题描述】:

我对使用领域数据库很陌生。我查看了领域文档,发现了一个 RLMObject 类方法 attributesForProperty:

我不明白它在做什么。

你能解释我什么时候可以和需要使用它吗?

感谢您的帮助。

【问题讨论】:

    标签: ios swift realm


    【解决方案1】:

    您可以在继承自RLMObject 的实体类中覆盖此方法,以指定属性方面的附加属性,这些属性会影响数据库的架构和行为。目前,您唯一的选择是是否将某个属性编入索引。

    假设您有一个像文档中那样的模型类:

    @interface Dog : RLMObject
    @property NSInteger age;
    @property NSString *name;
    @end
    

    v0.91.0 及以上版本:

    0.91.0自发布以来就是easier to define indexed properties。如果你想让name 列被索引,那么你可以通过覆盖类方法来做到这一点。

    + (NSArray *)indexedProperties {
        return @[@"age", @"name"];
    }
    

    直到 v0.91.0:

    在此版本之前,您可以指定索引列,如下所示:

    + (RLMPropertyAttributes)attributesForProperty:(NSString *)propertyName {
        RLMPropertyAttributes attributes = [super attributesForProperty:propertyName];
        if ([propertyName isEqualToString:@"name"]) {
            attributes |= RLMPropertyAttributeIndexed;
        }
        return attributes;
    }
    

    【讨论】:

    • 应该是return @[@"age", @"name"];
    • 已修复并更新到最新的次要版本。感谢您指出,但请随时编辑。 ;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-24
    • 1970-01-01
    相关资源
    最近更新 更多