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

The Archive Base Latest Questions

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

ForwardInvocation does exist, but it is slow and has the annoying problem of compiler

  • 0

ForwardInvocation does exist, but it is slow and has the annoying problem of compiler warnings. So that got me to thinking — is there a way to use macroes to quickly implement a bunch of getter methods that get the property in question from another object?

For example, if I have a Car object, it might want to implement the following:

Car.h:

@class SparkPlug;
@class Engine;

. . .

-(int) nPistons;
-(float) horsepower;
-(SparkPlug*) sparkPlug;

Car.m:

. . .

-(int) nPistons {
    return self.engine.nPistons;
}

-(float) horsepower {
    return self.engine.horsepower;
}

-(SparkPlug*) sparkPlug {
    return self.engine.sparkPlug;
}

Question — would it be possible to set up some macroes so that by making one change somewhere, I could add another such method to both the header and implementation files?

e.g. MagicForwardingMacro (nPistons, int, engine);

Ideally, in such a way that the macroes would be reusable if I later wanted to later use a similar strategy to get the firstName, lastName, placeOfBirth, and dateOfBirth properties of a Person from his or her birthCertificate.

  • 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-23T10:28:45+00:00Added an answer on May 23, 2026 at 10:28 am

    The easiest way is probably to add the methods dynamically:

    • Add the properties to a category so the compiler doesn’t complain too much.
    • Clone a suitable IMP in +[NSObject resolveInstanceMethod:]. You’ll need to poke the Objective-C runtime.

    Elaborating on the second step:

    For each type, add a method like

    -(int)getEngineInt {
      return (int()(id,SEL))(objc_msgSend)(engine, _cmd);
    }
    

    Note that for structs you need objc_msgSend_stret and for floats/doubles you might need objc_msgSend_fpret (I think you only need it on i386; not sure about AMD64). The easy hack to support both the simulator and device is something like (I forget the macro name GCC uses…)

    #if __i386
    #define objc_msgSend_fpret objc_msgSend
    #endif
    

    Now to implement +resolveInstanceMethod:, you need to know the class you’re forwarding to ahead of time. Let’s say it’s Engine.

    +(BOOL)instancesRespondToSelector:(SEL)name
    {
      return [Engine instancesRespondToSelector:name];
    }
    
    +(BOOL)resolveInstanceMethod:(SEL)name
    {
      // Do we want to super-call first or last? Who knows...
      if ([super resolveInstanceMethod:name]) { return YES; }
      // Find the return type, which determines the "template" IMP we call.
      const char * returntype = [Engine instanceMethodSignatureForSelector:name].methodReturnType;
      if (!returnType) { return NO; }
    
      // Get the selector corresponding to the "template" by comparing return types...
      SEL template = NULL;
      if (0 == strcmp(returntype,@encode(int))
      {
        sel = @selector(getEngineInt);
      }
      else if (0 == strcmp(Returntype,@encode(float))
      {
        ...
      }
      if (!sel) { return NO; }
      Method m = class_getInstanceMethod(self,template);
      return class_addMethod(self, name, method_getImplementation(m), method_getTypeEncoding(m));
    }
    

    Alternatively, there’s a slightly undocumented method -forwardingTargetForSelector: which may be fast enough for your needs.

    EDIT: Alternatively, you can loop over the properties/methods dynamically. There doesn’t appear to be an obvious way to introspect categories, but you can define them in a protocol, do something like @interface Engine:NSObject<Engine> ... @interface Car(DynamicEngine)<Engine> and use objc_getProtocol("Engine") and then protocol_copyMethodDescriptionList()/protocol_copyPropertyList() to get the methods, and then add the getters. I’m not sure if properties are added to the “method description list”. Also note that the “copy” functions do not copy methods/properties from superclasses, which (in this case) is what you want.

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

Sidebar

Related Questions

Does somebody else has randomly seen crashes in ASIInputStream forwardInvocation: during the use of
I know that I can forward messages to instances of a class using -forwardInvocation:
I'm writing a clone of OpenStruct in Objective-C, using forwardInvocation: . However, the compiler
Either my debugger is broken or there's something fundamental that I am not understanding.
I'm looking for a way to make an NSInvocation invoke a specific IMP .
I'm having trouble getting forwardInvocation to work. For some reason, the Objective-C runtime completely
I'm using @dynamic properties in combination with -forwardInvocation: to generate properties at runtime (like
I would like to code a proxy that forwards method invocations to another object
I am having a problem while trying to upload a photo with my iOS
I have the following two class: //file FruitTree.h @interface FruitTree : NSObject { Fruit

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.