NSBundle understanding and mainBundle class method explanation, nsbundlemainbundle

Source: Internet
Author: User

NSBundle understanding and mainBundle class method explanation, nsbundlemainbundle

Similar

 NSString *file = [[NSBundle mainBundle] pathForResource:name ofType:nil];

This code generates the full file path.

So what is it: first look at the NSBundle class and inherit the NSObject class directly.

This class object represents the location of the code and resource files in the app in the file system. In other words, it is to locate the resources used by the Program (Code, graphics, music and other data) in the file system, and can dynamically load, or unload executable code.

Our program is a bundle. in the Finder, an application looks no different from other files. but it is actually a directory that contains nib files, compiled code, and other resources. we call this directory the main bundle of the program. In xcode, xcode generates the corresponding resource directory package when using the application, framework, or plug-in.

 

Class method:

MainBundle method details

+ (NSBundle *)mainBundle

Returns an NSBundle class object, which is a full path. This path stores the currently executable app path or returns nil. After the app and Build, the resource file is directly copied to the root directory, so the read method should be as follows:

NSString *earth = [[NSBundle mainBundle] pathForResource:@"Brad Cox" ofType:@"png"];

After compilation, all mainBundle resources are put under RootFolder. Therefore, you can directly access them without specifying the internal path. Generally, this mainbundle is the absolute path of the current executable app under the root directory,

That is:

Path starting from the finder root directory + app name +. app suffix. Resources are directly accessed in the app package. For example;

/Users/dashuai/Library/Application Support/iPhone Simulator/5.1/Applications/3B8EC78A-5EEE-4C2F-B0CB-4C3F02B996D2/tomcat. app/drink.png

 

Related Article

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.