我正在尝试从我的包中读取文件。我知道以前有人问过这个问题,但我已经阅读了其他解决方案,但其中似乎不符合我的情况。
我有一个 XML 文件。我可以在项目导航器中看到它:
我还可以通过转到项目/构建阶段/复制捆绑资源来检查它是否包含在捆绑包中
我尝试使用文件管理器获取其路径,但没有成功,使用 inDirectory 而不是使用 inDirectory:
let filePath = Bundle.main.path(forResource: "config", ofType: "xml", inDirectory:"Resources")
我有点不知道下一步该尝试什么。
Edit:
我让它工作了,但这似乎是比我需要的更多的代码。
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)
}
}
试试这个代码:
if let filePath = Bundle.main.url(forResource: "config.xml", withExtension: nil, subdirectory: "/Resources") {
/// Do something with the filePath
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)