Search code examples
objective-ccocoainterface-buildernsnumberformatternsdecimalnumber

Show NSDecimalNumber as currency in IB


I have a core data entity with a property amount, which is a NSDecimalNumber. For this property the entity's class has a method defined with an NSDecimalNumber as argument.

In Interface Builder I bound a table column to this property (using an NSArrayController) and on the column's cell I put an NSNumberFormatter. The formatter is configured in IB as 'currency'.

Now, when I try to enter a number, I get the following error:

-[NSCFNumber decimalNumberBySubtracting:]: unrecognized selector sent to instance 0x1001d5590

Apparently my setter method is receiving a regular NSNumber rather than an NSDecimalNumber. Can I configure my formatter differently, perhaps in code rather than IB, or is the only option to add an additional setter with an NSNumber as argument?


Solution

  • It's not clear to me exactly where your issue is. Either the formatter is giving you a straight NSNumber instead of a NSDecimalNumber or the core data is.

    To make the NSNumberFormatter give you NSDecimalNumbers, use the method -setGeneratesDecimalNumbers:

    To make the data store give you NSDecimalNumbers, make sure the relevant attribute in the model is set to "decimal number".