NSBundle Introduction: It is a singleton class that is used to load resources (2011-09-22 17:51:37)
reproduced
Tags: gossip |
Category: IOS |
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 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.
Some of the resources in the bundle can be localized. For example, for foo.nib, we can have two versions: one for English-speaking users and one for French-speaking users. There will be two subdirectories in the bundle: English.lproj and French.lproj, where we put the respective versions of the foo.nib file. When the program needs to load the foo.nib file, the bundle is automatically loaded according to the language set. We will discuss the localization in detail in Chapter 16
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.
Extension is optional
NSString *path = [Goodbundle pathforimageresource:@ "Mom"];
Nsimage *momphoto = [[Nsimage alloc] initwithcontentsoffile:path];
The bundle can contain a library. If we get a class from the library, the bundle connects to the library and looks for that 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 get
Class AClass = [Goodbundle principalclass];
ID aninstance = [[AClass alloc] init];
As you can see, NSBundle has a lot of uses. In this, NSBundle is responsible for loading the nib file (in the background). We can also load nib files without Nswindowcontroller, using NSBundle directly:
BOOL successful = [NSBundle loadnibnamed:@ "about" owner:someobject];
Note Oh, we've specified an object someobject as the nib file's Owner
when using Initwithcontentsoffile, the file path is written using the Initwithcontentsoffile method to initialize the object by reading the contents of a file. But how should the path of the file be determined? You can use the NSBundle object to get it. For example, the current program in the directory has a file re.xml, we want the contents of the file as a NSData data source to initialize a NSData object, can be implemented in the following way: NSString*filepath = [[NSBundle mainbundle] Pathforresouse:@"Re"OfType:@"xml "]; NSData *data = [[NSData alloc] initwithcontentsoffile:filepath]; Read the contents of plist: NSString *datapath = [[NSBundle Mainbundle] pathforresource:@"Data"oftype:@"Plist"]; Self.data =[Nsarray Arraywithcontentsoffile:datapath]; Delete local file NSString* thepath=[self getuserdocumentdirectorypath]; Nsmutablestring* fullpath=[[[ nsmutablestring alloc]init]autorelease]; [FullPath Appendstring:thepath]; NSString* idstring=[IDArray ObjectAtIndex:indexPath.row]; NSString* Covername=[nsstring stringWithFormat:@"/%@.jpg", idstring]; [FullPath Appendstring:covername]; Nsfilemanager*Defaultmanager;defaultmanager=[Nsfilemanager Defaultmanager];-(BOOL) removeItemAtPath: (NSString *) path error: (NSERROR * *) Error BOOL boolvalue=[Defaultmanager Removeitematpath:fullpath error:nil];if(boolvalue) {NSLog (@"Remove cover image OK");}-(nsstring*) Getuserdocumentdirectorypath {Nsarray* Array =nssearchpathfordirectoriesindomains (NSDocumentDirectory, nsuserdomainmask,yes);if([array count])return[Array Objectatindex:0];Else return @"";}
OBJECTIVE-C: Reprint: NSBundle's specific introduction