Question

I an developing some code in which I use a scanner to get to NSNumbers from a string, say x and y.

Now I want to compute something simple from x and y, say, z = 10.0/(x + y/60.0)/60.0). I can't do this directly, since the compiler doesn't like ordinary arithmetic symbology applied to number objects.

So, I tried defining xD and yD, of type double, and then tried a type conversion

xD = (double) x; yD = (double) y;

but that also gives a compile error. Just how does one get NSNumber objects converted to be used in ordinary arithmetic expressions? I did considerable browsing of the literature, and didn't find the answer.

Thanks in advance for any help.

John Doner

Was it helpful?

Solution

Use the "value" methods of the NSNumber class such as doubleValue.

// Initialized to something earlier
NSNumber * x, y;

//Convert NSNumbers to double
double xD = [x doubleValue]; 
double yD = [y doubleValue];

// You can now use regular arithmetic operators
double zD = 10.0 / ((x + y / 60.0) / 60.0);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top