2012-01-03 6 views
0

キーを使ってNSMutableArrayから値を取得する方法を知りたいと思います。配列がこのように構築されていますiPhone:キーを使ってNSMutable配列から値を取得する方法

qtype = [[NSMutableArray alloc] init]; 
    while (dicjson = (NSDictionary*)[enumerator nextObject]) { 
    question_id = [dicjson objectForKey:@"question_id"];   
    question_text = [dicjson objectForKey:@"question_text"]; 
    question_type = [dicjson objectForKey:@"question_type"]; 

    [qtype addObject:[NSDictionary dictionaryWithObject:question_type forKey:question_id]]; 

} 

私はテーブルのセル内の配列を移入しています:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
NSString *type = [qtype objectAtIndex:[indexPath row]]; 

} 

出力は私が欲しいものではありません「65 = YN」のようなものですが。私は「65」だけを抽出したいと思います。 私に何か考えてもらえれば、非常に感謝しています。

+0

これはNSMutableArrayでは不可能だと思います。 NSMutableDictionaryはこれを行います。 –

答えて

0

NSDictionaryを "qtype"に保存しているからです。あなたが欲しいものを達成することができますので、質問タイプのための辞書が1つのキーだけが含まれているようだ

NSDictionary *dict = [qtype objectAtIndex:[indexPath row]]; 
NSString *type = [[dict allValues] lastObject];//since you have only one object stored. 
1

が、覚えておいてください:試し辞書はキーでの作業順不同コレクションである、配列は注文コレクションであるのに対し、インデックスを扱うとにかく

tableView:cellForRowAtIndexPath:に、辞書で唯一のキー(質問IDである)を取得するためにコードを変更:あなたは、配列を使用する理由実際に

-(UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSArray *dictKeys = [[qtype objectAtIndex:[indexPath row]] allKeys]; 
    NSString *type = [dictKeys objectAtIndex:0]; 
} 
1

を、私は本当に理解していません辞書の

qtype = [[NSMutableArray alloc] init]; 
while (dicjson = (NSDictionary*)[enumerator nextObject]) { 
question_id = [dicjson objectForKey:@"question_id"];   
question_text = [dicjson objectForKey:@"question_text"]; 
question_type = [dicjson objectForKey:@"question_type"]; 

[qtype addObject:question_id]; 

} 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
NSString *type = [qtype objectAtIndex:[indexPath row]]; 

} 

をしかし、あなたは他の目的にQTYPEが必要な場合、あなたは

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSDictionary *dict = [qtype objectAtIndex:[indexPath row]]; 
    NSString *type = [[dict allKeys] objectAtIndex:0]; 

} 
+0

ありがとう、それは私のために働く。私はそれがサーバーからjsonコードを読み込んでいるので、そのようにする必要があります。 – user973067

0

はあなたがNSDictionaryタイプのものであり、配列の各オブジェクトにアクセスすることができます得ることができます:
あなたは、単に質問テキストの配列を構築することができます。以下はそのコードです

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSDictionary *dict = [qtype objectAtIndex:[indexPath row]]; 
    NSString *question_id = [dict objectForKey:@"question_id"];  
    NSString *question_text = [dict objectForKey:@"question_text"]; 
    NSString *question_type = [dict objectForKey:@"question_type"]; 
}