title: How to efficiently flatten a NSOutlineView?
body: <p>I have a NSOutlineView which is binded to my NSTreeController. The content of the NSTreecontroller (myTreeController) is set with data using the command:
      [self.myTreeController setContent:self.myArrayOfFiles];
  The content of the array consists of NSTreeNode parent and children objects that are added using a NSTreeNode subclass (TKnode): </p>
  
  <p><code>[TKnode treeNodeWithRepresentedObject:myRootObject]</code>.</p>
  
  <p>This works very well and displays my NSoutlineView correctly.  But when I want to iterate over the content of the NSOutlineView I need to flatten it and store the objects in an array.  This is where I could need some help as the solution I have is not optimal and potentially prone to errors. </p>
  
  <p>I first return the content of the NSTreeController using:</p>
  
  <pre><code> - (NSArray *)rootNodes;
  {
      return [[myTreeController arrangedObjects] childNodes] ;
  }
  </code></pre>
  
  <p>This returns a NSTreeControllerTreeNode array of size one that holds the tree structure. I then access the first layer of the tree structure using the childNodes method.</p>
  
  <pre><code>- (NSArray *)flattenedNodes
  {
      NSMutableArray *mutableArray = [NSMutableArray array]; 
      for (TKnode *rootnode in [self rootNodes]){
          for (TKnode *node in [rootnode childNodes]){
  
              if (![[node representedObject] isLeaf]){
                  [mutableArray addObjectsFromArray:[self descendants:[node representedObject]]];
              }
              else {
                  [mutableArray addObject:[[node representedObject] representedObject]];
              }
          }
      }
      DLog(@"My files: %lu",[mutableArray count]);
      return [[mutableArray copy] autorelease];
  }
  </code></pre>
  
  <p>The children of the children are accessed recursively using the following method:</p>
  
  <pre><code>- (NSArray *)descendants:(TKnode *) node
  {
      NSMutableArray *descendantsArray = [NSMutableArray array]; 
      for (TKnode *mynode in [node childNodes]) {
  
          [descendantsArray addObject:[mynode representedObject]];
  
          if (!mynode.isLeaf){
              [descendantsArray addObjectsFromArray:[self descendants:mynode]];
          }
      }
      return [[descendantsArray copy] autorelease]; // return immutable
  }
  </code></pre>
  
  <p>This works and returns an array containing all of my objects, not the NSTreeNodes, but the objects I am interested in. But it seems error prone that I have to call representedObject sometimes twice on an object to access my object. Is this the way I am supposed to work with NSTReeController and NSTreeNodes? Is there a better way? The reason I wanted to use NSTreeNode and NSTreeController in the first place was to get some of the already implemented methods for free such as sorting and arrangedObjects, which I am used to with NSTableView. But the method I use to access the NSTreeController content does not seem correct ? Should I use the <code>arrangedObjects</code> to get the content of the NSTReeController or should I use a different approach? Any suggestions for how to correctly flatten a NSOutlineView is highly appreciated.</p>
  
  <p>Thanks! Cheers, Trond</p>