Using NSBundle to load my resources

笑着哭i 提交于 2019-12-21 16:55:39

问题


I wanted to separate my resources, nib files and localization files into a common reusable bundle. And so I created a bundle for my ios application and specified resources to be included inside the bundle using build phases, copy bundle resources. But, now if I try to load the bundle, I am not able to load the bundle. I try using [NSBundle allBundles] and the array shows only the main apps bundle.

I also tried to enumerate the directory for NSApplicationPath but again the only bundle available is my application default bundle. I wanted to learn this technique and make use of it to separate my resources. Any help and suggestions would be greatly appreciated. Thank you


回答1:


[NSBundle bundleWithPath:[NSString stringWithFormat:@"%@/%@", [[NSBundle mainBundle] bundlePath], pathToYourBundleWithinTheDotAppDirectory];

Let me know how you get on.




回答2:


Try something like this:

NSBundle* bundle=[NSBundle bundleWithIdentifier: @"bundle name"];

And make sure that you have selected these options when you have dragged the bundle to the project:




回答3:


For projects with one bundle, I use:

// in this example, we load test.png from the bundle
NSString *pathToResource = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"png"];

What makes this so convenient is that when you have localized files, this provides the path the the file for the current user locale. This is handy since localized files are not in the main directory, but are rather in their own subfolders (for example, English localized files are in the @"en.lproj" subfolder), and calling them by name is a hassle because you need the full path. This method gets the path for you.



来源:https://stackoverflow.com/questions/13809982/using-nsbundle-to-load-my-resources

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!