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中的每个成员进行无条件编码? 任何建议表示赞赏,非常感谢。     
已邀请:
我认为这里的问题是你分别归档GraphNode和GraphEdges对象。将所有相关的GraphNode和GraphEdges对象组合在某种容器中,可能是“Graph”,这样它们就会成为同一个对象图的一部分。我相信NSKeyedArchiver会将每个对象归档一次。     

要回复问题请先登录注册