Bundle使用&NSBundle

 Bundle就是一个内部结构按照标准规则组织的特殊目录,理解为directory即可;

IOS的应用都是通过bundle进行封装的,对应的bundle类型是Application类型,平时我们通过XCode编译出来的Target(即我们开发的应用),其实就是一个Application类型bundle,即一个文件夹!但是Finder会把这个bundle当做一个文件显示给我们,因为这个bundle自身也是一个package,而Mac系统会把所有的package当做一个文件来对待,显示给用户,从而防止用户误操作导致程序文件损坏或丢失。

bundle对iOS开发者来讲,最大的方便就是可以非常简单地让自己的应用国际化,在不同的语言不同的地区,加载不同的资源文件,显示不同的语言文字,而实现这些只需要我们严格按照bundle的要求进行资源文件的存放即可,而不需要写大量代码判断地区语言。遵循的方法也很简单,只需要我们创建对应的"本地化文件夹"即可,例如我们要同时让图片"pic.png"在中文和英文下显示不同的内容,只需要创建两个本地化文件夹zh.lproj和en.lproj,分别放入同名但内容不同的"pic.png"即可。

bundle是一个目录,其中包含了程序会使用到的资源. 这些资源包含了如图像,声音,编译好的代码,nib文件(用户也会把bundle称为plug-in). 对应bundle,cocoa提供了类NSBundle.

我们的程序是一个bundle. 在Finder中,一个应用程序看上去和其他文件没有什么区别. 但是实际上它是一个包含了nib文件,编译代码,以及其他资源的目录. 我们把这个目录叫做程序的main bundle

 

代码:

NSBundle *myBundle = [NSBundle mainBundle];
一般我们通过这种方法来得到bundle.如果你需要其他目录的资源,可以指定路径来取得bundle
// Extension is optional
NSString *path = [goodBundle pathForImageResource:@"Mom"];
NSImage *momPhoto = [[NSImage alloc] initWithContentsOfFile:path];
bundle中可以包含一个库. 如果我们从库得到一个class, bundle会连接库,并查找该类:
Class newClass = [goodBundle classNamed:@"Rover"];
id newInstance = [[newClass alloc] init];
如果不知到class名,也可以通过查找主要类来取得
Class aClass = [goodBundle principalClass];
id anInstance = [[aClass alloc] init];
可以看到, NSBundle有很多的用途.在这章中, NSBundle负责(在后台)加载nib文件. 我们也可以不通过NSWindowController来加载nib文件, 直接使用NSBundle:
BOOL successful = [NSBundle loadNibNamed:@"About" owner:someObject];
注意噢, 我们指定了一个对象someObject作为nib的File”s Owner

获取XML文件
NSString *filePath = [[NSBundle mainBundle] pathForResouse:@"re" ofType:@"xml"];
NSData *data = [[NSData alloc] initWithContentsOfFile:filePath];

获取属性列表
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ViewControllers" ofType:@"plist"]];

//bundle实际上是一个目录,其中包含了程序会使用的资源,包括:图像,声音,编译好的代码,nib文件等,cocoa中对应的类是NSBundle Carbon中对应的是CFBundleRef
    NSBundle *bundle = [NSBundle mainBundle];
    NSLog(@"mainBundlePath:%@",[bundle bundlePath]);
    
    //使用bundle类可以访问该项目中的所有资源,mainBundle对应的是该项目的根目录,想要获取其中的资源,需要使用对应的方法
    
    //如果该路径为NULL,说明该路径下找不到指定的资源 一个路径对应着一个资源文件,然后根据path或者url把文件读入到内存里
    NSString *imagePah = [bundle pathForResource:@"button_bg" ofType:@"png"];
    NSLog(@"imagePath:%@",imagePah); 
    UIImage *image = [[UIImage alloc] initWithContentsOfFile:imagePah];
    NSLog(@"imageDescription:%@",[image description]);
    NSLog(@"image size:%d",[image size]);
    
 

 

   NSBundle *mainBundle = [NSBundle mainBundle];
   NSString *path = [mainBundle pathForResource:@"副本221" ofType:@"png"inDirectory:@"未命名文件夹"];
    
    BOOL flag = NO;
    NSLog(@"%@",path);
    flag = [[NSFileManager defaultManager] fileExistsAtPath:path];
    
    NSLog(@"%@",mainBundle.bundlePath);
//    NSLog(@"%@",mainBundle.bundleURL);
//    NSLog(@"%@",mainBundle.infoDictionary);
//    NSLog(@"%@",[mainBundle.infoDictionary objectForKey:@"CFBundleVersion"]);
    
    NSLog(@"%@", [mainBundle pathsForResourcesOfType:@"png" inDirectory:@"未命名文件夹"]);
 

 

加载视图:
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TableFooterView" owner:self options:nil];

    TableFooterView *footerView = (TableFooterView *)[nib objectAtIndex:0];
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值