gpt4 book ai didi

swift - 在 iOS 应用程序中查找文件

转载 作者:行者123 更新时间:2023-11-28 13:32:06 25 4
gpt4 key购买 nike

我知道我的文件在 data/Containers/Bundle/Application 中

我想列出来试试

let documentsUrl =  FileManager.default.urls(for: .applicationDirectory, in: .allDomainsMask).first!

do {
// Get the directory contents urls (including subfolders urls)
let directoryContents = try FileManager.default.contentsOfDirectory(at: documentsUrl, includingPropertiesForKeys: nil)
print(directoryContents)

} catch {
print(error)
}

但是我收到消息:

Error Domain=NSCocoaErrorDomain Code=260 "The file “Applications” couldn’t be opened because there is no such file."

那么如何按捆绑列出文件?

最佳答案

您可以获得您的包 url Bundle.main.bundleURL,获取其父目录并列出其内容:

extension URL {
var parentDirectory: URL? {
return (try? resourceValues(forKeys: [.parentDirectoryURLKey]))?.parentDirectory
}
}

if let bundleDirectory = Bundle.main.bundleURL.parentDirectory {
print(bundleDirectory)
do {
let directoryContents = try FileManager.default.contentsOfDirectory(at: bundleDirectory, includingPropertiesForKeys: nil)
print(directoryContents)
} catch {
print(error)
}
}

关于swift - 在 iOS 应用程序中查找文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57212749/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com