2016-05-16 4 views
-4

これは私のデータです。NSDictionaryからオブジェクト全体を取得する方法

NSDictionary *dictResponse = [[NSDictionary alloc]init]; 

//Here is dictResponse value is 

( {

"created_on" = "0000-00-00 00:00:00"; 
    id = 627; 
    "modified_on" = "0000-00-00 00:00:00"; 
    name = ""; 
    "user_id" = 99; 
}, 

{ 

    "created_on" = "2016-05-06 14:43:45"; 
    id = 625; 
    "modified_on" = "2016-05-06 14:43:45"; 
    name = Ggg; 
    "user_id" = 99; 
}, 

{ 
    "created_on" = "2016-05-03 17:21:52"; 
    id = 623; 
    "modified_on" = "2016-05-03 17:21:52"; 
    name = Qwerty; 
    "user_id" = 99; 
}, 
{ 
    "created_on" = "2016-04-29 20:12:38"; 
    id = 601; 
    "modified_on" = "2016-04-29 20:12:38"; 
    name = Teat2; 
    "user_id" = 99; 
}, 
{ 

    "created_on" = "2016-04-29 20:12:27"; 
    id = 600; 
    "modified_on" = "2016-04-29 20:12:27"; 
    name = Test1; 
    "user_id" = 99; 
}, 
{ 

    "created_on" = "2016-05-09 13:04:00"; 
    id = 626; 
    "modified_on" = "2016-05-09 13:04:00"; 
    name = Testios; 
    "user_id" = 99; 
}) 

今、私は私のdictResponse

から完全な1セットのオブジェクト、すなわち

{ 

    "created_on" = "2016-04-29 20:12:27"; 
    id = 600; 
    "modified_on" = "2016-04-29 20:12:27"; 
    name = Test1; 
    "user_id" = 99; 
} 

にアクセスしたいので、私は dictResponを使用する場合se [0]またはdictResponse 1 ..エラーが発生しています。NSDictionaryから1つのセット全体を取得するにはどうすればよいですか?

以下のコンソールログ出力を確認してください。 enter image description here

+0

投稿した辞書*(dictResponse)*は有効な辞書ではありません。各内部辞書は*キー*で表されなければなりません。 – ozgur

+1

あなたの応答は辞書の配列です。 –

+0

あなたの応答は辞書の配列であり、要件ごとにそのインデックスを使用してエントリを取得できます。 –

答えて

0

あなたはそのように行うことができます:あなたはobjectForKeyメソッドを使用して、辞書からオブジェクトを取得するには

id arrResponse = dictResponse; 
if ([arrResponse isKindOfClass:[NSArray class]] && arrResponse != nil && arrResponse != (id)[NSNull null]) 
{ 
    NSArray *arrResults = arrResponse;   
//if you want retrieve specific entries then here is the code for that 
for (int i = 0 ; i<arrResults.count; i++) { 
     NSString *strName = [[arrResults objectAtIndex:i]valueForKey:@"name"]; 
     NSString *strCreated_on = [[arrResults objectAtIndex:i]valueForKey:@"created_on"]; 
     NSString *strModified_on = [[arrResults objectAtIndex:i]valueForKey:@"modified_on"]; 
     NSInteger userID = [[arrResults objectAtIndex:i]valueForKey:@"user_id"]; 
     } 
    } 
+0

はいこれが助けました!、 ありがとう! – Ganesh

0

インデックス(dictResponse [0]など)を使用して辞書内のオブジェクトにアクセスすることはできません。辞書内のオブジェクトはインデックスを持たず、キーを持っています。

NSObject* myObject = [dictResponse objectForKey:@"my_key"]; 
+0

'key'が' name'ならば、どの名前が得られますか? –

+0

キーは、オブジェクトが辞書に追加されるときに設定されます。したがって、 "Test1"という名前のオブジェクトが辞書に追加されたときにキー "Test1"が使用されていれば、[dictResponse objectForKey:@ "Test1"]で検索することができます。 –

+0

ここで 'key'は' name'で、値は 'Test1'ですので' [dictResponse objectForKey:@ "Test1"]; ' –

0

あなたの質問に十分な詳細を与えられていないので、以下は推測です:

so when I use dictResponse[0] or dictResponse[1] .. am getting error

あなたはどのようなエラーが発生しているかは決してありません。私はコンパイルエラーを実行していないと思いますか?

Yes I know this is not valid dictionary value but it is possible to get something like that value in NSDictionary, so question is not wrong, yes my response is array of dictionary. Means from console log I can give dictResponse[0] and it prints first part as expected.

正確に、あなたは型システムを破ると辞書の参照を保持として入力される変数に配列参照を格納することができます:あなたが書いたコメントのように

あなたはあなたのコードを知っているように見えるが間違っていますしかし、そうすることは良い考えではありません!

コンパイラは、dictResponse[0]を構文解析すると、配列のインデックス作成操作として認識するため、配列のインデックス付けを行うdictResponseのメソッドを探します。すべてはそれを約dictResponseを知っており、これはコンパイラの時でが起きているのを覚えて、あなたはそれがNSDictionary *で述べていることがあるので、エラーを与えて、1を見つけることができませんNSDictionaryでサポートされている配列のインデックス方法を探し、:

Expected method to read an array element not found on object of type 'NSDictionary *'

変数を正しく入力し、コードが機能するかどうかを確認してください。

HTH

関連する問題