How to know the type of number stored on NSNumber?

470 Views Asked by At

How to know the type of the number is stored on NSNumber object? In other words, is there a way to check the entry type of the number when NSNumber was created?

Let's suppose that I store some random number on NSNumber object:

NSNumber *intNumber = [NSNumber numberWithInt:1];

Of course I will want to retrieve it using the intValue method later:

int theValue = [intNumber intValue];

But if the number came from a unknown source, an API JSON response or whatever and I just parse it as a NSDictionary, resulting on something like this:

{
    "Some key" : 1,
    "Some other key" : 74.1234
}

We can assume that Some key has an int value, while Some other key has a double.

Is there a way to check which type is the appropriated to parse the value on each key? Something that I could use like:

for (NSNumber *number in _arrayOfNumbers) {

    if (/*number is integer*/) {

        //do integer stuff

    } else if (/*number is double*/) {

        //do double stuff
    }
}

I don't know if I would need to use this on a real situation, but I was just wondering If there is a way to check the "entry type" of the number.

Thanks in advance.

1

There are 1 best solutions below

1
On BEST ANSWER

Use CFNumberGetType

CFNumberGetType((CFNumberRef)number);

You can throw this value into a switch statement for each value of the enum CFNumberType.