LoginSignup
2
1

More than 5 years have passed since last update.

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

Last updated at Posted at 2012-12-26

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使えば楽なのかもしれないっすね。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1