Violet
Violet

Reputation: 25

How can I get the file path where the file is in my project folder?

I have a project on desktop and a file in this folder so this file is in the same folder as my .m .h files. I want to get the path of this file. The NSBundle or NSHomeDerictory didn't give me the correct path. It seems the paths they gave is related to the simulator, not my working space.

Upvotes: 1

Views: 5875

Answers (1)

Jsdodgers
Jsdodgers

Reputation: 5312

What you want is to use [[NSBundle mainBundle] pathForResource:resource ofType:type]; to get the path of the file.

For example, if I were to instantiate an NSMutableArray from the contents of a plist file, I would do something like:

NSString *path = [[NSBundle mainBundle] pathForResource:@"savedArray" ofType:@"plist"];
NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:path];

You can do the same thing with resources of different types as well.

Upvotes: 4

Related Questions