PDA

View Full Version : [noob] Cocoa - CoreData question




sebastijan
Jan 2, 2007, 02:07 PM
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:

http://shrani.si/files/picture2qc7m.png

P.S.: Sorry for my bad English.



caveman_uk
Jan 2, 2007, 02:14 PM
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.

sebastijan
Jan 2, 2007, 02:18 PM
Can you show me this, please? some sample?

Thanks in advance!

caveman_uk
Jan 3, 2007, 02:25 AM
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 (http://cocoadevcentral.com/articles/000080.php)).

sebastijan
Jan 7, 2007, 06:48 AM
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 (http://cocoadevcentral.com/articles/000080.php)).

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.

robbieduncan
Jan 7, 2007, 08:11 AM
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.

sebastijan
Jan 7, 2007, 08:22 AM
tnx robbieduncan for example.

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

robbieduncan
Jan 7, 2007, 08:29 AM
tnx robbieduncan for example.

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

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

sebastijan
Jan 7, 2007, 08:31 AM
ok. at least some hint?

robbieduncan
Jan 7, 2007, 08:41 AM
Read the Apple documentation (http://developer.apple.com/documentation/Cocoa/Conceptual/KeyValueObserving/Concepts/DependentKeys.html#//apple_ref/doc/uid/20002179-BAJEAIEE) 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...

sebastijan
Jan 7, 2007, 10:55 AM
+ (void)initialize {
[MyNumber setKeys:
[NSArray arrayWithObjects:@"string", nil]
triggerChangeNotificationsForDependentKey:@"@sum.intValue"];
}

why doesn't this work?

robbieduncan
Jan 7, 2007, 10:56 AM
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.

sebastijan
Jan 7, 2007, 10:58 AM
tnx. it works.