Search code examples
iosobjective-ccocoa-touchfile-managementnsfilewrapper

Reading Resource Text Files on iPhone


This is mainly an advice question, so any pointers are much appreciated.

I have a text file that I would like to draw on when filling UITableViews. I just don't know when I should actually read the file, or even how. Do I use NSFileManager, or NSFileWrapper, or should I roll my own? Will a Object Placeholder be needed in the Nib file?

The idea is to create an "inventory" system that utilizes a navigation-tableView-tabBar combo.

I've tried to figure out something, but after starting and restarting, I realized I should ask for help.

Specs include Xcode4, some basic understanding, and about 13 different trashed projects.


Solution

  • When and how you read the file will depend largely on the file size. If it is a fairly small file, then you can read it synchronously on -(void)viewDidLoad. If it is larger, then you will need to read it asynchronously and display an activity indicator until the read is finished. Here is a simple method for reading a text file into an NSString:

    - (NSString *) getTextFromFile:(NSString *)filePath {
       return [NSString stringWithContentsOfFile:filePath usedEncoding:nil error:nil];
    }