Re: Binding to an accessor that is only valid for subclass (was: Node hierarchy with subclasses)
Re: Binding to an accessor that is only valid for subclass (was: Node hierarchy with subclasses)
- Subject: Re: Binding to an accessor that is only valid for subclass (was: Node hierarchy with subclasses)
- From: Mike Abdullah <email@hidden>
- Date: Wed, 3 Feb 2010 15:08:56 +0000
On 2 Feb 2010, at 06:39, BareFeet wrote:
>> On Jan 29, 2010, at 9:38 AM, Jerry Krinock wrote:
>>
>>>
>>> The problem is that when a subclass A is selected, the UI elements bound to subclass B no longer have valid bindings so generate an error. How can I solve this?
>>
>> You can also uncheck the "Raises For Not Applicable Keys" in the bindings inspector pain in Interface Builder for your text fields.
>
> Thanks for the tip. Once I read it, it seemed like the logical solution. However, it doesn't seem to make any difference. All I could find in the documentation about that checkbox/attribute was:
>
> NSRaisesForNotApplicableKeysBindingOption
> An NSNumber object containing a Boolean value that specifies if an exception is raised when the binding is bound to a key that is not applicable—for example when an object is not key-value coding compliant for a key.
>
>
> To recall, I have something like this:
>
> @interface TopClass : NSObject
> {
> // some ivars
> }
>
> // some accessors
>
> @end
>
> @interface SubClass : TopClass
> {
> NSString* subValue;
> }
>
> - (NSString) subValue;
>
> @end
>
> I have an NSTreeController that contains a list of TopClass and SubClass items. The user selects an item in that list.
>
> I have a text field in Interface Builder whose value is bound (ie via bindings) to the subValue accessor, ie: NSTreeController -> selection.subValue
>
> Obviously, the subValue accessor is only valid if the currently selected item is a SubClass object. If the user selects a TopClass item, it throws an exception. How can I prevent this?
With "Raises for Not Applicable Keys" turned off, the exception will still be thrown. But, the bound object will catch it and treat as a not applicable result.
The only way not to throw the exception in the first place is to make -valueForKey: return NSNotApplicableMarker. I'd suggest doing this by overriding -valueForUndefinedKey:
_______________________________________________
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