Better approach to Serializing tree node
I need to Serialize tree node, as of now I did not find any way to write reference to parent node point using object serializing using NSCoding protocol
My node class开发者_StackOverflow社区
@interface FNode : NSObject
{
@private
NSString* name;
NSObject* data;
FNode* parent;
NSMutableDictionary* childs;
}
@property (retain) NSString* name;
@property (retain) NSObject* data;
@property (assign)FNode* parent;
@property (retain)NSMutableDictionary* childs;
@end
As of now I create all data in tree as a NSArray before I encode it and in decoding I take NSArray of data and create the tree.
I am just wondering is there any better approach to this problem … I appreciate your thought.
Why do you build that array first? Each node should know about encoding itself including the childs (but not the parent, because you would have a loop). TheN you simply encode the tree by storing the root node. That encodes the whole tree.
While decoding, you can set the parent reference.
精彩评论