Re: KVO autonotifying complaining about custom setter return value
Re: KVO autonotifying complaining about custom setter return value
- Subject: Re: KVO autonotifying complaining about custom setter return value
- From: "Jim Turner" <email@hidden>
- Date: Wed, 5 Mar 2008 11:34:34 -0600
Chris,
On Tue, Mar 4, 2008 at 9:47 PM, Chris Suter <email@hidden> wrote:
>
>
> On 05/03/2008, at 2:10 PM, Kyle Sluder wrote:
>
> > On Tue, Mar 4, 2008 at 11:49 AM, Jim Turner
> > <email@hidden> wrote:
> >> In attempting to use a custom setter for a object, I'm getting the
> >> following message in the console the first time that object is
> >> instantiated:
> >>
> >> KVO autonotifying only supports -set<Key>: methods that return void.
> >> Autonotifying will not be done for invocations of -[MyObject
> >> setValue:]
> >>
> >> [snip]
> >>
> >> -(void) setValueForBindings:(id)_value
> >> {
> >> [self willChangeValueForKey:@"value"];
> >> {
> >> BOOL didSetOK = [self setStringValue:(NSString
> >> *)_value];
> >> // Do something with didSetOK
> >> }
> >> [self didChangeValueForKey:@"value"];
> >> }
> >
> > Please re-read the second sentence of that error again. You are
> > manually invoking -willChangeValueForKey: and -didChangeValueForKey:
> > and KVO is complaining "I've already done that!". Either stop doing
> > that, or implement -automaticallyNotifiesObserversForKey: to return NO
> > for the "bindings" key.
Actually, these were never getting called at all. Read below.
>
> Implementing -automaticallyNotifiesObserversForKey: will solve the
> problem but just removing the calls to -willChangeValueForKey: and -
> didChangeValueForKey: won't.
>
> I suspect the real cause of the problem is that there's a setValue:
> method defined in the superclass that returns something.
Actually, there was a -(BOOL) setValue: method in the object itself
that was getting called, I simply didn't see it in there. KVO/KVC was
finding it instead of my defined setter method, as it should have.
Having +automaticallyNotifiesObserversForKey: return NO for "value"
stopped the warning, but observation stopped as well, regardless if I
posted will/didChangeValueForKey because my custom setter still wasn't
getting called (the ivar was getting accessed directly)
When I implement +accessInstanceVariablesDirectly and have it return
NO, KVO then complains that my object is now no longer compliant even
though I have valid values for getter and setter defined in the
property declaration. The object responds to my custom methods but is
missing the connection that the property should be using those as well
instead of the default.
I filed a bug (rdar://problems/5781977) as this doesn't appear to be
proper behavior. I'd be happy to be told I'm wrong if you can point
out what I'm missing.
>
> If you're going to be using KVO/KVC on your objects, you should
> probably reconsider whether it's a good idea for your setter/getter
> methods to use non-default names. Obviously, you can change the name
> of your property if it clashes with methods in your superclass.
>
> - Chris
>
>
Thanks
--
Jim
http://nukethemfromorbit.com
_______________________________________________
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