【问题标题】:NSBundle.mainBundle().pathForResource returns nilNSBundle.mainBundle().pathForResource 返回 nil
【发布时间】:2015-09-23 03:19:26
【问题描述】:

我正在尝试为 Swift 编写一个简单的 IO 包装器。

为了对此进行测试,我在项目根目录中有一个名为“Test.txt”的文件。

按照其他遇到此问题的人的建议,我已将此文件添加到 Build Bundle Resources 中的 Build Phases。

我已经实现了一个非常简单的 File 类,它有一个读取函数,目的是输出文件的内容。

class File2{
    let resourceName: String
    let type: String
    let bundle = NSBundle.mainBundle()


    init(resourceName: String, type: String = "txt"){
        self.resourceName = resourceName
        self.type = type
        println(self.bundle)
    }

    func read(){
        let path = self.bundle.pathForResource("Test.txt", ofType: "txt") //Hard coded these in just to make sure Strings contained no whitespace
        println(path) //This returns nil...why?
        var error:NSError?
        //print(String(contentsOfFile:path!, encoding:NSUTF8StringEncoding, error: &error)!)
        //return String(contentsOfFile:path!, encoding:NSUTF8StringEncoding, error: &error)!
    }
}

当我打印包的内容时,我会得到一个指向我的文件系统上特定位置的 URI,我假设它是应用程序在模拟器中的虚拟位置。导航到它显示它确实包含我的“Test.txt”文件。

现在我要做的就是获取该文件的路径。

我通过调用来做到这一点:self.bundle.pathForResource("Test.txt", ofType: "txt")

这将返回“nil”

为什么? :)

【问题讨论】:

  • let path = self.bundle.pathForResource("Test.txt", ofType: "txt"),表示寻找资源Text.txt.txt...参数中不需要txt之一。

标签: ios objective-c swift nsbundle


【解决方案1】:

不要在 name 参数中包含 .txt,将其作为 extension 参数传递。
来自documentation

扩展
要定位的文件的文件扩展名。
如果指定空字符串或 nil,则假定扩展名不存在,并且该文件是遇到的第一个与 name 完全匹配的文件。

Swift3

let bundle = Bundle.main
let path = bundle.path(forResource: "Test", ofType: "txt")

Swift1 & Swift2

let bundle = NSBundle.mainBundle()
let path = self.bundle.pathForResource("Test", ofType: "txt")

目标-C

NSBundle* bundle = [NSBundle mainBundle];
NSString* path = [bundle pathForResource:@"Test" ofType:@"txt"];

【讨论】:

  • 没有文字。谢谢。
  • 斯威夫特 let path = self.bundle.pathForResource("Test", ofType: "txt")
  • 您也可以将类型保留为 nil 并使用完整的资源名称:let path = self.bundle.pathForResource("Test.txt", ofType:nil)
  • 我找到了一个解决我遇到的非常相似的问题的答案,也从 Bundle 返回 nil,在我的情况下,与代码无关,这是导入文件的方式.谢谢里奥·达布斯。可在:stackoverflow.com/questions/34548771/…>.
【解决方案2】:

替换

let path = self.bundle.pathForResource("Test.txt", ofType: "txt") 

let path = self.bundle.pathForResource("Test", ofType: "txt") 

【讨论】:

    【解决方案3】:

    更换你的

     let path = self.bundle.pathForResource("Test.txt", ofType: "txt") 
    

    let path = NSBundle.mainBundle().pathForResource("Test", ofType: "txt") 
    

    【讨论】:

      【解决方案4】:

      ofType 参数附加到资源名称,所以替换这一行:

       let path = self.bundle.pathForResource("Test.txt", ofType: "txt") 
      

      到这样的事情:

       let path = self.bundle.pathForResource("Test", ofType: "txt") 
      

      Build Bundle Resources也是需要检查的。

      【讨论】:

      • 当您想从测试用例中使用资源时,该资源必须处于顶级设置中。
      【解决方案5】:

      在 swift 3.0 中,用

      编写
      let path = Bundle.main.path(forResource: "Test", ofType: "txt")
      

      【讨论】:

        【解决方案6】:

        NSBundle.mainBundle().pathForResource 返回 nil 的另一个原因是文件未正确添加到目标。当您拖放捆绑文件时,请确保选中“添加到目标”复选框和“如果需要复制项目”复选框。

        【讨论】:

          【解决方案7】:

          对于那些试图在单元测试中访问资源的人,我遇到了一个问题,即在主包中找不到资源,我的解决方案是在所有包中搜索路径,这样我就不会必须指定一个包标识符,其中fileName 是传递给函数的字符串,当然类型可以是您想要的任何类型。

          NSString *path;
          
          for (NSBundle *bundle in [NSBundle allBundles]) {
              path = [bundle pathForResource:fileName ofType:@"json"];
              if (path) {
                  break;  // Here is your path.
              }
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-02-27
            • 1970-01-01
            • 1970-01-01
            • 2023-03-03
            • 2015-02-25
            • 2012-03-09
            相关资源
            最近更新 更多