Download data files to iPhone

How to read data file in iPhone project? For example, let's say I have a static file called "level.dat", which is structured as follows: obstacles: 10 time: 100 obstacle1: 10,20 ...

I would like to read the contents of the file in NSString, then do the parsing. How to read the contents of a file in a line? Also, where should the level.dat file be in the project? Should it be in the "Resources" section or only in the main directory?

Thanks in advance!

+6
iphone cocoa-touch
source share
4 answers

See this answer: How to fopen () on an iPhone? , which shows how to access the resources in your kit. Once you have the path, just use [NSString stringWithContentsOfFile: encoding: error:].

NSString *path = [[NSBundle mainBundle] pathForResource: @"level" ofType: @"dat"] NSError *error = nil; NSString *data = [NSString stringWithContentsOfFile: path encoding: NSUTF8StringEncoding error: &error]; 
+18
source share

While this is not what you asked for, consider turning your files into plists. You will have to reformat them into XML, but then you can load them directly into NSDictionary with:

 dict = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"levels" ofType:@"plist"]]; 
+3
source share

Do you consider placing data in an SQLite database instead of a flat file? I find the API is very easy to use on the iPhone.

Here's how I do my entire data warehouse over the phone now.

0
source share

If you need help analyzing a data row, there will be a useful article on Cocoa For Scientists

-one
source share

All Articles