views:

24

answers:

1

I need to create an object in one class hierarchy that wraps an object from a different one. They both have very similar interfaces, and I'd like to automatically have my wrapper forward messages it doesn't recognize to its target in lieu of implementing its own properties. I got this to work using something like this:

- (id)forwardingTargetForSelector:(SEL)sel 
{
    if ([self.wrappedObject respondsToSelector:sel])
        return self.wrappedObject;

    return self;
}

But this doesn't work for key-value coding. How can I go about having my wrapper implement key-value coding in a way that uses the properties of its wrapped object?

A: 

You'll have to overwrite valueForKey: and setValue:forKey:. An example:

- (id)valueForKey:(NSString *)key {
  if (/* wrapped object has key */)
    return [self.wrappedObject valueForKey: key];
  else
    return [super valueForKey: key];
}

The tricky part will be to determine the keys the wrapped object does implement. The easy way is to hard-code them, but that's not too nice. If you want to do it very generic, you'll do something using the reflection APIs of Objective-C. Here are two ideas:

  • All your properties are actually declared properties. You can use the runtime function class_getProperty to check it's existence and optionally extract additional information. The check could then look like this: if (class_getProperty([self.wrappedObject class], [key UTF8String]) != nil) {...}.

  • Your properties could also have accessors only. Then you should construct the selectors from the key and use [self.wrappedObject respondsToSelector: ...] just as you did before. The getter's name is the key plus a colon. The problem with this, however, is properties that have different getter/setter name. The first option is definitely the best.

Max Seelemann
Excellent, thank you for the tips! Our project is moving away from this approach at the moment, but I will probably have to use this solution later. Much appreciated.
Carl Veazey