如何添加两个 NSNumber 对象?
现在这一定很简单,但是如何对两个 NSNumber
求和呢? 就像:
[one floatValue] + [two floatValue]
或者存在更好的方式吗?
Now this must be easy, but how can sum two NSNumber
? Is like:
[one floatValue] + [two floatValue]
or exist a better way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
确实没有更好的方法,但如果可以避免的话,你真的不应该这样做。
NSNumber
作为标量数字的包装器存在,因此您可以将它们存储在集合中并与其他NSObjects
一起以多态方式传递它们。 它们并不真正用于在实际数学中存储数字。 如果对它们进行数学运算,它比仅对标量执行运算要慢得多,这可能就是为什么没有方便的方法的原因。例如:
在消息调度上至少需要 21 条指令,无论这些方法需要多少代码来对值进行拆箱和重新装箱(可能是几百个)才能完成 1 条指令的数学运算。
因此,如果您需要在字典中存储数字,请使用
NSNumber
,如果您需要将可能是数字或字符串的内容传递到函数中,请使用NSNumber
,但是如果您只是想用标量 C 类型进行数学计算。There is not really a better way, but you really should not be doing this if you can avoid it.
NSNumber
exists as a wrapper to scalar numbers so you can store them in collections and pass them polymorphically with otherNSObjects
. They are not really used to store numbers in actual math. If you do math on them it is much slower than performing the operation on just the scalars, which is probably why there are no convenience methods for it.For example:
Is blowing at a minimum 21 instructions on message dispatches, and however much code the methods take to unbox the and rebox the values (probably a few hundred) to do 1 instruction worth of math.
So if you need to store numbers in dicts use an
NSNumber
, if you need to pass something that might be a number or string into a function use anNSNumber
, but if you just want to do math stick with scalar C types.NSDecimalNumber(NSNumber) 拥有您正在寻找的所有好东西:
...
如果对计算性能感兴趣,则转换为 C++ 数组 std::vector 或类似的。
现在我不再使用 C 数组了; 使用错误的索引或指针很容易崩溃。 将每个新的 [] 与删除 [] 配对非常繁琐。
NSDecimalNumber (subclass of NSNumber) has all the goodies you are looking for:
...
If computing performance is of interest, then convert to C++ array std::vector or like.
Now I never use C-Arrays anymore; it is too easy to crash using a wrong index or pointer. And very tedious to pair every new [] with delete[].
您可以使用
编辑:
正如 ohho 所观察到的,此示例用于将两个保存整数值的
NSNumber
实例相加。 如果您想将两个保存浮点值的NSNumber
相加,您应该执行以下操作:You can use
Edit:
As observed by ohho, this example is for adding up two
NSNumber
instances that hold integer values. If you want to add up twoNSNumber
's that hold floating-point values, you should do the following:当前的得票最高的答案将导致难以诊断的错误和由于使用漂浮。 如果您要对 NSNumber 值进行数字运算,则应首先转换为 NSDecimalNumber,然后对这些对象执行运算。
来自文档:
因此,您应该将 NSNumber 实例转换为 NSDecimalNumbers
[NSNumber十进制值]
的方式,执行你想要的任何算术,然后在完成后分配回一个NSNumber。在 Objective-C 中:
在 Swift 3 中:
The current top-voted answer is going to lead to hard-to-diagnose bugs and loss of precision due to the use of floats. If you're doing number operations on NSNumber values, you should convert to NSDecimalNumber first and perform operations with those objects instead.
From the documentation:
Therefore, you should convert your NSNumber instances to NSDecimalNumbers by way of
[NSNumber decimalValue]
, perform whatever arithmetic you want to, then assign back to an NSNumber when you're done.In Objective-C:
In Swift 3:
为什么不使用 NSxEpression ?
您还可以构建一个 NSExpression,它可以重复用于使用不同的参数进行计算,如下所示:
例如,我们可以循环计算相同的解析表达式,每次使用不同的“Y”值:
Why not use
NSxEpression
?You can also build an NSExpression that can be reused to evaluate with different arguments, like this:
For instance, we can loop evaluating the same parsed expression, each time with a different "Y" value:
在 Swift 中,您可以使用 Bolt_Swift 库 https://github.com/williamFalcon/Bolt_Swift 获得此功能。
例子:
In Swift you can get this functionality by using the Bolt_Swift library https://github.com/williamFalcon/Bolt_Swift.
Example: