2012-04-07 5 views
1

私はいくつかのjson出力を読んでいます...ちょうどいくつかの整数。最初のNSLogは物事を完璧に出力します。この場合、3つの要素があります。私は特定の要素にアクセスする方法を理解していません。NSMutableArrayから "int"を取得していますか?

NSMutableArray *json = (NSMutableArray*)[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; 

NSLog(@"json: %@ \n",json); 

int count = (int)[json objectAtIndex:0]; 
int count1 = (int)[json objectAtIndex:1]; 
int count2 = (int)[json objectAtIndex:2]; 
NSLog(@"count %i %i %i\n",count,count1,count2); 
+0

なぜ、idがメソッド-intValueを宣言すると、intにキャストされるのですか? – CodaFi

答えて

3

それはそれらがNSNumbersある可能性が高いです。これを試してみてください:

int count = [[json objectAtIndex:0] intValue]; 
int count1 = [[json objectAtIndex:1] intValue]; 
int count2 = [[json objectAtIndex:2] intValue]; 
NSLog(@"count %i %i %i\n",count,count1,count2); 
+0

それは..ありがとうございます。 –

3

NSArrayオブジェクトが含まれている、あなたは、これは動作しません、intにキャストべきではありません。コードを確認し、NSJSONSerializationの出力を確認します。それは整数だ場合、それは通常NSNumberのインスタンスですので、試してみてください。

int count = [[json objectAtIndex:0] intValue]; 
2

NSArrayNSMutableArrayは、キーまたは値としてint sおよびその他の非IDオブジェクトを使用することはできませんので、キャストが仕事に行くのではありません。ほとんどの値は、あなたがそれらにintValueを呼び出す必要がありますので、タイプNSNumberである:

int count = [[json objectAtIndex:0] intValue]; 
int count1 = [[json objectAtIndex:1] intValue]; 
int count2 = [[json objectAtIndex:2] intValue]; 
関連する問題