accessing a file using [NSBundle mainBundle] pathForResource: ofType:inDirectory:
I have a file paylines.txt
added inside the folder named TextF开发者_运维问答iles
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)
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. :)
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
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".
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"];
All you need to do is make sure that the checkbox shown below is checked.
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?
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.
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.
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.
Select Project / Specific Folder --> Right Click --> Add Files to Project -- > Select the File you want to add.
This worked for me.
精彩评论