2012-02-23 13 views
0

辞書内から配列を取得し、その配列オブジェクトから辞書を取得する方法を知りたいと思います。どのようにして辞書から配列を取り出し、その配列内の辞書を検索するのですか?

私はこのhttp://ios.biomsoft.com/2011/09/11/simple-xml-to-nsdictionary-converter/

itemDict 
(
     { 
     postID =   { 
      text = 26; 
     }; 
     score =   { 
      text = 20; 
     }; 
     title =   { 
      text = "http://www.google.com quiz"; 
     }; 
     url =   { 
      text = "http://www.google.com"; 
     }; 
    }, 
     { 
     postID =   { 
      text = 27; 
     }; 
     score =   { 
      text = 10; 
     }; 
     title =   { 
      text = "http://www.google.com"; 
     }; 
     url =   { 
      text = "http://www.google.com"; 
     }; 
    }, 
     { 
     postID =   { 
      text = 3; 
     }; 
     score =   { 
      text = 41; 
     }; 
     title =   { 
      text = "http://www.google.com"; 
     }; 
     url =   { 
      text = "http://www.google.com"; 
     }; 
    }, 
     { 
     postID =   { 
      text = 35; 
     }; 
     score =   { 
      text = 10; 
     }; 
     title =   { 
      text = "Sample Video"; 
     }; 
     url =   { 
      text = "http://www.google.com"; 
     }; 
    }, 
     { 
     postID =   { 
      text = 43; 
     }; 
     score =   { 
      text = 30; 
     }; 
     title =   { 
      text = "sample"; 
     }; 
     url =   { 
      text = "http://www.google.com"; 
     }; 
    } 
) 

答えて

1

いいえ、あなたがしたいことは次のとおりです。

投稿されたリンクから収集できるものは、トップレベルのNSDictionaryです。そこにはXMLが含まれています。 NSDictionaryの中には、あなたの例ではitemDictというオブジェクトがありますが、それは私の目にはNSArrayのように見えます。

ここからは、NSArrayをもう1つ入れておきます。

この配列内の各オブジェクトは、一致するキーとオブジェクトのNSDictionaryです。明らかに私はこのビットについて話している。

{ 
    postID =   { 
     text = 26; 
    }; 
    score =   { 
     text = 20; 
    }; 
    title =   { 
     text = "http://www.google.com quiz"; 
    }; 
    url =   { 
     text = "http://www.google.com"; 
    }; 
}, 

次のようなものが、十分に深く潜ることができます。

NSArray *itemArray = [xmlDictionary objectForKey:@"itemDict"]; 

for (NSDictionary *dict in itemArray) { 

    NSLog(@"Dictionary: %@", dict); 

} 

このコードには、NSArray内の各NSDictionaryの出力が表示されます。ここからは、NSDictionaryで何でもしたいという単純なケースです。

EDIT

はちょうど別の顔を持っていたし、私もでこれを追加しようと思いました。 上記のコードで各NSDictionaryを作成した後、別のオブジェクトのキーがあり、そのオブジェクトが別の辞書に格納されているようです。あなたが私に尋ねるならば、XMLで本当に奇妙な何か...

しかし、回避することができます以下の。

NSArray *itemArray = [xmlDictionary objectForKey:@"itemDict"]; 

for (NSDictionary *dict in itemArray) { 

    NSLog(@"Dictionary: %@", dict); 

    for (NSDictionary *dictObject in [dict allKeys]) { 

     NSLog(@"Dictionary Object: %@", dictObject); 

    } 

} 

これがあなたのやりたいことでない場合は、質問を編集して回答を修正することができます。

希望に役立ちます!

1

は、あなたがそれを解析し、このようなものがあるとしましょう、次のよ:

(
//object 0 
    { 
    postID= 26; 
    score = 20; 
    title = www.google.com 
    url = www.google.com 
    }, 
//object 1 
    { 
    postID= 27; 
    score = 10; 
    title = www.google.com 
    url = www.google.com 
    }... 
    ) 

これはあなたの配列です。

ザobjectAtIndex:0この例である:

{ 
postID= 26; 
score = 20; 
title = www.google.com 
url = www.google.com 
} 

辞書です。

関連する問題