That's because you're getting an object pointer back, and any object pointer 
except for nil evaluates to true. Assuming you know it will only be one object 
inserted at a time, you'd want to do [[change 
objectForKey:NSKeyValueChangeNewKey] boolValue].

Cheers,
Chuck


----- Original Message ----
> From: Andre Masse <[EMAIL PROTECTED]>
> To: Cocoa Developers <cocoa-dev@lists.apple.com>
> Sent: Tuesday, November 11, 2008 5:08:20 PM
> Subject: BOOL madness
> 
> 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;
> 
> 
> So, I add some logging, flags and everything to see what's going on:
> 
> version 2
> ----------------
> - (void) observeValueForKeyPath:(NSString *)keyPath
>                        ofObject:(id)object
>                          change:(NSDictionary *)change
>                         context:(void *)context    
> {
>     if(object == model) {
>         if([keyPath isEqualToString:@"isDirty"]) {
>                         BOOL flag = [change 
> objectForKey:NSKeyValueChangeNewKey];
>             NSLog(@"change value = %@", [change 
> objectForKey:NSKeyValueChangeNewKey]);
>             if(flag) {
>                 [self setDocumentEdited:YES];
>             }
>             else {
>                 [self setDocumentEdited:NO];
>             }
>     
>         }
>     }    
> }
> 
> 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?
> 
> Thanks,
> 
> Andre Masse
> 
> _______________________________________________
> 
> 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/acharlieblue%40yahoo.com
> 
> This email sent to [EMAIL PROTECTED]



      
_______________________________________________

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]

Reply via email to