Pregunta

I have created a directory tree structure of files and folders using NSTreeNode which I show in a NSOutlineView. My problem is that some folders are empty and I would like to remove these from being shown. I have tried recursively to iterate through each element of the tree structure to remove elements, but it is clearly much easier to build a tree than to tear it down. My iterations have started at root and then I iterate through each nodes childnodes until I reach the top of the tree. However, as some folders can contain several empty and non-empty folders it becomes difficult to efficiently remove elements I am not certain are emtpy. Just because a folder only contains another folder does not mean I can remove it as child folders may eitehr be empty or contain files.

I think I need to find the top element/directory of the tree and then iterate backwards towards to my rootnode through all of the parents childnodes and check if each folder is empty. If a folder is empty or only contain the folder I am currently in, remove the folder and the parent folder, and so on until I reach root.

However, I am unable to get this to work. Does anybody have a method to find the elements defining the top of the tree? Or is there a good way to sort/remove empty elements in a NSTreeNode structure?

I appreciate all the help I can get on this. Thanks. Cheers, Trond

¿Fue útil?

Solución

In pseudo code:

BOOL removeTheChildless(NSTreeNode *tree)
{
   for each NSTreeNode *child
   {
      if ( removeTheChildless(child) )
          removeChild child from tree
   }

   return tree.isLeaf;
}

The "for each" can be based on mutableChildNodes. HTH.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top