【问题标题】:Xcode bundle pathXcode 包路径
【发布时间】:2018-02-06 05:41:06
【问题描述】:

我有这个:

guard let dbPath = Bundle.main.path(forResource: "FluidDatabase",
                                         ofType: "sqlite",
                                    inDirectory: "Models") else {
    assertionFailure()
    return
}

文件位于此处: MainAppFolder/Models/FluidDatabase.sqlite.

它不断被 assertionFailure 捕获。

【问题讨论】:

  • 不需要在Application.app/Contents/Resources/Models/FluidDatabase.sqlite吗?
  • @user1118321:这取决于操作系统。在 iOS 上,它必须在 OP 的路径中。在 macOS 上,您的路径是正确的。
  • 文件是否在构建时正确复制?它没有在根应用程序文件夹中结束?可能想尝试使用相同的方法,但没有 directory 参数,看看它是否可以通过这种方式找到文件。

标签: swift xcode nsbundle


【解决方案1】:

您似乎将不必要的“模型”传递到inDirectory中,如果您将该Sqlite文件作为参考文件添加到您的项目中,那么您可以直接从bundle中获取它的路径。

斯威夫特 4

    guard let dbPath = Bundle.main.path(forResource: "FluidDatabase",ofType: "sqlite",inDirectory: nil)
        else {
                assertionFailure()
                return
        }

【讨论】:

  • 嘿@Latcie 你觉得我的回答有帮助吗?
【解决方案2】:

好的,所以问题是文件未包含在文件检查器中我的项目的目标成员中。

【讨论】:

    猜你喜欢
    • 2010-11-20
    • 1970-01-01
    • 2012-03-09
    • 2015-10-05
    • 2013-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-19
    相关资源
    最近更新 更多