Re: BOOL madness
Re: BOOL madness
- Subject: Re: BOOL madness
- From: Graham Cox <email@hidden>
- Date: Wed, 12 Nov 2008 12:16:49 +1100
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 (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
References: | |
| >BOOL madness (From: Andre Masse <email@hidden>) |