Created
June 13, 2014 18:24
-
-
Save PaulSolt/47f5d75805f6a91e14d3 to your computer and use it in GitHub Desktop.
Load Data in iPhone App
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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