从Realm中的子类调用[super defaultPropertyValues]是一个好习惯吗?

时间:2015-08-30 12:36:45

标签: ios objective-c realm

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

例如:

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

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

比我定义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    

1 个答案:

答案 0 :(得分:2)

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

如果您只是简单地对RLMObject进行子类化,则无需在这些类方法上调用super

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

我个人认为这是一种良好的做法,因为它意味着你不会在这些方法中创建冗余信息(即,如果你在课程A中改变了任何内容,那么{{1}会是很好的原样)。 :)