NSFileManager列出目录内容,不包括目录 [英] NSFileManager list directory contents excluding directories

查看:271
本文介绍了NSFileManager列出目录内容,不包括目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法告诉 - [NSFileManager contentsOfDirectoryAtURL:includePropertiesForKeys:options:error:] 方法在收集目录的内容时排除目录名称? p>

我有一个树视图显示文件夹,并希望在表视图中显示ONLY文件,但我似乎找不到一个键或任何其他方式来排除文件夹。我想我可以迭代返回的数组只填充文件到第二个数组,将用作数据源,但这种双重处理似乎有点狡猾。



我也尝试从 tableView:viewForTableColumn:row:返回 nil c $ c>方法如果 NSURL 是一个目录,但是只会导致表中的一个空行,所以这也不好。



当然有一种方法可以告诉 NSFileManager 我只想要文件?

解决方案

使用目录枚举可以更深入一些。



这是怎么回事?

  NSDirectoryEnumerator * dirEnumerator = [localFileManager enumeratorAtURL :directoryToScan includePropertiesForKeys:[NSArray arrayWithObjects:NSURLNameKey,NSURLIsDirectoryKey,nil] options:NSDirectoryEnumerationSkipsSubdirectoryDe​​scendants errorHandler:nil]; 
NSMutableArray * theArray = [NSMutableArray array];

(NSURL * theURL in dirEnumerator){

//检索文件名。从NSURLNameKey,在枚举期间缓存。
NSString * fileName;
[theURL getResourceValue:& fileName forKey:NSURLNameKey error:NULL];

//检索目录。从NSURLIsDirectoryKey,
//在枚举期间缓存。

NSNumber * isDirectory;
[theURL getResourceValue:& isDirectory forKey:NSURLIsDirectoryKey error:NULL];


if([isDirectory boolValue] == NO)
{
[theArray addObject:fileName];
}
}

//此处的Array包含所有文件名


Is there a way to tell the -[NSFileManager contentsOfDirectoryAtURL:includingPropertiesForKeys:options:error:] method to exclude directory names when gathering the contents of a directory?

I have a tree view showing folders and want to show ONLY files in the table view, but I can't seem to find a key or any other way to exclude folders. I suppose I could iterate the returned array to stuff only files into a second array, which will be used as the data source, but this double-handling seems a bit dodgy.

I also tried returning nil from the tableView:viewForTableColumn:row: method if the NSURL was a directory, but that only results in a blank row in the table, so that's no good either.

Surely there is a way to just tell NSFileManager that I only want files?

解决方案

You can go a little deeper with a directory enumerator.

How about this?

NSDirectoryEnumerator *dirEnumerator = [localFileManager enumeratorAtURL:directoryToScan includingPropertiesForKeys:[NSArray arrayWithObjects:NSURLNameKey, NSURLIsDirectoryKey,nil] options:NSDirectoryEnumerationSkipsSubdirectoryDescendants  errorHandler:nil];
NSMutableArray *theArray=[NSMutableArray array];

for (NSURL *theURL in dirEnumerator) {

    // Retrieve the file name. From NSURLNameKey, cached during the enumeration.
    NSString *fileName;
    [theURL getResourceValue:&fileName forKey:NSURLNameKey error:NULL];

    // Retrieve whether a directory. From NSURLIsDirectoryKey, also
    // cached during the enumeration.

    NSNumber *isDirectory;
    [theURL getResourceValue:&isDirectory forKey:NSURLIsDirectoryKey error:NULL];


    if([isDirectory boolValue] == NO)
    {
        [theArray addObject: fileName];
    }
}

// theArray at this point contains all the filenames

这篇关于NSFileManager列出目录内容,不包括目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆