An understanding of IOS NSBundle and an explanation of Mainbundle methods

Source: Internet
Author: User

Often see a similar

NSString *file = [[NSBundle mainbundle] Pathforresource:name Oftype:nil];

This code is used to get the full path of file.

Then what is it: First look at the NSBundle class, directly inherit the NSObject class.

The object of this class, which represents the location of the files of the Code and resources in the app in the file system, is the location of the resources (code, graphics, music, etc.) that the program uses, and the executable code can be loaded dynamically or unloaded.

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, in Xcode, when using the application, framework, or plugin, Xcode generates the corresponding resource's directory package.

Class method:

Mainbundle Method Detailed

+ (NSBundle *) mainbundle

Returns an object of the NSBundle class that is an entirely path, the path of the currently executable app that the path holds, or nil returned. After the app, build, the resource file is copied directly to the root directory, so the method of reading should be:

NSString *earth = [[NSBundle mainbundle] pathforresource:@ "Brad Cox" oftype:@ "PNG"];

After compiling, Mainbundle's resources are placed under RootFolder, so you can access them directly and don't specify internal paths. Typically, this mainbundle is the absolute path of the current executable app under the root directory,

That is

The Name+.app suffix of the path +app where the Finder root begins. Resources are accessed directly, in the app's package. such as

/users/dashuai/library/application Support/iphone simulator/9.1/applications/ 3b8ec78a-5eee-4c2f-b0cb-4c3f02b996d2/tomcat.app/drink.png

An understanding of IOS NSBundle and an explanation of Mainbundle methods

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.