How do I accomplish a postorder enumeration of a directory (that is, get subdirectories before the parents)?
Normally I would use FileManager.enumerator(at:includingPropertiesForKeys:options:errorHandler:)
for enumeration. And there is a DirectoryEnumerationOptions
named includesDirectoriesPostOrder
.
Unfortunately it does not seem to work. If I create the enumerator like this:
FileManager.default.enumerator(at: url,
includingPropertiesForKeys: resourceKeys,
options: [.skipsPackageDescendants,
.skipsHiddenFiles,
.includesDirectoriesPostOrder])
The enumerator does not provide the results postorder. And in fact if you check the isEnumeratingDirectoryPostOrder
, it returns false.
Am I using this incorrectly, or is it just broken? Is there some other way to accomplish this? I see that CFURLEnumerator
has a similar option, though I don't know if it works any better. Plus I would rather not wrestle with Core Foundation code if I can avoid it.
I tried it in Objective-C, and it does work, but it's not super obvious how it works. If you look up NSDirectoryEnumerationIncludesDirectoriesPostOrder
in the header, comments say:
NSDirectoryEnumerationIncludesDirectoriesPostOrder causes the NSDirectoryEnumerator to enumerate each directory a second time after all of its contained files have been enumerated. Use NSDirectoryEnumerator.isEnumeratingDirectoryPostOrder to differentiate a post-order enumerated directory from a pre-order one.
What this means is that the enumerator visits a directory, then visits the contents of the directory, and then visits the directory again, and the second time, the isEnumeratingDirectoryPostOrder
property of the enumerator is true.
Maybe you should file a feedback about the documentation.