【问题标题】:Is it a good practice to call [super defaultPropertyValues] from child class in Realm?从 Realm 的子类调用 [super defaultPropertyValues] 是一种好习惯吗?
【发布时间】:2015-08-30 12:36:45
【问题描述】:

我的问题不仅涉及+ (NSDictionary *)defaultPropertyValues 方法,还涉及+ (NSArray *)ignoredProperties+ (NSArray *)indexedProperties 等其他方法。

例如:

//.h - file
@interface A : RLMObject
@property NSString propertyA;
@end

//.m - file 
@implementation A
+ (NSDictionary *)defaultPropertyValues {
      return @{@"propertyA":@""};
}
@end

比我定义的class B继承形式A

//.h - file
@interface B : A
@property NSString propertyB;
@end

//.m - file 
@implementation B
+ (NSDictionary *)defaultPropertyValues {
   NSMutableDictionary *defaultValues = [[super  defaultPropertyValues]mutableCopy];
  [defaultValues setObject:@"" forKey:@"propertyB"];
  return defaultValues;
@end    

【问题讨论】:

    标签: ios objective-c realm


    【解决方案1】:

    (免责声明:我为 Realm 工作。)

    如果您只是将RLMObject 子类化,则无需在这些类方法上调用super

    话虽如此,在您的情况下,您随后将RLMObject 的子类子类化,那么是的,这绝对是确保您不会破坏父类功能的好方法。

    我个人认为这是一种很好的做法,因为这意味着您不会在这些方法中创建冗余信息(即,如果您更改了 A 类中的任何内容,B 类就可以了)。 :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-14
      • 1970-01-01
      • 2011-10-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多