有没有办法告诉-[NSFileManager contentsOfDirectoryAtURL:includingPropertiesForKeys:options:error:]
收集目录内容时排除目录名称的方法?
我有一个显示文件夹的树视图,并且只想在表视图中显示文件,但我似乎找不到键或任何其他方式来排除文件夹。我想我可以迭代返回的数组,仅将文件填充到第二个数组中,该数组将用作数据源,但这种双重处理似乎有点狡猾。
我也尝试过返回nil
来自tableView:viewForTableColumn:row:
方法如果NSURL
是一个目录,但这只会导致表中出现空白行,所以这也不好。
当然有一种方法可以告诉NSFileManager
我只想要文件?
您可以使用目录枚举器更深入一些。
这个怎么样?
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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)