有没有办法告诉 – [NSFileManager contentsOfDirectoryAtURL:includingPropertiesForKeys:options:error:]方法在收集目录内容时排除目录名?
我有一个树视图显示文件夹,并希望在表视图中只显示文件,但我似乎无法找到一个键或任何其他方式来排除文件夹.我想我可以迭代返回的数组,只将文件填充到第二个数组中,这个数组将用作数据源,但这种双重处理似乎有点狡猾.
我也尝试从tableView:viewForTableColumn:row:方法返回nil,如果NSURL是一个目录,但只会导致表中的空行,所以这也没有用.
解决方法
您可以更深入地了解目录枚举器.
这个怎么样?
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