NSBundle.mainBundle().pathForResource(_:_:)

I am stumped.


I cannot get my code to do what I want it to do.


When I run the following code, the first line generates a nil.


if let path = NSBundle.mainBundle().pathForResource("med", ofType: nil) {

do {

let data = try String(contentsOfFile: path)

textview.text = data

} catch {

print("saveReminder failed")

let nsError = error as NSError

print("\(nsError.localizedDescription)")

}

} else {

print("Error!")

}

When I run the following code, the first line generates a nil.

It’s possible that there’s some other reason for this but in 99.9% of cases (scientifically proven! :-)

pathForResource(_:ofType)
returns nil because the resource isn’t present in your bundle. Three suggestions:
  • give the resource an extension — Resources without an extension are supported but they’re a little weird. Given that you’re reading this as a string, you can, at a minimum, use

    .txt
    .
  • check the Copy Bundle Resources build phase of your target to make sure the resource is listed there

  • check that you have the case right — iOS devices use a case sensitive file system.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

How do I check the Copy Bundle Resources build phase?

I figured out how to check the Copy Bundle Resources build phase. I checked all that you listed. They all check out.

I added the Info.plist file to the Copy Bundle Resources list and when I try to run the project I get an error that says, "The file [ProjectName] couldn't be opened because you don't have permission to view it."

Could it be looking in the wrong directory and I have to use pathForResource(_:_:inDirectory:)?


What I did to to create the resource was to select "New Data Set" from the menu that pops up when I click on the plus sign ( + ). Then I moved the file from Finder to the place in the data set for the resource. Is that all I need to do?

I added the Info.plist file to the Copy Bundle Resources list …

Don’t do that. The

Info.plist
file is integrated into your app via a completely different mechanism (the Info.plist File build setting,
INFOPLIST_FILE
).

Could it be looking in the wrong directory and I have to use

pathForResource(_:_:inDirectory:)
?

That depends on how you added the file.

What I did to to create the resource was to select "New Data Set" from the menu that pops up when I click on the plus sign ( + ).

Ah, this is the first mention you’ve made of asset catalogs. It would be helpful if you could provide that sort of info up front.

Data sets in an asset catalog are accessed via the NSDataAsset class.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

I've run into this trouble before. I don't remember ever learning how I was supposed to add the file. I had always assumed I was supposed to add the file to the assests file by adding a new data set. I don't remember learning any other way to include a file as part of my project. What am I supposed to do before I use the NSBundle.mainBundle().pathForResource(_:ofType:) statement?

If you want to use NSBundle's pathForResource method, just drag your file into the list of files (source files, storyboards, etc.) under your project name on the left side of Xcode. A blue line will show up between other files as you drag up and down the list to help you see where the file will show on the list. When you drop the file, a windows will pop up. Make sure to check "Copy items if needed" and check the box next to your project name in "Add to targets:".

Thanks.


Which do I select of the choices of Add group or Add reference?

I don't think I've ever added folders, so it didn't really matter, but I would probably use "Create groups" to make sure the files don't end up scattered around in various folders, but are all right there in the project folder on disk.

NSBundle.mainBundle().pathForResource(_:_:)
 
 
Q