NSBundle的使用,注意mainBundle和Custom Bundle的区别

摘要:
1.[NSBundlemainBundle],文件夹实际上是一个组。例如,在构建左侧的树文件管理器后,文件将直接复制到根目录,因此读取方法应如下:NSString*earth=[[NSBundlemainBundle]pathForResource:@“BradCox”的类型:@“png”];2.使用文件夹时,Build结果仍然使用相同的方法,不需要指定文件夹路径//i

1、[NSBundle mainBundle],文件夹其实是Group,如左侧的树形文件管理器

NSBundle的使用,注意mainBundle和Custom Bundle的区别第1张

Build之后,文件直接就复制到了根目录下,于是读取的方法,应该是这样:

NSString *earth = [[NSBundle mainBundle] pathForResource:@"Brad Cox" ofType:@"png"];

2、使用文件夹的时候,Build结果

NSBundle的使用,注意mainBundle和Custom Bundle的区别第2张

依然使用同样的方法,不需要制定文件夹路径

//inDirectory参数可有可无
NSString *earth = [[NSBundle mainBundle] pathForResource:@"Matthijs" ofType:@"png" inDirectory:@"Images/MoreImages"]; 
NSString *earth = [[NSBundle mainBundle] pathForResource:@"Matthijs" ofType:@"png"];

编译之后,mainBundle的资源都是放到RootFolder下,所以,可以直接访问,不要指定内部路径

3、使用其他的Bundle

NSBundle的使用,注意mainBundle和Custom Bundle的区别第3张

NSString *resourceBundle = [[NSBundle mainBundle] pathForResource:@"Resources" ofType:@"bundle"];
    NSLog(@"resourceBundle: %@", resourceBundle);
    NSString *earth = [[NSBundlebundleWithPath:resourceBundle] pathForResource:@"Matthijs"ofType:@"jpg"inDirectory:@"Images/MoreImages"];
 
    NSLog(@"path: %@", earth);

使用Custom bundle,访问内部的子文件夹,需要指定inDirectory参数,这个mainBundle不同,这是为何?

注意:Since
bundles other than the main bundle can have folders embedded inside them, to access
files inside folders of a bundle other than the main bundle it is best to use the pathFor
Resource:ofType:inDirectory: method of NSBundle to explicitly specify the folder in
which a specific file/resource exists.

免责声明:文章转载自《NSBundle的使用,注意mainBundle和Custom Bundle的区别》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇《深入理解Java内存模型》读书总结Unity3D ShaderLab 模拟精灵动画下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

WKWebView-b

上一篇文章我们使用了JavaScriptCore框架重写了之前的示例,iOS8苹果偏爱HTML5,重构了UIWebVIew,给我们带来了WKWebView,使其性能、稳定性、功能大幅度提升,也更好的支持了HTML5的新特性。这篇文章就们就拿WKWebView来小试牛刀 回到顶部 一、WKWebView Framework WKWebView的14个类与...

iOS 三种录制视频方式

随着每一代 iPhone 处理能力和相机硬件配置的提高,使用它来捕获视频也变得更加有意思。它们小巧,轻便,低调,而且与专业摄像机之间的差距已经变得非常小,小到在某些情况下,iPhone 可以真正替代它们。 这篇文章讨论了关于如何配置视频捕获管线 (pipeline) 和最大限度地利用硬件性能的一些不同选择。 这里有个使用了不同管线的样例 app,可以在Gi...

IOS开发NSString与int和float的相互转换以及字符串拼接、NSString、NSData、char* 类型之间的转换

一、NSString与int和float的相互转换 NSString *tempA = @"123"; NSString *tempB = @"456"; 1.字符串拼接 NSString *newString = [NSString stringWithFormat:@"%@%@",tempA,tempB]; 2.字符转intint intStr...

iOS 拼接字符串

NSString  *str1=@"我爱"; NSString *str2=@"祖国"; (1)  NSString *all= [NSString stringWithFormat:@"%@>%@",str1,str2];    (2)  NSString *all2= [type stringByAppendingString:subtype];...

iOS中FMDB的使用

1在日常的开发中,我们需要用到离线缓存将数据信息存入数据库,在没有网络的时候进行加载,而我们IOS用的就是sqlite3数据库,用原生的sql我们也能实现,但是书写起来比较麻烦,尤其是其它语言转过来的程序员会感觉吃力,下面我们介绍一款比较优秀还简单的第三方FMDB 2 FMDB的下载地址 https://github.com/ccgus/fmdb 3在FM...

iOS OpenURL用法简介

参考:http://www.2cto.com/kf/201401/274753.html      http://blog.sina.com.cn/s/blog_a170e5c80101gsdj.html          http://blog.csdn.net/perfect_promise/article/details/7793735      ...