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

  • Home
  • SEARCH
  • 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

In my project I need to create a business object validation layer that will
I have the need to take a string argument and create an object of
I need to dynamically create a Video object in ActionScript 2 and add it
I need to create simple reusable javascript object publishing several methods and parameterized constructor.
I need to create a data transfer object, which I will use for storing
I need to create a custom volume slider for a WMP object. The current
Using reflection, I need to investigate a user DLL and create an object of
I need to create a historical timeline starting from 1600's to the present day.
I need to create an XML schema that looks something like this: <xs:element name=wrapperElement>
I have a class A that implements IEquatable<>, using its fields (say, A.b and

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.