2014-06-23 69 views
-6

我有一個NSMutableArray,看起來完全一樣:獲取特定的NSMutableArray值

(
    { 
     nodeChildArray = (
      { 
       nodeContent = 11111122; 
       nodeName = MyId; 
      }, 
      { 
       nodeName = ShowTask; 
      }, 
      { 
       nodeContent = 11115295; 
       nodeName = MyNumber; 
      }, 
      { 
       nodeContent = 000003; 
       nodeName = CatId; 
      }, 
     ); 
     nodeName = Request; 
    }, 
    { 
     nodeChildArray = (
      { 
       nodeContent = 22222233; 
       nodeName = MyId; 
      }, 
      { 
       nodeName = ShowTask; 
      }, 
      { 
       nodeContent = 11115295; 
       nodeName = MyNumber; 
      }, 
      { 
       nodeContent = 000003; 
       nodeName = CatId; 
      }, 
    ); 
     nodeName = Request; 
    } 
) 

我如何可以創建一個包含身份識別碼的數值數組或mutableArray?例如,具有(11111122,22222233)的數組。如何獲取與nodeName匹配的nodeContent的這個值? 這就像我想要獲取特定節點名稱的nodeContent。

+0

'NSMutableArray's沒有鍵 - >值對。你確定你正在使用'NSMutableArray'數據類型而不是'NSDictionary'或類似的東西嗎? – esqew

+0

是的,我確定它是NSMutableArray :( 如何實現這一點?你有想法嗎? – user3768313

+0

一個NSMutableArray不是一個稀疏數組。要有一個索引爲22222233的元素,你的數組需要包含22222233個元素。你不需要這個,你需要一個NSMutableDictionary。或者你想要將你的nodeContent和nodeName數據存儲在一個數組中,並且使用一個NSDictionary作爲數組元素的索引 –

回答

0

看您發佈的數據,我看到字典的外部陣列。這些字典中的每一個都包含它自己的字典數組。內部字典包含您的實際數據,其中包含關鍵字NodeContent和NodeName。

我猜你想要遍歷所有最內層的字典,查找具有「MyID」的nodeName值的實體,併爲這些字典收集NodeContent值的數組。

來圖你的數據結構,這是我所看到的:

Array 
    Dictionary 
    array 
     Dictionary 

因此,代碼可能是這樣的:

NSMutableArray *nodeContents = [NSMutableArray new]; 

for (NSDictionary *outerDict in outerArray) 
{ 
    NSArray *nodeChildArray = outerDict[@"nodeChildArray]; 
    for (NSDictionary innerDict in nodeChildArray) 
    { 
    if ([innerDict[@"nodeName"] isEqualToString "MyID"]) 
    { 
     NSString *thisNodeContent = innerDict[@"nodeContent"]; 
     if (thisNodeContent != nil) 
     [nodeContents addObject: thisNodeContent]; 
    } 
    } 
} 
0

很難告訴你的NSMutableArray的樣子從你上面貼了什麼,但我會假設它包含看起來像這樣的對象:

@interface MyObject 
{ 
    NSInteger nodeContent; 
    NSString *nodeName; 
} 

你想通過所有的元素循環你的陣列,並創建每個元素一個NSDictionary鍵/值對:

NSMutableDictionary *myDictionary = [[NSMutableDictionary alloc] init]; 
// myArray is the NSMutableArray you got from (wherever) 
for (MyObject obj in myArray) 
{ 
    NSString *key = [NSString stringWithFormat:@"%d", obj.nodeContent]; 
    [myDictionary setObject:obj forKey:key]; 
} 

現在你有一個包含以下鍵一個NSMutableDictionary:

11111122 
22222233 

每個鍵都有一個與之關聯的對象。該對象與原始數組中的對象相同。然後,您可以檢索與對象:

MyObject *myObj = [myDictionary objectForKey:@"11111122"]; 
+0

我從Wsdl2Code生成的類中獲得NSMutableArray,並且在那個NSMutableArray中,如你所見,我有一個MyId字段,我需要存儲在某個數組中的MyId-s,然後在TableViewController中顯示它們。但我不知道如何獲得這些,因爲你看到所有字段都有nodeName和nodeContent。 – user3768313

+1

然後循環throu gh數組併爲每個元素創建一個NSDictionary鍵/值對。關鍵是nodeContent(轉換爲NSString)。該值可以是任何需要構建以包含該元素的信息的數據結構。 –

+0

你能幫我嗎?:對不起,謝謝你的時間。至少有一些代碼示例?再次感謝 – user3768313

0

也許像這樣的工作:

NSMutableArray *contentsForMyId = [NSMutableArray array]; 
for (NSDictionary *dict in YOURARRAY) { 
    for (NSDictionary *element in (NSArray*)dict[@"nodeChildArray"]) { 
     if ([element[@"nodeName"] isEqualToString:@"MyId"]) { 
      [contentsForMyId addObject:element[@"nodeContent"]]; 
     } 
    } 
} 
+0

我試圖做到這一點,但我得到一個錯誤,說: 屬性'append'找不到類型爲「NSMutableArray *」的對象:(「 – user3768313

+0

」它是一個代碼,它可以幫助你得到它的工作。像我想獲取特定nodeName的nodeContent – user3768313

+1

這是Objective C,不是Swift :)附加行應該是'[contentsForMyId addObject:element [@「nodeContent」]]' – JeremyP