存储在NSMutableDictionaries中而不知道会有多少?

时间:2011-08-01 18:02:12

标签: iphone objective-c

我试着寻找我需要的东西但是我没有成功。

我正在从webservice读取XML字符串,我需要将我的信息存储到核心数据中。我必须从Department开始排序,它指向所选部门中的所有子部门,然后当选择子部门时,它将列出该子部门中的所有项目。 这个问题是我收到了数据表中的所有信息,因此它有部门和子部门的重复。 因此,为了对它进行排序,我想将信息存储到我有一个字典数组的位置,每个字典包含每个部门的名称,然后是一个字典数组,其中包含每个子部门的名称,然后是另一个字典数组所有项目信息。

我需要它像下面这样......

  1. 部门词典数组
    1. 部门词典
      1. 持有部门名称的字符串
      2. 子部门词典数组
        1. 子部门词典
          1. 持有子部门名称的字符串
          2. 项目词典数组
            1. 项目词典
              1. 持有项目名称的字符串
              2. 持有更多项目信息的字符串
  2. 但问题是我不知道如何实现这一点,所以当我第一次遇到一个部门时,我保存它,然后我稍后遇到的那个部门的任何子部门都可以存储在该部门字典的数组中,并且任何子部门的项目也是如此。我甚至可以这样做吗?

    (对不起,我不知道如何以任何其他方式表达......)

    XML代码示例:

            <QSR_VIEWS_INVENTORY_ITEMS_LIST diffgr:id="QSR_VIEWS_INVENTORY_ITEMS_LIST1" msdata:rowOrder="0"><CompanyID>104</CompanyID><QSRInventoryItemID>111</QSRInventoryItemID><Description>Test Item 111</Description><Department>_</Department><Subdepartment>_</Subdepartment><SequenceNumber>0</SequenceNumber><CountDisplayUnitName>CA</CountDisplayUnitName><CountDisplayUnitInCase>1.0000</CountDisplayUnitInCase><ReorderAt>0.0000</ReorderAt><ReorderTo>0.0000</ReorderTo><CaseUnitName>CA</CaseUnitName><CaseInCase>1.0000</CaseInCase><PackUnitName>_</PackUnitName><PacksInCase>0.0000</PacksInCase><StackUnitName>_</StackUnitName><StacksInCase>0.0000</StacksInCase><EachUnitName>_</EachUnitName><EachInCase>0.0000</EachInCase><InLocation1>N</InLocation1><InLocation2>N</InLocation2><InLocation3>N</InLocation3><InLocation4>N</InLocation4><InLocation5>N</InLocation5><InLocation6>N</InLocation6><InLocation7>N</InLocation7><InLocation8>N</InLocation8><InLocation9>N</InLocation9><InLocation10>N</InLocation10><InLocation11>N</InLocation11><InLocation12>N</InLocation12><InLocation13>N</InLocation13><InLocation14>N</InLocation14><InLocation15>N</InLocation15><OnShiftCountSheet>Y</OnShiftCountSheet><OnDayCountSheet>Y</OnDayCountSheet><OnWeekCountSheet>Y</OnWeekCountSheet><OnMonthCountSheet>Y</OnMonthCountSheet><OnWasteCountSheet>Y</OnWasteCountSheet><EquivalentToItemID>0</EquivalentToItemID><EquivalentCaseFactor>0.0000</EquivalentCaseFactor></QSR_VIEWS_INVENTORY_ITEMS_LIST>
    

2 个答案:

答案 0 :(得分:1)

我认为您需要的是一个额外的字典,其中包含所有单个部门和子部门的密钥,然后您使用该字典检查您是否已经拥有新部门,如果不是,在适当的地方创建它。内存开销应该可以忽略不计,额外的字典只会指向现有对象。

答案 1 :(得分:0)

Answer provided by Alex Mayfield


注意:有可能比我做的更好的方法。

BOOL addI = YES;
BOOL addS = YES;
BOOL addD = YES;
int I;
int S;
int D;

for(int i = 0;  i < [DArray count]; i++)
{
    DDictionary = [DArray objectAtIndex:i];
    //NSLog([NSString stringWithFormat:@"%@ vs %@", soapResults12, [DDictionary objectForKey:@"Title"]]);
    if([soapResults12 isEqualToString:[DDictionary objectForKey:@"Title"]])
    {
        D = i;
        //NSLog(@"GOT IT");
        SArray = [DDictionary objectForKey:@"Subdepartments"];
        for(int k = 0; k < [SArray count]; k++)
        {
            SDictionary = [SArray objectAtIndex:k];
            soapResults11 = [soapResults11 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
            //NSLog([NSString stringWithFormat:@"%@ vs %@", soapResults11, [SDictionary objectForKey:@"Title"]]);
            if([soapResults11 isEqualToString:[SDictionary objectForKey:@"Title"]])
            {
                S = k;
                //NSLog(@"GOT IT");
                IArray = [SDictionary objectForKey:@"Items"];
                for(int r = 0; r < [IArray count]; r++)
                {
                    IDictionary = [IArray objectAtIndex:r];
                    //NSLog([NSString stringWithFormat:@"%@ vs %@", soapResults1, [IDictionary objectForKey:@"Title"]]);
                    if([soapResults1 isEqualToString: [IDictionary objectForKey:@"Title"]])
                    {
                        //NSLog(@"GOT IT");
                        I = r;
                        r = [IArray count];
                        addI = NO;
                        addS = NO;
                        addD = NO;
                    }
                    else
                    {
                        addI = YES;
                        addS = NO;
                        addD = NO;
                    }

                }
                k = [SArray count];

            }
            else
            {

                addS = YES;
                addD = NO;
            }

        }
        i = [DArray count];

    }
    else
    {

        addD = YES;
    }

}
if(addI && !addS && !addD)
{                        
    IDictionary = [[NSMutableDictionary alloc] init];
    [IDictionary setObject:soapResults1 forKey:@"Title"];
    [IArray addObject:IDictionary];
    [SDictionary setObject:IArray forKey:@"Items"];
    [SArray replaceObjectAtIndex:S withObject:SDictionary];
    [DDictionary setObject:SArray forKey:@"Subdepartments"];
    [DArray replaceObjectAtIndex:D withObject:DDictionary];
}
if(addS && !addD)
{
    IDictionary = [[NSMutableDictionary alloc] init];
    IArray = [[NSMutableArray alloc] init];
    [IDictionary setObject:soapResults1 forKey:@"Title"];
    [IArray addObject:IDictionary];
    SDictionary = [[NSMutableDictionary alloc] init];
    [SDictionary setObject:soapResults11 forKey:@"Title"];
    [SDictionary setObject:IArray forKey:@"Items"];
    [SArray addObject:SDictionary];
    [DDictionary setObject:SArray forKey:@"Subdepartments"];
    [DArray replaceObjectAtIndex:D withObject:DDictionary];

}    

if(addD)
{
    IDictionary = [[NSMutableDictionary alloc] init];
    IArray = [[NSMutableArray alloc] init];
    [IDictionary setObject:soapResults1 forKey:@"Title"];
    [IArray addObject:IDictionary];
    SDictionary = [[NSMutableDictionary alloc] init];
    SArray = [[NSMutableArray alloc] init];
    [SDictionary setObject:soapResults11 forKey:@"Title"];
    [SDictionary setObject:IArray forKey:@"Items"];
    [SArray addObject:SDictionary];
    DDictionary = [[NSMutableDictionary alloc] init];
    [DDictionary setObject:soapResults12 forKey:@"Title"];
    [DDictionary setObject:SArray forKey:@"Subdepartments"];
    [DArray addObject:DDictionary];
}