Rai*_*tle 4 cocoa objective-c nsfilemanager
有没有办法告诉-[NSFileManager contentsOfDirectoryAtURL:includingPropertiesForKeys:options:error:]方法在收集目录的内容时排除目录名?
我有一个树视图显示文件夹,并希望在表视图中只显示文件,但我似乎无法找到一个键或任何其他方式来排除文件夹.我想我可以迭代返回的数组,只将文件填充到第二个数组中,这个数组将用作数据源,但这种双重处理似乎有点狡猾.
我也尝试nil从tableView:viewForTableColumn:row:方法返回,如果它NSURL是一个目录,但只会导致表中的空行,所以这也没有好处.
当然有一种方法可以说NSFileManager我只想要文件?
Mic*_*ann 15
您可以更深入地了解目录枚举器.
这个怎么样?
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
Run Code Online (Sandbox Code Playgroud)
rma*_*ddy 11
最好的选择是用于enumeratorAtURL:includingPropertiesForKeys:options:errorHandler:填充排除文件夹的数组.
NSFileManager *fm = [[NSFileManager alloc] init];
NSDirectoryEnumerator *dirEnumerator = [fm enumeratorAtURL:directoryToScan
includingPropertiesForKeys:@[ NSURLNameKey, NSURLIsDirectoryKey ]
options:NSDirectoryEnumerationSkipsHiddenFiles | NSDirectoryEnumerationSkipsSubdirectoryDescendants
errorHandler:nil];
NSMutableArray *fileList = [NSMutableArray array];
for (NSURL *theURL in dirEnumerator) {
NSNumber *isDirectory;
[theURL getResourceValue:&isDirectory forKey:NSURLIsDirectoryKey error:NULL];
if (![isDirectory boolValue]) {
[fileList addObject:theURL];
}
}
Run Code Online (Sandbox Code Playgroud)
这将为您提供NSURL表示文件的对象数组.
| 归档时间: |
|
| 查看次数: |
9125 次 |
| 最近记录: |