NumberFormatters and coredata

Discussion in 'Mac Programming' started by MrFusion, Jun 27, 2006.

  1. macrumors 6502a

    Joined:
    Jun 8, 2005
    Location:
    West-Europe
    #1
    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
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    NSDecimalNumber 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.
     
  3. thread starter macrumors 6502a

    Joined:
    Jun 8, 2005
    Location:
    West-Europe
    #3
    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.
     
  4. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    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."
     
  5. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #5
    Looks like you might need a Value Transformer.

    I'd hazard this this may be a bug in the formatter when not running in 10.0 compatibility mode.
     
  6. thread starter macrumors 6502a

    Joined:
    Jun 8, 2005
    Location:
    West-Europe
    #6
    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.
     

Share This Page