NSDirectoryEnumerator fileAttributes为nil

时间:2014-11-15 02:43:05

标签: ios file enumeration

我只想抓住顶级目录

NSDirectoryEnumerator *directoryEnumerator = [fileManager enumeratorAtURL:[NSURL URLWithString:directory]
                                         includingPropertiesForKeys:[NSArray arrayWithObjects:NSURLIsDirectoryKey, nil]
                                                            options: NSDirectoryEnumerationSkipsHiddenFiles | NSDirectoryEnumerationSkipsSubdirectoryDescendants
                                                       errorHandler:nil];

for (NSURL *file in directoryEnumerator) {
    NSDictionary *attributes = [directoryEnumerator fileAttributes];
    if ([attributes objectForKey:NSFileType] == NSFileTypeDirectory) {
        NSLog(@"Yes its a directory");
    }
}

但每次我为fileAttributes得到nil。使用nsdirectoryenumerator时,我不应该能够获取fileAttribute吗?

1 个答案:

答案 0 :(得分:2)

您不想在枚举器上调用fileAttributes。相反,您希望从URL获取所需的属性。

NSDirectoryEnumerator *directoryEnumerator = [fileManager enumeratorAtURL:[NSURL URLWithString:directory]
                                         includingPropertiesForKeys:@[NSURLIsDirectoryKey]
                                                            options: NSDirectoryEnumerationSkipsHiddenFiles | NSDirectoryEnumerationSkipsSubdirectoryDescendants
                                                       errorHandler:nil];

for (NSURL *file in directoryEnumerator) {
    NSNumber *isDirectory;
    [url getResourceValue:&isDirectory forKey:NSURLIsDirectoryKey error:NULL];
    if ([isDirectory boolValue]) {
        NSLog(@"Yes its a directory");
    }
}