开发者

Creating an Objective-C object that wraps another object with the same interface - KVC/KVO issues

开发者 https://www.devze.com 2023-01-12 21:32 出处:网络
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

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开发者_如何学Python 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消