【问题标题】:Bundle.main.url cannot find fileBundle.main.url 找不到文件
【发布时间】:2022-11-27 06:16:40
【问题描述】:

我尝试了来自不同帖子的几个答案,但没有一个解决了我的问题。

我所处的情况:

  • 我在<ProjectRoot>/XcodeTests/res/ 有一个my_file.txt 文件
  • 创建my_file.txt时,我确保目标Xcode测试被选中
  • res文件夹和my_file.txt都存在于Finder确认的存储目录中
  • 建造阶段Xcode测试,复制捆绑资源确实包括my_file.txt

以下代码抛出Fatal error: Couldn't find requested file

import Foundation

guard let myFileURL = Bundle.main.url(forResource: "my_file", withExtension: "txt")
else
{
    fatalError("Couldn't find requested file")
}

我应该怎么做才能让Bundle.main.url 找到my_file.txt

【问题讨论】:

    标签: ios swift xcode macos bundle


    【解决方案1】:

    我应该怎么做才能让 Bundle.main.url 找到 my_file.txt?

    没有什么。主包不可能看到测试目标内部。测试中的代码可以通过它自己的包查看您的文本文件,但这不是主包。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-25
      • 2021-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-09
      相关资源
      最近更新 更多