Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you store "int" values in an NSMutableArray* or NSMutableDictionary*? Chronic problems with JSON data that come in as integers.

How do you store "int" values in an NSMutableArray or NSMutableDictionary? Chronic problems with JSON data that come in as integers.

Should I try to store these integers as NSNumber objects or just as strings that contain the integer?

How dangerous is it to do a "raw" (int) conversion on the pointers if I know the values will always be Natural Numbers (numbers>=0 including zero for null.)

The integers I'm always working with are Foreign Key Id's from a database.

like image 944
MikeN Avatar asked Jan 21 '11 22:01

MikeN


1 Answers

Use NSNumber like this:

int yourInt = 5;
[myMutableArray addObject:[NSNumber numberWithInt:yourInt]];

Or using modern Objective C syntax, you can use the following for expressions:

[myMutableArray addObject:@(2 + 3)];

Or for lone numbers:

[myMutableArray addObject:@5];
like image 180
theChrisKent Avatar answered Oct 03 '22 23:10

theChrisKent