IOS开发-UI学习-NSBundle和NSHomeDirectory的区别

时间:2016-03-30 15:05:02   收藏:0   阅读:1353

NSBundle和NSURL的区别:

在项目的工程中添加一个文件,本例程添加的是aa.txt,文件的内容为百度: www.baidu.com,现在要使用NSBundle和NSURL分别去获取内容,代码如下:

 1 //    读取文件内容
 2 //    方法1:按照文件路径读取
 3     NSString *pathBundle = [[NSBundle mainBundle]pathForResource:@"aa" ofType:@"txt"];
 4     NSString *outstringbundle = [NSString stringWithContentsOfFile:pathBundle encoding:NSUTF8StringEncoding error:nil];
 5     
 6 //    方法2:按照URL读取
 7     NSURL *pathUrl = [[NSBundle mainBundle]URLForResource:@"aa" withExtension:@"txt" subdirectory:nil];
 8     NSString *outstringUrl = [NSString stringWithContentsOfURL:pathUrl encoding:NSUTF8StringEncoding error:nil];
 9     
10     NSLog(@"%@\n////////\n%@",outstringbundle,outstringUrl);

输出结果如下:

1 2016-03-30 14:48:02.939 沙盒机制and文件路径[11786:518929] 百度: www.baidu.com
2 ////////
3 百度: www.baidu.com

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!