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

10

44

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)
Avesta answered 7/6, 2011 at 9:33 Comment(0)
A
10

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

Avesta answered 27/1, 2012 at 7:30 Comment(1)
Yes, as it is said in the link: In the pop-up window make sure you select "Create Folder References for any added folders" instead of "Create groups for any added folders"Neodymium
J
167

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. :)

Jespersen answered 20/10, 2011 at 12:15 Comment(4)
For me the file was there, so I deleted it and added it again, cleaned and it worked!Nayarit
In my case, somehow the 'Add to Targets' checkbox for my app was unchecked; didn't realize it until I deleted the file and then added it again. Oops!Hardej
Emphasizing comments from others and other similar answers: sometimes you do this right and it still doesn't work. If so, be sure to remove the file from the bundle in that Project pane (BuildPhases/Bundle Resources), and then re-add it. And then Project>Clean. That finally worked for me!Methodical
yes, also had to delete it and re-add to get it to work. thank you.Arp
S
11

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"];
Sachsse answered 17/8, 2013 at 13:12 Comment(0)
A
10

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

Avesta answered 27/1, 2012 at 7:30 Comment(1)
Yes, as it is said in the link: In the pop-up window make sure you select "Create Folder References for any added folders" instead of "Create groups for any added folders"Neodymium
C
10

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

Cableway answered 15/4, 2015 at 10:17 Comment(0)
C
4

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

enter image description here

Coaction answered 16/5, 2016 at 5:37 Comment(0)
Z
3

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?

Zoie answered 28/5, 2014 at 18:47 Comment(0)
A
1

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.

Antiphon answered 9/12, 2013 at 14:35 Comment(1)
if you could also tell me what i did to have this file removed from the copy files list, i would appreciate itFerneferneau
S
1

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.

Synesthesia answered 25/3, 2014 at 12:4 Comment(0)
P
1

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.

Pulmonate answered 13/6, 2016 at 21:54 Comment(0)
R
0

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

This worked for me.

Rebarbative answered 28/2, 2014 at 12:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.