Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1051203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:53:40+00:00 2026-05-16T16:53:40+00:00

I need to create an object in one class hierarchy that wraps an object

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T16:53:40+00:00Added an answer on May 16, 2026 at 4:53 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create a 'container' object or class in Python, which keeps a
I am trying to create a UML class diagram and corresponding class definition (in,
For reasons I would rather not discuss, I need to create a custom authentication
I am new in android world. I am trying to learn it. I have
I am exploring the DynamicObject model in .NET 4.0. The application is one where
I am receiving an XML document from a REST service which shall be parsed
I want to declare a variable which holds a class which implements a specific
Ok, not really sure how to word, but will try my best. I have
Recently I came up with the idea of simple pattern for dynamic creation of
I get a dictionary of a bunch of game pieces with the three properties:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.