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

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

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

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

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

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

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

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

3、使用其他的Bundle

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.

  原文地址:http://www.cnblogs.com/iihe602/archive/2013/01/17/2865280.html

时间: 2024-08-01 18:41:11

IOS NSBundle的使用,注意mainBundle和Custom Bundle的区别的相关文章

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

1.[NSBundle mainBundle],文件夹其实是Group,如左侧的树形文件管理器 Build之后,文件直接就复制到了根目录下,于是读取的方法,应该是这样: NSString *earth = [[NSBundle mainBundle] pathForResource:@"Brad Cox" ofType:@"png"]; 2.使用文件夹的时候,Build结果 依然使用同样的方法,不需要制定文件夹路径 //inDirectory参数可有可无 NSStr

IOS NSBundle 的理解和 mainBundle 类方法详解

常看到类似的 NSString *file = [[NSBundle mainBundle] pathForResource:name ofType:nil]; 这样的代码,用来获取 file 的完全路径. 那么它是什么东西呢:先看 NSBundle 类,直接继承NSObject类. 这个类的对象,代表了 app 中代码和资源的文件在文件系统里所在的位置,通俗的说,就是定位了程序使用的资源(代码,图形,音乐等数据)在文件系统里的位置,并可以动态的加载.or卸载掉可执行代码. 我们的程序是一个bu

iOS 关于nil和Nil及null与<null>的区别

问题是这样的. NSDictionary *sample = [NSJSONSerialization JSONObjectWithData:received options:NSJSONReadingMutableLeaves error:&error]; NSString *messageInfo = [sample objectForKey:@"message"]; sample是一个字典,messsageInfo是从字典中根据key值取得的,然后通过log可以知道mes

iOS中NSNotification、delegate、KVO三者之间的区别与联系?

iOS中NSNotification.delegate.KVO三者之间的区别与联系? delegate.notification和KVO他们的功能比较类似,那么在实际的编程中,如何选择这些方式呢? 在开发ios应用的时候,我们会经常遇到一个常见的问题:在不过分耦合的前提下,controllers间怎么进行通信.在IOS应用不断的出现三种模式来实现这种通信: 1.委托delegation: 2.通知中心Notification Center: 3.键值观察key value observing,K

iOS ERROR ITMS-9000: "Missing or invalid signature. The bundle 'com.google.GPPSignIn3PResources' at

上传app时出现的错误: ERROR ITMS-9000: "Missing or invalid signature. The bundle 'com.google.GPPSignIn3PResources' at bundle path 'Payload/My_app_name.app/GooglePlus.bundle' is not signed using an Apple submission certificate." 解决办法: 把ShareSDK里的GooglePlu

IOS NSBundle使用(访问文件夹)

NSBundle的相关信息 1.一个NSBundle代表一个文件夹,利用NSBundle能访问对应的文件夹 2.利用mainBundle就可以访问软件资源包中的任何资源 3.模拟器应用程序的安装路径: /Users/aplle/资源库/Application Support/iPhone Simulator/7.1/Applications 代码:访问plist文件 NSString *path=[[NSBundle mainBundle]pathForResource:@"statuses.p

iOS:NSBundle的具体介绍

NSBundle介绍:它是一个单例类,用来加载资源 bundle是一个目录,其中包含了程序会使用到的资源. 这些资源包含了如图像,声音,编译好的代码,nib文件(用户也会把bundle称为plug-in). 对应bundle,cocoa提供了类NSBundle. 我们的程序是一个bundle. 在Finder中,一个应用程序看上去和其他文件没有什么区别. 但是实际上它是一个包含了nib文件,编译代码,以及其他资源的目录. 我们把这个目录叫做程序的main bundle bundle中的有些资源可

NSBundle 的理解和 mainBundle 类方法详解

常看到类似的 NSString *file = [[NSBundle mainBundle] pathForResource:name ofType:nil]; 这样的代码,来产生 file 的完全路径. 那么它是什么东西呢:先看 NSBundle 类,直接继承NSObject类. 这个类的对象,代表了 app 中代码和资源的文件在文件系统里所在的位置,通俗的说,就是定位了程序使用的资源(代码,图形,音乐等数据)在文件系统里的位置,并可以动态的加载.or卸载掉可执行代码. 我们的程序是一个bun

iOS Core Data: 存储自定义对象 Save Custom NSObject

思路:将NSObject转化为NSData,然后将NSData存入到Core Data中 Core Data实现 添加数据: AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; NSManagedObjectContext *context = [appDelegate managedObjectContext]; NSManagedObject *newContact; newContact = [N