0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Objective-C】NSArrayやNSDictionaryにint型を入れる方法

Posted at

NSNumber型にキャスト

NSArrayやNSDictionaryにはNSObjectしか入れることができないという制約があります。
そのためint型を格納する際には、NSNumberでキャストする必要があります。
以上の例を下記に記します。

int one = 1;
int ten = 10;
int zero = 0;

//NSMutableArray型に格納
NSMutableArray *array = [NSMutableArray array];
[array addObject:[NSNumber numberWithInt:one]];
[array addObject:[NSNumber numberWithInt:ten]];
[array addObject:[NSNumber numberWithInt:zero]];

//NSMutableDictionary型に格納
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject: [NSNumber numberWithInt:one] forKey:@"one"];
[dict setObject: [NSNumber numberWithInt:ten] forKey:@"ten"];
[dict setObject: [NSNumber numberWithInt:zero] forKey:@"zero"];
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?