Help us understand the problem. What is going on with this article?

NSArrayやNSDictionaryにintとかを入れたいとき

More than 5 years have passed since last update.

NSArrayはなんでも入れられてとっても便利なんですが、NSObjectしかダメっていう制約があります。なのでint型とかも普通にダメで、数字を普通に入れたい時でもNSNumberでキャストしないと怒られます。
以下、0,1,2っていうintを入れたい時

int int0 = 0;
int int1 = 1;
int int2 = 2;
NSMutableArray *array = [NSMutableArray array];
[array addObject:[NSNumber numberWithInt:int0]];
[array addObject:[NSNumber numberWithInt:int1]];
[array addObject:[NSNumber numberWithInt:int2]];
NSLog(@"array - %@", array);

intとか使わないで最初からNSNumber使えば楽なのかもしれないっすね。

eggmobile
趣味でiPhoneアプリやゲームを作ってます
http://www.hugkey.com
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away