2012-12-28 44 views
2

我使用下面的代碼:字典不復制到其他字典,出現異常:詞典的說法是不是一個的NSDictionary

NSMutableDictionary *farmGatesDict = [[NSMutableDictionary alloc] 
       initWithDictionary:[xmlDictionary objectForKey:@"FarmGates"]]; 

     NSLog(@"value from dictionary : %@", [farmGatesDict objectForKey:@"FarmGate"]); 


NSMutableDictionary *farmDetailDict=[[NSMutableDictionary alloc] 
       initWithDictionary:[farmGatesDict objectForKey:@"FarmGate"]]; 

NSLog(@"%@", farmGateString); 

我使用這一點,因爲這本詞典是一個解析的XML文件,我想遍歷那個樹。我這是farmGatesDict第一代碼工作良好,但對於第二個是farmDetailDict應用是越來越有以下異常崩潰:

- [NSDictionary的initWithDictionary:copyItems:]:字典參數不是一個NSDictionary」

請建議我是我缺乏的背後..

下面是NSLog的數據...

2012-12-28 15:40:05.223 FarmGates[6170:c07] value from dictionary : (
     { 
     address =   { 
      text = "\n  2873 Huon Highwway, 2km south of Huonville, over the bridge on river side of road."; 
     }; 
     contact =   { 
      text = "\n  Contact 03 6264 1474"; 
     }; 
     description =   { 
      text = "\n  Shed Door Sales. Arguably the longest standing and most popular road side stall in the Huon. Incorporated with a modern apple packing facility and featuring the refrigerated displays this ensures the freshest stock direct from the grower."; 
     }; 
     fruits =   { 
      fruit =    (
           { 
        text = "\n  \n   Cherries"; 
       }, 
           { 
        text = "\n   Apple"; 
       } 
      ); 
      text = "\n  "; 
     }; 
     id = 1; 
     images =   { 
      image =    (
           { 
        text = "\n  \n   a1.jpg"; 
       }, 
           { 
        text = "\n   b1.jpg"; 
       }, 
           { 
        text = "\n   c1.jpg"; 
       } 
      ); 
      text = "\n  "; 
     }; 
     latitude =   { 
      text = "\n  -43.04964"; 
     }; 
     longitude =   { 
      text = "\n  147.03903"; 
     }; 
     name =   { 
      text = "\n \n \n  Griggs Grower Direct apples and cherries"; 
     }; 
     open =   { 
      text = "\n  Open daily except Christmas and Boxing Day."; 
     }; 
     region =   { 
      text = "\n  Huon Valley"; 
     }; 
     services =   { 
      service =    { 
       text = "\n  \n   NA"; 
      }; 
      text = "\n  "; 
     }; 
     text = "\n "; 
    }, 
) 
+2

檢查是否你的* [farmGatesDict objectForKey:@ 「出場」] *返回一個字典與否。 – spider1983

+0

@Ashu:檢查我的答案.... –

+0

我得到了這個問題,當我使用包含嵌套標籤和嵌套標籤時,「(」標誌即將到來,這不是起始的xml解析字典(xmlDictionary)但是我沒有解決方案可以任何人請建議的東西.. – Ashutosh

回答

3

您正在獲取一個數組,從NSLog可以很容易地看到。

(
     { 
     address =   { 
      text = "\n  2873 Huon Highwway, 2km south of Huonvil 

所以使用objectAtIndex:第一,然後去objectForKey:

NSMutableDictionary *farmDetailDict=[[NSMutableDictionary alloc] initWithDictionary:farmGatesDict[0]]; 
+0

我沒有在我的應用程序中使用一個名爲數組的單詞,這NSLog是爲以下代碼僅用於字典NSLog(@值字典:%@「,[farmGatesDict objectForKey:@」FarmGate「]); – Ashutosh

+0

字典的Nslog以{開頭,而數組爲(這就是爲什麼我說它包含數組。 –

+0

sign即將到來是因爲我是xmlDictionary,它包含來自xml文件的解析數據,並且每當嵌套標籤出現時,就會出現這個符號,並且您是正確的問題你可以建議解決方案完全一樣,因爲我不能發送代碼作爲其項目數據 – Ashutosh

0

看來[farmGatesDict objectForKey:@"FarmGate"]沒有返回NSDictionary。檢查它是什麼類型。

0

其崩潰前行只寫下面一行..

NSLog(@"value from dictionary : %@", [farmGatesDict objectForKey:@"FarmGate"]); 

和發佈NSLog的結果。我敢打賭,你所得到的值不是NSDictionary。的

farmGatesDict objectForKey:@"FarmGate"] 

0

檢查返回值嘗試NSLog

0

我懷疑[xmlDictionary objectForKey:@"FarmGates"]不返回NSDictionary

NSMutableDictionary *farmGatesDict = [[NSMutableDictionary alloc] 
       initWithDictionary:xmlDictionary]; 

它應該工作。