2

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?

nephilim
  • 129
  • 2
  • 8

2 Answers2

2

Core Data doesn't store NSDecimalNumber, only NSNumber. NSNumber doesn't have the method that gets called.

You either need to change the entities definition to use NSNumber or build your own NSValueTransformer to store the NSDecimalNumber in Core Data.

Please look here for more details about properties: http://developer.apple.com/library/ios/ipad/#documentation/Cocoa/Conceptual/CoreData/Articles/cdMOM.html%23//apple_ref/doc/uid/TP40002328-SW1

Christian Beer
  • 2,027
  • 15
  • 13
  • 1
    If the attribute type in the model is a decimal number, the generated object will be an NSDecimalNumber. – JeremyP Mar 06 '11 at 11:30
  • I just created a new project in which I created an entity with a decimal property. I then created an interface with a table and bound a column + formatter to this value. The result: 1. the NSNumberFormatter which I bound to an outlet returns NO to generatesDecimalNumber; 2. If I ask for the class from the property's value, I get NSDecimalNumber, using this code: NSLog(@"amount class = %@", [[obj amount] class]); – nephilim Mar 07 '11 at 18:03
1

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".

JeremyP
  • 84,577
  • 15
  • 123
  • 161