yskywalker
yskywalker

Reputation: 95

Math with NSNumbers and ints

In objective-c I am trying to evaluate the following expression: _c = _f / 5 * 8; It tells me that an int and a NSNumber are invalid arguments to a binary expression.

Whats wrong? I am just getting started with objective-c and cocoa but am familiar with php and basic and kinda familiar with javascript.

Upvotes: 5

Views: 10616

Answers (3)

Itai Ferber
Itai Ferber

Reputation: 29764

Objective-C has several different structures in place for you to use in calculations. From its C roots come the primitive numbers, ints, floats, doubles, etc, on which you can perform arithmetic operations directly (+, -, *, /, etc.). These are what you're looking to use.

On a higher lever, Objective-C also has NSNumber objects, which are simple wrappers for the primitive types listed above. They're used throughout Objective-C where primitives need to be stored (often within other objects such as arrays and dictionaries that don't take primitive values directly). Because NSNumbers are objects, you cannot perform direct arithmetic operations on them, you have to draw out their primitive values first (using intValue, for instance, to get an integer value, or doubleValue to get a double-precision floating point number). Because it's unclear what the variables represent in your question, I'm not going to venture a guess as to what it is you're trying to do (I don't want to mislead you), but you can find out more about NSNumber in the NSNumber Class Reference.

Finally, as Richard mentioned, there are NSDecimalNumbers. These are almost never used, since they're either simply not needed (they're designed to hold extremely high-precision numbers, far beyond the capacity of regular primitive values), or too complicated to use. They also have their own methods for performing arithmetic operations, and are generally irrelevant for everyday use. Again, if you're interested, look more into the NSDecimalNumber Class Reference.

For the most part, you're looking to use primitive numbers to do your calculations. When you need to store them, you can often 'box' and 'unbox' (store and retrieve) from NSNumber objects.

Upvotes: 11

Richard J. Ross III
Richard J. Ross III

Reputation: 55543

NSNumbers cannot have mathematical symbols applied to them. In your case, your code would actually look like this, using NSDecimalNumber instead (which has selectors to multiply and divide numbers):

NSDecimalNumber *_c = nil;
NSDecimalNumber *_f = [NSDecimalNumber decimalNumberWithString:@"7"];

_c = [[_f decimalNumberByDividingBy:[NSDecimalNumber decimalNumberWithString:@"5"]] decimalNumberByMultiplyingBy:[NSDecimalNumber decimalNumberWithString:@"8"]];

It's ugly, but it's how objective-c deals with numbers. Your other alternative is to get the doubleValues of each of your target numbers, and then multiplying them.

Upvotes: 0

Novarg
Novarg

Reputation: 7440

You can't do these things with objects(NSNumber in this case). So you will have to take its' int/double/long/float value. I don't know which one is NSNumber, so here are 2 solutions, 1 will work:

_c = [_f doubleValue] / 5 * 8;

or:

_c = [NSNumber numberWithDouble:(_f / 5 * 8)];

Hope it helps

Upvotes: 5

Related Questions