Bundle其實就是包,在項目中我們會把用到的資源打到一個bundle包中。而用NSBundle可以加載項目中的資源文件,比如plist文件或者xib等文件,只需要指定文件名和類型即可。
加載plist文件
//加載plist文件 NSBundle *bunle=[NSBundlemainBundle]; //根據文件類型和文件名獲取文件的全路徑 NSString *path= [bunle pathForResource:@"photo" ofType:@"plist"]; NSArray *array=[NSArrayarrayWithContentsOfFile:path]; |
//加載row.xib文件 xib=nib loadNibNamed:只傳xib文件的名稱就可以
//加載row.xib文件 xib=nib loadNibNamed:只傳xib文件的名稱就可以 NSBundle *bundle=[NSBundlemainBundle]; NSArray *array= [bundle loadNibNamed:@"row" owner:self options:nil]; |
加載bundle文件包,NSBundle mainBundle訪問的是項目中的資源包
// 0.獲取images.bundle的全路徑 NSString *path = [[NSBundle mainBundle] pathForResource:@"images" ofType:@"bundle"];
// 1.加載images.bundle這個壓縮包 NSBundle *bundle = [[NSBundle alloc] initWithPath:path];
// 2.獲得images.bundle中所有png圖片的全路徑 NSArray *paths = [bundle pathsForResourcesOfType:@"png" inDirectory:nil]; |