Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read resources files within a framework?

I built a framework and a cocoa application in Mac OS X. The framework is dynamically linked to the application. In a class of the framework, I need to read a resource file within this framework's resources folder. The code below

NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
NSString *characterCodeTable = [resourcePath stringByAppendingPathComponent:@"pymb.txt"];

does not work since "resourcePath" is the path to application's resources folder, not the framework's.

So how can I access the framework's resource folder in the code in this framework itself?

BTW: Is there any best practice to organize miscellaneous files in a framework/application bundle?

like image 408
Lingfeng Xiong Avatar asked Jul 06 '13 18:07

Lingfeng Xiong


1 Answers

Instead of calling +[NSBundle mainBundle], call either +[NSBundle bundleWithIdentifier:] or +[NSBundle bundleForClass:]. The former takes an NSString argument of the framework's indentifier; the latter takes a Class argument of a class provided by the framework. Then, you can call the usual NSBundle paths as necessary.

Full documentation can be found here.

like image 198
mipadi Avatar answered Oct 23 '22 21:10

mipadi