user3575678
user3575678

Reputation: 115

How to do multiplication and addition with NSNumber

I want to implement simple calculation with NSNumber.

For ex:

int a;
a=a*10;
a=a+1;
NSLog(@"%d",a);

How to do the same thing if i declare

NSNumber *a;

I want to implement the same logic with NSNumber which I implemented using integer. Thanks

Upvotes: 6

Views: 8479

Answers (3)

MSA
MSA

Reputation: 134

Multiplication and addition processes are very hard in NSNumber objects

Convert it to int value to perform processes than back to NSNumber object.

Here the code you need

NSNumber *a;
int b = [a intValue];
b = b * 10;
b = b + 1;
a = [NSNumber numberWithInt:b];

Upvotes: 4

DanZimm
DanZimm

Reputation: 2568

NSNumber is a wrapper class over any general integral type. In order to do arithmetic you must get the primitive type of data that you want to do arithmetic with, and then do the arithmetic and then wrap it up again in an NSNumber.

An example of multiplying a number by 5 and adding 4 is below:

NSNumber *num = // get this somewhere
num = @(num.intValue * 5 + 4);

In theory one can create a subclass with methods of -addInt and the such, but I personally use macros like follow:

#define NSNumberAddInt(num, i) num = @( num.intValue + i )
#define NSNumberMultiplyInt(num, i) num = @( num.intValue * i )
#define NSNumberMultiplyAddInt(num, i, j) num = @(num.intValue * i + j)

In reality I have some more complex macros that actually declare inline functions:

#define NSNumberDeclareAddType(type, utype) \\
  inline NSNumber *NSNumberAdd ## utype ( NSNumber *inNum, type i ) {
    return @( inNum. type ## Value + i );
  }
#define NSNumberDeclareMultiplyType(type, utype) \\
  inline NSNumber *NSNumberMultiply ## utype ( NSNumber *inNum, type i ) {
    return @( inNum. type ## Value * i );
  }
#define NSNumberDeclareMultiplyAddType(type, utype) \\
  inline NSNumber *NSNumberMultiplyAdd ## utype ( NSNumber *inNum, type i, type j ) {
    return @( inNum. type ## Value * i + j );
  }
#define NSNumberDecalreArithmeticType(type, utype) NSNumberDeclareAddType(type, utype) \\
  NSNumberDeclareMultiplyType(type, utype) \\
  NSNumberDeclareMultiplyAddType(type, utype)

And then adding lines like

NSNumberDecalreArithmeticType(int, Int)
NSNumberDecalreArithmeticType(float, Float)
NSNumberDecalreArithmeticType(double, Double)

Of course, in theory this can be simplified with c++ templates, but I'm no c++ guru (then again I'm no guru in general) so I don't want to post any c++ code.

Upvotes: 1

giorashc
giorashc

Reputation: 13713

There is no explicit support for doing math operations on NSNumber. NSNumber is used to wrap a primitive type number. (e.g. use it for storing in arrays/dicitionaries)

If you have an NSNumber instance and you want to make math operations you should extract its value into a primitive type :

int num = [numberInstance intValue]; 
num += 1; // Just for the example;

After you are done create a new instance for storing the new value (since NSNumber is immutable you cannot use the old NSNumber instance)

numberInstance = [NSNumber numberWithInt:num];

Upvotes: 8

Related Questions