ios - 列出 xCode 组中的所有文件 URL
问题描述
我的 Xcode 目录中有一个黄色文件夹“组”名称“pdfs”。我需要列出文件夹中的所有文件 URL PDF 才能在我的视图中列出。
func listDir(dir: String) {
// Create a FileManager instance
let fileManager = FileManager.default
let documentsURL = fileManager.urls(for: .documentDirectory,
in:.userDomainMask)[0].appendingPathComponent(dir)
do {
let fileUrls = try fileManager.contentsOfDirectory(at:documentsURL, includingPropertiesForKeys: nil)
// process files
print(fileUrls)
} catch {
print("Error while enumerating files \(documentsURL.path): \(error.localizedDescription)")
}
我有这个错误
枚举文件时出错 /Users/user/Library/Developer/CoreSimulator/Devices/EF0F4EB3-CFB0-42F0-B7F5-B36034508BA2/data/Containers/Data/Application/08019A23-FDD9-4FF8-AC1F-2C13CE293BA5/Documents/pdfs:文件“pdfs”无法打开,因为没有这样的文件。
解决方案
和代码
func listDir(dir: String) -> [URL]? {
let fileManager = FileManager.default
let documentsURL = Bundle.main.resourceURL!.appendingPathComponent(dir)
var fileUrls:[URL]?
do {
fileUrls = try fileManager.contentsOfDirectory(at:documentsURL, includingPropertiesForKeys: nil)
return fileUrls
} catch {
print("Error while enumerating files \(documentsURL.path): \(error.localizedDescription)")
return nil
}
}
推荐阅读
- java - 每两个破折号的正则表达式并根据匹配更改字符串
- javascript - 如何访问作为函数参数的数组的长度?
- command - 是否有命令可以在 Stata 中获取最大的 5 个值和最小的 5 个值?
- ada - 如何获得整数的 10 对数?
- typescript - Apollo Server 如何提供其模式?
- sql - 在 BigQuery 中对 STRUCT 的 ARRAY 进行条件搜索
- javascript - Vuex 从状态加载现有表单数据以进行编辑
- rust - 使用 PEST 解析器的 PEG 语法的歧义
- reactjs - 状态钩子中的计数器不递增
- javascript - 选择选项时反应更新多个组件