Skip to content

Instantly share code, notes, and snippets.

@xionglun
Last active January 3, 2016 13:58
Show Gist options
  • Save xionglun/8472649 to your computer and use it in GitHub Desktop.
Save xionglun/8472649 to your computer and use it in GitHub Desktop.
iOS read file in project.

If you use add file add some files in your project. You can access them like this:

    NSError *error;
    NSString *filepath = [[NSBundle mainBundle] pathForResource:@"hello" ofType:@"txt"];  // I add __hello.txt__ into my project.
    NSString *filecontent = [NSString stringWithContentsOfFile:filepath encoding:NSUTF8StringEncoding error:&error];
    NSLog(@" content: %@", filecontent);
    

And If you want read file line by line. You can do this:

     // this array's length is the number of lines.
     NSArray *fileContent = [[NSString stringWithContentsOfFile:filepath encoding:NSUTF8StringEncoding error:&error] componentsSeparatedByString:@"\n"];
     
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment