开发者

Adding NSArrays to NSDictionary

开发者 https://www.devze.com 2023-02-09 17:34 出处:网络
I am parsing a XML file with a format of: **NOTE: This is a very simplified version of the XML. There are 11 divisions, and 87 departments total

I am parsing a XML file with a format of:

**NOTE: This is a very simplified version of the XML. There are 11 divisions, and 87 departments total

<division>
    <name> Sciences </name>
    <departments>

        <department>
            <name> Computer Science </name>
        </department>

        <department>
            <name> Biology </name>
        </department>

        <department>
            <name> Chemistry </name>
        </department>

    </department>
</division>

What I am hoping to do is display this info in a UITableView, with Division names as the Sections, and the department names within each appropriate section.

I have a NSDictionary called divisionDict which I want to store NSArrays for each division; containing the departments. I also have a NSMutableArray called departmentArray, which contains each of the departments. So essentially, I want a divisionDict filled with departmentArrays.

Here is my code for parsing the XML, which works perfect, I am just having trouble storing separate arrays in the dictionary. When it goes through the parse now, and I try to print out the elements in the array with key "Sciences", it prints the departments for every division, not just the Sciences.

if(node_divisions)
{
    node_division = [TBXML childElem开发者_运维知识库entNamed:@"division" parentElement:node_divisions]; 

    while (node_division) 
    {
        node_divisionName = [TBXML childElementNamed:@"name" parentElement:node_division];];
        node_departments = [TBXML childElementNamed:@"departments" parentElement:node_division];
        node_department = [TBXML childElementNamed:@"department" parentElement:node_departments];
        divisionName = [TBXML textForElement:node_divisionName];
        while(node_department)
        {
            node_departmentName = [TBXML childElementNamed:@"name" parentElement:node_department];
            departmentName = [TBXML textForElement:node_departmentName];
            //add the department name to the array
            [departmentArray addObject:departmentName];

            node_department = node_department->nextSibling;
        }
        //add the departmentArray to the dictionary, using the division name as the key
        [divisionDict setObject:departmentArray forKey:divisionName];;
        node_division = node_division->nextSibling;
    }
}

Any help is greatly appreciated!!! I know its something simple I am missing probably but I have been looking at this for too many hours now and I just can't see it. If you need any other info, just let me know, I tried to explain everything in detail.

Also, here is a picture that hopefully helps show what I am trying to describe: http://i.stack.imgur.com/a9nSb.png


It looks like you're adding all of the departments for each division to the same array. I think you just need to create a new array for each division in the loop:

while (node_division) 
{
    departmentArray = [NSMutableArray array]; //add this line
0

精彩评论

暂无评论...
验证码 换一张
取 消