I am using the DragnDropOutlineVIew example which creates a NSTreeNode from a saved dictionary using the following method :
- (NSTreeNode *)treeNodeFromDictionary
NSDictionary *)dictionary {
// We will use the built-in NSTreeNode with a representedObject that is our model object - the SimpleNodeData object.
// First, create our model object.
NSString *nodeName = [dictionary objectForKey:NAME_KEY];
SimpleNodeData *nodeData = [SimpleNodeData nodeDataWithName:nodeName];
// The image for the nodeData is lazily filled in, for performance.
// Create a NSTreeNode to wrap our model object. It will hold a cache of things such as the children.
NSTreeNode *result = [NSTreeNode treeNodeWithRepresentedObject:nodeData];
// Walk the dictionary and create NSTreeNodes for each child.
NSArray *children = [dictionary objectForKey:CHILDREN_KEY];
for (id item in children) {
// A particular item can be another dictionary (ie: a container for more children), or a simple string
NSTreeNode *childTreeNode;
if ([item isKindOfClass:[NSDictionary class]]) {
// Recursively create the child tree node and add it as a child of this tree node
childTreeNode = [self treeNodeFromDictionary:item];
} else {
// It is a regular leaf item with just the name
SimpleNodeData *childNodeData = [[SimpleNodeData alloc] initWithName:item];
childNodeData.container = NO;
childTreeNode = [NSTreeNode treeNodeWithRepresentedObject:childNodeData];
[childNodeData release];
}
// Now add the child to this parent tree node
[[result mutableChildNodes] addObject:childTreeNode];
}
return result;
}
What I want to do is take the TreeNode im using from this example and put it back into the same dictionary type, so essentially the reverse of the above method
ie - (NSDictionary *)dictionaryFromTreeNode
NSTreeNode *)treeNode
I am bad a trying to visualize recursions, would anyone be so kind as to create this method?
- (NSTreeNode *)treeNodeFromDictionary
// We will use the built-in NSTreeNode with a representedObject that is our model object - the SimpleNodeData object.
// First, create our model object.
NSString *nodeName = [dictionary objectForKey:NAME_KEY];
SimpleNodeData *nodeData = [SimpleNodeData nodeDataWithName:nodeName];
// The image for the nodeData is lazily filled in, for performance.
// Create a NSTreeNode to wrap our model object. It will hold a cache of things such as the children.
NSTreeNode *result = [NSTreeNode treeNodeWithRepresentedObject:nodeData];
// Walk the dictionary and create NSTreeNodes for each child.
NSArray *children = [dictionary objectForKey:CHILDREN_KEY];
for (id item in children) {
// A particular item can be another dictionary (ie: a container for more children), or a simple string
NSTreeNode *childTreeNode;
if ([item isKindOfClass:[NSDictionary class]]) {
// Recursively create the child tree node and add it as a child of this tree node
childTreeNode = [self treeNodeFromDictionary:item];
} else {
// It is a regular leaf item with just the name
SimpleNodeData *childNodeData = [[SimpleNodeData alloc] initWithName:item];
childNodeData.container = NO;
childTreeNode = [NSTreeNode treeNodeWithRepresentedObject:childNodeData];
[childNodeData release];
}
// Now add the child to this parent tree node
[[result mutableChildNodes] addObject:childTreeNode];
}
return result;
}
What I want to do is take the TreeNode im using from this example and put it back into the same dictionary type, so essentially the reverse of the above method
ie - (NSDictionary *)dictionaryFromTreeNode
I am bad a trying to visualize recursions, would anyone be so kind as to create this method?