Creating NSDictionary for NSTreeNode

Discussion in 'Mac Programming' started by jsnuff1, Dec 31, 2010.

  1. jsnuff1 macrumors 6502a

    Joined:
    Oct 4, 2003
    Location:
    NY
    #1
    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?
     
  2. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #2
    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;
    }
    
     
  3. jsnuff1 thread starter macrumors 6502a

    Joined:
    Oct 4, 2003
    Location:
    NY
    #3
    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;
        
    }
     

Share This Page