views:

1196

answers:

3

I'm building a app that need manage money datatype.

I'm new on Obj-c, so I can't see the ligth in the use of NSDecimalNumber.

For example, in my unit test I do this:

@interface SamplePerson : NSObject {
    NSString *name;
    NSDate *birthDate;
    NSInteger size;
    NSNumber *phone;
    NSDecimal balance;
    BOOL *isOk;
}

@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSDate *birthDate;
@property (nonatomic) NSInteger size;
@property (nonatomic, retain) NSNumber *phone;
@property (nonatomic) NSDecimal balance;
@property (nonatomic) BOOL *isOk;
--    
o.balance = [NSDecimalNumber decimalNumberWithDecimal: [[NSNumber numberWithLong: 12.5] decimalValue]];

But get a warning:

warning: passing argument 1 of 'save:' from distinct Objective-C type

Overally, I dound this issue more complex than expected. I could hack using integers but I never do that in my 10+ years coding in Foxpro, Delphi, .NET, Python... never have issue for doing this kind of work.

Anyway, I wanna know how do this. I read the web but only found the same code as above and info like "Use NSDecimalNumber or get killed!".

I wanna know how:

  • Assing simple values, like 12.5
  • Simple math
  • How store & load from sqlite.

Thank you.

(btw: exist a library or utility that solve this? this sound like a common headache, rigth?)

+1  A: 

The reason you're getting that warning is because you declared balance as a primitive NSDecimal, instead of an NSDecimalNumber *. Take a look at this question as to why you should use NSDecimalNumber.

Marc Charbonneau
A: 

also 12.5 is not a long, so you can't use

[NSNumber numberWithLong: 12.5]
rustyshelf
A: 

Auch!

I feel soooo noob now ;).

mamcx