Re: Explanation needed regarding dependent keys
Re: Explanation needed regarding dependent keys
- Subject: Re: Explanation needed regarding dependent keys
- From: Michael Norris <email@hidden>
- Date: Sun, 29 Jul 2007 21:28:24 +0200
Thanks, but actually, you'll only get an infinite loop IF you set the
other ivar using a setter (i.e. using [self setFoo:foo], called from
- (void)setBar:(id)Bar, and vice versa). However, if you just change
the internal ivar "manually", as I have done here, then you won't get
stuck in an infinite loop, as you are not changing the key in a KVO-
compliant manner, and thus the observation notification won't be
triggered.
That's the trick.
- Michael
on 7/28/07 3:55 PM, email@hidden purportedly said:
[self setKeys: [NSArray arrayWithObjects:@"text", nil]
triggerChangeNotificationsForDependentKey:@"unformattedText"];
[self setKeys: [NSArray arrayWithObjects:@"unformattedText", nil]
triggerChangeNotificationsForDependentKey:@"text"];
If these two methods were to actually work as expected, an infinite
loop
would be created. Consider that changing "text" will trigger a
change for
"unformattedText", which by means of the second method call will
trigger a
change for "text", which will trigger a change for
"unformattedText" ad
infinitum.
Perhaps because of this, or some other implementation issue, what
you are
doing above is considered undefined (IIRC). There are discussions
on various
dependent key limitations on cocoadev. So my 2ยข is that you won't
be able to
get dependent keys to work.
An alternative is to do this manually, but you have to watch out for
infinite loops here as well. I have had to do this, and my solution
was to
use a char ivar as a three-state flag:
in interface:
char calledSetter; // default to 0
// when this setter is called, set calledSetter to 1
-(void)setText:(id)value
{
// check calledSetter
if( calledSetter == 0 ) calledSetter = 1;
// set value
// set unformatted if not already set
if( calledSetter == 2 ) [self setUnformattedText:whatever];
// always clear flag
calledSetter = 0;
}
Likewise, -setUnformattedText: would set calledSetter to 2, and the
conditional changes to:
if( calledSetter == 1 ) [self setText:whatever];
Best,
Keary Suska
Esoteritech, Inc.
"Demystifying technology for your home or business"
_______________________________________________
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