Loading data from a dictionary of dictionaries into an array in Objective C for an iphone app
- by Kat
I have a UINavigationController consisting of a tableview I want to load some data into.
I have a dictionary plist containing Dictionaries for each Train Line which in turn have dictionaries for each station with the relevant information along with one string lineName. I need to collect the station Names keys and add them to an array to populate my table (This is working).
The line names are stored as a string in my lines dictionary with the key being "lineName"
Root->|
|
|->TrainLine1(Dictionary)->|
| |-> lineName (String)
| |-> Station1 (Dictionary)
| |-> Station2 (Dictionary)
|
|
|->TrainLine2(Dictionary)->|
| |-> lineName (String)
| |-> Station1 (Dictionary)
| |-> Station2 (Dictionary)
Am I going about this the wrong way? Should I reorganise my plist?
The code below crashes the app.
- (void)viewDidLoad {
self.title = @"Train Lines";
NSString *path = [[NSBundle mainBundle] pathForResource:@"lineDetails" ofType:@"plist"];
NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:path];
NSMutableArray *array = [[NSMutableArray alloc] init];
NSMutableArray *lineNameArray = [[NSMutableArray alloc] init];
NSString *key;
for (key in dictionary) {
NSMutableDictionary *secondDictionary = [NSDictionary dictionaryWithDictionary:[dictionary valueForKey:key]];
[lineNameArray addObject:key];
NSLog(@"Adding this in array:%@", key);
[array addObject:[secondDictionary objectForKey:kLineNameKey]];
}
self.trainLines = array;
self.trainLineKeys = lineNameArray;
NSLog(@"Array contents:%@", self.trainLineKeys);
[lineNameArray release];
[array release];
[dictionary release];
[super viewDidLoad];
}