简体   繁体   中英

NSBundle.mainBundle().pathForResource returns nil

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

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

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

构建捆绑资源

I have implemented a very simple File class with one read function with the intent to output the contents of the 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.

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

This returns "nil"

Why? :)

Do not include the .txt in the name parameter, pass it as the extension parameter.
From the documentation :

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.

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"];

在swift 3.0中,写一下

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

Replace your

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

with

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

Replace

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

with

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

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 fileName is a string passed into the function and of course the type can be anything you'd want.

NSString *path;

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

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

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

to something like this:

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

The Build Bundle Resources is also necessary to check.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM