How to do multiplication and addition with NSNumber
Asked Answered
G

3

6

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

Gynaecomastia answered 29/5, 2014 at 6:57 Comment(1)
This has nothing to do with Xcode. What have you tried? (this is trivial.)Escudo
A
8

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];
Anjanette answered 29/5, 2014 at 7:5 Comment(0)
H
4

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];
Hemminger answered 21/11, 2014 at 15:46 Comment(0)
O
1

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.

Occupation answered 29/5, 2014 at 8:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.