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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T14:52:18+00:00 2026-05-19T14:52:18+00:00

I’d like to be able to pass all the arguments received in my method

  • 0

I’d like to be able to pass all the arguments received in my method to a different method, as generically as possible.

Ideally, this would be done by passing a dictionary or some system variable (similar to _cmd).

In other words, I’m looking for something like the arguments array in javascript, or anything giving me access to the currently called method’s list of arguments.

  • 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-19T14:52:18+00:00Added an answer on May 19, 2026 at 2:52 pm

    I think what you are looking for is NSObject’s forwardInvocation: It gets passed an NSInvocation object that contains the information you want. NSInvocation also has a nice method called invokeWithTarget: that pretty much forwards the method call just like if you’ve called it directly.

    The runtime will call fowardInvocation: if you’re object is sent a message that it doesn’t have a method for, provided you also override methodSignatureForSelector: so the runtime can create the NSInvocation object.

    If all your arguments are objects the method forwardInvocation method will look something like this:

    @implementation Forwarder
    
    @synthesize friendObject;
    
    - (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {
        return [self.friendObject methodSignatureForSelector:aSelector];
    }
    
    - (void)forwardInvocation:(NSInvocation *)anInvocation {
        NSLog("Forwarding method: %@", [anInvocation selector]);
    
        NSMethodSignature *sig = [anInvocation methodSignature];
    
        // Get the juicy argument list info from [anInvocation methodSignature]
        // NOTE: Arguments 0 and 1 are for self and _cmd So we'll skip those.
    
        int numberOfArgs = [[anInvocation methodSignature] numberOfArguments];
    
        // Assuming all arguments are objects.
        id objPointer;
        NSMutableArray *argArray = [NSMutableArray array];
    
        for (int i = 2; i < numberOfArgs; i++) {
            [anInvocation getArgument:&objPointer atIndex:i];
            [argArray addObject:objPointer];
        }
    
        // Now argArray contains the array of all the arguments.
    }
    
    @end
    

    The hard part is that you need to make buffers to hold the argument values. If all the arguments are objects or the same type you can use the above code but It’s much more complicated to make a generic function if you use C types. You can use NSMethodSignature‘s getArgumentTypeAtIndex: but it returns a string encoding of the type and sizeof wont help you there. You would need to make a map of type names to size_ts for malloc/calloc.

    Edit: I added a concrete example of what I glossed over as // Get the juicy info in methodSignature As you can see what you want to do is possible but it’s pretty tough.
    (Check out Apple’s documentation on Type Encodings and NSMethodSignature‘s signatureWithObjCTypes:.)

    Edit2: This might be better as a separate answer but Here’s a complete (and tested) listing of how you can make use of the listing above to make a method that gets called with an arguments array like in JavaScript.

    First make a delegate protocol that the Forwarder object will call when a method is called.

    @protocol ForwarderDelegate <NSObject>
    
    - (void)selectorCalled:(SEL)selector withArguments:(NSArray *)args;
    
    @end
    

    Then make the actual Forwarder:

    @interface Forwarder : NSObject {
    
      @private
        NSObject *interfaceObject;
        id<ForwarderDelegate> delegate;
    }
    
    // Some object whose methods we want to respond to.
    @property (nonatomic, retain) NSObject *interfaceObject;
    @property (nonatomic, retain) id<ForwarderDelegate> delegate;
    
    @end
    
    @implementation Forwarder
    
    @synthesize interfaceObject;
    @synthesize delegate;
    
    - (NSMethodSignature *)methodSignatureForSelector:(SEL)selector {
        return [interfaceObject methodSignatureForSelector:selector];
    }
    
    - (void)forwardInvocation:(NSInvocation *)anInvocation {
    
        int numberOfArgs = [[anInvocation methodSignature] numberOfArguments];
    
        NSMutableArray *args = [NSMutableArray array];
    
        id ref;
        for (int i = 2; i < numberOfArgs; i++) {
            [anInvocation getArgument:&ref atIndex:i];
            [args addObject:ref];
        }
    
        // Call the method on the interface (original) object.
        if ([self.interfaceObject respondsToSelector:[anInvocation selector]]) {
            [anInvocation invokeWithTarget:self.interfaceObject];
        }
    
        [self.delegate selectorCalled:[anInvocation selector] withArguments:args];
    }
    
    @end
    

    Now you can instantiate the forwarder that takes some object and forwards any calls to the delegate. If both the target and the delegate are the same object it would work like this:

    @interface testreflectAppDelegate : NSObject <UIApplicationDelegate, ForwarderDelegate> {
        UIWindow *window;
    }
    
    
    @end
    
    @implementation testreflectAppDelegate
    
    @synthesize window;
    
    - (BOOL)application:(UIApplication *)application 
        didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
        [window makeKeyAndVisible];
    
        Forwarder *forwarder = [[[Forwarder alloc] init] autorelease];
        forwarder.delegate = self;
        forwarder.interfaceObject = self;
    
    
        [((id)forwarder) doFoo:[NSNumber numberWithInt:1] 
                       withBar:[NSNumber numberWithInt:2]];
    
        return YES;
    }
    
    - (void)doFoo:(NSNumber *)foo withBar:(NSNumber *)bar {
        NSLog(@"doFoo:withBar: called. Args: %d %d", [foo intValue], [bar intValue]);
    }
    
    - (void)doFoo:(NSNumber *)foo {
        NSLog(@"doFoo called. Args: %d", [foo intValue]);
    }
    
    - (void)selectorCalled:(SEL)selector withArguments:(NSArray *)args {
        NSLog(@"selectorCalled: %s with %d arguments", selector, [args count]);
        [self doFoo:[args objectAtIndex:0]];
    }
    
    @end
    

    Running this should output something like:

    testreflect[3098:207] doFoo:withBar: called. Args: 1 2
    testreflect[3098:207] selectorCalled: doFoo:withBar: with 2 arguments
    testreflect[3098:207] doFoo called. Args: 1
    

    Again this version will only work with id typed arguments. But can work with other types if you use the above mentioned TypeEncodings.

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

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have some data like this: 1 2 3 4 5 9 2 6
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I've got a string that has curly quotes in it. I'd like to replace
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have text I am displaying in SIlverlight that is coming from a CMS
I want to count how many characters a certain string has in PHP, but

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.