我只想获取顶级目录
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 时我不应该能够获得 fileAttributes 吗?
最佳答案
您不想在枚举器上调用 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");
}
}
关于ios - NSDirectoryEnumerator fileAttributes 为零,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26941929/