2012-02-27 4 views
0

私はXMLフィードから取得してNSDictionariesとして解析するこのデータを持っています。データ構造は、このようなものです:Objective-C for inループネストNSDictionaries

item = { 
    address =  { 
    text = "1 Union Street"; 
    }; 
    data =  { 
    text = "Hello."; 
    }; 
    event_date =  { 
    text = "2012-02-27"; 
    }; 
    event_type =  { 
    text = pubQuiz; 
    }; 
    latitude =  { 
    text = "57.144994"; 
    }; 
    longitude =  { 
    text = "-2.10143170000003"; 
    }; 
    name =  { 
    text = "Mobile Tester"; 
    }; 
    picture =  { 
    text = "public://pictures/event_default.png"; 
    }; 
    time =  { 
    text = "23.00"; 
    }; 
    vname =  { 
    text = Test; 
    }; 
} 
//More items 

各サブセクション、すなわち、アドレス、データ、EVENT_DATEなどはすべてNSDictonariesです。

コレクション全体を繰り返し、各セクション内の「テキスト」を取得して、これらのプロパティを持つ「アイテム」の配列を作成したいとします。私はObjective-Cのfor..inループ構造を使ってみましたが、これまでのところ成功していませんでした。誰かがいくつかのポインタを持っていますか?ネストされたNSDictionariesをどのようにトラバースするかについての良いチュートリアルや例を読んでいただければ幸いです。

EDIT:

よしそうでは、XMLを解析した後、私はそこにその構造を取得します。私は何をしたいのは、すべての内部辞書の「テキスト」フィールドを抽出するための「アイテム」構造を横断ある - このような何か:

foreach(item in array of dictionaries) { 
    myItem.address = item.address.text; 
    myItem.data = item.data.text; 
    ... 
} 

のように。私はこれでこれが少し明確になることを願っています。

私が立ち往生している部分は、NSDictionariesを走査しながら項目のプロパティを設定する場所です。これは私がこれまで持っているものです。

出力
for(NSDictionary *item in self.eventsArray) { 
    for (NSDictionary *key in [item allKeys]) { 
     NSLog(@"%@", key); 
     id value = [item objectForKey:key]; 
     if ([value isKindOfClass:[NSDictionary class]]) { 
      NSDictionary* newDict = (NSDictionary*)value; 
      for (NSString *subKey in [newDict allKeys]) { 
       NSLog(@"%@", [newDict objectForKey:subKey]); 
      } 
     } 
    } 
} 

アドレス 1ユニオンストリート データ こんにちは。 ...

必要なプロパティを設定するために作成しているオブジェクトで適切な属性を選択する方法がわかりません。

+0

重複していますか? http://stackoverflow.com/questions/3320511/how-to-iterate-nested-dictionaries-in-objective-c-iphone-sdk – Almo

+0

各項目が何かを持つことが可能なので、ちょうど興味深い辞書の辞書ですテキスト以外? – Joe

+0

あなたが望む出力の説明はあいまいです。どのように見えるかの例を提示する必要があります。 – yuji

答えて

0

:それは、以下の辞書を作成します

NSMutableDictionary *items = [[NSMutableDictionary alloc] init]; 

for (NSDictionary *dict in dictionaries) 
{ 
    [items setValue:[dict objectForKey:@"text"] forKey:[dict description]]; 
} 

以下:

id eventsArray = [[[[XMLReader dictionaryForXMLData:responseData error:&parseError] objectForKey:@"root"] objectForKey:@"events"] objectForKey:@"event"]; 
if([eventsArray isKindOfClass:[NSDictionary class]]) 
{ 
    //there's only one item in the XML, so there's only an NSDictionary created. 
    for(NSString *item in (NSDictionary*)eventsArray) 
    { 
     //check what each item is and deal with it accordingly 
    } 
} 
else 
{ 
    //There's more than one item in the XML, an array is created 
    for(NSDictionary *item in (NSArray*)eventsArray) 
    { 
     for (NSString *key in [item allKeys]) 
     { 
      //check what value key is and deal with it accordingly 
     } 
    } 
} 

これですべての要素にアクセスできます。

0

あなたは(ここでは辞書と呼ばれる)元1からのアイテムと呼ばれる新しい辞書を作成することができます私がした項目を取得するには

item = { 
address = "1 Union Street", 
data = "Hello.", 
.... 
}