Core data - binding related problem in NSNumberFormatter and NSDatePicker

Discussion in 'Mac Programming' started by Monaj, Dec 14, 2010.

  1. Monaj macrumors regular

    Joined:
    May 24, 2009
    #1
    Hi all,

    I am trying a core-data sample application, from Cocoa Programming - Aaron Hillegass.

    In it there is an entity Car, with following properties:

    1. condition - Int 16
    2. datePurchased - Date
    3. makeModel - String
    4. onSpecial - Boolean
    5. photo - Binary
    6. price - Decimal

    There are certain view objects such as-

    1. TableView
    2. DatePicker
    3. Image Well, etc.

    Some of the bindings performed are-

    1. TableView -> TableColumn with NSNumberFormatter (set as currency)-> value: arrangedObjects.price
    2. DatePicker -> value: selection.datePurchased

    Problems are:

    1. In TableColumn with NSNumberFormatter it is not accepting any decimal numbers which I am entering for currency, such as - 123,00 or 123.45 or +235 or 567. It is always displaying this alert message: Formatting error.

    2. Whenever I am selecting a row in table and changing its corresponding value in date picker, it is simultaneously changing the values for other records, ie. if for first row in table I have set it to 12/25/2004, it is keeping it 12/25/2004 for other rows.

    Can anyone help me to resolve my problems? The source code can be found here

    Thanks,

    Monaj
     
  2. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #2
    Post code, how are we supposed to "guess" what is incorrect. All I can really say is "user error" at this point.
     
  3. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #3
    He linked to the whole project zipped up. Not as obvious since the code isn't inline, but that's probably better than what we normally get.

    -Lee
     
  4. JoshDC macrumors regular

    Joined:
    Apr 8, 2009
    #4
    1. It wants a currency symbol as well as a number. Ticking the lenient checkbox seems to fix this.

    2. It's not changing the date, just that any new objects don't have a date to change to (and therefore the bound date picker doesn't change). You need to set the Car's class to an NSManagedObject subclass in the data model, create that subclass and override awakeFromInsert to set a default. This page gives you a bit of information on how to create a correct NSManagedObject subclass.
     
  5. Monaj thread starter macrumors regular

    Joined:
    May 24, 2009
    #5
    Hi JoshDC, thanx for your reply :)

    Regarding:
    Can you tell me where can I find the lenient check box? I tried to search it in IB for NumberFormatter but was unable to find it.

    Thanks,

    Monaj
     
  6. JoshDC macrumors regular

    Joined:
    Apr 8, 2009
    #6
    Set the behaviour to Mac OS X 10.4+ Default, style to currency and there should be a Lenient checkbox next to the Localize one.
     
  7. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #7
    Ahhh THERE it is. Link was small, no attachment, glossed over it.
     
  8. Monaj, Dec 15, 2010
    Last edited by a moderator: Dec 16, 2010

    Monaj thread starter macrumors regular

    Joined:
    May 24, 2009
    #8
    Hi JoshDC,

    Once again thanks for your reply, but still unable to find lenient check box :(

    I am posting the image of Inspector -> attributes property, so that you can help me to find it:

    [​IMG]

    Thanks,

    Monaj

    Your interpretation is correct, when I set the default value of datePurchased attribute to some value say- 2010-12-10 in MyDocument.xcdatamodel, it started working as intended :)

    So I am now thinking that can we set default value to today's date in xcdatamodel ?

    Thanks,

    Monaj
     
  9. JoshDC macrumors regular

    Joined:
    Apr 8, 2009
    #9
    Looks like you might be running an older version of Interface Builder to me. You can call setLenient: programatically (see NSNumberFormatter Class Reference).

    You can't set today's date without going the subclassing route I explained before. It may seem a lot of effort, but it's worth learning for more complex Core Data applications!
     

Share This Page