Bundle.main.path(forResource ...在查找xml文件时总是返回nil [英] Bundle.main.path(forResource... always returns nil when looking for xml file
问题描述
我正在尝试从我的捆绑包中读取文件.我知道之前已经有人问过这个问题,但是我已经阅读了其他SO解决方案,但似乎我个人没有.
I am trying to read a file from my bundle. I know this has been asked before but I have read the other SO solutions and not of them seem to be of my situation.
我有一个XML文件.我可以在项目导航器中看到它:
I have an XML file. I can see it in the project navigator:
,我还可以通过转到项目/构建阶段/复制捆绑包资源"来检查它是否包含在捆绑包中
and I can also check that it is included in the bundle by going to Project/Build Phases/Copy Bundle Resource
我尝试使用没有运气的文件管理器,使用inDirectory而不使用inDirectory来获取其路径:
I've tried to get its path using the file manager with no luck, using inDirectory and not using inDirectory:
let filePath = Bundle.main.path(forResource: "config", ofType: "xml", inDirectory:"Resources")
接下来的尝试让我有些茫然.
I'm kind of at a loss as to what to try next.
我可以使用它,但这似乎比我需要的代码更多.
I got it to work but this seems to be way more code than I should need.
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
}
这篇关于Bundle.main.path(forResource ...在查找xml文件时总是返回nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!