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]; |
博客介绍了如何在iOS开发中利用NSBundle来加载项目中的资源文件,如plist和xib。通过NSBundle的loadNibNamed方法,可以便捷地加载xib文件,仅需提供文件名。同时,NSBundle的mainBundle属性用于访问项目的资源包,实现对资源的高效管理。

937

被折叠的 条评论
为什么被折叠?



