Question

I have the following line of code

NSNumber *myValue = loadTempValue*0.420;

where I am trying to set the value of *myValue to the value of loadTempValue*0.420,

However, I get the error

Invalid operands to binary expression ('NSNumber *" and 'double')

Can someone advise how to set this out?

Was it helpful?

Solution

It seems that loadTempValue is also an NSNumber. In that case you want:

NSNumber *myValue = @([loadTempValue doubleValue] * 0.420);

Why are you using NSNumber objects for these values?

If loadTempValue was a double you could just do:

double myValue = loadTempValue * 0.42;

OTHER TIPS

loadTempValue is an NSNumber * and 0.420 is a float. You're trying to multiply an object by a float which the compiler does not understand.

What you want to do it get the float value from loadTempValue and then multiply that by 0.420. You do that this way:

[loadTempValue floatValue] * 0.420;

From there, it seems like you want to put that value back into an NSNumber * object, you do that like this:

@([loadTempValue floatValue] * 0.420);

The @( ... ) syntax was recently introduced to Objective-C. It's called object literal notation for numbers. It is a shorthand way of writing [NSNumber numberWithFloat: ...]

Finally, you will want to assign the result to a variable called myValue; you can accomplish that like this:

NSNumber *myValue = @([loadTempValue floatValue] * 0.420);

Try:

NSNumber *myValue = @([loadTempValue doubleValue] * 0.420);

or

NSNumber *myValue = [NSNumber numberWithDouble:([loadTempValue doubleValue] * 0.420)];

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top