NSDecimal、NSDecimalNumber、CFNumber 之间的正确选择是什么? [英] What is the right choice between NSDecimal, NSDecimalNumber, CFNumber?

查看:33
本文介绍了NSDecimal、NSDecimalNumber、CFNumber 之间的正确选择是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读了很多关于 NSDecimal、NSNumber、NSNumberDecimal、CFNumber 的书……对我来说,它开始像丛林一样.

I've read a lot about NSDecimal, NSNumber, NSNumberDecimal, CFNumber... and it begins to be a kind of jungle to me.

基本上,我正在尝试创建一个简单的模型类来处理简单的计算,如下所示:

Basically, I'm trying to create a simple model class that will handle simple computations, like this one:

#import <Foundation/Foundation.h>


@interface Test : NSObject
{
    float rate;
    float amount;
    int duration;
}

- (float)capitalizedAmount;

@end

@implementation Test

- (float)capitalizedAmount {
    return (amount*pow((1.0+rate),duration));
}

@end

我想以字符串的形式访问这些方法和设置器,因为我计划有很多其他的类,我只保留一个字段列表来进行键值编码.

I want to access these methods and setters with their names as strings, since I plan to have a lot of other classes like this one, and I am only keeping a list of field to do key value coding.

// This is just the desired behavior
// This evidently won't work with the previous class definition
Test *obj = [[Test alloc] init];
[NSNumber numberWithInt:10]
...
float r;
r = [obj performSelector:NSSelectorFromString(@"capitalizedAmount")];

我知道这是不可能的,performSelector: 将返回一个对象,因此 capitalizedAmount 应该返回一个对象.我已经阅读了有关 NSInvocation 的内容以及 comp.lang 上的 Objective-C Faq 中的相关部分.

I understand that this is not possible, that performSelector: will return an object, and thus that capitalizedAmount should return an object. I've read things about NSInvocation and the relevant part in the Objective-C Faq on comp.lang.

我也明白我应该使用 NSDecimalNumber,但有两件事我想知道:

I also understand that I should use NSDecimalNumber, but there is two things that I would like to know:

  1. 对于更复杂的类(只有这种财务计算,在 UITableView 中显示),内存开销和性能损失是否可以接受?我没有太多的 C 背景...
  2. 使用像 decimalNumberByAdding: 这样的函数是不是太挑剔和复杂了?使用 Python 很容易定义 __add__ 以将运算符与对象一起使用.我是否应该从 NSDecimalNumber 获取浮点值,然后进行计算并返回包裹在 NSDecimalNumber 中的结果?你会如何处理这个问题?
  1. Are the memory overhead and performance loss acceptable for a somewhat more complicated class (only financial computations of this kind, showed in an UITableView)? I do not have much background in C...
  2. Isn't it too fastidious and complicated to use functions like decimalNumberByAdding:? With Python it was easy to define __add__ to use operators with objects. Should I get float values from NSDecimalNumber, then do the computations and returns the result wrapped in an NSDecimalNumber? How would you deal with this problem?

我正在寻找一个简单而漂亮的解决方案!

I am looking for a simple and beautiful solution!

同一领域的另一个问题:CFBoolean 是 iPhone Core Foundation 上 BOOL 的对象包装器吗?

Just another question in the same area: is CFBoolean the object wrapper for BOOL on iPhone Core Foundation?

非常感谢您的帮助!

推荐答案

如果你正在处理金融计算,你真的应该使用 base-10 算法来避免标准 base-2 浮点类型可能发生的舍入错误.所以它要么是 NSDecimal 要么是 NSDecimalNumber.由于您正在编写面向对象的代码,因此 NSDecimalNumber 是您的正确选择.

If you are dealing with financial computations, you really should use base-10 arithmetic to avoid the rounding errors that can occur with the standard base-2 floating point types. So it's either NSDecimal or NSDecimalNumber. And since you're writing object-oriented code, NSDecimalNumber is the right choice for you.

回答您的问题:只有测试您的代码才能揭示您是否可以接受内存开销和性能损失.我并没有真正使用 NSDecimalNumber,但我敢打赌,Apple 的实现非常高效,足以满足大多数人的需求.

To answer your questions: only testing of your code can reveal whether the memory overhead and performance loss are acceptable to you. I haven't really worked much with NSDecimalNumber but I'd wager that Apple's implementation is quite efficient and will be more than adequate for most people's needs.

不幸的是,您将无法避免 decimalNumberByAdding: 之类的东西,因为 Objective-C 不像 C++ 那样支持运算符重载.我同意这会让你的代码变得不那么优雅.

Unfortunately, you won't be able to avoid the likes of decimalNumberByAdding: since Objective-C does not support operator overloading like C++ does. I agree that it makes your code somewhat less elegant.

对您发布的代码的一条评论:r = [obj performSelector:NSSelectorFromString(@"capitalizedAmount")]; 相当不雅观.要么

One comment on the code you posted: r = [obj performSelector:NSSelectorFromString(@"capitalizedAmount")]; is rather unelegant. Either

r = [obj performSelector:@selector(capitalizedAmount)];

甚至是简单的

r = [obj capitalizedAmount];

除非出于其他原因需要 NSSelectorFromString 语法,否则会更好.

would be better unless you require the NSSelectorFromString syntax for some other reason.

这篇关于NSDecimal、NSDecimalNumber、CFNumber 之间的正确选择是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆