Re: noob question regarding (programmatic) bindings and data type coercion
Re: noob question regarding (programmatic) bindings and data type coercion
- Subject: Re: noob question regarding (programmatic) bindings and data type coercion
- From: Ken Thomases <email@hidden>
- Date: Tue, 17 Jun 2008 21:39:25 -0500
On Jun 17, 2008, at 12:16 PM, Stuart Malin wrote:
1) from what (or where) does the property "value" of the NSTextField
arise? Neither the class reference for NSTextField or NSControl have
such an instance method (at least not that I've yet found).
Bindings are different than properties. The names of bindings are in
a different "namespace" than properties or keys (in the KVC/KVO
sense). A class declares what bindings it supports using the
NSKeyValueBindingCreation informal protocol and its +exposeBinding:
method.
The bindings supported by NSTextField are documented in the Cocoa
Bindings Reference:
http://developer.apple.com/documentation/Cocoa/Reference/CocoaBindingsRef/BindingsText/NSTextField.html
2) I used "value" for the binding because that is how I saw some
sample code. Given that I don't quite know what "value" is, I
changed to bind to "intValue" (presuming that setIntValue would be
invoked on the textField). This works, but changed the behavior of
the app in one small way:
In the Nib, the valueTextField has a string value "hello" in place
because I wanted to see if the initial value of the Person's object
would be displayed when the binding is established. When I bind to
"value" the textField is immediately set to 0 (that is, I never see
the word "hello"), but NOT when I bind to "intValue" the textField
shows "hello" until I click the button to increase the age property
-- why the difference?
3) Then I got curious about what ensures compliance of data types,
and wondered if II'd cause runtime errors if I passed the wrong data
type, so I tried changing the bind invocation to bind to
"stringValue" -- that worked just fine. As did binding to
"floatValue". I'm presuming something must be coercing the person
object's age (which is int data) but what? Is this an artifact of
the control, the textField, or the binding mechanism?
4) Further, using "floatValue" does not cause the initial value of
age (0) to be displayed (same result as with intValue), but using
stringValue does (which matches use of "value"). Why?
None of "intValue", "stringValue" or "floatValue" are legitimate
bindings for NSTextField. Read the "How Do Bindings Work?" chapter of
Cocoa Bindings Programming Topics <http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaBindings/Concepts/HowDoBindingsWork.html
> to see that a class must have custom logic to support whatever
bindings it exposes. Since bindings are built on top of KVC and KVO,
you may be getting some seemingly correct behavior when you establish
such unsupported bindings, but that's just accidental.
When you use those unsupported bindings, I'm betting that view-
initiated changes are not propagated to the model. Have you tried
typing into your text field to see if that sets the Person object's age?
Cheers,
Ken
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please 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