itterating through nested array and dict

Discussion in 'iOS Programming' started by tiltem, Oct 30, 2010.

  1. tiltem, Oct 30, 2010
    Last edited by a moderator: Nov 4, 2010

    tiltem macrumors newbie

    Joined:
    Oct 18, 2010
    #1
    I have a plist with the following structure:

    <dictionary>
    - <array>
    --- <dictionary>
    --- <dictionary>
    --- <dictionary>
    --- <dictionary>
    --- <dictionary>
    - <array>
    --- <dictionary>
    --- <dictionary>
    --- <dictionary>
    --- <dictionary>


    I need to know how to add the contents of a key within the nested dictionaries into a tableViewCell from within the:

    Code:
    (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    method.
    That is each nested dictionary has a key named "type" i need the populate the table with the values for the objectForKey:mad:"type" within all arrays.

    I know how to do it for just one array, but not sure how to go to the next one. hope this makes sence, and thanks for the help!
     
  2. jvaneat macrumors newbie

    Joined:
    Nov 4, 2010
    Location:
    Colorado
    #2
    Try doing this:

    Code:
    NSEnumerator *enumerator = [baseDictionary keyEnumerator];
    id key;
    		
    while ((key = [enumerator nextObject])) {
        NSArray *array = [baseDictionary objectForKey:key];
    
        NSEnumerator *arrayEnumerator = [array objectEnumerator];
        id someDictionary;
    					
        while(someDictionary = [arrayEnumerator nextObject])
        {
            // we have the needed dictionary
            NSString *type = [someDictionary objectForKey:@"type"];
        }
    }
    
    
     
  3. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #3
    Is each array a separate section in the table?
     
  4. tiltem thread starter macrumors newbie

    Joined:
    Oct 18, 2010
    #4
    I ended up changing my plist a little. Added an array to the root:
    <array>
    -<dictionary>
    -- <array>
    --- <dictionary>
    --- <dictionary>
    --- <dictionary>
    --- <dictionary>
    --- <dictionary>
    -<dictionary>
    -- <array>
    --- <dictionary>
    --- <dictionary>
    --- <dictionary>
    --- <dictionary>

    then creating a new array to hold only the arrays with a

    Code:
    for (NSDictionary *dict in myPlist){
       if([dict objectForKey:@"theArray"]){
           [newArray addObjectsFromArray:[dict objectForKey:@"theArray"]];
      }
    }
    
    They are all in one section of the table. If there is a more direct way of doing it please share :)

    Thanks!
     

Share This Page