在项目中添加一个本地文件

例如:添加aa.txt,文件的内容为:lalala。
使用NSBundle和NSURL分别去获取内容,代码如下:

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

输出结果如下:

1.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容