views:

28

answers:

2

Hi Guys,

I'm looking for some guidance/code-snippets/tutorial on passing some simple data into my app. I have some data in a text file at the moment.

Name,Age,Weight Name,Age,Weight etc, etc,etc

There is not much. It will be read only since i wont need to change it.

How can i go about reading this into my app? What do you recommend? Nothing to difficult please, as I am still learning and advanced topics are still scary. Doesnt need to be fast or awesome etc. Just work and simple to understand.

Can iphone apps parse text files that are stored in the app bundle?

Many Thanks, Code

+1  A: 

You can get a path to the file like this:

NSString* path = [[NSBundle mainBundle] pathForResource @"data" ofType: @"txt"];

And then load it into a string like this:

NSString* data = [NSString stringWithContentsOfFile: path
    encoding: NSUTF8Encoding error: NULL];

You can then use the split functions of NSString to split the data in lines and then in individual items.

Give it a try, if it does not work, post some code here.

St3fan
That sounds like an cool solution! Will give it a shot now :)
Code
thanks St3fan :)
Code
A: 

Or.. you can introduce your data in a plist file. You can create it in XCode directly.. and read it either like a dictionary or an array, depends on how you edited the plist file in the first place.

A plist is a key/value type of file (XML) so this helps a lot. Reading from a plist is very easy. Google that.

The best thing about this is that you retrieve your data very easily in the form you wanted in the first place.

Cheers

PS. Another idea to store large amounts of text is sqlite3..

lupu1001