How to know whether property in Cocoa class is KVO-compliant?
How to know whether property in Cocoa class is KVO-compliant?
- Subject: How to know whether property in Cocoa class is KVO-compliant?
- From: Jerry Krinock <email@hidden>
- Date: Sun, 10 Jan 2010 19:58:53 -0800
After studying some about bindings during the last week, I decided that, just for fun, I would bind an NSSegmentedControl to its window controller using a binding instead of target/action. I thought that selectedSegment might be a KVO-compliant property of NSSegmentedControl because it has a -selectedSegment and -setSelectedSegment: method.
So I exposed a binding named @"foo" in my window controller's +initialize, in -awakeFromNib I added this:
[windowController bind:@"foo"
toObject:segmentedControl
withKeyPath:@"selectedSegment"
options:0] ;
and finally in a "windowWillClose" cleanup method I added an unbind:.
The above code compiled and ran without any bitchin' from out of the console, and at any time in the program if I send -infoForBinding:@"foo" to windowController, I log the expected result:
bindingInfo = {
NSObservedKeyPath = selectedSegment;
NSObservedObject = <NSSegmentedControl: 0x1b879c90>;
NSOptions = {
NSMultipleValuesPlaceholder = <null>;
NSNoSelectionPlaceholder = <null>;
NSNotApplicablePlaceholder = <null>;
NSNullPlaceholder = <null>;
NSRaisesForNotApplicableKeys = 1;
NSValueTransformer = <null>;
NSValueTransformerName = <null>;
};
}
But it "just didn't work". Flipping that segmented control in the user interface never caused my window controller's -setFoo: to run.
Did I do anything wrong?
If not, I conclude that NSSegmentedControl's is not KVO-compliant for property 'selectedSegment'. That is, the class implementation probably changes the view without invoking the setter.
I changed to target/action and it works fine.
So how does one know whether a property in Cocoa class is KVO-compliant? I can't find an answer in the Key-Value Observing Programming Guide. I thought maybe there's a little point system:
1. Property has a setter and getter; i.e. -bar and -setBar:. Add 2 points.
2. I remember seeing, every now and then, in newer API documentation, a notation that this "property is observeable", or something like that. Add 8 points.
3. Add 1 point for each Mac OS X dot release that the class first appeared in. 0 points for 10.0, 1 point for 10.1, etc.
4. If the class is a "view layer" class, subtract 2 points. Most people don't usually bind to view classes.
5. Test it and see if it works. Add 5 points. What is the possibility that it could stop working in a future Mac OS X release?
_______________________________________________
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