我有一个简单的场景,我想用Mantle解析Json的User模型并将其持久化到领域数据库中:

为了使用Mantle库,模型接口必须扩展 MTLModel 类,如下所示:

@interface User: MTLModel<MTLJSONSerializing>
@property(nonatomic,copy) NSString *name;
@property(nonatomic,copy) NSString *email;
@end

为了将模型持久化,我必须声明第二个接口,该接口从 RLMObject 扩展:
@interface RLMUser:RLMObject
@property(nonatomic,copy) NSString *name;
@property(nonatomic,copy) NSString *email;
@end

如您所见,由于必须扩展 RLMObject ,因此我必须实现User类的另一种类型。

有办法避免这种重复吗?

最佳答案

基于使用协议的思想,我创建了一个 super class (gist here):
@interface ModelBase : RLMObject <MTLJSONSerializing, MTLModel>
然后,如@David Snabel-Caunt所说,我最终实现了MTLModel类的某些功能(来自MTLModel.m的复制粘贴)。

最后使用它,您只需要对其进行子类化。

关于ios - Realm + Mantle:在集成两个框架时如何避免多重继承重复?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37461076/

10-10 23:28