一,文本文件的内容。

【读书笔记】iOS-读取文本文件

 

二,工程目录

 

【读书笔记】iOS-读取文本文件

 

三,ViewController.m

【读书笔记】iOS-读取文本文件
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //读取文本文件
    [self readFromText];
}
#pragma -mark -functions
//读取文本文件
-(void)readFromText
{
    NSError *error;
    NSString *textFieldContents=[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"chenlihua" ofType:@"txt"] encoding:NSUTF8StringEncoding error:&error];
    NSLog(@"--textFieldContents---%@-----",textFieldContents);
    if (textFieldContents==nil) {
        NSLog(@"---error--%@",[error localizedDescription]);
    }
    NSArray *lines=[textFieldContents componentsSeparatedByString:@"\n"];
    NSLog(@"number of lines in the file:%ld",[lines count]);
}
【读书笔记】iOS-读取文本文件

 

四,输出如下图所示:

 

【读书笔记】iOS-读取文本文件
2015-07-29 22:12:32.692 读取文本文件[1362:70871] --textFieldContents---1
2
3
4
5
6
7
8
9
-----
2015-07-29 22:12:32.692 读取文本文件[1362:70871] number of lines in the file:10
2015-07-29 22:16:09.573 读取文本文件[1362:73057] Terminating since there is no system app.
【读书笔记】iOS-读取文本文件

 

参考资料:《iOS数据库应用高级编程(第2版)》

相关文章:

  • 2021-09-04
  • 2022-12-23
  • 2022-12-23
  • 2022-02-12
  • 2021-08-05
  • 2021-08-14
猜你喜欢
  • 2021-09-24
  • 2021-05-22
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-18
  • 2022-01-01
相关资源
相似解决方案