Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

jsnuff1

macrumors 6502a
Original poster
Oct 4, 2003
730
340
NY
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?
 
Here's my implementation of the method. Note that it won't work if the root tree node has no child nodes. An if at the start to cope with this situation would suffice if you have a need.

Code:
- (NSDictionary *)dictionaryFromTreeNode:(NSTreeNode *)treeNode {
	// First recover the name of the current treeNode
	SimpleNodeData *nodeData = [treeNode representedObject];
	NSString *nodeName = [nodeData name];
	
	// Walk the child nodes
	NSArray *childNodes = [treeNode childNodes];
	NSMutableArray *children = [NSMutableArray arrayWithCapacity:[childNodes count]];

	for (NSTreeNode *childTreeNode in childNodes) {
		id item;
		if (![childTreeNode isLeaf]) {
			// Recursively convert the child tree node into it's dictionary
			item = [self dictionaryFromTreeNode:childTreeNode];
		} else {
			// It is a leaf item with just the name
			SimpleNodeData *childNodeData = [childTreeNode representedObject];
			item = [childNodeData name];
		}
		[children addObject:item];
	}

	// Create a dictionary holding the name and children of treeNode
	NSDictionary *dictionary =
		[NSDictionary dictionaryWithObjectsAndKeys:
			nodeName, NAME_KEY,
			children, CHILDREN_KEY,
			nil];
		
	return dictionary;
}
 
Thanks! I ended up staying up all night an figured this out on my own. We both pretty much did the exact same thing, so its good to know it must be right!

Code:
- (NSDictionary *)dictionaryFromTreeNode:(NSTreeNode *)treeNode {
    // We will use the built-in NSTreeNode with a representedObject that is our model object - the SimpleNodeData object.
    // First, create our model object.
	
    // The image for the nodeData is lazily filled in, for performance.
    
	SimpleNodeData *rootNode = [treeNode representedObject];
	
	NSMutableArray *nodeArray = [NSMutableArray arrayWithCapacity:1];
	
	NSDictionary *result = [NSDictionary dictionaryWithObjectsAndKeys: rootNode.name, NAME_KEY, nodeArray, CHILDREN_KEY, nil];
    
    // Walk the dictionary and create NSTreeNodes for each child.
    NSArray *children = [treeNode childNodes];
    
	
    for (NSTreeNode* item in children) {
        // A particular item can be another dictionary (ie: a container for more children), or a simple string
        id child;
		
		SimpleNodeData *node = [item representedObject];
		
        if (node.container) {
            // Recursively create the child tree node and add it as a child of this tree node
			child = [self dictionaryFromTreeNode:item];
			
			
        } else {
            // It is a regular leaf item with just the name
            child = node.name
        }
		
		
        // Now add the child to this parent tree node
        [nodeArray addObject:child];
    }
    return result;
    
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.