在Swift Playerd中获取文件夹结构';s资源文件夹

在Swift Playerd中获取文件夹结构';s资源文件夹,swift,swift-playground,Swift,Swift Playground,我想获得Swift游乐场资源文件夹中的子文件夹结构 我尝试过使用这种方法,但运气不佳 let docsPath = Bundle.main.resourcePath! + "/Resources" let directoryContents = try? fileManager.contentsOfDirectory(atPath: docsPath) 这导致nil,我无法获得文件夹的树结构,这可能吗?解决了它 let docsPath = Bundle.main.resourcePath!

我想获得Swift游乐场资源文件夹中的子文件夹结构

我尝试过使用这种方法,但运气不佳

let docsPath = Bundle.main.resourcePath! + "/Resources"
let directoryContents = try? fileManager.contentsOfDirectory(atPath: docsPath)
这导致
nil
,我无法获得文件夹的树结构,这可能吗?

解决了它

let docsPath = Bundle.main.resourcePath!
let urls = Bundle.main.urls(forResourcesWithExtension: nil, subdirectory: "dataset/train")

我建议允许
NSBundle
准确地查找资源所在的位置,而不是

let docsPath = Bundle.main.resourcePath! + "/Resources"
您可以在类中的某个地方使用(此外,此类类可以转移到实际项目中)

你得到了确切的路径

顺便说一句,它可以是

/var/folders/gs/hkhcv3rx5wd5lqrfh1ll6g40000gn/T/com.apple.dt.Xcode.pg/resources/620432D5-9F12-4FC8-B271-17153FC9D797/dataset


这就是为什么最好避免硬编码。

正如我在删除的答案
Bundle.main.resourcePath中所说的指向Xcode应用程序的Resources文件夹。没有额外的子文件夹
资源
<代码>URL(对于扩展名为:nil的资源,子目录为:nil)
列出了Xcode和当前游戏场的所有资源。
let docsPath = Bundle(for: type(of: self)).path(forResource: "dataset", ofType: nil)