一、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);
}
打印日誌如下: