PDA

View Full Version : NumberFormatters and coredata




MrFusion
Jun 27, 2006, 05:39 AM
I found out that to get more advanced formatters for your numbers, you need to program it yourself, instead of using the formatter in IB.

However, when I do this, I get the following error:

Unacceptable type of value for attribute: property = "fo"; desired type = NSDecimalNumber; given type = NSCFNumber; value = foo

So the formatter gives back NSCFNumber, but coredata needs NSDecimalNumber. Is there some easy way to convert them or to tell the formatter it needs to give back a NSDecimalNumber, without much glue code?

thanks
a newbie to ooredata



robbieduncan
Jun 27, 2006, 05:42 AM
NSDecimalNumber (http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSDecimalNumber_Class/Reference/Reference.html) appears to be a subclass of NSNumber so you should be able to use it in place.

Instead of creating an NSNumber simply create an NSDecimalNumber using the exact same method.

MrFusion
Jun 27, 2006, 05:50 AM
NSDecimalNumber (http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSDecimalNumber_Class/Reference/Reference.html) appears to be a subclass of NSNumber so you should be able to use it in place.

Instead of creating an NSNumber simply create an NSDecimalNumber using the exact same method.

Well, I never create a NSNumber. I have a entity in coredata that is an "Decimal" type, which is hooked up through bindings to a textfield.
The only code I have is the following:
myFormatter = [[[NSNumberFormatter alloc] init] autorelease];
[myFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[myFormatter setHasThousandSeparators:NO];
[myFormatter setNumberStyle:NSNumberFormatterScientificStyle];
[[myTextfield cell] setFormatter:myFormatter];

I needs this because the IB formatter (supports only 10.0 style behavior) doesn't allow scientific notation and such.

The problem is somewhere in the communication between the textfield/formatter and coredata.

robbieduncan
Jun 27, 2006, 01:24 PM
Hmm OK then. I'm still looking around the docs.

You line where you set 10.4 behaviour is redundant though. From the docs

"Number formatters you initialize with init adopt the v10.4 formatter behavior."

robbieduncan
Jun 27, 2006, 01:29 PM
Looks like you might need a Value Transformer (http://developer.apple.com/documentation/Cocoa/Conceptual/ValueTransformers/ValueTransformers.html).

I'd hazard this this may be a bug in the formatter when not running in 10.0 compatibility mode.

MrFusion
Jun 28, 2006, 07:15 AM
Hmm OK then. I'm still looking around the docs.

You line where you set 10.4 behaviour is redundant though. From the docs

"Number formatters you initialize with init adopt the v10.4 formatter behavior."
Ok. I'll delete that line then.
I solved it by setting the attribute to double (I thought I tried that before posting here) and changing the numberstyle for each field depending on the way I want it displayed.
At the same time I changed the name of the attribute from "version" to "myVersion". The xml file also has a version-value. This probably doesn't matter, though.
It works, and that's good enough for me.