CocoaDev

Edit AllPages

Is it possible to perform a conditional sum from the Data Model Editor and Interface Builder? I.e., something in the vein of:

SELECT SUM(expense) FROM Transactions WHERE category = “bank”;

(Transactions is the Entity, expense and category are attributes) I would then bind an NSTextField to this hypothetical attribute (fetched property?) in the bindings info panel and be done with it. Or should I dig into doing things programmatically? (I’ve been trying to avoid that, as I’m a rather inexperienced programmer). Thanks! Marco


Offhand, I’d say that you need to break that up into different steps. For instance, you can get all the Transactions where category = “bank” using NSPredicate. You could then use the @sum key value operator. Example:

NSArray *bankTransactions = [allTransactions filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@”category equals[cd] %@”, @”bank”]]; NSNumber *sum = [bankTransactions valueForKeyPath:@”@sum.expense”]; NSLog(@”Sum is %@”, sum);

If you wanted to do bindings, you could set up a -(NSNumber *)bankSum method and have it change using the following in a Transaction class implementation/category:


Would such a solution be implementable in the Data Model Designer, meaning without code?


Sooner or later, you’re going to have to write code. Core Data is actually an advanced technology; while it can be used to very easily create NibWare, creating real applications will require a significant understanding of Cocoa including KeyValueCoding, KeyValueObserving, and CocoaBindings.


Thanks for the answer. Time, I suppose, to dig into this Cocoa coding thing. Not looking forward to the inevitable feelings of despair. Thanks again!


For the record, here is how I solved it in Interface Builder:

Now, I am trying to sort some of the popups. I am trying to follow Apple’s NSPersistentDocument tutorial, adapt it to non-document based application and generally make it work less CoreData-ish (who invented the three click process to insert your data in the table?).


Why is a predicate like : “name LIKE[cd] ‘georges’” 10 times slower than “name BEGINSWITH[cd] ‘georges’ AND name ENDSWITH[cd] ‘georges’” ????