<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>toxicsoftware.com &#187; NSInvocation</title>
	<atom:link href="http://toxicsoftware.com/tag/nsinvocation/feed/" rel="self" type="application/rss+xml" />
	<link>http://toxicsoftware.com</link>
	<description>RANDOMIZE USR 0</description>
	<lastBuildDate>Sun, 01 Aug 2010 17:49:39 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Grab that Invocation</title>
		<link>http://toxicsoftware.com/grab-that-invocation/</link>
		<comments>http://toxicsoftware.com/grab-that-invocation/#comments</comments>
		<pubDate>Sat, 24 Jun 2006 00:25:43 +0000</pubDate>
		<dc:creator>schwa</dc:creator>
				<category><![CDATA[Default]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[NSInvocation]]></category>
		<category><![CDATA[Source]]></category>

		<guid isPermaLink="false">http://toxicsoftware.com/grab-that-invocation/</guid>
		<description><![CDATA[CInvocationGrabber is a Cocoa class to help with creating NSInvocation objects. NSInvocation objects are Cocoa&#8217;s equivalent of &#8220;functors&#8221; and are extremely handy. But unfortunately creating them is often a pain. For example, take the following code showing how to create &#8230; <a href="http://toxicsoftware.com/grab-that-invocation/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><a href="http://toxic-public.googlecode.com/svn/tags/BlogTag_20070927_729/Projects/Misc/InvocationGrabber/">CInvocationGrabber</a> is a Cocoa class to help with creating <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSInvocation_Class/index.html#//apple_ref/doc/uid/TP40003671">NSInvocation</a> objects.</p>

<p>NSInvocation objects are Cocoa&#8217;s equivalent of &#8220;<a href="http://en.wikipedia.org/wiki/Function_object">functors</a>&#8221; and are extremely handy. But unfortunately creating them is often a pain. For example, take the following code showing how to create a simple NSInvocation:</p>

<div style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; "><span style="color:#400080;">NSInvocation</span> *theInvocation = [<span style="color:#400080;">NSInvocation</span> <span style="color:#6c0540;">invocationWithMethodSignature:</span>[theString <span style="color:#6c0540;">methodSignatureForSelector:</span><span style="color:#881350;">@selector</span>(insertString:atIndex:)]];<br />
[theInvocation <span style="color:#6c0540;">setSelector:</span><span style="color:#881350;">@selector</span>(insertString:atIndex:)];<br />
[theInvocation <span style="color:#6c0540;">setTarget:</span>theString];<br />
<span style="color:#400080;">NSString</span> *theFirstArgument = <span style="color:#760f15;">&quot;Hello World&quot;</span>;<br />
[theInvocation <span style="color:#6c0540;">setArgument:</span>&amp;theFirstArgument <span style="color:#6c0540;">atIndex:</span><span style="color:#0000ff;">2</span>];<br />
<span style="color:#881350;">unsigned</span> theSecondArgument = <span style="color:#0000ff;">42</span>;<br />
[theInvocation <span style="color:#6c0540;">setArgument:</span>&amp;theFirstArgument <span style="color:#6c0540;">atIndex:</span><span style="color:#0000ff;">3</span>];</div>

<p>The code to create the same NSInvocation using my CInvocationGrabber class looks like this:</p>

<div style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; ">CInvocationGrabber *theGrabber = [CInvocationGrabber <span style="color:#6c0540;">invocationGrabber</span>];<br />
[[theGrabber <span style="color:#6c0540;">prepareWithInvocationTarget:</span>theString] <span style="color:#6c0540;">insertString:</span><span style="color:#760f15;">@&quot;Hello World&quot;</span> <span style="color:#6c0540;">atIndex:</span><span style="color:#0000ff;">42</span>];<br />
<span style="color:#400080;">NSInvocation</span> *theInvocation = [theGrabber <span style="color:#6c0540;">invocation</span>]</div>

<p>Obviously the primary benefit is that there is a lot less code. A secondary benefit is that you don&#8217;t have to store your parameters on the heap and can just pass them into the &#8216;grabbed&#8217; method.</p>

<p>Now that I&#8217;ve been using CInvocationGrabber for a few months I find I am using NSInvocations more and more. I&#8217;m using them in places where I would normally have written a little delegate method. For example just today I needed to write code that called a method after a time delay. Normally I would have wrapped it up in a NSTimer delegate method, but with CInvocationHelper I just used the invocation variant of NSTimer. Although I wasn&#8217;t saving many lines of code in this example, I was reducing the clutter in the class.</p>

<p>Cocoa developers with a keen eye might spot the similarity between CInvocationGrabber and Cocoa&#8217;s NSUndoManager APIs. This code is directly inspired by NSUndoManager&#8217;s prepareWithInvocation method.</p>
]]></content:encoded>
			<wfw:commentRss>http://toxicsoftware.com/grab-that-invocation/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>
