[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: question regarding explicit exception in NSKeyValueCoding.m:429 -[NS

From: Sebastian Reitenbach
Subject: Re: question regarding explicit exception in NSKeyValueCoding.m:429 -[NSObject(KeyValueCoding) setValue:forUndefinedKey:]
Date: Thu, 28 Jul 2011 20:15:05 +0200
User-agent: SOGoMail 1.3.8

On Thursday, July 28, 2011 16:25 CEST, David Chisnall <address@hidden> wrote: 
> On 28 Jul 2011, at 15:06, Sebastian Reitenbach wrote:
> > the exception happens here:NSKeyValueCoding.m:429 
> > -[NSObject(KeyValueCoding) setValue:forUndefinedKey:] 
> > This exception is unconditionally. I first tried to 
> > WANT_DEPRECATED_KVC_COMPAT, and recompiled, but that did not helped.
> > Then I commented out the exception, and OGo is just fine with that.
> > 
> > Obviously, libFoundation did not raised an exception, so I'm a bit 
> > wondering, who is/was right?
> GNUstep is right.  On OS X:
> $ cat undefined.m
> #import <Cocoa/Cocoa.h>
> int main(void)
> {
>       [NSAutoreleasePool new];
>       [[NSObject new] setValue: @"foo" forUndefinedKey: @"bar"];
>       return 0;
> }
> $ clang -framework Cocoa undefined.m 
> $ ./a.out
> 2011-07-28 15:17:05.872 a.out[56109:903] *** Terminating app due to uncaught 
> exception 'NSUnknownKeyException', reason: '[<NSObject 0x100111410> 
> setValue:forUndefinedKey:]: this class is not key value coding-compliant for 
> the key bar.'
> > I don't have a mac, so cannot check what would happen there if I'd run into 
> > this setValue:forUndefinedKey:?
> It throws an NSUnknownKeyException.  This is the documented behaviour when 
> you try to set a nonexistent key.  Why is OGo trying to do this?  What is it 
> expecting to happen?
> > Anyways, instead of commenting out the exception, I guess I need to put an 
> > exception handler somewhere, and catch it?
> > Am I on the right track here?
> No, silently discarding missing key error is almost certainly the wrong thing 
> to do.  If that is the desired behaviour for a specific class, then it should 
> override setValue:forUndefinedKey: and discard it, but more likely it should 
> not be setting values for keys that don't exist...

OK, then I need to figure out what class this is, In frame #6 I see this 

#6  0x007ef02e in -[WOKeyPathAssociation setValue:inComponent:] 
(self=0x8829bea8, _cmd=0x20743db0, _value=0x8674e6a8, _component=0x8bbb3708)
    at WOKeyPathAssociation.m:961

958       // we do not check the return value, because a set is allowed to fail
959       // (in SOPE ;-) [if there is no accessor, a backsync is just ignored]
960     #if 1
961       _setValue(self, _value, _component);
962     #else
963       if (!_setValue(self, _value, _component)) {
964         [self logWithFormat:@"could not set value %@ component %@",
965               _value, _component];
(gdb) print self
$1 = (class WOKeyPathAssociation *) 0x8829bea8

so I added a setValue:forUndefinedKey: to WOKeyPathAssociation, but it did not 
picked it up, it still throws the Exception in NSKeyValueCoding.m?
Maybe I need a hint how else to override this method.

Well, besides you said silently discarding the missing key error is maybe not 
the right way, putting an exception handler around the _setValue() helped...


> David 

reply via email to

[Prev in Thread] Current Thread [Next in Thread]