[noob] Cocoa - CoreData question

Discussion in 'Mac Programming' started by sebastijan, Jan 2, 2007.

  1. macrumors member

    Joined:
    May 22, 2005
    Location:
    London, UK
    #1
    OK. First I use CoreData and have one question. How can I sum numbers in NSTableView and show them in NSTextField? Like in this pic:

    [​IMG]

    P.S.: Sorry for my bad English.
     
  2. Guest

    caveman_uk

    Joined:
    Feb 17, 2003
    Location:
    Hitchin, Herts, UK
    #2
    Unless you're doing anything else with the data then Core Data is way overkill for what you're doing. All you need is an array and perhaps use cocoa bindings (ie an NSArrayController) to handle the boilerplate code.
     
  3. thread starter macrumors member

    Joined:
    May 22, 2005
    Location:
    London, UK
    #3
    Can you show me this, please? some sample?

    Thanks in advance!
     
  4. Guest

    caveman_uk

    Joined:
    Feb 17, 2003
    Location:
    Hitchin, Herts, UK
    #4
    There's not much to see in terms of code as basically the only code is the setting up of an NSMutableArray and a small method for summing the contents. All the cocoa bindings stuff is done graphically in interface builder. There's quite a few cocoa bindings examples knocking around on the web (like this one).
     
  5. thread starter macrumors member

    Joined:
    May 22, 2005
    Location:
    London, UK
    #5
    tnx caveman_uk.

    But can i get some example - source or xcode proj - for this? please.

    I know how to use table in coreData and some bindings stuff... but i dont know how to sum numbers in table and show them in TextField.
     
  6. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #6
    See the attached. This does it without using CoreData as there is no need for that here. The summing is done entirely in the bindings system ( :cool: ). Note that the sum only recalculates when you add or remove an item from the array. I'm sure you can sort this but I'm not about to worry about how to do that now.
     

    Attached Files:

    • Sum.zip
      File size:
      42.3 KB
      Views:
      25
  7. thread starter macrumors member

    Joined:
    May 22, 2005
    Location:
    London, UK
    #7
    tnx robbieduncan for example.

    but how can i sum number on every change i made and not only on add or remove?
     
  8. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #8
    As I said I'm not about to deal with that: it's a challenge for you to work out on your own.
     
  9. thread starter macrumors member

    Joined:
    May 22, 2005
    Location:
    London, UK
    #9
    ok. at least some hint?
     
  10. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #10
    Read the Apple documentation on Dependent Keys...

    Edit to add: and yes I have checked that this solves the problem. You need to add a single class level method with a single line of code in it...
     
  11. thread starter macrumors member

    Joined:
    May 22, 2005
    Location:
    London, UK
    #11
    Code:
    + (void)initialize {
        [MyNumber setKeys:
            [NSArray arrayWithObjects:@"string", nil]
            triggerChangeNotificationsForDependentKey:@"@sum.intValue"];
    }
    why doesn't this work?
     
  12. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #12
    Because @sum is not part of the key within this object. The dependent key in the object is simply intValue. The @sum is a directive to sum across the collection of these objects.
     
  13. thread starter macrumors member

    Joined:
    May 22, 2005
    Location:
    London, UK
    #13
    tnx. it works.
     

Share This Page