ios NSBundle.mainBundle().pathForResource 返回 nil

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31238031/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 06:44:28  来源:igfitidea点击:

NSBundle.mainBundle().pathForResource returns nil

iosobjective-cswiftnsbundle

提问by JaminB

I am trying to write a simple IO wrapper for Swift.

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

To test this I have a file named "Test.txt" in my project root.

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

I have added this file to Build Phases within Build Bundle Resources, as suggested by everyone else who has had this problem.

正如其他遇到此问题的人所建议的那样,我已将此文件添加到 Build Bundle Resources 中的 Build Phases。

Build Bundle Resources

构建捆绑资源

I have implemented a very simple File class with one read function with the intent to output the contents of the file.

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

When I print the contents of the bundle I get a URI to a specific location on my filesystem, which I assume is the virtual location of the app in the simulator. Navigating to it reveals that it does indeed contain my "Test.txt" file.

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

Now all I want to do is get the path to that file.

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

I do this by calling: self.bundle.pathForResource("Test.txt", ofType: "txt")

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

This returns "nil"

这将返回“零”

Why? :)

为什么?:)

回答by SwiftArchitect

Do not include the .txtin the nameparameter, pass it as the extensionparameter.
From the documentation:

不要.txtname参数中包含 ,将其作为扩展参数传递。
文档

extension
The filename extension of the file to locate.
If you specify an empty string or nil, the extension is assumed not to exist and the file is the first file encountered that exactly matches name.

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

Swift3

斯威夫特3

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

Swift1 & Swift2

斯威夫特1 & 斯威夫特2

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

Objective-C

目标-C

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

回答by Vin

In swift 3.0, write with

在 swift 3.0 中,用

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

回答by Abhishek Sharma

Replace your

更换你的

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

with

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

回答by Kirit Vaghela

Replace

代替

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

with

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

回答by Mahadev Mandale

Another reason for NSBundle.mainBundle().pathForResource returns nil is file is not properly added to the target. When you drag and drop the file in bundle, please make sure that "Add To Target" checkbox and "Copy items if needed" checkbox is selected.

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

回答by Aly Yakan

For those of you who are trying to access resources in Unit Tests, I faced a problem where the resource was not found in the main bundle and my solution was to search for the path in all bundles, this way I don't have to specify a bundle identifier, where fileNameis a string passed into the function and of course the type can be anything you'd want.

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

NSString *path;

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

回答by Golya Gilice

The ofTypeparameter appended to the resource name, so replace this line:

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

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

to something like this:

像这样:

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

The Build Bundle Resourcesis also necessary to check.

构建包资源也是必要的检查。