1

我正在尝试从给定的起始路径对文件系统的结构进行建模。目标是NSOutlineView从该路径开始创建文件系统的标准。

我有一个名为fileSystemItem. 它具有以下(非常标准的)关系和属性:

  • parentItem (指向另一个fileSystemItem对象)
  • isLeafYES用于文件;NO用于文件夹)
  • childrenItems(其他数组fileSystemItems
  • fullPath( NSString; 对象的文件路径)

我的问题是:我如何使用NSDirectoryEnumerator来构建模型?如果我这样做:

// NOTE: can't do "while (file = [dirEnum nextObject]) {...} because that sets 
// file to an auto-released string that doesn't get released until after ALL 
// iterations of the loop are complete. For large directories, that means our 
// memory use spikes to hundreds of MBs. So we do this instead to ensure that 
// the "file" string is released at the end of each iteration and our overall 
// memory footprint stays low.

NSDirectoryEnumerator *dirEnum = [aFileManager enumeratorAtPath:someStartingPath];
BOOL keepRunning = YES;
while (keepRunning)
{
    NSAutoreleasePool *innerPool = [[NSAutoreleasePool alloc] init];

    NSString *file = [dirEnum nextObject];
    if (file == nil) break;

    // ... examine "file". Create a fileSystemItem object to represent this item.
    // If it's a folder, we need to create a fileSystemItem for each item in the folder
    // and each fileSystemItem's "parentItem" relationship needs to be set to the 
    // fileSystemItem we're creating right here for "file." How can I do this inside
    // the directoryEnumerator, because as soon as we go to the next iteration of the    
    // loop (to handle the first item in "file" if "file" is a folder), we lose the  
    // reference to the fileSystemItem we created in THIS iteration of the loop for 
    // "file". Hopefully that makes sense... 

    [innerPool drain];
}

startingPath如果我编写一个递归函数来查看其中的每个项目,并且如果该项目是一个文件夹,则在该文件夹上再次调用自身,依此类推,我可以看到如何构建模型。但是我怎样才能建立模型NSDirectoryEnumerator呢?我的意思是,这就是这个类存在的原因,对吧?

4

2 回答 2

0

可以使用另一种目录枚举:

enumeratorAtURL:includingPropertiesForKeys:options:errorHandler:

这个枚举器有额外的有用选项,并允许迭代具有预取属性的 NSURL 实例,例如NSURLNameKeyNSURLIsDirectoryKeyNSURLParentDirectoryURLKey等......它可以帮助摆脱递归的使用。

于 2013-11-14T21:10:32.643 回答
-2

如果该文件是目录,则需要创建一个新的目录枚举器;NSDirectoryEnumerator 枚举一个目录,而不是系统上的每个目录。所以是的,你必须使用递归。

于 2012-01-08T10:39:25.197 回答