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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:29:08+00:00 2026-05-10T20:29:08+00:00

How exactly does NSInvocation work? Is there a good introduction? I’m specifically having issues

  • 0

How exactly does NSInvocation work? Is there a good introduction?

I’m specifically having issues understanding how the following code (from Cocoa Programming for Mac OS X, 3rd Edition) works, but then also be able to apply the concepts independently of the tutorial sample. The code:

- (void)insertObject:(Person *)p inEmployeesAtIndex:(int)index {     NSLog(@"adding %@ to %@", p, employees);     // Add inverse of this operation to undo stack     NSUndoManager *undo = [self undoManager];     [[undo prepareWithInvocationTarget:self] removeObjectFromEmployeesAtIndex:index];     if (![undo isUndoing])         [undo setActionName:@"Insert Person"];          // Finally, add person to the array     [employees insertObject:p atIndex:index]; }  - (void)removeObjectFromEmployeesAtIndex:(int)index {     Person *p = [employees objectAtIndex:index];     NSLog(@"removing %@ from %@", p, employees);     // Add inverse of this operation to undo stack     NSUndoManager *undo = [self undoManager];     [[undo prepareWithInvocationTarget:self] insertObject:p                                        inEmployeesAtIndex:index];     if (![undo isUndoing])         [undo setActionName:@"Delete Person"];          // Finally, remove person from array     [employees removeObjectAtIndex:index]; } 

I get what it’s trying to do. (BTW, employees is an NSArray of a custom Person class.)

Being a .NET guy, I try to associate unfamiliar Obj-C and Cocoa concepts to roughly analogous .NET concepts. Is this similar to .NET’s delegate concept, but untyped?

This isn’t 100% clear from the book, so I’m looking for something supplemental from real Cocoa/Obj-C experts, again with the goal that I understand the fundamental concept beneath the simple(-ish) example. I’m really looking to be able to independently apply the knowledge — up until chapter 9, I was having no difficulty doing that. But now …

  • 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. 2026-05-10T20:29:09+00:00Added an answer on May 10, 2026 at 8:29 pm

    According to Apple’s NSInvocation class reference:

    An NSInvocation is an Objective-C message rendered static, that is, it is an action turned into an object.

    And, in a little more detail:

    The concept of messages is central to the objective-c philosophy. Any time you call a method, or access a variable of some object, you are sending it a message. NSInvocation comes in handy when you want to send a message to an object at a different point in time, or send the same message several times. NSInvocation allows you to describe the message you are going to send, and then invoke it (actually send it to the target object) later on.


    For example, let’s say you want to add a string to an array. You would normally send the addObject: message as follows:

    [myArray addObject:myString]; 

    Now, let’s say you want to use NSInvocation to send this message at some other point in time:

    First, you would prepare an NSInvocation object for use with NSMutableArray‘s addObject: selector:

    NSMethodSignature * mySignature = [NSMutableArray     instanceMethodSignatureForSelector:@selector(addObject:)]; NSInvocation * myInvocation = [NSInvocation     invocationWithMethodSignature:mySignature]; 

    Next, you would specify which object to send the message to:

    [myInvocation setTarget:myArray]; 

    Specify the message you wish to send to that object:

    [myInvocation setSelector:@selector(addObject:)]; 

    And fill in any arguments for that method:

    [myInvocation setArgument:&myString atIndex:2]; 

    Note that object arguments must be passed by pointer. Thank you to Ryan McCuaig for pointing that out, and please see Apple’s documentation for more details.

    At this point, myInvocation is a complete object, describing a message that can be sent. To actually send the message, you would call:

    [myInvocation invoke]; 

    This final step will cause the message to be sent, essentially executing [myArray addObject:myString];.

    Think of it like sending an email. You open up a new email (NSInvocation object), fill in the address of the person (object) who you want to send it to, type in a message for the recipient (specify a selector and arguments), and then click ‘send’ (call invoke).

    See Using NSInvocation for more information. See Using NSInvocation if the above is not working.


    NSUndoManager uses NSInvocation objects so that it can reverse commands. Essentially, what you are doing is creating an NSInvocation object to say: ‘Hey, if you want to undo what I just did, send this message to that object, with these arguments’. You give the NSInvocation object to the NSUndoManager, and it adds that object to an array of undoable actions. If the user calls ‘Undo’, NSUndoManager simply looks up the most recent action in the array, and invokes the stored NSInvocation object to perform the necessary action.

    See Registering Undo Operations for more details.

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

Sidebar

Ask A Question

Stats

  • Questions 117k
  • Answers 118k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer class Program { static void Main(string[] args) { Timer timer… May 11, 2026 at 10:50 pm
  • Editorial Team
    Editorial Team added an answer Use GROUP_CONCAT SELECT GROUP_CONCAT(bar) FROM TABLE GROUP BY foo; May 11, 2026 at 10:50 pm
  • Editorial Team
    Editorial Team added an answer Your code doesn't work because the function is not returning… May 11, 2026 at 10:50 pm

Related Questions

What is the difference between == and === ? How exactly does the loosely
Why is operator '&' defined for bool?, and operator '&&' is not? How exactly
I am interested in using the ASP.NET Cache to decrease load times. How do
I'm wondering about instances when it makes sent to use #define and #if statements.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.