NSBundle.mainBundle().pathForResource返回空值

36

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

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

我已将此文件添加到构建阶段中的Build Bundle Resources中,正如其他人建议的那样。

Build Bundle Resources

我实现了一个非常简单的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)!
    }
}

当我打印bundle的内容时,我得到了一个指向我的文件系统上特定位置的URI,我认为这是模拟器中应用程序的虚拟位置。转到该位置后,发现它确实包含我的“Test.txt”文件。

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

我通过调用以下代码来实现:self.bundle.pathForResource("Test.txt", ofType: "txt")

这将返回“nil”

为什么? :)


让路径等于self.bundle.pathForResource("Test.txt", ofType: "txt"),意思是寻找资源Text.txt.txt...参数中的一个txt是不必要的。 - holex
7个回答

68

请不要在name参数中包含.txt,而是将它作为extension参数传递。
来自文档

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

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")

Objective-C

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

1
没有话可说。谢谢。 - JaminB
Swiftlet path = self.bundle.pathForResource("Test", ofType: "txt") - SwiftArchitect
1
你也可以将类型保留为nil并使用完整的资源名称:let path = self.bundle.pathForResource("Test.txt", ofType:nil) - Adam Evans
我找到了一个解决方案,它适用于我遇到的非常相似的问题,也从Bundle返回nil。在我的情况下,与代码无关,而是文件导入的方式。感谢Leo Dabus。可在以下网址查看:https://dev59.com/DVsW5IYBdhLWcg3w0aDL。 - Marco

18

在 Swift 3.0 中,使用

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

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

with

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

7

替换

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

使用

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

1

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

NSString *path;

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

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

0

ofType参数附加到资源名称,因此请替换此行:

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

变成这样:

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

需要检查构建捆绑资源


资源必须在顶层设置中,才能从测试用例中使用。 - Golya Gilice

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接