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.
You can also add value like this
NSMutableArray *values = [NSMutableArray alloc]init];
[values addObject:[NSNumber numberWithInt:23]];
You need to use
NSNumber
, as others have said, or use anNSString
with the string representaton of the number. It really doesn't matter which, but theNSNumber
will be more efficient.You can't just throw in an
int
, let it be interpreted as a pointer, and then cast it back on the way out. The reason is that the object (that theint
will be interpreted as a pointer to) will be sent theretain
message when it is added to the array, and this will definately cause a crash since it will be an invalid pointer.You can also use:
Use
NSNumber
objects (e.g. using[NSNumber numberWithInt:value]
)Use NSNumber like this:
Or using modern Objective C syntax, you can use the following for expressions:
Or for lone numbers: