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 …
According to Apple’s NSInvocation class reference:
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.
NSInvocationcomes 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.NSInvocationallows 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:Now, let’s say you want to use
NSInvocationto send this message at some other point in time:First, you would prepare an
NSInvocationobject for use withNSMutableArray‘saddObject:selector:Next, you would specify which object to send the message to:
Specify the message you wish to send to that object:
And fill in any arguments for that method:
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,
myInvocationis a complete object, describing a message that can be sent. To actually send the message, you would call: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 (
NSInvocationobject), fill in the address of the person (object) who you want to send it to, type in a message for the recipient (specify aselectorand arguments), and then click ‘send’ (callinvoke).See Using NSInvocation for more information. See Using NSInvocation if the above is not working.
NSUndoManagerusesNSInvocationobjects so that it can reverse commands. Essentially, what you are doing is creating anNSInvocationobject to say: ‘Hey, if you want to undo what I just did, send this message to that object, with these arguments’. You give theNSInvocationobject to theNSUndoManager, and it adds that object to an array of undoable actions. If the user calls ‘Undo’,NSUndoManagersimply looks up the most recent action in the array, and invokes the storedNSInvocationobject to perform the necessary action.See Registering Undo Operations for more details.