一、bundle
bundle是一个目录,其中包含了程序会使用到的资源.这些资源包含了如图像,声音,编译好的代码,nib文件(用户也会把bundle称为plug-in).对应bundle,cocoa提供了类NSBundle.
二、使用
工程中的目录结构
1、通过bundle获取资源路径或Url
//通过bundle获取资源路径
-(void)getPath{
NSBundle * mainBundle = [NSBundle mainBundle];
//1
//获取图片路径
NSString * myTestPngPath = [mainBundle pathForResource:@"MyTest" ofType:@"png"];
NSLog(@"myTestPath = %@",myTestPngPath);
//获取plist文件路径
NSString * myTestPlistPath = [mainBundle pathForResource:@"MyTest" ofType:@"plist"];
NSLog(@"myTestPlistPath = %@",myTestPlistPath);
//获取MP3路径
NSString * myTestMp3Path = [mainBundle pathForResource:@"MyTest" ofType:@"mp3"];
NSLog(@"myTestMp3Path = %@",myTestMp3Path);
//2
//逻辑路径下的文件可以直接使用文件名
NSString * myTestGroupPath = [mainBundle pathForResource:@"MyTestGroup" ofType:@"plist"];
NSLog(@"myTestGroupPath = %@",myTestGroupPath);
//物理路径下的文件需要补全路径才能获得
NSString * myTestFolderPath = [mainBundle pathForResource:@"folder/MyTestFolder" ofType:@"plist"];
NSLog(@"myTestFolderPath = %@",myTestFolderPath);
//3
//可以将路径转为url使用
NSURL * url = [NSURL fileURLWithPath:myTestPngPath];
//相当于
NSURL * url1 = [mainBundle URLForResource:@"MyTest" withExtension:@"png" ];
}
2、通过bundle获取plist文件内容
//获取*.plist文件及内容
- (void)getPlist {
//获取plist文件信息
NSBundle * bundle = [NSBundle mainBundle];
//获取info.plist内容
NSURL * infoUrl = [bundle URLForResource:@"Info.plist" withExtension:nil];
NSDictionary * infoDict = [NSDictionary dictionaryWithContentsOfURL:infoUrl];
NSArray * infoKeys = [infoDict allKeys];
//PS:拿到了字典就可以拿到plist文件中的内容
NSLog(@"infoDict = %@",infoDict);
NSLog(@"infoKeys = %@",infoKeys);
//获取MyTest.plist内容
NSURL * myTestUrl = [bundle URLForResource:@"MyTest.plist" withExtension:nil];
NSDictionary * myTesDict = [NSDictionary dictionaryWithContentsOfURL:myTestUrl];
NSArray * myTestKeys = [myTesDict allKeys];
NSLog(@"myTesDict = %@",myTesDict);
NSLog(@"myTestKeys = %@",myTestKeys);
//获取MyTestGroup.plist内容
NSURL * myTestGroupUrl = [bundle URLForResource:@"MyTestGroup.plist" withExtension:nil];
NSDictionary * myTesGroupDict = [NSDictionary dictionaryWithContentsOfURL:myTestGroupUrl];
NSArray * myTestGroupKeys = [myTesGroupDict allKeys];
NSLog(@"myTesGroupDict = %@",myTesGroupDict);
NSLog(@"myTestGroupKeys = %@",myTestGroupKeys);
//获取MyTestFolder内容
NSURL * myTestFolderUrl = [bundle URLForResource:@"folder/MyTestFolder.plist" withExtension:nil];
NSDictionary * myTesFolderDict = [NSDictionary dictionaryWithContentsOfURL:myTestFolderUrl];
NSArray * myTestFolderKeys = [myTesFolderDict allKeys];
NSLog(@"myTesFolderDict = %@",myTesFolderDict);
NSLog(@"myTestFolderKeys = %@",myTestFolderKeys);
}
获取info.plist内容还有一种方法
//获取Info.plist文件及内容
-(void)getInfoPlist{
//拿到主bundle
NSBundle * bundle = [NSBundle mainBundle];
#if 0
NSString * str = [bundle objectForInfoDictionaryKey:@"CFBundleIdentifier"];
NSLog(@"str = %@",str);
#elif 1
NSDictionary * infoDic = bundle.infoDictionary;
NSString * str1 = infoDic[@"CFBundleIdentifier"];
NSLog(@"str1 = %@",str1);
// 获取字典里的所有key
NSArray * keys = [infoDic allKeys];
#endif
NSLog(@"infoDic = %@",infoDic);
NSLog(@"keys = %@",keys);
NSLog(@"keysCount = %lu",(unsigned long)keys.count);
}
3、获取nib文件
//获取nib文件
-(void)getNib{
NSBundle * mainBundle = [NSBundle mainBundle];
NSArray *nibs=[mainBundle loadNibNamed:@"MyTest" owner:self options:nil];
NSLog(@"nibs = %@",nibs);
UIView * view = nibs[0];
NSArray * subViews = view.subviews;
NSLog(@"subViews = %@",subViews);
}
打印日志如下: