pathForResource返回nil

时间:2018-01-26 22:45:26

标签: ios swift

我有两个名为GoogleService-Info.plist的文件。一个是文件夹资源 - >暂存和另一个是在文件夹资源 - >生产。它们都被添加到Copy Bundle Resources。我访问其中一个的代码是:

if let path = Bundle.main.path(forResource: "GoogleService-Info", ofType: "plist", inDirectory: "Resources/Staging") {
    print("this should work but doesn't")
}

它总是返回零。为什么找不到文件?

1 个答案:

答案 0 :(得分:1)

资源将放在您的应用包的根目录中。您在应用程序包中获取文件夹的唯一时间是在项目中创建文件夹引用时。这些是蓝色文件夹,而不是黄色。

将您的代码更改为(删除inDirectory部分):

if let path = Bundle.main.path(forResource: "GoogleService-Info", ofType: "plist") {
    print("this should work but doesn't")
}