Loading data from a dictionary of dictionaries into an array in Objective C for an iphone app

Posted by Kat on Stack Overflow See other posts from Stack Overflow or by Kat
Published on 2010-03-31T04:04:49Z Indexed on 2010/03/31 4:43 UTC
Read the original article Hit count: 410

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];
}

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about iphone