使用 NSDirectoryEnumerator 为文件系统建模

发布于 2024-12-03 05:00:22 字数 2063 浏览 0 评论 0原文

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

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

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

我的问题是:如何使用 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 构建模型呢?我的意思是,据说这就是这个类存在的原因,对吗?

I'm trying to model the structure of the filesystem from a given starting path. The goal is to create a standard NSOutlineView of the filesystem from that path onwards.

I've got a model object called fileSystemItem. It has the following (very standard) relationships and properties:

  • parentItem (points to another fileSystemItem object)
  • isLeaf (YES for files; NO for folders)
  • childrenItems (array of other fileSystemItems)
  • fullPath (NSString; file path of object)

My question is: how do I use NSDirectoryEnumerator to build the model? If I do this:

// 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];
}

I can see how to build the model if I write a recursive function that looks at each item in startingPath and, if that item is a folder, calls itself again on that folder and so on. But how can I build the model with NSDirectoryEnumerator? I mean, supposedly that's why the class exists, right?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

输什么也不输骨气 2024-12-10 05:00:22

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

enumeratorAtURL:includePropertiesForKeys:options:errorHandler:

此枚举器具有其他有用的选项,并允许使用预取属性迭代 NSURL 实例,例如 NSURLNameKey、NSURLIsDirectoryKeyNSURLParentDirectoryURLKey 等...它可以帮助摆脱递归的使用。

It is possible to use another kind of directory enumeration:

enumeratorAtURL:includingPropertiesForKeys:options:errorHandler:

This enumerator has additional useful options and allows to iterate NSURL instances with prefetched attributes, such as NSURLNameKey, NSURLIsDirectoryKey, NSURLParentDirectoryURLKey, etc... It could help to get rid of recursion usage.

冷清清 2024-12-10 05:00:22

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

If that file is a directory, you'll need to create a new directory enumerator; an NSDirectoryEnumerator enumerates one directory, not every directory on the system.
So yes, you'll have to use recursion.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文