IOS 之 NSBundle 使用
2012-12-27 10:22
274 查看
初学IOS开发的时候,经常看到这样的代码,
[[NSBundle mainBundle] pathForResource:@"someFileName" ofType:@"yourFileExtension"];
[YourViewController initWithNibName:"YourViewController" bundle:nil];
一开始还不是很理解,通过google,慢慢的知道bundle在ios中的作用。
Bundle是什么呢?bundle是一个目录,其中包含了程序会使用到的资源. 这些资源包含了如图像,声音,编译好的代码,nib文件(用户也会把bundle称为plug-in). 对应bundle,cocoa提供了类NSBundle.bundle中的有些资源可以本地化.例如,对于foo.nib,我们可以有两个版本: 一个针对英语用户,一个针对法语用户. 在bundle中就会有两个子目录:English.lproj和French.lproj,我们把各自版本的foo.nib文件放到其中. 当程序需要加载foo.nib文件时,bundle会自动根据所设置的语言来加载.
//在程序中获得main bundle
NSBundle bundle = [NSBundle mainBundle]; //很简单
//一般我们通过这种方法来得到bundle.如果你需要其他目录的资源,可以指定路径来取得bundle
NSBundle otherBundle = [NSBundle bundleWithPath:@"~/.myApp/Good.bundle"];
//一旦我们有了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"]];
[[NSBundle mainBundle] pathForResource:@"someFileName" ofType:@"yourFileExtension"];
[YourViewController initWithNibName:"YourViewController" bundle:nil];
一开始还不是很理解,通过google,慢慢的知道bundle在ios中的作用。
Bundle是什么呢?bundle是一个目录,其中包含了程序会使用到的资源. 这些资源包含了如图像,声音,编译好的代码,nib文件(用户也会把bundle称为plug-in). 对应bundle,cocoa提供了类NSBundle.bundle中的有些资源可以本地化.例如,对于foo.nib,我们可以有两个版本: 一个针对英语用户,一个针对法语用户. 在bundle中就会有两个子目录:English.lproj和French.lproj,我们把各自版本的foo.nib文件放到其中. 当程序需要加载foo.nib文件时,bundle会自动根据所设置的语言来加载.
//在程序中获得main bundle
NSBundle bundle = [NSBundle mainBundle]; //很简单
//一般我们通过这种方法来得到bundle.如果你需要其他目录的资源,可以指定路径来取得bundle
NSBundle otherBundle = [NSBundle bundleWithPath:@"~/.myApp/Good.bundle"];
//一旦我们有了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"]];
相关文章推荐
- IOS 之 NSBundle 使用
- IOS 之 NSBundle 使用
- IOS NSBundle的使用,注意mainBundle和Custom Bundle的区别
- IOS NSBundle使用(访问文件夹)
- IOS开发-plist文件的使用-NSBundle使用
- IOS 之 NSBundle 使用
- IOS 之 NSBundle 使用
- iOS: ARC和非ARC下使用Block属性的问题
- 开源中国iOS客户端学习——(十三)使用UIWebView控件布局视图
- iOS本地推送与取消本地通知—UILocalNotification的使用
- IOS图像处理(9)使用CoreImage滤镜
- iOS swift sqlite使用,上传及定时更新
- iOS:第三方框架MJPhotoBrowser图片浏览器的使用
- iOS pch 文件的使用
- iOS-FMDB 使用
- iOS开发之使用Runtime给Model类赋值
- iOS中使用Fastlane实现自动化打包和发布
- iOS:在使用Cocoapods安装shareSDK时出现的link路径错误
- iOS开发24:使用SQLite3存储和读取数据
- 【iOS开发】简单UI部分UIButton的创建和UITextField的使用