OC-NSBundle

什么是bundle

bundle是一个包含了某个程序会使用资源的目录,这些资源比如图像,编译好的代码,nib文件(UI文件)
有两种表现形式
一种是保存可执行代码 app就是一个bundle 因为每个App内部的结构都是固定的,而且App这个Bundle就是在编程中经常使用的main bundle。
另一种就是一些资源 比如图像,nib文件(UI文件)

添加到工程里面的资源会在编译的时候复制到main bundle 只有在copy bundle Resouce中的资源才会被复制过去 直接向工程中添加的资源和通过Asset Catalog方式添加的资源都会在Copy Bundle Resource中可能没有显示,这是需要点击Copy Bundle Resouce下面的"+"按钮手动添加,否则main Bundle中就不会有该资源文件,在代码中使用该资源文件时可能会出现问题

获取Bundle

获取mainBundle

NSBundle *myBundle = [NSBundle mainBundle];

获取其他Bundle

NSBundle *goodBundle;
goodBundle = [NSBundle bundleWithPath:@"~/.myApp/Good.bundle"];

获取Bundle中的资源

NSString *path = [goodBundle pathForImageResource:@"Mom"];

Bundle可以包含一个库 库里面可以包含class

bundle会连接库,并查找该类

Class newClass = [goodBundle classNamed:@"Rover"];
id newInstance = [[newClass alloc] init];

Bundle可以加载nib文件

loadNibNamed

//一
    NSBundle *bundle = [NSBundle bundleForClass:[self class]];
    if (![bundle loadNibNamed:self.className owner:self topLevelObjects:nil]) {
        return;
    }
//二
    HYLShopView *shopView=[[[NSBundle mainBundle]loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil] firstObject];

initWithNibName

    self = [super initWithNibName:NSStringFromClass([self class]) bundle:[NSBundle bundleForClass:[self class]]];
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值