NSBundle 的使用

NSBundle 读取图片 plist text

    NSBundle *mainbundle=[NSBundle mainBundle];
    //使mainBundle 对象获取图片的路径
    NSString *imagePath=[mainbundle pathForResource:@"coffe" ofType:@"png"];
    //使用mainBundle 对象获取到 plist 文件的路径
    NSString *plistPath=[mainbundle pathForResource:@"testplist" ofType:@"plist"];
    //使用mainbundle 对象获取到 TXT 文件路径;
    NSString *txtPath=[mainbundle pathForResource:@"testText" ofType:@"txt"];

处理上面路径;

//处理以上路径的资源
    //imagePath
    UIImage *image=[[UIImage alloc] initWithContentsOfFile:imagePath];
    //plistPath 将plist 文件的内容读到字典中去;
    NSMutableDictionary *data=[[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
    NSLog(@"%@",data);

    //textpath 将text 文件内容输出到字符串中
    NSString *txtcontents=[[NSString alloc] initWithContentsOfFile:txtPath encoding:NSUTF8StringEncoding error:nil];

通过路径获取到APP的信息如 版本号 ;

    NSDictionary *dicAppInfo=[[NSBundle mainBundle] infoDictionary];
    NSString *AppName=[dicAppInfo objectForKey:@"CFBundleDisplayName"];
    NSString *AppVersion=[dicAppInfo objectForKey:@"CFBundleShortVersionString"];
    NSString *AppBuild=[dicAppInfo objectForKey:@"CFBundleVersion"];

    NSLog(@"APP应用的名称:%@",AppName);
     NSLog(@"APP应用的版本号为:%@",AppVersion);
     NSLog(@"APP应用的build版本号为:%@",AppBuild);
时间: 2024-08-18 18:00:33

NSBundle 的使用的相关文章

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

NSBundle

bundle是一个目录,其中包含了程序会使用到的资源. 这些资源包含了如图像,声音,编译好的代码,nib文件(用户也会把bundle称为plug-in). 对应bundle,cocoa提供了类NSBundle. 1.获取bundle + (NSBundle *)mainBundle; //app的目录 NSBundle *file = [NSBundle mainBundle]; NSLog(@"%@",file); 2.加载plist文件 - (nullable NSString *

NSBundle、NSArray、NSDictionay

读取plist文件 NSDictionary  *dict= [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"fileName" ofType:@"plist"]]; NSArray*arr=[NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"

NSBundle,UIImage,UIButton的使用

1.NSBundle 1> 一个NSBundle代表一个文件夹,利用NSBundle能访问对应的文件夹 2> 利用mainBundle就可以访问软件资源包中的任何资源 3> 案例: // File : 全路径 // NSBundle : 一个NSBundle代表一个文件夹 // 利用mainBundle就可以访问软件资源包中的任何资源 NSBundle *bundle = [NSBundle mainBundle]; // 获得imageData.plist的全路径 NSString *

[[NSBundle mainBundle] pathForResource:@"name" ofType:@"type"] 找不到对应的文件解决方法

最近在使用[[NSBundle mainBundle] pathForResource:@"name" ofType:@"type"]时,找不到其对应的文件,文件当时是通过 右键->add files to 的方式添加到工程里面的,但是使用[[NSBundle mainBundle] pathForResource:@"name" ofType:@"type"]时,无论如何都找不到文件,经过了重启工程 .clear工程以

NSBundle UIImageView &UIButton

1.NSBundle 1> 一个NSBundle代表一个文件夹,利用NSBundle能访问对应的文件夹 2> 利用mainBundle就可以访问软件资源包中的任何资源 3> 模拟器应用程序的安装路径 /Users/aplle/资源库/Application Support/iPhone Simulator/7.1/Applications 2.UIImageView和UIButton 1> 使用场合 * UIImageView: 如果仅仅是显示图片,不需要监听图片的点击 * UIB

iOS:NSBundle的具体介绍

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

iOS学习笔记(十三)——获取手机信息(UIDevice、NSBundle、NSLocale)

iOS的APP的应用开发的过程中,有时为了bug跟踪或者获取用反馈的需要自动收集用户设备.系统信息.应用信息等等,这些信息方便开发者诊断问题,当然这些信息是用户的非隐私信息,是通过开发api可以获取到的.那么通过那些api可以获取这些信息呢,iOS的SDK中提供了UIDevice,NSBundle,NSLocale. UIDevice UIDevice提供了多种属性.类函数及状态通知,帮助我们全方位了解设备状况.从检测电池电量到定位设备与临近感应,UIDevice所做的工作就是为应用程序提供用户

IOS开发NSBundle对象使用详解

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

NSBundle详解

首先了解一个概念,当app安装到手机或者模拟器上,整个项目会被打包成一个文件夹,如果需要在文件夹中寻找特定文件,在程序中则用到NSBundle [NSBundle mainBundle] 找到对应app的主文件夹(软件资源包),就可以访问主文件夹中任意文件. 模拟器app安装程序路径 /Users/aplle/资源库/Application Support/iPhone Simulator/7.1/Applications