Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
like image 407
Yadnesh Avatar asked Jun 07 '11 09:06

Yadnesh


3 Answers

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

like image 116
Neelam Verma Avatar answered Oct 01 '22 20:10

Neelam Verma


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

like image 31
Yadnesh Avatar answered Oct 01 '22 21:10

Yadnesh


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

like image 33
dawid Avatar answered Oct 01 '22 20:10

dawid