Ext.: http://blog.csdn.net/guozh/article/details/8823674
I. Introduction of NSBundle
A bundle is a directory that contains the resources that the program will use.
These resources include like, sound, compiled code, NIB files (the user will also refer to bundles as plug-in).
The corresponding BUNDLE,COCOA provides class NSBundle.
Our program is a bundle. In the Finder, an application looks no different from other files.
But it's actually a directory that contains nib files, compiled code, and other resources.
We call this directory the program's main bundle.
Some of the resources in the bundle can be localized.
For example, for foo.nib, we can have two versions: one for English-speaking users and one for French-speaking users.
There will be two subdirectories in the bundle: English.lproj and French.lproj,
We put each version of the foo.nib file into it. When the program needs to load the foo.nib file, the bundle is automatically loaded according to the language you set.
Second, NSBundle common code
Get the program's main bundle by using the following method
NSBundle *myBundle = [NSBundle mainBundle];
In general, we use this method to get bundles. If you need resources for another directory, you can specify a path to get the bundle
NSBundle *goodBundle;
goodBundle = [NSBundle bundleWithPath:@"~/.myApp/Good.bundle"];
Once we have the NSBundle object, we have access to the resources in it.
// Extension is optional
NSString *path = [goodBundle pathForImageResource:@"Mom"];
NSImage *momPhoto = [[NSImage alloc] initWithContentsOfFile:path];
The bundle can contain a library. If we get a class from the library, the bundle connects to the library and looks for that class:
Class newClass = [goodBundle classNamed:@"Rover"];
id newInstance = [[newClass alloc] init];
If you do not know the class name, you can also find the main class to get
Class aClass = [goodBundle principalClass];
id anInstance = [[aClass alloc] init];
As you can see, NSBundle has a lot of uses. In this chapter, NSBundle is responsible for loading the nib file (in the background). We can also load nib files without Nswindowcontroller, using NSBundle directly:
BOOL successful = [NSBundle loadNibNamed:@"About" owner:someObject];
Note Oh, we've specified an object someobject as the nib file "s Owner
Get XML file
NSString *filePath = [[NSBundle mainBundle] pathForResouse:@"re" ofType:@"xml"];
NSData *data = [[NSData alloc] initWithContentsOfFile:filePath];
Get Property List
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ViewControllers" ofType:@"plist"]]; 3Q ~ ~ Growing up ...
iOS Development NSBundle Object usage