When I attempt to create an NSNumber using the numberWithLongLong with a number greater than -2 and less than 13 it returns a number that is casted as an (int).
I see this if I look at the Xcode debugger after stepping over my line.
NSNumber* numberA = [NSNumber numberWithLongLong:-2]; //Debugger shows as (long)-2
NSNumber* numberB = [NSNumber numberWithLongLong:-1]; //Debugger shows as (int)-1
NSNumber* numberC = [NSNumber numberWithLongLong:12]; //Debugger shows as (int)12
NSNumber* numberD = [NSNumber numberWithLongLong:13]; //Debugger shows as (long)13
To put my problem in context, I am using a long long value for an epoch date that I will end up serializing using BSON and sending across the wire to a webservice. The webservice requires the date to be a java Long.
Thanks in advance
You have discovered that NSNumber (actually, its CFNumber counterpart) has a cache for integers between -1 and 12 inclusive. Take a look at the CFNumberCreate
function in CFNumber.c
to see how it works.
It looks like you can force it not to use the cache by passing your own allocator to CFNumberCreate
. You'll need to look at the CFAllocator
documentation.
But note that the CFNumberCreate
manual says this:
The
theType
parameter is not necessarily preserved when creating a newCFNumber
object.
So even if you bypass the cache, you might not get back an object whose objCType
is q
(which means long long
). It looks like the current implementation will return q
but that could change in a future version.
You are allowed to write your own NSNumber
subclass if you need to guarantee that objCType
returns q
. Read “Subclassing Notes” in the NSNumber Class Reference.