使用 Swift 5 从项目中的子组获取文件路径

Get path of file from a SubGroup in project with Swift 5

我要访问plist文件的路径

我正在使用以下行访问路径

当文件位于根文件夹中时,我可以通过以下行访问

let filePath = Bundle.main.path(forResource: Constants.googleServiceFileName, ofType: "plist")

但是当我在子组中移动文件并使用以下代码时 return nil

let filePath = Bundle.main.path(forResource: Constants.googleServiceFileName, ofType: "plist",inDirectory: "Firebase")

但它是 return 零 ..

您在 Xcode 中保留资源的组不一定与其在包中的最终位置相关。您的文件可能仍在顶级 Resources 目录中。

如果您想完全确定构建将您的文件放在哪里,请在报告导航器中查找构建日志,找到 "Copy " 行并使用右侧边缘的按钮将其展开。那会告诉你它的确切位置。

在上面,我在 testData 组中有一个 Excel 电子表格。可以看到构建已经放到了顶层Resources目录下。