Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>According to <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSInvocation_Class/Reference/Reference.html" rel="noreferrer">Apple's NSInvocation class reference</a>:</p> <blockquote> <p>An <code>NSInvocation</code> is an Objective-C message rendered static, that is, it is an action turned into an object.</p> </blockquote> <p>And, in a <em>little</em> more detail:</p> <p>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. <code>NSInvocation</code> 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. <code>NSInvocation</code> allows you to <em>describe</em> the message you are going to send, and then <em>invoke</em> it (actually send it to the target object) later on.</p> <hr> <p>For example, let's say you want to add a string to an array. You would normally send the <code>addObject:</code> message as follows:</p> <pre><code>[myArray addObject:myString]; </code></pre> <hr> <p>Now, let's say you want to use <code>NSInvocation</code> to send this message at some other point in time:</p> <p>First, you would prepare an <code>NSInvocation</code> object for use with <code>NSMutableArray</code>'s <code>addObject:</code> selector:</p> <pre><code>NSMethodSignature * mySignature = [NSMutableArray instanceMethodSignatureForSelector:@selector(addObject:)]; NSInvocation * myInvocation = [NSInvocation invocationWithMethodSignature:mySignature]; </code></pre> <p>Next, you would specify which object to send the message to:</p> <pre><code>[myInvocation setTarget:myArray]; </code></pre> <p>Specify the message you wish to send to that object:</p> <pre><code>[myInvocation setSelector:@selector(addObject:)]; </code></pre> <p>And fill in any arguments for that method:</p> <pre><code>[myInvocation setArgument:&amp;myString atIndex:2]; </code></pre> <p>Note that object arguments must be passed by pointer. Thank you to <a href="https://stackoverflow.com/users/53790/ryan-mccuaig">Ryan McCuaig</a> for pointing that out, and please see <a href="http://developer.apple.com/mac/library/documentation/cocoa/reference/foundation/Classes/NSInvocation_Class/Reference/Reference.html#//apple_ref/occ/instm/NSInvocation/setArgument:atIndex:" rel="noreferrer">Apple's documentation</a> for more details.</p> <p>At this point, <code>myInvocation</code> is a complete object, describing a message that can be sent. To actually send the message, you would call:</p> <pre><code>[myInvocation invoke]; </code></pre> <p>This final step will cause the message to be sent, essentially executing <code>[myArray addObject:myString];</code>.</p> <p>Think of it like sending an email. You open up a new email (<code>NSInvocation</code> 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 <code>selector</code> and arguments), and then click "send" (call <code>invoke</code>).</p> <p>See <a href="http://developer.apple.com/DOCUMENTATION/Cocoa/Conceptual/DistrObjects/Tasks/invocations.html" rel="noreferrer">Using NSInvocation</a> for more information. See <a href="https://web.archive.org/web/20121014210741/https://developer.apple.com/library/mac/#/web/20121015003159/https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/DistrObjects/Tasks/invocations.html" rel="noreferrer">Using NSInvocation</a> if the above is not working.</p> <hr> <p><code>NSUndoManager</code> uses <code>NSInvocation</code> objects so that it can <em>reverse</em> commands. Essentially, what you are doing is creating an <code>NSInvocation</code> 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 <code>NSInvocation</code> object to the <code>NSUndoManager</code>, and it adds that object to an array of undoable actions. If the user calls "Undo", <code>NSUndoManager</code> simply looks up the most recent action in the array, and invokes the stored <code>NSInvocation</code> object to perform the necessary action.</p> <p>See <a href="http://developer.apple.com/documentation/Cocoa/Conceptual/UndoArchitecture/Tasks/RegisteringUndo.html" rel="noreferrer">Registering Undo Operations</a> for more details.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload