XCode / iOS:NSCoding - 具有两个公共对象数组的对象图的持久性
我有两个类似于准系统形式的类:
@interface GraphNode {
NSMutableArray *edges;
}
@implementation GraphNode
-(id)initWithCoder:(NSCoder *)coder {
if (self = [super init]) {
edges = [coder decodeObjectForKey:@"edges"];
}
return self;
}
-(void)encodeWithCoder:(NSCoder*)coder {
[coder encodeObject:edges forKey:@"edges"];
}
@end
@interface GraphEdges {
NSMutableArray *edges;
}
@implementation GraphEdges
-(id)initWithCoder:(NSCoder *)coder {
if (self = [super init]) {
edges = [coder decodeObjectForKey:@"edges"];
}
return self;
}
-(void)encodeWithCoder:(NSCoder*)coder {
[coder encodeObject:edges forKey:@"edges"];
}
@end
您会注意到它们似乎表达了相同的行为 - 编码/解码NSMutableArray,其内容是实现NSCoding的GraphEdge类型的对象。实践中类之间的差异在于GraphEdges在其edges
属性中创建边,而GraphNode仅在其自己的edges
中添加对GraphEdges.edges中已存在的GraphEdge实例的引用。
我的问题是,当解码GraphNode和GraphEdges对象时,不会维护对象图,因为每个类都为每个类创建存储在edges
属性中的原始GraphEdge对象的自己版本。
我试图在GraphNode中的整个NSMutableArray *边缘上执行encodeConditionalObject,同时保持GraphEdges中的编码无条件,但不出所料,这只是无法编码GraphNode.edges,因为条件放在数组本身,而不是它的对象。
解决方案是在两种情况下迭代数组,并对GraphNode的每个edges
成员执行条件编码,但对GraphEdges中的每个成员进行无条件编码?
任何建议表示赞赏,非常感谢。
没有找到相关结果
已邀请:
1 个回复
嘘伪