Bundle is a directory that containsProgramResources used. These resources include images, sounds, and compiled resources.Code, NIB file (the user will also call the bundle plug-in). corresponding to the bundle, cocoa provides the nsbundle class.
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.
Some resources in bundle can be localized. for example, for foo. NIB, we can have two versions: one for English users and one for French users. the bundle contains two subdirectories: English. lproj and French. lproj. place the NIB file in it. when the program needs to load Foo. when a NIB file is created, the bundle is automatically loaded based on the language specified. we will discuss localization in detail in chapter 16
Use the following method to obtain the main bundle of the program.
Nsbundle * mybundle = [nsbundle mainbundle];
This method is usually used to obtain bundle. If you need resources from other directories, you can specify a path to obtain bundle.
Nsbundle * goodbundle;
Goodbundle = [nsbundle bundlewithpath :@"~ /. MyApp/Good. Bundle "];
Once we have an nsbundle object, we can access the resources in it.
// Extension is optional
Nsstring * Path = [goodbundle pathforimageresource: @ "mom"];
Nsimage * momphoto = [[nsimage alloc] initwithcontentsoffile: path];
Bundle can contain a library. If we get a class from the library, bundle connects to the library and looks for the 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 obtain
Class Aclass = [goodbundle principalclass];
Id aninstance = [[Aclass alloc] init];
We can see that nsbundle has many functions. In this case, nsbundle is responsible for loading nib files (in the background). We can also load nib files without nswindowcontroller and directly use nsbundle:
Bool successful = [nsbundle loadnibnamed: @ "about" Owner: someobject];
Note: We have specified an object someobject as the file's owner of nib.
When initwithcontentsoffile is used, you can use the initwithcontentsoffile method to initialize an object by reading the content of a file. But how should I determine the file path? AvailableNsbundle. For example, there is a file re. XML in the directory where the current program is located.NsdataTo initializeNsdataObject, which can be implemented using the following method: |
Nsstring * Filepath = [[Nsbundle Mainbundle] pathforresouse :@"Re" Oftype :@"XML"]; Nsdata * Data = [[Nsdata Alloc] initwithcontentsoffile: filepath]; |
Nsstring * Datapath = [[Nsbundle Mainbundle] pathforresource :@"Data" Oftype :@"Plist"]; Self. Data = [NsarrayArraywithcontentsoffile: datapath]; |
Nsstring * Thepath = [Self Getuserdocumentdirectorypath]; |
Nsmutablestring * Fullpath = [[[Nsmutablestring Alloc] init] autorelease]; |
[Fullpath appendstring: thepath]; |
Nsstring * Idstring = [idarray objectatindex: indexpath. Row]; |
Nsstring * Covername = [Nsstring Stringwithformat :@"/Author @.jpg", Idstring]; |
[Fullpath appendstring: covername]; |
Nsfilemanager * Defamanager manager; |
Defaultmanager = [Nsfilemanager Defaultmanager]; |
-(Bool) Removeitematpath :(Nsstring *) Path error :(Nserror **) Error Bool Boolvalue = [defaultmanager removeitematpath: fullpath error: Nil]; |
Nslog(@"Remove cover image OK"); |
-(Nsstring*) Getuserdocumentdirectorypath { |
Nsarray* Array = Nssearchpathfordirectoriesindomains( Nsdocumentdirectory, Nsuserdomainmask, Yes); |
Return [Array objectatindex: 0]; |