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