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:
- 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...
- 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!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…