On 12 Nov 2008, at 12:08 pm, Andre Masse wrote:
Hi,
I'm having trouble converting an object to a BOOL. In my window
controller, I'm observing the model keyPath isDirty which is a BOOL.
version 1
----------------
- (void) observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
{
if(object == model) {
if([keyPath isEqualToString:@"isDirty"]) {
[self setDocumentEdited:[change
objectForKey:NSKeyValueChangeNewKey]];
}
}
}
This doesn't work. No matter what the value of isDirty is,
setDocumentEdited: is always called with YES;
[]
Now, the logging shows that [change
objectForKey:NSKeyValueChangedNewKey] toggle between 0 and 1
according to the value of isDirty, but flag is always YES... I also
tried using a plain c bool and got the same result (true). Any idea
what I'm doing wrong?
Yes. The value of the BOOL is wrapped by an NSNumber object so that it
can be stored by the change dictionary. You need to do this:
[self setDocumentEdited:[[change objectForKey:NSKeyValueChangeNewKey]
boolValue]];
Note that wherever KVC/KVO returns a scalar value, it is wrapped in an
NSValue or NSNumber as appropriate, and NSNull is substituted for nil.
hth, Graham
_______________________________________________
Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com
This email sent to [EMAIL PROTECTED]