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 the 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.
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.
A nsbundle bundle, which is a specific type of file in which the content follows a specific structure.
One of the main functions of nsbundle is to obtain resources in the Resource folder .
When using [NSData datawithcontentoffile:@ "foo"] in programming, it is always impossible to read the correct file contents. It is possible to use [NSData datawithcontentoffile:[[nsbundle mainbundle] pathforresource:@ "foo" oftype:@ "".
Because when using relative path, in fact his relative current directory is not the directory that the program runs, but "/". Only the path generated using [NSBundle Mainbundle] is the true path to the file. Here's a note: instead of using any relative path directly in future development, use the absolute path after the calculation.
I. Getting pictures
1. NSString *path = [[Nsbuddle mainbuddle] pathforresource:@ "resourcename" [email protected] "resourcetype"];
UIImage *image = [[UIImage Imagewithcontentsoffile:path];
2. UIImage *image = [UIImage imagenamed:@ "ImageName"];
Two. Get the plist file
Nsarray *array =[[nsarrayalloc]initwithcontentsoffile:[[nsbundlemainbundle]pathforresource:@ "name" ofType:@ "plist "]];
Nsdictionary *dict=[arrayobjectatindex:index];//Convert the contents of the plist file into a dictionary-------turn (http://blog.csdn.net/bihailantian1988 /article/details/7703358)
Finally remember: the path used by iOS development is the bundle path
Bundle paths used in iOS development