ios - pathForResource 返回 nil

标签 ios swift

我有两个文件,都命名为 GoogleService-Info.plist。一个在文件夹 Resources -> Staging 中,另一个在文件夹 Resources -> Production 中。它们都被添加到 Copy Bundle Resources。我访问其中一个的代码是这样的:

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

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

最佳答案

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

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

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

关于ios - pathForResource 返回 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48470640/

相关文章:

ios - FBSDKAppEvents.activateApp() 无法识别的选择器发送到实例

php - 使用 file_get_contents 将数据从 Swift 传递到 PHP for JSON?

ios - 不执行 segue

ios - UIWebView:停止滚动,但不要禁用它

iphone - 如何在 iOS 中播放从网络接收的视频流?

ios - 在 NavBar 上使用 Back 后,RefreshControl 会导致 TableView 的第一个 Cell 出错

ios - 如何确定 iOS 上的用户带宽

ios - 应用程序终止时的位置更新

ios - 文本字段应该 swift 更改范围内的字符

ios按钮仅在点击时显示图像