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

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

问题描述

我读了很多关于NSDecimal,NSNumber,NSNumberDecimal,CFNumber ...,它开始是一种丛林给我。



基本上,我'm尝试创建一个简单的模型类来处理简单的计算,例如:

  #import& .h> 


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

- (float)capitalizedAmount;

@end

@implementation测试

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

@end

我想要访问这些方法和setters的名字作为字符串,因为我计划有很多其他类像这样,我只保留一个字段列表做键值编码。

  //这只是所需的行为
//这显然不适用于以前的类定义
Test * obj = [[Test alloc] init ];
[NSNumber numberWithInt:10]
...
float r;
r = [obj performSelector:NSSelectorFromString(@capitalizedAmount)];

我明白这是不可能的, performSelector:将返回一个对象,因此 capitalizedAmount 应该返回一个对象。我已经阅读关于 NSInvocation 和Objective-C Faq on comp.lang的相关部分的内容。



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


  1. 内存开销和性能损失可以接受一个更复杂的类(只有这种类型的财务计算,在UITableView中显示)?我没有很多C的背景...

  2. 使用 decimalNumberByAdding:这样的函数不是太重要和复杂吗?使用Python,很容易定义 __ add __ 以使用具有对象的运算符。我应该从 NSDecimalNumber 获得浮点值,然后进行计算并返回包装在 NSDecimalNumber 中的结果?您将如何处理这个问题?

我在寻找一个简单而美观的解决方案!



在同一领域的另一个问题: CFBoolean iPhone上的 BOOL

解决方案

非常感谢您的帮助!



<如果你正在处理财务计算,你真的应该使用base-10算术来避免标准基-2浮点类型可能出现的舍入误差。所以它是NSDecimal或NSDecimalNumber。因为你正在写面向对象的代码,NSDecimalNumber是你的最佳选择。



回答你的问题:只有测试你的代码可以揭示内存开销和性能损失是可以接受的。我没有真正的工作与NSDecimalNumber,但我敢说,苹果的实施是非常有效的,将是大多数人的需要足够。



不幸的是,你赢了因为Objective-C不支持像C ++那样的操作符重载,所以不能避免使用 decimalNumberByAdding:



对您发布的代码有一个评论: r = [obj performSelector:NSSelectorFromString(@ capitalizedAmount)]; 相当不忠。

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

甚至简单

  r = [obj capitalizedAmount];除非你需要 NSSelectorFromString ,否则

由于某些其他原因的语法。


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")];

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.

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

  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!

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

Thank you very much for your help!

解决方案

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.

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.

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.

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

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

or even the simple

r = [obj capitalizedAmount];

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

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

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