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: Richard Frith-Macdonald
Subject: Re: Re[4]: Recent key-value encoding changes to NSObject.
Date: Mon, 11 Feb 2002 17:48:43 +0000

On Monday, February 11, 2002, at 04:01 PM, Manuel Guesdon wrote:


Take this hierarchy Class X derived from EOGenericRecord derived from NSObject
Say X will have 2 values: a and b.
o X will store A in an ivar (_a) the other is stored in EOGenericRecord in a dictionary "_values". _value is a special dictionary which know it's keys (including a even if it won't store it itself in our case. There's a reason for this that I could explain if you want :-).

X will have the following methods:
        -(id)a
        {
                [self storedValueForKey:@"a"];
        }
        -(void)setA:(id)anA
        {
                [self takeStoredValue:anA
                        ForKey:@"a"];
        }
        -(id)b
        {
                [self storedValueForKey:@"a"];
        }
        -(void)setB:(id)aB
        {
                [self takeStoredValue:aB
                        ForKey:@"b"];
        }


[x valueForKey:@"a"] should return the ivar _a (not the dictioanry value for a)

Ok. -valueForKey: calls -a, which calls -storedValueForKey: which accesses _a

 [x valueForKey:@"b"] should return the  dictionary value.

Ok. -valueForKey: calls -b, which calls -storedValueForKey: which tries to call -_b (doesn't exist),
then tries to store into _b (doesn't exist) then calls -b ... recursion.

How to fix ...

Option 1 ... override -storedValueForKey: to behave as you want ...

a. try to call -_b, then try to access ivar, then your special code to handle the dictionary etc. This does involve some code duplication ... but not very much and it's pretty simple/obvious.


Option 2 ... as I described before, modify a few standard methods somewhat like this -

- (BOOL) respondsToSelector: (SEL)aSel
{
  BOOL result = [super respondsToSelector: aSel];

  if (result == NO && this_is_a_private_get_method(aSel))
    return YES;

  return result;
}

- (NSMethodSignature*) methodSignatureForSelector: (SEL)aSel
{
  NSMethodSignature     *sig = [super methodSignatureForSelector: aSel];

  if (sig == nil && this_is_a_private_get_method(aSel))
    return signature_of_method;

  return imp;
}

- (IMP) methodForSelector: (SEL)aSel
{
  IMP   imp = [super methodForSelector: aSel];

  if (imp == forwarding_method && this_is_a_private_get_method(aSel))
    return get_method;
  return imp;
}

- (id) theGetMethod
{
  // examine _cmd and fetch the appropriate value from the dictionary.
}

I cant really see much point in doing it that way just to avoid a little
code duplication.




reply via email to

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