【讀書筆記】iOS-讀取文本文件


一,文本文件的內容。

 

二,工程目錄

 

 

三,ViewController.m

復制代碼
- (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]);
}
復制代碼

 

四,輸出如下圖所示:

 

復制代碼
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數據庫應用高級編程(第2版)》


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM