Bundle.main.path(forResource... 在查找 xml 文件时总是 returns nil
Bundle.main.path(forResource... always returns nil when looking for xml file
我正在尝试从我的包中读取一个文件。我知道之前有人问过这个问题,但我已经阅读了其他 SO 解决方案,但其中似乎没有我的情况。
我有一个 XML 文件。我可以在项目导航器中看到它:
我还可以通过转到 Project/Build Phases/Copy 捆绑资源
检查它是否包含在捆绑中
我尝试使用文件管理器获取它的路径,但没有成功,使用 inDirectory 而不是使用 inDirectory:
let filePath = Bundle.main.path(forResource: "config", ofType: "xml", inDirectory:"Resources")
我有点不知道接下来要尝试什么。
编辑:
我让它工作了,但这似乎比我需要的代码多得多。
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
}
我正在尝试从我的包中读取一个文件。我知道之前有人问过这个问题,但我已经阅读了其他 SO 解决方案,但其中似乎没有我的情况。
我有一个 XML 文件。我可以在项目导航器中看到它:
我还可以通过转到 Project/Build Phases/Copy 捆绑资源
检查它是否包含在捆绑中我尝试使用文件管理器获取它的路径,但没有成功,使用 inDirectory 而不是使用 inDirectory:
let filePath = Bundle.main.path(forResource: "config", ofType: "xml", inDirectory:"Resources")
我有点不知道接下来要尝试什么。
编辑:
我让它工作了,但这似乎比我需要的代码多得多。
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
}