Math with NSNumbers and ints

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

What's wrong? I am just starting with objective-c and cocoa, but am familiar with php and basic and familiar with javascript.

+4
source share
3 answers

Objective-C has several different structures for use in computing. From its roots C come primitive numbers, int s, float s, double s, etc., on which you can perform arithmetic operations directly ( + , - , * , / , etc.). This is what you want to use.

On a higher lever, Objective-C also has NSNumber objects, which are simple wrappers for the primitive types listed above. They are used throughout Objective-C where primitives must be stored (often within other objects, such as arrays and dictionaries, which do not accept primitive values ​​directly). Since NSNumber are objects, you cannot perform direct arithmetic operations on them, you must first display their primitive values ​​(using intValue , for example, to get an integer value or doubleValue to get double precision floating-point numbers). Since it’s not clear what the variables in your question are, I’m not going to risk guessing what you are trying to do (I don’t want to mislead you), but you can learn more about NSNumber in the NSNumber Reference .

Finally, as Richard mentioned, there is NSDecimalNumber s. They are almost never used, because they are either simply not needed (they are designed to store extremely high-precision numbers, far beyond the scope of ordinary primitive values) or are too complicated to use. They also have their own methods for performing arithmetic operations and, as a rule, are not related to everyday use. Again, if you're interested, look at the NSDecimalNumber Reference .

For the most part, you want to use primitive numbers to do your calculations. When you need to store them, you can often “insert” and “unpack” (save and retrieve) from NSNumber objects.

+10
source

You cannot do this with objects (in this case, NSNumber). So you have to take the value "int / double / long / float". 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 this helps

+4
source

NSNumber cannot have mathematical symbols applied to them. In your case, your code would look like this: use NSDecimalNumber instead (which has selectors for multiplying and dividing the number):

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

It's ugly, but it's like objective-c deals with numbers. Another alternative is to get doubleValues ​​of each of your target numbers, and then multiply them.

+1
source

Source: https://habr.com/ru/post/1403645/


All Articles