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 6143883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:34:44+00:00 2026-05-23T18:34:44+00:00

I am in a situation where I want to dynamically generate getters and setters

  • 0

I am in a situation where I want to dynamically generate getters and setters for a class at runtime (in a similar manner to what NSManagedObject does behind the scenes). From my understanding, this is possible using resolveInstanceMethod: on a specific class. At this point, you would have to use class_addMethod to dynamically add the method based on the selector. I understand this at a theoretical level, but I haven’t delved much into the obj-c runtime, so I was curious if there were any great examples of how to do this. Most of my knowledge comes from this article:

http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtDynamicResolution.html

Any thoughts / examples?

  • 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-23T18:34:44+00:00Added an answer on May 23, 2026 at 6:34 pm

    The only nice discussion I know is at Mike Ash’s blog post. It’s not that hard, actually.

    I once needed to split a big NSManagedObject subclass into two, but decided to keep the fact an implementation detail so that I don’t have to rewrite other parts of my app. So, I needed to synthesize getter and setter which sends [self foo] to [self.data foo], automatically.

    To achieve that, I did the following:

    1. Prepare the new method, already in my class.

      - (id)_getter_
      {
          return objc_msgSend(self.data, _cmd);
      }
      
      - (void)_setter_:(id)value 
      {
          objc_msgSend(self.data, _cmd,value);
      }
      

      Note that _cmd has the selector in it. So, usually, _cmd is either @selector(_getter_) or @selector(_setter_) in these methods, but I’m going to plug the implementation of _getter_ as the implementation of foo. Then, _cmd contains @selector(foo), and thus calls self.data‘s foo.

    2. Write a generic synthesizing method:

      +(void)synthesizeForwarder:(NSString*)getterName
      {
          NSString*setterName=[NSString stringWithFormat:@"set%@%@:",
                [[getterName substringToIndex:1] uppercaseString],[getterName substringFromIndex:1]];
          Method getter=class_getInstanceMethod(self, @selector(_getter_));
          class_addMethod(self, NSSelectorFromString(getterName), 
                          method_getImplementation(getter), method_getTypeEncoding(getter));
          Method setter=class_getInstanceMethod(self, @selector(_setter_:));
          class_addMethod(self, NSSelectorFromString(setterName), 
                          method_getImplementation(setter), method_getTypeEncoding(setter));
      }
      

      Note that this is a class method. So self stands for the class. Note also that I didn’t hardcode type encodings (which tells Objective-C runtime what the arguments of the particular method are). The syntax of type encodings is documented, but constructing by hand is very error-prone; I wasted a few days that way until Mike Ash told me to stop it. Generate it using an existing method.

    3. Generate forwarders at the earliest possible time:

       +(void)load  
       {
           for(NSString*selectorName in [NSArray arrayWithObjects:@"foo", @"bar", @"baz",nil]){
              [self synthesizeForwarder:selectorName];
           }
       }
      

      This generates foo, setFoo:, bar, setBar:, and baz, setBaz:.

    Hope this helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a situation where i want to return List<> from this function public
Here is my situation... I am trying to dynamically generate a bunch of stuff
Situation: I want to provide a website service where users can enter some data
I have a sort of unique situation....I want to populate a ModelChoiceField based of
So the situation is: I want to optimize my code some for doing counting
I have a situation where I want a bash script to replace an entire
I am in the situation where I want to overwrite the set method that
I have a situation where I want certain code to be executed no matter
I have a situation where i want to add LinePragmas to CodeDom objects. But
I am in a situation where I want to restructure my site's urls. That

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.