1.概念:

An NSBundle object represents a location in the file system that groups code and resources that can be used in a program. NSBundle objects locate program resources, dynamically load and unload executable code, and assist in localization. You build a bundle in Xcode using one of these project types: Application, Framework, plug-ins.
大概翻译过来:
NSBundle 对象指代相应应用程序下的所有可用的文件系统。就是说,可以用NSBundle操作应用程序下,所有可用的资源(包括,xib文件,数据文件,图片 等)。
NSBundle 英语中的解释是:“捆,束”的意思,那我们可以理解为:
NSBundle是将程序中所有资源捆在一起的对象
 
2.bundle的说明:

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

bundle中的有些资源可以本地化.例如,对于foo.nib,我们可以有两个版本: 一个针对英语用户,一个针对法语用户. 在bundle中就会有两个子目录:English.lproj和French.lproj,我们把各自版本的foo.nib文件放到其中. 当程序需要加载foo.nib文件时,bundle会自动根据所设置的语言来加载.

通过使用下面的方法得到程序的main bundle
NSBundle *myBundle = [NSBundle mainBundle];

一般我们通过这种方法来得到bundle.如果你需要其他目录的资源,可以指定路径来取得bundle
NSBundle *goodBundle;
goodBundle = [NSBundle bundleWithPath:@"~/.myApp/Good.bundle"];

一旦我们有了NSBundle 对象,那么就可以访问其中的资源了

// 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

3.bundle的用法:

1)获取app的info.plist详细信息

版本号:Bundle version

 NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];

应用标识:Bundle identifier

 NSString *bundleId = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIdentifier"];

应用名称:Bundle display name

 NSString *appName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"];

Bundle name

 NSString *appName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleName"];

2.应用程序语言本地化

app本地化宏

 #define XLocalizedString(key, comment)        [[NSBundle mainBundle] localizedStringForKey:(key) value:@"" table:nil]

中英文两个Localizable.strings文件中键值对,例如

 "none" = "确定";
"none" = "none";

宏的用法:(返回NSString *)

 localizedString("none", "这是注释")

3.获取包内文件路径和文件

获取app包路径

NSString *path = [[NSBundle mainBundle] bundlePath];

app资源目录路径

NSString *resPath = [[NSBundle mainBundle] resourcePath];
 获取资源目录下a.bundle
NSString* path = [resPath stringByAppendingPathComponent:@"a.bundle"];
NSBundle *bundle = [NSBundle bundleWithPath:path];
 获取app包的readme.txt文件路径
NSString *path = [[NSBundle mainBundle] pathForResource:@"readme" ofType:@"txt"];



//一旦我们有了bundle,就可以访问其中的资源文件了。
NSString path = [otherBundle pathForImageResource:@"img"];
NSImage img = [[NSImage alloc] initWithContentsOfFile:path];
//bundle中可以包含一个库. 如果我们从库得到一个class, bundle会连接库,并查找该类:
Class newClass = [otherBundle classNamed:@"Person"];
id person = [[newClass alloc] init];
//如果不知到class名,也可以通过查找主要类来取得
Class aClass = [otherBundle principalClass];
id classInstance = [[aClass alloc] init];
//可以看到, NSBundle有很多的用途.在这章中, NSBundle负责(在后台)加载nib文件. 我们也可以不通过NSWindowController来加载nib文件, 直接使用NSBundle:
BOOL flag = [NSBundle loadNibNamed:@"ViewController" 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文件的创建和使用(二)的更多相关文章

  1. Bundle文件的创建和使用(一)

    经常会出现某个需求:将自己的模块或者开放类,封装成静态库给其他人提供方便的调用. 但是当你的模块中需要大量使用xib,图片,音频或者其他资源文件时,无法添加至静态库.这个时候就需要将一些资源文件封装至 ...

  2. iOS 封装SDK以及封装时bundle文件的处理

    这篇教程的主要目的是解释怎么样在你的iOS工程中创建并使用一个SDK,俗称.a文件. 环境:xcode 9.0 创建一个静态库工程 打开Xcode,点击File\New\Project, 选择iOS\ ...

  3. linux初级学习笔记二:linux操作系统及常用命令,文件的创建与删除和命名规则,命令行展开以及linux中部分目录的作用!(视频序号:02_3)

    本节学习的命令:tree,mkdir,rmdir,touch,stat,rm 本节学习的技能:Linux中主要的目录作用以及特殊的目录文件: 文件的命名规则,命令行展开: 文件的创建与删除: Linu ...

  4. ReactNative生成android平台的bundle文件命令

    ReactNative生成android平台的bundle文件命令 2016年11月03日 23:23:28 阅读数:4869 注:如果assets文件没有正确生成,需要手机创建或授权 网上的其它的很 ...

  5. 🙈 如何隐藏你的热更新 bundle 文件?

    如果你喜欢我写的文章,可以把我的公众号设为星标 ,这样每次有更新就可以及时推送给你啦. 前段时间我们公司的一个大佬从一些渠道得知了一些小道消息,某国民级 APP 因为 Apple App Store ...

  6. 自定义 bundle 包的创建

    在我们使用第三方框架时,常常看到XXX.bundle的文件. 我们找到该文件,显示包内容,大致看到很多资源文件:图片.配置文本.XIB文件……   什么是Bundle文件? 简单理解,就是资源文件包. ...

  7. 用 C# 轻松读取、改变文件的创建、修改、访问时间

    创建时间是文件存入到电脑中的时间,而修改时间则是改变起内容的最后时间 // 读取文件的创建.修改.访问时间FileInfo fi = new FileInfo("C://test.txt&q ...

  8. Linux_文件及文件夹[创建][复制][移动][删除][重命名]

    一.文件/文件夹创建 1.文件的创建 touch , vi/vim/nano , ... 语   法: touch [-acfm][-d <日期时间>][-r <参考文件或目 录&g ...

  9. Eclipse+Maven创建webapp项目<二> (转)

    Eclipse+Maven创建webapp项目<二> 1.开启eclipse,右键new——>other,如下图找到maven project 2.选择maven project,显 ...

随机推荐

  1. HDU2841 Visible Trees(容斥原理)

    题目..大概就是有个m*n个点的矩形从(1,1)到(m,n),问从(0,0)出发直线看过去最多能看到几个点. 如果(0,0)->(x,y)和(0,0)->(x',y')两个向量平行,那后面 ...

  2. ZOJ 3911 Prime Query ZOJ Monthly, October 2015 - I

    Prime Query Time Limit: 1 Second      Memory Limit: 196608 KB You are given a simple task. Given a s ...

  3. 【BZOJ】1087: [SCOI2005]互不侵犯King(状压dp)

    http://www.lydsy.com:808/JudgeOnline/problem.php?id=1087 状压dp是第一次写啊,我也是才学TAT.状压dp一般都用一个值表示集合作为dp的一个状 ...

  4. FFT模板

    我终于下定决心学习FFT了. orzCHX,得出模板: #include<cstdio> #include<cctype> #include<queue> #inc ...

  5. libtiff4.04

    http://www.linuxfromscratch.org/blfs/view/svn/general/libtiff.html 安装方法 : ./configure --prefix=/usr ...

  6. JavaScript事件大全3

    //无模式的提示框 //屏蔽按键 <html> <head>    <meta http-equiv="Content-Type" content=& ...

  7. 处理异常的 try,catch

    try catch finally 1.将预见可能引发异常的代码包含在try语句块中. 2.如果发生了异常,则转入catch的执行.catch有几种写法: catch 这将捕获任何发生的异常. cat ...

  8. C#常用功能函数小结(.NET 4.5)

    今天有空,把C#常用的功能总结一下,希望对您有用.(适用于.NET Framework 4.5) 1. 把类转换为字符串(序列化为XML字符串,支持xml的namespace) using Syste ...

  9. String对象的方法

    Function Description Example CompareTo() Compares one string to another ("Hello").CompareT ...

  10. html5_d登陆界面_注册界面

    <!DOCTYPE html><html><head><script type="text/javascript">function ...