gnustep-dev
[Top][All Lists]
Advanced

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

Re: Re[4]: Recent key-value encoding changes to NSObject.


From: Nicola Pero
Subject: Re: Re[4]: Recent key-value encoding changes to NSObject.
Date: Mon, 11 Feb 2002 17:38:41 +0000 (GMT)

>  >|  valueForKey: to lookup the key in the ivars.  If none is found, to lookup
>  >|  in the dictionary. (you do this by overriding
>  >|  handleValueForUnboundKey:)
> 
> It's more complicated (see your next point).
> 
> 
>  >| but then, you want to implement a 
>  >| 
>  >|  - getSpecialKey
>  >|  - setSpecialKey: (id)value
>  >| 
>  >| method in the class, with the following implementation:
>  >| 
>  >|  - (id) getSpecialKey
>  >|   {
>  >|     return [self storedValueForKey: @"SpecialKey"];
>  >|   }
>  >| 
>  >|  - setSpecialKey: (id)value
>  >|   {
>  >|     [self takeSoredValue: value  ForKey: @"SpecialKey"];
>  >|   }
>  >| 
>  >| so your problem is that these methods would enter an infinite loop.
> 
> Yes, that's the problem. But it works with WO.

I think that's ugly silly and confusing.

Anyway, if that is what Apple does and you want to do a similar thing, I
suggest that you do as follows in your custom subclass -

- (id) valueForKey: (id) key
{
  /* If there is an ivar, use it.  */
  if (GSInfoInstanceVariableInfo (self, key, et, cetera)
     || GSInstanceVariableInfo (self, [NSString stringWithFormat: @"_%@",
                                      key], et, cetera))
    {
      return [super valueForKey: key];
    }
 
  /* Else, look in the dictionary.  */
  {
    id dictionaryValue;

    dictionaryValue = [self 
whatever_private_method_in_EOXXX_to_get_the_key_value_from_the_dictionary: key];
    if (dictionaryValue != nil)
      {
        return dictionaryValue;
      }
  }

  /* Else, use the normal lookup which catches getXXX.  */
  return [super valueForKey: key];
}

This should support all your needs - 

 ivars take precedence over everything else.
 dictionary values take precedence over getXXX.
 getXXX are still used when all else fails.

The strange accrocgh with -(id)getA and -(id)getB using valueForKey: @"a"
and valueForKey: @"b" should work.

Since no special private API in gnustep-base is needed, it would make
Richard and me happy.




reply via email to

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