Skip to content

Instantly share code, notes, and snippets.

@PaulSolt
Created June 13, 2014 18:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PaulSolt/47f5d75805f6a91e14d3 to your computer and use it in GitHub Desktop.
Save PaulSolt/47f5d75805f6a91e14d3 to your computer and use it in GitHub Desktop.
Load Data in iPhone App
- (void)loadData {
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *filepath = [documentsDirectory stringByAppendingPathComponent:@"cityData.txt"];
NSError *error = nil;
NSString *textFromFile = [NSString stringWithContentsOfFile:filepath encoding:NSUTF8StringEncoding error:&error];
if(error) {
NSLog(@"Error: Unable to load file: %@", [error localizedDescription]);
} else {
// Use the data
NSArray *lineArray = [textFromFile componentsSeparatedByString:@"\n"];
// Parse the line into 3 parts (city - city - distance)
for(NSString *line in lineArray) {
NSArray *singleLineArray = [line componentsSeparatedByString:@"-"];
NSString *firstCity = singleLineArray[0];
NSString *secondCity = singleLineArray[1];
NSString *distanceInMiles = singleLineArray[2];
NSLog(@"City 1: %@ to City 2: %@ is %@ miles", firstCity, secondCity, distanceInMiles);
// TODO: store the distance in a NSDictionary with a custom NSObject
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment