Fri*_*ail 4 dependencies circular-dependency realm
我有循环依赖的问题:当使用新的RLMLinkingObjects反向关系时,我收到以下错误:
Type argument 'RCon *' does not satisfy the bound ('RLMObject *') of type parameter 'RLMObjectType'
Run Code Online (Sandbox Code Playgroud)
我有两个课程RCon和RSan.RCon有多个RSan引用,RSan由多个RCon引用,因此它是多对多关系.以下是类的声明示例.
头等舱:
// RSan.h
#import <Realm/Realm.h>
#import <UIKit/UIKit.h>
@class RCon;
@interface RSan : RLMObject
@property (readonly) RLMLinkingObjects<RCon*>* cons;
@end
RLM_ARRAY_TYPE(RSan)
Run Code Online (Sandbox Code Playgroud)
另一类:
// RCon.h
#import <Realm/Realm.h>
#import <UIKit/UIKit.h>
#import "RSan.h"
@interface RCon : RLMObject
@property RLMArray<RSan*><RSan>* sans;
@end
RLM_ARRAY_TYPE(RCon)
Run Code Online (Sandbox Code Playgroud)
这是由于Objective-C编译器的限制.RLMArray需要它们的元素的泛型约束应该是它的子类RLMObject.但Objective-C编译器无法从@class转发声明中识别它.
要解决这个问题,我认为唯一的方法是@interface在同一个文件中声明它们,然后使用类扩展声明它们的属性.如下:
#import <Realm/Realm.h>
#import <UIKit/UIKit.h>
@interface RCon : RLMObject
@end
RLM_ARRAY_TYPE(RCon)
@interface RSan : RLMObject
@end
RLM_ARRAY_TYPE(RSan)
@interface RCon()
@property RLMArray<RSan*><RSan>* sans;
@end
@interface RSan()
@property (readonly) RLMLinkingObjects<RCon*>* cons;
@end
Run Code Online (Sandbox Code Playgroud)
注意:所有avobe代码应该在同一个文件中.