首页 > 解决方案 > 如何隐藏像example.app这样的包文件?

问题描述

我在大纲视图上列出目录,但找不到隐藏包文件的方法。

let urls = try FileManager.default.contentsOfDirectory(at: url,                                                                   
                  includingPropertiesForKeys: [.isDirectoryKey],                                                                   
                  options: [.skipsHiddenFiles, .skipsPackageDescendants])

我只能跳过常规文件,如 text.txt 列出所有扩展名为 .app 的文件

标签: swiftxcodemacos

解决方案


skipsPackageDescendants选项旨在防止深度目录遍历进入包。该标志并不意味着“忽略所有包”。它实际上意味着“不要枚举包内的内容”。

contentsOfDirectory()不会进行深度目录遍历,因此该选项在该上下文中毫无意义。该enumerator(at:...)方法将通过不枚举包中的文件来尊重该标志.app;它仍然会枚举.app自身。

如果您对某些类型的文件不感兴趣,您可以使用

let urls = try FileManager.default.contentsOfDirectory(
  at: url,
  includingPropertiesForKeys: [.isDirectoryKey],                                                                   
  options: [.skipsHiddenFiles]
).filter { $0.pathExtension != "app" }

推荐阅读