Cocoa binding always produces zero for bound value
Cocoa binding always produces zero for bound value
- Subject: Cocoa binding always produces zero for bound value
- From: Tron Thomas <email@hidden>
- Date: Sun, 23 Mar 2008 18:18:19 -0700
I am having a problem with Cocoa bindings that I cannot figure out.
There is some object that is observing the value of another object. An
example for the relevant implementation of the classes is as follows:
@implementation SomeObject
- (id)initWithObject:
(AnotherObject*)object
{
self = [super init];
if(nil == self){
return nil;
}
[object addObserver:self forKeyPath:@"value" options:0 context:nil];
return self;
}
- (void)observeValueForKeyPath:(NSString*)keyPath
ofObject:(id)object
change:(NSDictionary*)change
context:(void*)context
{
if([keyPath isEqualToString:@"value"]){
float value = 2.0f;
value = [object value];
::NSLog(@"The observed value for %@ is %f", object, value);
}
}
@end
@implementation AnotherObject
- (void)setValue:
(float)value
{
_value = value;
::NSLog(@"The value for %@ was updated to %f.", self, _value);
}
- (float)value
{
::NSLog(@"The value for %@ is %f.", self, _value);
return _value;
}
@end
_value is a private member of the AnotherObject class.
When I run the program, I get output like the following:
2008-03-23 17:48:24.218 MyProgram[276:813] The value for <AnotherObject:
0x188240> was updated to 2.000000.
2008-03-23 17:48:24.219 MyProgram[276:813] The value for <AnotherObject:
0x188240> is 2.000000.
2008-03-23 17:48:24.223 MyProgram[276:813] The observed value for
<AnotherObject: 0x188240> is 0.000000
2008-03-23 17:48:24.224 MyProgram[276:813] The value for <AnotherObject:
0x188240> is 2.000000.
2008-03-23 17:48:24.225 MyProgram[276:813] The value for <AnotherObject:
0x188240> is 2.000000.
2008-03-23 17:48:24.267 MyProgram[276:813] The value for <AnotherObject:
0x188240> was updated to 3.000000.
2008-03-23 17:48:24.268 MyProgram[276:813] The value for <AnotherObject:
0x188240> is 3.000000.
2008-03-23 17:48:24.270 MyProgram[276:813] The observed value for
<AnotherObject: 0x188240> is 0.000000
2008-03-23 17:48:24.270 MyProgram[276:813] The value for <AnotherObject:
0x188240> is 3.000000.
2008-03-23 17:48:24.273 MyProgram[276:813] The value for <AnotherObject:
0x188240> is 3.000000.
2008-03-23 17:48:24.315 MyProgram[276:813] The value for <AnotherObject:
0x188240> was updated to 4.000000.
2008-03-23 17:48:24.319 MyProgram[276:813] The value for <AnotherObject:
0x188240> is 4.000000.
2008-03-23 17:48:24.320 MyProgram[276:813] The observed value for
<AnotherObject: 0x188240> is 0.000000
2008-03-23 17:48:24.321 MyProgram[276:813] The value for <AnotherObject:
0x188240> is 4.000000.
2008-03-23 17:48:24.322 MyProgram[276:813] The value for <AnotherObject:
0x188240> is 4.000000.
The problem is even though the value is updated to some non-zero value,
the observed value is always zero. I have tried stepping through the
code and verified that when I step into [AnotherObject value] method
from the call in [SomeObject
observeValueForKeyPath:ofObject:change:context:], the _value instance
member is non-zero. Yet when I return to the calling method, the local
value variable gets set to zero. This does not make sense.
What would allow the SomeObject instance to get the proper value for
AnotherObject when observing it?
_______________________________________________
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