Bundle.main.path(forResource... 查找 xml 文件时总是返回 nil

Pru*_*goe 5 xml bundle filepath swift

我正在尝试从我的包中读取文件。我知道以前有人问过这个问题,但我已经阅读了其他解决方案,但其中似乎不符合我的情况。

我有一个 XML 文件。我可以在项目导航器中看到它:

在此输入图像描述

我还可以通过转到项目/构建阶段/复制捆绑资源来检查它是否包含在捆绑包中

在此输入图像描述

我尝试使用文件管理器获取其路径,但没有成功,使用 inDirectory 而不是使用 inDirectory:

 let filePath = Bundle.main.path(forResource: "config", ofType: "xml", inDirectory:"Resources") 
Run Code Online (Sandbox Code Playgroud)

我有点不知道下一步该尝试什么。

编辑:

我让它工作了,但这似乎是比我需要的更多的代码。

let arrFiles = getAllFiles()
        let fileName = strFileName + "." + strFileExtension

        for thisObj in arrFiles {
            if thisObj == "config.xml" {

                let filePath = Bundle.main.path(forResource: thisObj, ofType: nil)
                print(filePath)
            }

        }
Run Code Online (Sandbox Code Playgroud)

小智 5

试试这个代码:

if let filePath = Bundle.main.url(forResource: "config.xml", withExtension: nil, subdirectory: "/Resources") {

   /// Do something with the filePath

}
Run Code Online (Sandbox Code Playgroud)