- Realm 数据库
1.1 模型采用 数组属性 以及默认值的问题
@interface StudentModel : RLMObject
@property (nonatomic, assign) NSInteger name;
@property (nonatomic, copy) NSInteger age;
@end
//对象在数组中使用时,必须采用该方式,声明对象遵循协议
/* Properties on `RLMObject`s of type `RLMArray` must have an associated type.*/
RLM_ARRAY_TYPE(StudentModel) //放在数组中时,必要说明
@interface ParagraphModel : RLMObject
@property (nonatomic, assign) NSInteger cls_id;
@property (nonatomic, copy) NSString *className;
@property (nonatomic, strong) RLMArray< StudentModel > *studentLists; //数组<对象>属性
@property (nonatomic, strong) RLMArray< StudentModel > *teachLists;
@end
@implementation ParagraphModel
//声明数组属性
+(NSDictionary*)objectClassInArray{
return @{@"studentLists":[StudentModel class]};
}
//设置默认值
+(NSDictionary*)defaultPropertyValues{
return @{@"cls_id":0,@"className":@"",@"studentLists":[[RLMArray alloc] initWithObjectClassName:[StudentModel className]]};
}
@end
方法1:采用 [[RLMArray alloc] initWithObjectClassName:[StudentModel className]]
方法2: 采用@[]
+ (NSDictionary *)defaultPropertyValues{
return @{@"studentLists":[[RLMArray alloc] initWithObjectClassName:[StudentModel className]],@"teachLists":@[]};
}