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 nsbundle *mybundle = [NSBundle Mainbundle] by using the following method:
In general, we use this method to get bundles. If you need resources for other directories, you can specify a path to get bundle NSBundle *goodbundle; Goodbundle = [NSBundle bundlewithpath:@ "~/.myapp/good.bundle"];
Once we have the NSBundle object, we can access 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, directly using nsbundle:bool successful = [NSBundle loadnibnamed:@ "about" Owner:someobject] ; Note Oh, we've specified an object someobject as the nib file's Owner
使用initWithContentsOfFile时,文件路径的写法 使用initWithContentsOfFile方法可以通过读取一个文件的内容来初始化对象。 但文件的路径应该怎么确定呢? 可以使用NSBundle的对象来获取。 例如当前程序所在目录下有个文件re.xml,我们要将该文件的内容做为NSData的数据源来初始化一个NSData对象,可以用下面的方法来实现: |
NSString*filePath = [[NSBundle mainBundle] pathForResouse:@"re" ofType:@"xml"]; NSData *data = [[NSDataalloc] initWithContentsOfFile:filePath]; |
NSString*dataPath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"]; self.data = [NSArrayarrayWithContentsOfFile:dataPath]; |
NSString* thePath=[selfgetUserDocumentDirectoryPath]; |
NSMutableString* fullPath=[[[NSMutableStringalloc]init]autorelease]; |
[fullPath appendString:thePath]; |
NSString* idString=[idArray objectAtIndex:indexPath.row]; |
NSString* coverName=[NSStringstringWithFormat:@"/%@.jpg",idString]; |
[fullPath appendString:coverName]; |
NSFileManager*defaultManager; |
defaultManager = [NSFileManagerdefaultManager]; |
- (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]; |
2014.12.6 nsbundle Details of Use