iOS Development NSBundle Object usage

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.