简体   繁体   English

如何使用NSNumber进行乘法和加法

[英]How to do multiplication and addition with NSNumber

I want to implement simple calculation with NSNumber. 我想用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. 我想用NSNumber实现相同的逻辑,我使用整数实现。 Thanks 谢谢

There is no explicit support for doing math operations on NSNumber . NSNumber进行数学运算没有明确的支持。 NSNumber is used to wrap a primitive type number. NSNumber用于包装基本类型编号。 (eg use it for storing in arrays/dicitionaries) (例如,用它存储在数组/ dicitionaries中)

If you have an NSNumber instance and you want to make math operations you should extract its value into a primitive type : 如果您有一个NSNumber实例并且想要进行数学运算,则应将其值提取为基本类型:

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) 完成后,创建一个用于存储新值的新实例(因为NSNumber是不可变的,所以不能使用旧的NSNumber实例)

numberInstance = [NSNumber numberWithInt:num];

Multiplication and addition processes are very hard in NSNumber objects 在NSNumber对象中,乘法和加法过程非常困难

Convert it to int value to perform processes than back to NSNumber object. 将其转换为int值以执行进程而不是返回NSNumber对象。

Here the code you need 这里是您需要的代码

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

NSNumber is a wrapper class over any general integral type. NSNumber是任何通用整数类型的包装类。 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 . 为了进行算术运算,您必须获得要进行算术运算的原始数据类型,然后进行算术运算,然后在NSNumber再次将其包装起来。

An example of multiplying a number by 5 and adding 4 is below: 将数字乘以5并加4的示例如下:

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: 从理论上讲,可以使用-addInt等方法创建子类,但我个人使用如下的宏:

#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. 当然,理论上这可以用c ++模板简化,但我不是c ++ guru(然后我再一次不是大师)所以我不想发布任何c ++代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM