Yadnesh
Yadnesh

Reputation: 1327

accessing a file using [NSBundle mainBundle] pathForResource: ofType:inDirectory:

I have a file paylines.txt added inside the folder named TextFiles which resides inside the Resources folder of my iOS project in Xcode.

This is the code I use to access the file:

NSString* filePath = [[NSBundle mainBundle] pathForResource:@"paylines" ofType:@"txt" inDirectory:@"TextFiles"];
NSLog(@"\n\nthe string %@",filePath);

The code prints:

2011-06-07 14:47:16.251 slots2[708:207] 

the string  (null)

Upvotes: 44

Views: 109883

Answers (10)

Rajneesh071
Rajneesh071

Reputation: 31073

Try this, it's working for me.

NSString *str = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"paylines.txt"];  

For simulator

NSURL *rtfUrl = [[NSBundle mainBundle] URLForResource:@"paylines" withExtension:@".txt"];

Upvotes: 11

Brian Ogden
Brian Ogden

Reputation: 19212

After following @Neelam Verma's answer or @dawid's answer, which has the same end result as @Neelam Verma's answer, difference being that @dawid's answer starts with the drag and drop of the file into the Xcode project and @Neelam Verma's answer starts with a file already a part of the Xcode project, I still could not get NSBundle.mainBundle().pathForResource("file-title", ofType:"type") to find my video file.

I thought maybe because I had my file was in a Group nested in the Xcode project that this was the cause, so I moved the video file to the root of my Xcode project, still no luck, this was my code:

 guard let path = NSBundle.mainBundle().pathForResource("testVid1", ofType:"mp4") else {
        print("Invalid video path")
        return
    }

Originally, this was the name of my file: testVid1.MP4, renaming the video file to testVid1.mp4 fixed my issue, so, at least the ofType string argument is case sensitive.

Upvotes: 1

RawMean
RawMean

Reputation: 8717

All you need to do is make sure that the checkbox shown below is checked.

enter image description here

Upvotes: 4

Neelam Verma
Neelam Verma

Reputation: 3274

I was also having the same problem. The Solution i found is ( in xcode 4.x):

Go to : Target -> "Build Phases" -> "copy bundle Resources" Then add that particular file here. If that file is already added , delete it and add it again.

clean the project and RUN. It works. :)

Upvotes: 167

dawid
dawid

Reputation: 374

When I drag files in, the "Add to targets" box seems to be un-ticked by default. If I leave it un-ticked then I have the problem described. Fix it by deleting the files then dragging them back in but making sure to tick "Add to targets". enter image description here

Upvotes: 10

evanflash
evanflash

Reputation: 377

Are you attempting to do this inside of an XCTest and on the verge of smashing your laptop? This is the thread for you: Why can't code inside unit tests find bundle resources?

Upvotes: 3

megha
megha

Reputation: 483

In case of Mac OSX,

Go to Targets -> Build Phases click + to Copy new files build phases Select product directory and drop the file there.

Clean and run the project.

Upvotes: 1

YSR fan
YSR fan

Reputation: 735

Select Project / Specific Folder --> Right Click --> Add Files to Project -- > Select the File you want to add.

This worked for me.

Upvotes: 0

Valeriy
Valeriy

Reputation: 71

Go to "Target" -> "Build Phases", select your target, select the “Build Phases” tab, click “Add Build Phase”, and select “Add Copy Files”. Change the destination to “Products Directory”. Drag your file into the “Add files” section.

Upvotes: 1

Yadnesh
Yadnesh

Reputation: 1327

well i found out the mistake i was committing i was adding a group to the project instead of adding real directory for more instructions

Upvotes: 10

Related Questions