• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
A few bindings questions from a bindings-newbie
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

A few bindings questions from a bindings-newbie


  • Subject: A few bindings questions from a bindings-newbie
  • From: Shamyl Zakariya <email@hidden>
  • Date: Mon, 2 Apr 2007 13:50:55 -0400

Hi,

It's obvious that bindings & CoreData are the future for Cocoa apps, but I'd put off learning how to use them since most of my work is a C+ + core ( OpenGL + physics ) with a Cocoa UI. Nonetheless, it's high time I learned, so I've jumped in by rewriting my expression calculator program to use bindings.

So, my calculator uses a C++ engine I wrote, called solver, to which you pass an expression, say "(foo/5)^bar" and you assign values to the "foo" and "bar" variables. E.g., it's a simple general purpose expression solver. To make this cocoa-bindings-happy, I wrote a clean ObjC wrapper to my C++ giving me two classes, "Calculator" and "Variable". Both Variable & Calculator try to be KVC compliant, so in my ( mostly working ) app I have something like this:

1) Variable has two properties, "name" and "value", both of which are strings ( My solver parses the strings into numbers ). Easy enough.
2) Calculator has the properties "expression", "result", and "variables". Expression is a string, result is display string calculated from the values in "expression" and "variables", and variables is a mutable array of Variable instances. The idea being that you can call something like so:


Calculator *calc = [[Calculator alloc] init];
[calc setExpression: @"(foo/5)^bar"];
[calc setVariables: [NSMutableArray arrayWithObjects: [Variable variableWithName: @"foo" andValue: @"10"], ... and so on


NSLog( @"Answer: %@", [calc result] ); // result is a string but you can get numbers too


Finally, in the Calculator class's initialize method I've registered the dependancy of "result" on "expression" and "variables" as such:


+ (void) initialize
{
[self setKeys: [NSArray arrayWithObjects: @"variables", @"expression", nil] triggerChangeNotificationsForDependentKey: @"result"];
}


In IB I've wired it up such that entering an expression in the input field, and hitting enter puts the correct answer into the result field. Awesome! Bindings work. If I add some variables to the variables table, and then enter an expression into the input field, the correct answer goes into the result field. Double-plus awesome!

But here's where things get cranky and since I'm new to bindings, I don't know how to handle this... ( for what it's worth, this is a rewrite, my old app used traditional pre 10.3 cocoa with IB actions, and table data sources, and the like )

First, if I have an expression in the input field that's dependent on one of my variables, if I change the variable in the table ( double clicking it and entering a new value ) I'd like to see the result field updated. In principle, I would expect the call to setKeys: triggerChangeNotificationsForDependentKey: to handle this automagically, but through some NSlogging of what actually happens, I see that -result is only called when variables are added or deleted, not when their contained values change. Sure, I could have Variable have a pointer to Calculator and tell it to update, but I feel like there ought to be a way via bindings to say that -result is dependent not just on the variables array, but on the values contained in that array.

Second, since the above doesn't work, in principle tabbing up to the entry field and hitting enter should "re-commit" the expression to calculate, but I guess bindings is being smart and not updating the value since it hasn't changed. So, to see the result updated, I have to clear the field, hit enter once to commit, and then re-enter my expression. How do I make bindings be less smart about this?

There's more but I don't want this message to get too long. If I can get the above working, I'll likely pester some more with regards to input validation.

Thanks,

email@hidden
	"authentic frontier gibberish"



_______________________________________________

Cocoa-dev mailing list (email@hidden)

Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Prev by Date: NSObjectController can't observe NSArrayController's selection?
  • Next by Date: Re: error displaying @count via bindings
  • Previous by thread: Re: NSObjectController can't observe NSArrayController's selection?
  • Next by thread: optimizing background image drawing with -drawRect
  • Index(es):
    • Date
    • Thread