2011-08-01 28 views
0

我試圖尋找我需要什麼,但我這樣做不成功。存儲在NSMutableDictionaries不知道會有多少?

我讀從一個web服務的XML字符串,我需要我的信息存儲到核心數據。我必須有它排序開始處指向所選部門的所有子部門,然後當一個SUBDEPARTMENT選擇它會列出所有在SUBDEPARTMENT的項目。 這個問題是我收到了我在數據表中的所有信息,所以它有重複的部門和子部門。 所以對它進行排序我要存儲信息的地方,我有雙手各持有在每個分部門的名稱,然後詞典的另一個數組的部門,然後字典的數組名字典的數組保存所有項目信息。

我需要的是像下面...

  1. 部辭書陣列
    1. 部字典
      1. 串保持部門名稱
      2. 陣列SUBDEPARTMENT字典
        1. 部門級字典
          1. 串保持SUBDEPARTMENT名稱
          2. 數組項詞典的
            1. 項目字典
              1. 串保持商品名稱
              2. 串保持更多的項目信息
但問題是我不知道如何做到這一點,這樣,當我第一次遇到一處我保存它,然後該部門後來我遇到的任何SUBDEPARTMENT正好可以存儲在該部門字典的數組以及子部門的任何項目都是如此。我甚至可以這樣做嗎?

(對不起它不漂亮我無法弄清楚如何把它的任何其他方式...)

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> 
+0

我認爲鍵值編碼是關鍵。 – dasdom

+0

請你可以添加一個你得到的XML字符串結構的例子嗎? –

+0

@Martin皮爾金頓:好的,我添加了它。這只是一條線,我得到了超過100個。 –

回答

1

我認爲,所有你需要的是一個額外的詞典對所有個別部門和子部門都有一個關鍵字,那麼你可以使用該字典來檢查你是否已經擁有新的部門,如果你沒有,請在適當的地方創建它。內存開銷應該可以忽略不計,額外的字典只會指向現有的對象。

+0

好的謝謝我會試試看。 –

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]; 
} 
相關問題