[noob] Cocoa - CoreData question

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

  1. macrumors member

    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:


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


    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. macrumors member

    Can you show me this, please? some sample?

    Thanks in advance!
  4. Guest


    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. macrumors member

    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


    Staff Member

    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
  7. macrumors member

    tnx robbieduncan for example.

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


    Staff Member

    As I said I'm not about to deal with that: it's a challenge for you to work out on your own.
  9. macrumors member

    ok. at least some hint?
  10. Moderator


    Staff Member

    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. macrumors member

    + (void)initialize {
        [MyNumber setKeys:
            [NSArray arrayWithObjects:@"string", nil]
    why doesn't this work?
  12. Moderator


    Staff Member

    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. macrumors member

    tnx. it works.

Share This Page