<?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>Saturnboy</title>
	<atom:link href="http://saturnboy.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://saturnboy.com</link>
	<description>Code, Work, and Life</description>
	<lastBuildDate>Wed, 23 Oct 2013 16:53:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Html5 Canvas Clock</title>
		<link>http://saturnboy.com/2013/10/html5-canvas-clock/</link>
		<comments>http://saturnboy.com/2013/10/html5-canvas-clock/#comments</comments>
		<pubDate>Wed, 23 Oct 2013 16:53:18 +0000</pubDate>
		<dc:creator>justin</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[canvas]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[js]]></category>

		<guid isPermaLink="false">http://saturnboy.com/?p=1990</guid>
		<description><![CDATA[Since Flex is Dead, I decided to rebuild my fancy Flex Clock using the html5 canvas element. Here is the result: (click to see it in action) Lots of very vanilla drawing code that is fairly boring, but one interesting part is how the new canvas-based clock uses svg paths. The old flex code used [...]]]></description>
			<content:encoded><![CDATA[<p>Since <a href="http://saturnboy.com/2013/03/adobe-killed-this-blog/">Flex is Dead</a>, I decided to rebuild my fancy <a href="http://saturnboy.com/2010/11/programmatic-skinning-in-flex-4-an-fxg-clock/">Flex Clock</a> using the html5 canvas element.  Here is the result:</p>
<div class="span-14 top" style="min-height:510px; text-align:center;">
<a href="http://saturnboy.com/proj/html5/clock/clock.html"><img src="/proj/html5/clock/canvas-clock.png" width="500" height="500" title="html5 canvas clock" /></a>
</div>
<div style="text-align:center; padding-bottom:10px;">(click to see it in action)</div>
<p class="bottom">Lots of <i>very</i> vanilla drawing code that is fairly boring, but one interesting part is how the new canvas-based clock uses svg paths.  The old flex code used svg paths liberally as simple way to draw shapes, but since canvas doesn&#8217;t support drawing svg path data directly I had to write my own:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> drawPath<span style="color: #009900;">&#40;</span>ctx<span style="color: #339933;">,</span>path<span style="color: #339933;">,</span>fill<span style="color: #339933;">,</span>ang<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    ctx.<span style="color: #660066;">save</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    ctx.<span style="color: #660066;">rotate</span><span style="color: #009900;">&#40;</span>ang <span style="color: #339933;">==</span> undefined <span style="color: #339933;">?</span> <span style="color: #CC0000;">0</span> <span style="color: #339933;">:</span> ang  <span style="color: #339933;">*</span> Math.<span style="color: #660066;">PI</span> <span style="color: #339933;">/</span> <span style="color: #CC0000;">180.0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    ctx.<span style="color: #660066;">beginPath</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #003366; font-weight: bold;">var</span> parts <span style="color: #339933;">=</span> path.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">' '</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>parts.<span style="color: #660066;">length</span> <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> part <span style="color: #339933;">=</span> parts.<span style="color: #660066;">shift</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>part <span style="color: #339933;">==</span> <span style="color: #3366CC;">'M'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            coords <span style="color: #339933;">=</span> parts.<span style="color: #660066;">shift</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">','</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            ctx.<span style="color: #660066;">moveTo</span><span style="color: #009900;">&#40;</span>coords<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> coords<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>part <span style="color: #339933;">==</span> <span style="color: #3366CC;">'L'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">continue</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>part <span style="color: #339933;">==</span> <span style="color: #3366CC;">'Z'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>part.<span style="color: #660066;">indexOf</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">','</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;=</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            coords <span style="color: #339933;">=</span> part.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">','</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            ctx.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span>coords<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> coords<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    ctx.<span style="color: #660066;">closePath</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    ctx.<span style="color: #660066;">fillStyle</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>fill <span style="color: #339933;">==</span> undefined <span style="color: #339933;">?</span> <span style="color: #3366CC;">'#000'</span> <span style="color: #339933;">:</span> fill<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    ctx.<span style="color: #660066;">fill</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    ctx.<span style="color: #660066;">restore</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The <code>drawPath()</code> function takes svg path data as input (with the restriction that it contains only M (move to), L (line to), and Z (close path) elements), and executes <code>moveTo()</code> and <code>lineTo()</code> calls in the canvas element instead.  It does zero error checking, so the incoming svg path data must be well formatted.</p>
<p class="bottom">If you call it like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">drawPath<span style="color: #009900;">&#40;</span>ctx<span style="color: #339933;">,</span> <span style="color: #3366CC;">'M 0,0 L 0,10 10,10 10,0 Z'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>You&#8217;d get a nice 10px by 10px square as expected.</p>
<h3>Conclusion</h3>
<p>It&#8217;s no surprise that canvas is just as powerful as Flex or SVG as far as drawing shapes and gradients, but it would definitely make canvas extra cool if it had more intergrated svg path support.  Yes, of course, I could have used pure svg if I wanted to draw a bunch of svg shapes, but I had set my mind on using canvas only.</p>
<h5>Files</h5>
<ul>
<li><a href="http://saturnboy.com/proj/html5/clock/clock.html">clock.html</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://saturnboy.com/2013/10/html5-canvas-clock/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Adobe killed this blog</title>
		<link>http://saturnboy.com/2013/03/adobe-killed-this-blog/</link>
		<comments>http://saturnboy.com/2013/03/adobe-killed-this-blog/#comments</comments>
		<pubDate>Sun, 24 Mar 2013 18:20:54 +0000</pubDate>
		<dc:creator>justin</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Life]]></category>
		<category><![CDATA[Work]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[goals]]></category>

		<guid isPermaLink="false">http://saturnboy.com/?p=1951</guid>
		<description><![CDATA[Adobe killed this blog when they killed Flex. Well I guess Flex is not all the way killed, more like stumbing around with a knife in the neck. So what&#8217;s next? I&#8217;m breaking out my crystal ball to see what the future holds&#8230; Flex&#8217;s Future With a singular, spectacular blog post Adobe eviscerated Flex and [...]]]></description>
			<content:encoded><![CDATA[<p>Adobe killed this blog when they killed Flex. Well I guess Flex is not all the way killed, more like stumbing around with a knife in the neck. So what&#8217;s next? I&#8217;m breaking out my crystal ball to see what the future holds&#8230;</p>
<h3>Flex&#8217;s Future</h3>
<p class="bottom">With a singular, spectacular <a href="http://blogs.adobe.com/flex/2011/11/your-questions-about-flex.html">blog post</a> Adobe eviscerated Flex and any hope of ever using Flex again. Thanks Adobe! Flex is dead dead, not just a little dead. No one in their right mind should <b><i>ever</i></b> start development on a Flex app. Period.  I&#8217;ll make a nice pull quote just so you understand my opinion on the matter:</p>
<blockquote style="font-size:24px;margin:0;text-align:center;"><p style="margin:0;">&ldquo;Flex is dead!&rdquo;<span style="font-size:16px;">&nbsp;&mdash;&nbsp;Justin</span></p>
</blockquote>
<p><a href="http://www.spoon.as/">Spoon</a> is not dead, nor is the open sourcing of Flex on Apache (as <a href="http://flex.apache.org/">Apache Flex</a>), but alas, no amount of open sourcing or intense community love will change things in this particular case. But since everyone is in love with zombies nowadays, I&#8217;m sure there are plenty of people and companies out there that see the simulacrum of life in Flex and call it life.</p>
<h3>Flex Mobile&#8217;s Future (aka Adobe AIR)</h3>
<p>Flex Mobile, aka Adobe&#8217;s cross-compiler that take AS3 code as input and outputs iOS binaries (as a fully formed IPA) and also Android binaries (APK), is decidedly not dead yet. But the most unfortunate part is that it certainly should be. In general, cross-platform mobile solutions (particularly Flex Mobile) really suffer when the make contact with reality (a real app with real functionality and real users). Sure they can be made to look pretty, sure they make great demos, and sure they lower the initial bar for developer. But you will never <i>ever</i> have as much control as you will when you go native. It is still the case today (March 24 2013) that the only good answer is go native, and write it twice.  Twice being once for iOS and once for Android.</p>
<h3>Flash Player&#8217;s Future</h3>
<p>Banned from iOS, unsupported by Chrome Mobile, officially discontinued on all interesting mobile platforms, click to install on desktop Safari, no 64-bit flash player on linux, and now <a href="https://msujaws.wordpress.com/2012/04/11/opting-in-to-plugins-in-firefox/">click to run</a> on Firefox. Hmmm&#8230;I wonder what is next? How about dead. Yep, my crystal ball says that the flash player itself is on the gallows.</p>
<p>Let&#8217;s think about how Adobe makes money from Flash (this is coming from a developer that actually knows nothing)&#8230; From what I can tell, they sell really nice authoring tools, they sell some server-side tools (mainly Flash MediaServer, aka FMS), and they have a bunch of <a href="http://gaming.adobe.com/">game stuff</a>. I love games and I love watching video online, but does the flash-related parts of any of those mostly desktop businesses look healthy and awesome to you, especially with the rapid growth of mobile and tablets?</p>
<h3>Adobe&#8217;s Future</h3>
<p>Adobe will be certainly fine (but again, I&#8217;m just some guy who writes code). I always think of them as a tools company. Photoshop is a sweet piece of software (Photoshop 1.0 source is <a href="http://computerhistory.org/atchm/adobe-photoshop-source-code/">now available</a> in the Computer History Museum). But the whole platform play that is Flash and Flex? I always felt that was Macromedia&#8217;s idea, and Adobe just went along for the ride after the aquistion. That ride isn&#8217;t much fun anymore.</p>
<p>And who just left? <a href="http://www.macrumors.com/2013/03/19/adobe-chief-technology-officer-kevin-lynch-reportedly-joining-apple/">Kevin Lynch</a> is moving to Apple (aka they guy who was chief software architect at Macromedia).</p>
<h3>My Future</h3>
<p>I know I&#8217;ll be doing some more speaking. And I&#8217;m trying hard to figure out how to create more teaching opportunities. I love teaching, but for whatever reason it&#8217;s hard to find the right teaching opportunity with the right students.  I&#8217;ll always be learning new feameworks and languages, because that is a constant. But writing more content for this blog, probably not so much&#8230;</p>
<p>I&#8217;m working on couple of app ideas right now, and still writing lots of code, but only a little bit of that work will make it all the way to this blog.  Watch my <a href="http://github.com/saturnboy">my github</a> for some new projects.</p>
]]></content:encoded>
			<wfw:commentRss>http://saturnboy.com/2013/03/adobe-killed-this-blog/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Rotation in Flex</title>
		<link>http://saturnboy.com/2011/09/rotation-in-flex/</link>
		<comments>http://saturnboy.com/2011/09/rotation-in-flex/#comments</comments>
		<pubDate>Tue, 20 Sep 2011 03:00:23 +0000</pubDate>
		<dc:creator>justin</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[flex45]]></category>
		<category><![CDATA[rotation]]></category>

		<guid isPermaLink="false">http://saturnboy.com/?p=1901</guid>
		<description><![CDATA[Many ways to do the same thing&#8230;but which is right? It&#8217;s an issue developers face every single day. Here&#8217;s one of those issues that I faced this past week: rotating an object in Flex. I was playing with the camera in Flex Mobile, and I quickly learned that it is landscape only even if the [...]]]></description>
			<content:encoded><![CDATA[<p>Many ways to do the same thing&#8230;but which is right?  It&#8217;s an issue developers face every single day.</p>
<p class="bottom">Here&#8217;s one of those issues that I faced this past week: rotating an object in Flex.  I was playing with the camera in Flex Mobile, and I quickly learned that it is landscape only even if the device is being held in portrait.  Really, Adobe?</p>
<blockquote><p>&#8220;&#8230;a Video object attached to the camera will only show upright video in a landscape-aspect orientation. Thus, mobile apps should use a landscape orientation when displaying video and should not auto-rotate.&#8221;<br /><span style="text-align:right;font-style:normal;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8211; official Adobe docs for <a href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/media/Camera.html">flash.media.Camera</a></span></p></blockquote>
<p>Sad but true.  So, if you wanted to build a video chat app and use the front camera you either always hold the iPhone in landscape or you hold it portrait and rotate the video in code.  <span class="quiet">(Why did I say iPhone?&#8230;Alas, Flex Mobile does <b>not</b> provide front camera support for Android&#8230;period&#8230;none!)</span></p>
<h3>Option 1: <code>rotation</code> Attribute</h3>
<p class="bottom">The first thing to try is to rotate the easy way, like this:

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">camera</span>.<span style="color: #006600;">rotation</span> = <span style="color: #cc66cc;">90</span>;</pre></div></div>

<p>Using actionscript, I just set the rotation to 90&deg;.  Of course, this rotates about (0,0), the upper left corner in Flex, and not what we need.</p>
<h3>Option 2: <code>rotation</code>, Plus <code>transformX</code> and <code>transformY</code></h3>
<p class="bottom">To rotate an object about its center, you can set <code>transformX</code> and <code>transformY</code> (and <code>transformZ</code> too), like this:

<div class="wp_syntax"><div class="code"><pre class="mxml" style="font-family:monospace;"><span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Button</span> id=<span style="color: #ff0000;">&quot;btn&quot;</span> label=<span style="color: #ff0000;">&quot;My Button&quot;</span></span>
<span style="color: #000000;">    rotation=<span style="color: #ff0000;">&quot;90&quot;</span></span>
<span style="color: #000000;">    transformX=<span style="color: #ff0000;">&quot;{btn.width * 0.5}&quot;</span></span>
<span style="color: #000000;">    transformY=<span style="color: #ff0000;">&quot;{btn2.height * 0.5}&quot;</span> <span style="color: #7400FF;">/&gt;</span></span></pre></div></div>

<p>Using MXML, I set rotation to 90&deg;, plus transformX and transformY to half the width and height of the object (in this case, a button).  By moving the rotation point from the top left to the center of the button, I get what I want: an object rotating about its center. <span class="quiet">[I had forgotten how this works...thanks go to Bindu for reminding me!]</span></p>
<h3>Option 3: Matrix Rotation</h3>
<p>Option #2 works because every component in Flex has a full <a href="http://en.wikipedia.org/wiki/Affine_transformation">affine transformation matrix</a> behind it (as of Flash Player v9, I think, but only exposed to MXML in Flex 4).  In MXML, when you set attributes like <code>rotation</code>, <code>transformX</code>, and <code>transformY</code>, you are just setting the underlying transformation matrix.</p>
<p class="bottom">You can also set the matrix directly, like this:

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> mat:Matrix = <span style="color: #000000; font-weight: bold;">new</span> Matrix<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
mat.<span style="color: #006600;">translate</span><span style="color: #66cc66;">&#40;</span>-W<span style="color: #66cc66;">/</span><span style="color: #cc66cc;">2</span>, -H<span style="color: #66cc66;">/</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>;
mat.<span style="color: #006600;">rotate</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">PI</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>;
mat.<span style="color: #006600;">translate</span><span style="color: #66cc66;">&#40;</span>+W<span style="color: #66cc66;">/</span><span style="color: #cc66cc;">2</span>, +H<span style="color: #66cc66;">/</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>;
mat.<span style="color: #0066CC;">concat</span><span style="color: #66cc66;">&#40;</span>myObj.<span style="color: #006600;">transform</span>.<span style="color: #006600;">matrix</span><span style="color: #66cc66;">&#41;</span>;
myObj.<span style="color: #006600;">transform</span>.<span style="color: #006600;">matrix</span> = mat;</pre></div></div>

<p>To get a rotation about the center of our object, we first translate it, then rotate, then translate it back.  Note the <code>concat()</code> operation, which just &#8220;adds&#8221; the current matrix to our new rotation matrix, and thus preserves any previous transforms (maybe our object had <code>scaleX</code> or <code>scaleY</code> applied).</p>
<h3>Option 4: Rotate Animation</h3>
<p class="bottom">Lastly, if we want to watch the rotation happen, we can use a <code>Rotate</code> animation, like this:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> rot:Rotate = <span style="color: #000000; font-weight: bold;">new</span> Rotate<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
rot.<span style="color: #006600;">angleBy</span> = <span style="color: #cc66cc;">90</span>;
rot.<span style="color: #0066CC;">duration</span> = <span style="color: #cc66cc;">1000</span>;
rot.<span style="color: #006600;">autoCenterTransform</span> = <span style="color: #000000; font-weight: bold;">true</span>;
rot.<span style="color: #0066CC;">target</span> = myObj;
rot.<span style="color: #0066CC;">play</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Here we just instantiate a new spark Rotate effect, set its various properties, and start it with a call to play().  The key property to force rotation about the center is <code>autoCenterTransform</code>, just set it to <code>true</code>.</p>
<h3>Conclusion</h3>
<p>When I need total control, I find myself doing the full matrix solution.  Otherwise for anything simple, the MXML attributes are best.</p>
<p class="bottom">Here&#8217;s an example project showing all options (view source enabled):</p>
<div id="flashcontent-rotation">
Flash is required.  <a href="http://www.adobe.com/go/getflashplayer">Get it here!</a>
</div>
<h5>Files</h5>
<ul>
<li><a href="http://saturnboy.com/proj/flex45/rotation/RotationTester.html">RotationTester</a> (<a href="http://saturnboy.com/proj/flex45/rotation/RotationTester.fxp">fxp download</a>)</li>
</ul>
<div>
<script type="text/javascript">
swfobject.embedSWF('http://saturnboy.com/proj/flex45/rotation/RotationTester.swf', 'flashcontent-rotation', '470', '310', '10.3.0', 'playerProductInstall.swf', false, { bgColor:'#eeeeee', base:'.' });
</script>
</div>
]]></content:encoded>
			<wfw:commentRss>http://saturnboy.com/2011/09/rotation-in-flex/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Spark TreeList</title>
		<link>http://saturnboy.com/2011/09/spark-treelist/</link>
		<comments>http://saturnboy.com/2011/09/spark-treelist/#comments</comments>
		<pubDate>Sat, 10 Sep 2011 04:36:57 +0000</pubDate>
		<dc:creator>justin</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[custom component]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[flex45]]></category>

		<guid isPermaLink="false">http://saturnboy.com/?p=1883</guid>
		<description><![CDATA[Did a little bit of vanilla Flex work recently, and I needed a Tree component to display an object hierarchy. Everyone, by now, hopefully knows that mx:DataGrid and mx:Tree are two of the crappiest, bug ridden, worst performing components from Flex 3. And, everyone by now, has left the buggy world of Flex 3 behind [...]]]></description>
			<content:encoded><![CDATA[<p>Did a little bit of vanilla Flex work recently, and I needed a Tree component to display an object hierarchy. Everyone, by now, hopefully knows that <code>mx:DataGrid</code> and <code>mx:Tree</code> are two of the crappiest, bug ridden, worst performing components from Flex 3. And, everyone by now, has left the buggy world of Flex 3 behind and entered the world Flex 4 and the vastly improved Spark components.  With the arrival of Flex 4.5 this summer, Adobe finally gave us a rewritten Spark-based <code>DataGrid</code>. Alas, no updated <code>Tree</code> yet, so I had to write my own.  So once again, I turned to the trusty combo of <code>List</code> plus custom <code>ItemRenderer</code> to make pure-Spark custom <code>TreeList</code> component that doesn&#8217;t suck.</p>
<p>Alex Harui is the guru of turning a Flex 4 Spark <code>List</code> into a look-alike for the old Flex 3 component using skins and a custom <code>ItemRenderer</code>. Alex has used <code>List</code> + <code>ItemRenderer</code> to make a <a href="http://blogs.adobe.com/aharui/2009/12/spark_list_spark_list_spark_da.html">DataGrid</a>, <a href="http://blogs.adobe.com/aharui/2010/01/spark_datefield_and_colorpicke.html">DateField</a>, <a href="http://blogs.adobe.com/aharui/2010/01/spark_datefield_and_colorpicke.html">ColorPicker</a>, <a href="http://blogs.adobe.com/aharui/2010/02/spark_menu_and_menubar.html">Menu + MenuBar</a>, and even an <a href="http://blogs.adobe.com/aharui/2009/12/displaying_tree-like_hierarchi.html">XML-based TreeList</a>. Unfortunately, Alex&#8217;s <code>TreeList</code> assumes incoming XML, and I needed a <code>TreeList</code> that could display a simple object hierarchy (root node with children, and those children have children, etc.). Since I couldn&#8217;t find exactly what I wanted, I decided to build it myself.</p>
<h3>Flattener</h3>
<p>The key step to getting a hierarchy of objects to display as a list is: flatten the list, duh!  Or at least flatten the part of the tree you wish to display. So, I built a simple adapter class that turns an object hierarchy into an <code>ArrayList</code> that can be given directly to a <code>List</code>&#8216;s <code>dataProvider</code>.</p>
<p class="bottom">Here&#8217;s the actual flattener, but with just the comments not the code:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyObjFlattenedList <span style="color: #0066CC;">extends</span> ArrayList <span style="color: #66cc66;">&#123;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">//the root object</span>
    <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">_root</span>:MyObj;
&nbsp;
    <span style="color: #808080; font-style: italic;">//list of open nodes</span>
    <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _openList:<span style="color: #0066CC;">Array</span>;
&nbsp;
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> MyObjFlattenedList<span style="color: #66cc66;">&#40;</span>root:MyObj<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #0066CC;">_root</span> = root;
        _openList = <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>;
        reset<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        ...
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> reset<span style="color: #66cc66;">&#40;</span>openList:<span style="color: #0066CC;">Array</span> = <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #808080; font-style: italic;">//init the flattened list, starting at root</span>
        _openList = <span style="color: #66cc66;">&#40;</span>openList == <span style="color: #000000; font-weight: bold;">null</span> ? <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> : openList<span style="color: #66cc66;">&#41;</span>;
        <span style="color: #000000; font-weight: bold;">var</span> a:<span style="color: #0066CC;">Array</span> = <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>;
        addItemToList<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">_root</span>, a<span style="color: #66cc66;">&#41;</span>;
        <span style="color: #0066CC;">this</span>.<span style="color: #006600;">source</span> = a;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> addItemToList<span style="color: #66cc66;">&#40;</span>obj:MyObj, a:<span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #808080; font-style: italic;">//recursively walk obj and all of its &quot;open&quot; children to build</span>
        <span style="color: #808080; font-style: italic;">//a flattened list that is returned in array a</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> isItemOpen<span style="color: #66cc66;">&#40;</span>obj:MyObj<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Boolean</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #808080; font-style: italic;">//true if obj has children and is &quot;open&quot;</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> openItem<span style="color: #66cc66;">&#40;</span>obj:MyObj<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #808080; font-style: italic;">//add all of obj's children (if any) to the list</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> closeItem<span style="color: #66cc66;">&#40;</span>obj:MyObj<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #808080; font-style: italic;">//remove all of obj's children (if any) from the list</span>
    <span style="color: #66cc66;">&#125;</span>
    ...
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>There&#8217;s really not much to it.  When instantiated with a root object, the object hierarchy is walked recursively to build a flattened list of all the <i>open</i> nodes.  Once the initial list is built, <code>openItem()</code> can be called to open a node, and add all its children to the list.  Alternately, <code>closeItem()</code> can be called to close a node, and remove all its children from the list.</p>
<h3>Design</h3>
<p class="bottom">I used some basic styling and skinning, but the <code>ItemRenderer</code> does the majority of the work. Here&#8217;s the abbreviated version of <code>MyObjRenderer.mxml</code> with all the boring stuff edited out:</p>

<div class="wp_syntax"><div class="code"><pre class="mxml" style="font-family:monospace;"><span style="color: #000000;">&lt;?xml version=<span style="color: #ff0000;">&quot;1.0&quot;</span> encoding=<span style="color: #ff0000;">&quot;utf-8&quot;</span>?<span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:ItemRenderer</span></span>
<span style="color: #000000;">    xmlns:fx=<span style="color: #ff0000;">&quot;http://ns.adobe.com/mxml/2009&quot;</span> </span>
<span style="color: #000000;">    xmlns:s=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/spark&quot;</span></span>
<span style="color: #000000;">    width=<span style="color: #ff0000;">&quot;100%&quot;</span></span>
<span style="color: #000000;">    autoDrawBackground=<span style="color: #ff0000;">&quot;false&quot;</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;fx:Script</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;">&lt;!<span style="color: #66cc66;">&#91;</span>CDATA<span style="color: #66cc66;">&#91;</span></span>
<span style="color: #000000;">            <span style="color: #66cc66;">&#91;</span>Bindable<span style="color: #66cc66;">&#93;</span> private var _obj:MyObj;</span>
<span style="color: #000000;">            <span style="color: #66cc66;">&#91;</span>Bindable<span style="color: #66cc66;">&#93;</span> private var _hasChildren:Boolean = false;</span>
&nbsp;
<span style="color: #000000;">            private var _list:MyObjFlattenedList;</span>
&nbsp;
<span style="color: #000000;">            override public function set data<span style="color: #66cc66;">&#40;</span>val:Object<span style="color: #66cc66;">&#41;</span>:void <span style="color: #66cc66;">&#123;</span></span>
<span style="color: #000000;">                super.data = val;</span>
&nbsp;
<span style="color: #000000;">                if <span style="color: #66cc66;">&#40;</span>val != null<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></span>
<span style="color: #000000;">                    _obj = val as MyObj;</span>
&nbsp;
<span style="color: #000000;">                    var ownerList:List = owner as List;</span>
<span style="color: #000000;">                    _list = ownerList.dataProvider as MyObjFlattenedList;</span>
&nbsp;
<span style="color: #000000;">                    btn.selected = _list.isItemOpen<span style="color: #66cc66;">&#40;</span>_obj<span style="color: #66cc66;">&#41;</span>;</span>
<span style="color: #000000;">                    _hasChildren = <span style="color: #66cc66;">&#40;</span>_obj.children != null &amp;&amp; _obj.children.length <span style="color: #7400FF;">&gt;</span></span> 0);
                }
            }
&nbsp;
            private function toggleHandler():void {
                if (btn.selected) {
                    _list.openItem(_obj);
                } else {
                    _list.closeItem(_obj);
                }
            }
        ]]&gt;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/fx:Script</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:states</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:State</span> name=<span style="color: #ff0000;">&quot;normal&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:State</span> name=<span style="color: #ff0000;">&quot;hovered&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:State</span> name=<span style="color: #ff0000;">&quot;selected&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:states</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Rect</span> ...<span style="color: #7400FF;">&gt;</span></span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:HGroup</span> ...<span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:ToggleButton</span> id=<span style="color: #ff0000;">&quot;btn&quot;</span> click=<span style="color: #ff0000;">&quot;toggleHandler()&quot;</span> visible=<span style="color: #ff0000;">&quot;{_hasChildren}&quot;</span> ... <span style="color: #7400FF;">/&gt;</span></span>
&nbsp;
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Group</span> id=<span style="color: #ff0000;">&quot;dot&quot;</span> visible=<span style="color: #ff0000;">&quot;{!_hasChildren}&quot;</span> ... <span style="color: #7400FF;">/&gt;</span></span>
&nbsp;
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Label</span> ... <span style="color: #7400FF;">/&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:HGroup</span><span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:ItemRenderer</span><span style="color: #7400FF;">&gt;</span></span></pre></div></div>

<p>Each rendered item has a background <code>Rect</code> and a <code>Label</code>.  But most importantly, each row has either a ToggleButton (if the object has children) or some non-interactive visuals (if the object doesn&#8217;t have children it just gets a dot).  The toggle button is the key interactive element used to open or close the node, everything else is part of the visual gravy added to make the list look good.</p>
<p>Focusing on the functional stuff, if you look in the script block, you&#8217;ll see two functions: a <code>data()</code> setter and a <code>toggleHandler()</code> to handle toggle button clicks.  As expected, clicking the toggle button calls <code>openItem()</code> or <code>closeItem()</code> on the flattener adapter which adds or removes children from the flattened list, respectively.  The setter mostly sets up the local variables, but it notably computes if the object has children or not, and also sets the initial state of the toggle button.</p>
<h3>Conclusion</h3>
<p>With just a little effort, we can have a nice usable Spark TreeList component that looks decent.  More importantly, we have total control and can make the TreeList look like anything our designer can throw at us.  As is always the case, the combo of <code>List</code> + <code>ItemRenderer</code> proves to be awesome.  I tried to cover all the interesting pieces, but for the details, you&#8217;ll need to check out the source code.</p>
<p class="bottom">Here&#8217;s the finished product (view source enabled):</p>
<div id="flashcontent-objtreelist">
Flash is required.  <a href="http://www.adobe.com/go/getflashplayer">Get it here!</a>
</div>
<p>Use it and enjoy.</p>
<h5>Files</h5>
<ul>
<li><a href="http://saturnboy.com/proj/flex45/obj_treelist/ObjTreeList.html">ObjTreeList</a> (<a href="http://saturnboy.com/proj/flex45/obj_treelist/ObjTreeList.fxp">fxp download</a>)</li>
</ul>
<div>
<script type="text/javascript">
swfobject.embedSWF('http://saturnboy.com/proj/flex45/obj_treelist/ObjTreeList.swf', 'flashcontent-objtreelist', '240', '360', '10.3.0', 'playerProductInstall.swf', false, { bgColor:'#eeeeee', base:'.' });
</script>
</div>
]]></content:encoded>
			<wfw:commentRss>http://saturnboy.com/2011/09/spark-treelist/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Intro to OCHamcrest</title>
		<link>http://saturnboy.com/2011/08/intro-to-ochamcrest/</link>
		<comments>http://saturnboy.com/2011/08/intro-to-ochamcrest/#comments</comments>
		<pubDate>Wed, 17 Aug 2011 05:04:09 +0000</pubDate>
		<dc:creator>justin</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Work]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://saturnboy.com/?p=1862</guid>
		<description><![CDATA[Sometimes I feel like I write more test code than real code. For unit tests on iOS our stack is OCHamcrest, OCMock, and GHUnit. For functional tests, there&#8217;s nothing better than FoneMonkey. For this post, I&#8217;m going to focus on OCHamcrest. Hamcrest was born in the Java world as the matcher framework in jMock. It [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes I feel like I write more test code than real code. For unit tests on iOS our stack is <a href="https://github.com/jonreid/OCHamcrest">OCHamcrest</a>, <a href="http://www.mulle-kybernetik.com/software/OCMock/">OCMock</a>, and <a href="https://github.com/gabriel/gh-unit">GHUnit</a>. For functional tests, there&#8217;s nothing better than <a href="http://www.gorillalogic.com/fonemonkey">FoneMonkey</a>. For this post, I&#8217;m going to focus on OCHamcrest.</p>
<p><a href="http://code.google.com/p/hamcrest/">Hamcrest</a> was born in the Java world as the matcher framework in <a href="http://www.jmock.org/">jMock</a>. It was quickly extracted into its own framework  and has become somewhat of a monster in the testing world. It&#8217;s now included directly in <a href="http://www.junit.org/">JUnit</a> (since v4.4), and has been ported to many languages (OCHamcrest in Objective-C, <a href="https://github.com/drewbourne/hamcrest-as3">Hamcrest-AS3</a> in Actionscript, <a href="https://github.com/jonreid/PyHamcrest">PyHamcrest</a> in Python, etc.).  Additionally, the matcher concept is generally useful, and Hamcrest is <a href="http://code.google.com/p/hamcrest/wiki/UsesOfHamcrest">used is lots of different places</a> (my favorite is collection filtering with Hamcrest in <a href="http://code.google.com/p/lambdaj/">LambdaJ</a>).</p>
<p>When writing unit tests, OCHamcrest offers lots of advantages over the vanilla SenTest assertions.  First, there&#8217;s a ton of matchers that really make life easy, especially when testing collections like <code>NSArray</code>.  Second, OCHamcrest matchers are very readable in code, almost self-documenting.  Lastly, OCHamcrest automatically provides excellent failure messages when actual is not equal to expected.</p>
<h3>Matching Strings</h3>
<p class="bottom">Some string matching examples:</p>
<ul>
<li><b>is</b> &#8211; match the complete string</li>
<li><b>startsWith</b> &#8211; match the beginning of a string</li>
<li><b>endsWith</b> &#8211; match the end of a string</li>
<li><b>containsString</b> &#8211; match part of the string</li>
<li><b>equalTo</b> &#8211; match the complete string</li>
<li><b>equalToIgnoringCase</b> &#8211; match the complete string but ignore case</li>
<li><b>equalToIgnoringWhiteSpace</b> &#8211; match the complete string but ignore <i>extra</i> whitespace (new line, tab, or double spaces)</li>
</ul>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>s <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;FooBar&quot;</span>;
&nbsp;
assertThat<span style="color: #002200;">&#40;</span>s, is<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;FooBar&quot;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
assertThat<span style="color: #002200;">&#40;</span>s, startsWith<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Foo&quot;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
assertThat<span style="color: #002200;">&#40;</span>s, endsWith<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Bar&quot;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
assertThat<span style="color: #002200;">&#40;</span>s, containsString<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;oo&quot;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
assertThat<span style="color: #002200;">&#40;</span>s, equalToIgnoringCase<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;foobar&quot;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
assertThat<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot; X <span style="color: #2400d9;">\n</span>  Y <span style="color: #2400d9;">\t</span><span style="color: #2400d9;">\t</span>  Z <span style="color: #2400d9;">\n</span>&quot;</span>, equalToIgnoringWhiteSpace<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;X Y Z&quot;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<blockquote class="deeper"><p><b>NOTE:</b> Technically, <code>is</code> isn&#8217;t really a matcher, it&#8217;s a <i>matcher decorator</i> that implicity converts to the <code>equalTo</code> matcher. [thanks Jon!]</p></blockquote>
<h3>Combining Matchers</h3>
<p class="bottom">You can combine multiple matchers with:</p>
<ul>
<li><b>allOf</b> &#8211; AND together all matchers</li>
<li><b>anyOf</b> &#8211; OR togehter all matches</li>
</ul>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>s <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;FooBar&quot;</span>;
&nbsp;
assertThat<span style="color: #002200;">&#40;</span>s, allOf<span style="color: #002200;">&#40;</span>startsWith<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Foo&quot;</span><span style="color: #002200;">&#41;</span>, endsWith<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Bar&quot;</span><span style="color: #002200;">&#41;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
assertThat<span style="color: #002200;">&#40;</span>s, anyOf<span style="color: #002200;">&#40;</span>startsWith<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Foo&quot;</span><span style="color: #002200;">&#41;</span>, startsWith<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Bar&quot;</span><span style="color: #002200;">&#41;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
assertThat<span style="color: #002200;">&#40;</span>s, anyOf<span style="color: #002200;">&#40;</span>endsWith<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Foo&quot;</span><span style="color: #002200;">&#41;</span>, endsWith<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Bar&quot;</span><span style="color: #002200;">&#41;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<blockquote class="deeper"><p><b>NOTE:</b> The list of matchers must be <code>nil</code> terminated.</p></blockquote>
<p class="bottom">You can invert a matcher, or multiple matchers, with:</p>
<ul>
<li><b>isNot</b> &#8211; negate the matcher</li>
</ul>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>s <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;FooBar&quot;</span>;
&nbsp;
assertThat<span style="color: #002200;">&#40;</span>s, isNot<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;foo&quot;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
assertThat<span style="color: #002200;">&#40;</span>s, isNot<span style="color: #002200;">&#40;</span>endsWith<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Baz&quot;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
assertThat<span style="color: #002200;">&#40;</span>s, isNot<span style="color: #002200;">&#40;</span>allOf<span style="color: #002200;">&#40;</span>startsWith<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Baz&quot;</span><span style="color: #002200;">&#41;</span>, endsWith<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Baz&quot;</span><span style="color: #002200;">&#41;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
assertThat<span style="color: #002200;">&#40;</span>s, isNot<span style="color: #002200;">&#40;</span>anyOf<span style="color: #002200;">&#40;</span>startsWith<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Baz&quot;</span><span style="color: #002200;">&#41;</span>, startsWith<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Baz&quot;</span><span style="color: #002200;">&#41;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<h3>Matching <code>nil</code></h3>
<p class="bottom">You can match <code>nil</code> with:</p>
<ul>
<li><b>nilValue()</b> &#8211; stands in for <code>nil</code></li>
<li><b>notNilValue()</b> &#8211; stands in for <code>!nil</code></li>
</ul>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSObject</span> <span style="color: #002200;">*</span>o <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
assertThat<span style="color: #002200;">&#40;</span>o, nilValue<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>s <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;FooBar&quot;</span>;
assertThat<span style="color: #002200;">&#40;</span>s, notNilValue<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<h3>Matching Classes</h3>
<p class="bottom">You can match an instance&#8217;s class with:</p>
<ul>
<li><b>instanceOf</b> &#8211; match the class</li>
</ul>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>s <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;FooBar&quot;</span>;
assertThat<span style="color: #002200;">&#40;</span>s, instanceOf<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> class<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<h3>Matching Numbers</h3>
<p class="bottom">One of the great pains of Objective-C is typing numbers from primitive types to objects and back again.  OCHamcrest has a variety of matchers the help make life easy.</p>
<ul>
<li><b>assertThatInt</b> &#8211; typed assert that expects an <code>int</code> (other types too: <code>assertThatFloat</code>, <code>assertThatDouble</code>, etc.)</li>
<li><b>equalToInt</b> &#8211; typed equals that takes an <code>int</code> (other types too: <code>equalToFloat</code>, <code>equalToDouble</code>, <code>equalToBool</code>, etc.)</li>
<li><b>closeTo</b> &#8211; match a number with a target number plus or minus a delta (both params are <code>double</code>)</li>
<li><b>lessThan</b> &#8211; match a number less than the given number (param is <code>NSNumber</code>), also <code>lessThanOrEqualTo</code></li>
<li><b>greaterThan</b> &#8211; match a number greater than the given number (param is <code>NSNumber</code>), also <code>greaterThanOrEqualTo</code></li>
</ul>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">assertThatInt<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">5</span>, equalToInt<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">5</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
assertThatFloat<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">3.14</span>, equalToFloat<span style="color: #002200;">&#40;</span>3.14f<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
assertThatBool<span style="color: #002200;">&#40;</span> <span style="color: #a61390;">false</span>, equalToBool<span style="color: #002200;">&#40;</span><span style="color: #a61390;">NO</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #400080;">NSNumber</span> <span style="color: #002200;">*</span>i <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithInt<span style="color: #002200;">:</span><span style="color: #2400d9;">5</span><span style="color: #002200;">&#93;</span>;
assertThat<span style="color: #002200;">&#40;</span>i, equalToInt<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">5</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
assertThat<span style="color: #002200;">&#40;</span>i, is<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithInt<span style="color: #002200;">:</span><span style="color: #2400d9;">5</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #400080;">NSNumber</span> <span style="color: #002200;">*</span>f <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithFloat<span style="color: #002200;">:</span>3.14f<span style="color: #002200;">&#93;</span>;
assertThat<span style="color: #002200;">&#40;</span>f, equalToFloat<span style="color: #002200;">&#40;</span>3.14f<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
assertThat<span style="color: #002200;">&#40;</span>f, is<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithFloat<span style="color: #002200;">:</span>3.14f<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>The easiest cleanest approach is to use <code>assertThatInt</code> with <code>equalToInt</code>, the next best option is to use the vanilla <code>assertThat</code> with <code>equalToInt</code>, the most verbose option is to use <code>NSNumber</code> everywhere.</p>
<p class="bottom">It&#8217;s easy to make rough number comparisons too:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSNumber</span> <span style="color: #002200;">*</span>f <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithFloat<span style="color: #002200;">:</span>3.14f<span style="color: #002200;">&#93;</span>;
assertThat<span style="color: #002200;">&#40;</span>f, closeTo<span style="color: #002200;">&#40;</span>3.0f, 0.25f<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
assertThat<span style="color: #002200;">&#40;</span>f, lessThan<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithInt<span style="color: #002200;">:</span><span style="color: #2400d9;">4</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
assertThat<span style="color: #002200;">&#40;</span>f, greaterThan<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithInt<span style="color: #002200;">:</span><span style="color: #2400d9;">3</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<blockquote class="deeper"><p><b>NOTE:</b> It is a little weird, but <code>closeTo</code> takes <code>double</code> params, but everything else expects <code>NSNumber</code> params.</p></blockquote>
<p class="bottom">Numeric comparisons also work great on dates too:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSDate</span> <span style="color: #002200;">*</span>now <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDate</span> date<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">//now minus 1000 seconds</span>
<span style="color: #400080;">NSDate</span> <span style="color: #002200;">*</span>beforeNow <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDate</span> dateWithTimeIntervalSinceNow<span style="color: #002200;">:-</span><span style="color: #2400d9;">1000</span><span style="color: #002200;">&#93;</span>; 
&nbsp;
assertThat<span style="color: #002200;">&#40;</span>now, greaterThan<span style="color: #002200;">&#40;</span>beforeNow<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<h3>Matching Arrays</h3>
<p class="bottom">Easily the best part of OCHamcrest is its ability to match lists of objects.  Array matchers are every powerful, but don&#8217;t forget to add the terminating <code>nil</code> to all lists.</p>
<ul>
<li><b>hasItem</b> &#8211; match if given item appears in the list</li>
<li><b>hasItems</b> &#8211; match if all given items appear in the list (in any order)</li>
<li><b>contains</b> &#8211; match exactly the entire array</li>
<li><b>containsInAnyOrder</b> &#8211; match entire array, but in any order</li>
<li><b>hasCountOf</b> &#8211; match the size of the array</li>
<li><b>empty</b> &#8211; match an empty array</li>
</ul>
<p class="bottom">Here some basic array examples:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>a <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSArray</span> array<span style="color: #002200;">&#93;</span>;
&nbsp;
assertThat<span style="color: #002200;">&#40;</span>a, is<span style="color: #002200;">&#40;</span>empty<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
assertThat<span style="color: #002200;">&#40;</span>a, hasCountOf<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p class="bottom">Here some <code>hasItem</code> examples:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>a <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSArray</span> arrayWithObjects<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;a&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;b&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;c&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
assertThat<span style="color: #002200;">&#40;</span>a, hasItem<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;a&quot;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
assertThat<span style="color: #002200;">&#40;</span>a, isNot<span style="color: #002200;">&#40;</span>hasItem<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;X&quot;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
assertThat<span style="color: #002200;">&#40;</span>a, hasItem<span style="color: #002200;">&#40;</span>equalToIgnoringCase<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;A&quot;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>The last matcher may look a little weird, but remember matchers expect a matcher as their input param, and only default to <code>equalTo</code> if none is given.  Thus, the first matcher <code>hasItem(@"a")</code> can be rewritten as <code>hasItem(equalTo(@"a"))</code>.</p>
<p class="bottom">We repeat the above example, but this time using numbers in our <code>NSArray</code>.  As you can see below, all the number matchers require us to explicitly use <code>equalToInt</code> everywhere:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>a <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSArray</span> arrayWithObjects<span style="color: #002200;">:</span>
    <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithInt<span style="color: #002200;">:</span><span style="color: #2400d9;">2</span><span style="color: #002200;">&#93;</span>,
    <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithInt<span style="color: #002200;">:</span><span style="color: #2400d9;">3</span><span style="color: #002200;">&#93;</span>,
    <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithInt<span style="color: #002200;">:</span><span style="color: #2400d9;">5</span><span style="color: #002200;">&#93;</span>,
    <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
assertThat<span style="color: #002200;">&#40;</span>a, hasItem<span style="color: #002200;">&#40;</span>equalToInt<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
assertThat<span style="color: #002200;">&#40;</span>a, isNot<span style="color: #002200;">&#40;</span>hasItem<span style="color: #002200;">&#40;</span>equalToInt<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">13</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
assertThat<span style="color: #002200;">&#40;</span>a, contains<span style="color: #002200;">&#40;</span>equalToInt<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span>, equalToInt<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">3</span><span style="color: #002200;">&#41;</span>, equalToInt<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">5</span><span style="color: #002200;">&#41;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p class="bottom">Here are some more complex array matchers:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>a <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSArray</span> arrayWithObjects<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;a&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;b&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;c&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
assertThat<span style="color: #002200;">&#40;</span>a, hasItems<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;b&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;a&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
assertThat<span style="color: #002200;">&#40;</span>a, contains<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;a&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;b&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;c&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
assertThat<span style="color: #002200;">&#40;</span>a, containsInAnyOrder<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;c&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;b&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;a&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
assertThat<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>a componentsJoinedByString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;,&quot;</span><span style="color: #002200;">&#93;</span>, is<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;a,b,c&quot;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>And as I show in the last matcher, you can always dump to a string, and just match strings.</p>
<h3>Matching Dictionaries</h3>
<p class="bottom">The dictionary matchers build on the array matchers:</p>
<ul>
<li><b>hasKey</b> &#8211; match a key</li>
<li><b>hasValue</b> &#8211; match a value</li>
<li><b>hasEntry</b> &#8211; match a key-value pair</li>
<li><b>hasEntries</b> &#8211; match a list of k-v pairs</li>
</ul>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>d <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithObjectsAndKeys<span style="color: #002200;">:</span>
    <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;valA&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;keyA&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;valB&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;keyB&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;valC&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;keyC&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
assertThat<span style="color: #002200;">&#40;</span>d, hasCountOf<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">3</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
assertThat<span style="color: #002200;">&#40;</span>d, isNot<span style="color: #002200;">&#40;</span>empty<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
assertThat<span style="color: #002200;">&#40;</span>d, hasKey<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;keyA&quot;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
assertThat<span style="color: #002200;">&#40;</span>d, isNot<span style="color: #002200;">&#40;</span>hasKey<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;keyX&quot;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
assertThat<span style="color: #002200;">&#40;</span>d, hasValue<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;valA&quot;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
assertThat<span style="color: #002200;">&#40;</span>d, hasEntry<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;keyA&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;valA&quot;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
assertThat<span style="color: #002200;">&#40;</span>d, hasEntries<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;keyA&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;valA&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;keyC&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;valC&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<h3>Matcher Error Messages</h3>
<p class="bottom">When a matcher fails, you get a standardize error message of: <code>Expected "foo", but was "bar"</code>.  This default message is easy to modify by using the <code>describedAs()</code> matcher in place of the typical <code>is()</code> matcher.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>s <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;bar&quot;</span>;
&nbsp;
assertThat<span style="color: #002200;">&#40;</span>s, is<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;foo&quot;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #11740a; font-style: italic;">//Expected &quot;foo&quot;, but was &quot;bar&quot;</span>
&nbsp;
assertThat<span style="color: #002200;">&#40;</span>s, describedAs<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;doh! this should be 'foo'&quot;</span>, equalTo<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;foo&quot;</span><span style="color: #002200;">&#41;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #11740a; font-style: italic;">//Expected doh! this should be 'foo', but was &quot;bar&quot;</span>
&nbsp;
assertThat<span style="color: #002200;">&#40;</span>s, describedAs<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;doh! this should be foo, %0, %1&quot;</span>, equalTo<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;foo&quot;</span><span style="color: #002200;">&#41;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;baz&quot;</span>, <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithInt<span style="color: #002200;">:</span><span style="color: #2400d9;">42</span><span style="color: #002200;">&#93;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #11740a; font-style: italic;">//Expected doh! this should be foo, &quot;baz&quot;, &lt;42&gt;, but was &quot;bar&quot;</span></pre></div></div>

<blockquote class="deeper"><p><b>NOTE:</b> The argument list for <code>describedAs()</code> <b>MUST</b> end with <code>nil</code> or your tests will crash instantly with no useful error message.</p></blockquote>
<h3>Building a Custom Matcher</h3>
<p class="bottom">Writing your own custom matchers is relatively easy.  Here&#8217;s an example of a matcher that matches the value of some property on an object:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &lt;OCHamcrestIOS/OCHamcrestIOS.h&gt;</span>
<span style="color: #6e371a;">#import &lt;objc/objc-api.h&gt;</span>
&nbsp;
<span style="color: #a61390;">@interface</span> HasProperty <span style="color: #002200;">:</span> HCBaseMatcher <span style="color: #002200;">&#123;</span>
    <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>property;
    id&lt;HCMatcher&gt; valueMatcher;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> hasProperty<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>aProperty value<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>id&lt;HCMatcher&gt;<span style="color: #002200;">&#41;</span>aValueMatcher;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> initWithProperty<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>aProperty value<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>id&lt;HCMatcher&gt;<span style="color: #002200;">&#41;</span>aValueMatcher;
&nbsp;
<span style="color: #a61390;">@end</span>
&nbsp;
OBJC_EXPORT id&lt;HCMatcher&gt; hasProperty<span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>property, <span style="color: #a61390;">id</span> valueMatcher<span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>We extends <code>HCBaseMatcher</code> with our custom <code>HasProperty</code> class.  We store the name of the property and a value matcher.</p>
<p class="bottom">And the implementation:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;HasProperty.h&quot;</span>
<span style="color: #6e371a;">#import &lt;OCHamcrestIOS/HCDescription.h&gt;</span>
<span style="color: #6e371a;">#import &lt;OCHamcrestIOS/HCWrapInMatcher.h&gt;</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> HasProperty
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> hasProperty<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>aProperty value<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>id&lt;HCMatcher&gt;<span style="color: #002200;">&#41;</span>aValueMatcher <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self alloc<span style="color: #002200;">&#93;</span> initWithProperty<span style="color: #002200;">:</span>aProperty value<span style="color: #002200;">:</span>aValueMatcher<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> initWithProperty<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>aProperty value<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>id&lt;HCMatcher&gt;<span style="color: #002200;">&#41;</span>aValueMatcher <span style="color: #002200;">&#123;</span>
    self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super init<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>self <span style="color: #002200;">!=</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        property <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>aProperty copy<span style="color: #002200;">&#93;</span>;
        valueMatcher <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>aValueMatcher retain<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
    <span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> dealloc <span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>property release<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>valueMatcher release<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>super dealloc<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>matches<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>item <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">SEL</span> propertyGetter <span style="color: #002200;">=</span> NSSelectorFromString<span style="color: #002200;">&#40;</span>property<span style="color: #002200;">&#41;</span>;
&nbsp;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>item respondsToSelector<span style="color: #002200;">:</span>propertyGetter<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>valueMatcher matches<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>item performSelector<span style="color: #002200;">:</span>propertyGetter<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
            <span style="color: #a61390;">return</span> <span style="color: #a61390;">YES</span>;
    <span style="color: #002200;">&#125;</span>
    <span style="color: #a61390;">return</span> <span style="color: #a61390;">NO</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> describeTo<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>id&lt;HCDescription&gt;<span style="color: #002200;">&#41;</span>description <span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>description appendText<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;an object with a property named '%@' with a value of {&quot;</span>, property<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>
        appendDescriptionOf<span style="color: #002200;">:</span>valueMatcher<span style="color: #002200;">&#93;</span>
        appendText<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;}&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@end</span>
&nbsp;
OBJC_EXPORT id&lt;HCMatcher&gt; hasProperty<span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>property, <span style="color: #a61390;">id</span> valueMatcher<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>HasProperty hasProperty<span style="color: #002200;">:</span>property value<span style="color: #002200;">:</span>HCWrapInMatcher<span style="color: #002200;">&#40;</span>valueMatcher<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>When we write a custom matcher, we must implement two methods, <code>matches:</code> to do the matching and <code>describeTo:</code> to provide feedback in case of match failure.  In the above code, we first construct a selector from the given property name, then call the selector to get the actual property value, and finally check if actual matches the expected value (given by the <code>valueMatcher</code>).</p>
<p class="bottom">Usage looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">Person <span style="color: #002200;">*</span>p <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>Person personWithFirstName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Joe&quot;</span> andLastname<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Doe&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
assertThat<span style="color: #002200;">&#40;</span>p, hasProperty<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;firstName&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Joe&quot;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p class="bottom">Or more importantly, we can now use our custom <code>hasProperty</code> matcher to match an arrays of objects:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>a <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSArray</span> arrayWithObjects<span style="color: #002200;">:</span>
    <span style="color: #002200;">&#91;</span>Person personWithFirstName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Joe&quot;</span> andLastname<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Doe&quot;</span><span style="color: #002200;">&#93;</span>,
    <span style="color: #002200;">&#91;</span>Person personWithFirstName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Joe&quot;</span> andLastname<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Smith&quot;</span><span style="color: #002200;">&#93;</span>,
    <span style="color: #002200;">&#91;</span>Person personWithFirstName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Jane&quot;</span> andLastname<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Allen&quot;</span><span style="color: #002200;">&#93;</span>,
    <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
assertThat<span style="color: #002200;">&#40;</span>a, contains<span style="color: #002200;">&#40;</span>
    hasProperty<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;firstName&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Joe&quot;</span><span style="color: #002200;">&#41;</span>,
    hasProperty<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;firstName&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Joe&quot;</span><span style="color: #002200;">&#41;</span>,
    hasProperty<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;firstName&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Jane&quot;</span><span style="color: #002200;">&#41;</span>,
    <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>That&#8217;s it.  Go forth and match.</p>
<p><b>UPDATE:</b> I put the above <code>hasProperty</code> matcher into <a href="https://github.com/jonreid/OCHamcrest/pull/2">a pull request</a>, and Jon Reid accepted it into OCHamcrest v1.6. He even wrote a <a href="http://jonreid.blogs.com/qualitycoding/2011/10/simplify-your-tests-with-ochamcrests-hasproperty.html">nice post</a> about it.  Get the lastest OCHamcrest from <a href="https://github.com/jonreid/OCHamcrest/">github</a>.</p>
<h3>Links</h3>
<ul>
<li><a href="https://github.com/jonreid/OCHamcrest">OCHamcrest</a> &#8211; github repo</li>
<li><a href="http://jonreid.github.com/OCHamcrest/">OCHamcrest Docs</a> &#8211; docs</li>
<li><a href="http://code.google.com/p/hamcrest/">Hamcrest</a> &#8211; on Java</a>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://saturnboy.com/2011/08/intro-to-ochamcrest/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Objective-C: The Good, The Bad, and The Ugly</title>
		<link>http://saturnboy.com/2011/03/objective-c-good-bad-ugly/</link>
		<comments>http://saturnboy.com/2011/03/objective-c-good-bad-ugly/#comments</comments>
		<pubDate>Tue, 22 Mar 2011 02:07:36 +0000</pubDate>
		<dc:creator>justin</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[Xcode]]></category>

		<guid isPermaLink="false">http://saturnboy.com/?p=1774</guid>
		<description><![CDATA[Over the past three years, I&#8217;ve been programming mostly Flex and Java, with few other languages sprinkled on top (Javascript, PHP, Erlang). Recently, I&#8217;ve made the transition to doing exclusively iOS development and slinging Objective-C all day long (I&#8217;ve even taken over the Boulder iOS meetup). Here&#8217;s my take on Objective-C as a programming language, [...]]]></description>
			<content:encoded><![CDATA[<p>Over the past three years, I&#8217;ve been programming mostly Flex and Java, with few other languages sprinkled on top (Javascript, PHP, Erlang).  Recently, I&#8217;ve made the transition to doing exclusively iOS development and slinging Objective-C all day long (I&#8217;ve even taken over the <a href="http://www.meetup.com/Boulder-iOS/">Boulder iOS meetup</a>).  Here&#8217;s my take on Objective-C as a programming language, along with a snapshot of life from the trenches of a professional Objective-C developer.</p>
<h2>The Good</h2>
<p>Using square brackets to call methods is a little wack, but you&#8217;ll get over that in about 20 minutes.  Honestly, the syntax is a little bumpy for a day or two, tops&#8230;after that, there&#8217;s a lot to like about Objective-C.</p>
<h3>Dynamic</h3>
<p>Objective-C is a very dynamic language, not quite at the level of Ruby, but it&#8217;s pretty dang close.  You&#8217;ve got the <code>id</code> data type when you need dynamic typing, but you can also use static typing for some compiler love.  You&#8217;ve got Categories for extending classes.  And you&#8217;ve got full-on runtime meta-programming that allows you to <a href="http://cocoawithlove.com/2010/01/what-is-meta-class-in-objective-c.html">create new classes</a> at runtime, <a href="http://theocacao.com/document.page/327">create new methods</a> on existing classes at runtime, and <a href="http://cocoadev.com/index.pl?MethodSwizzling">swizzle methods</a> at runtime.  Objective-C&#8217;s also got <a href="http://www.mikeash.com/pyblog/friday-qa-2009-03-27-objective-c-message-forwarding.html">message forwarding</a> (just a more verbose <code>method_missing</code> if you know Ruby) for handling unknown method calls at runtime.</p>
<p>Not to shabby.  With the exception of the <code>id</code> data type and using <code>@selector</code> everywhere, I seldom use the dynamic features.  Since dynamism is completely unobtrusive in Objective-C, I don&#8217;t have to worry about it, but I know it&#8217;s there if I need it.  Basically, it&#8217;s a big warm fuzzy.</p>
<h3>Blocks</h3>
<p>Closures are pretty sweet in any language, but the Objective-C implementation, called Blocks, is better than most.  The addition of Blocks means that functions are now first class objects in the runtime, meaning they can be created, saved, executed, copied, or passed to other functions at runtime.  Blocks are directly supported by the base Objective-C classes and in some iOS APIs too (examples include <code>NSArray</code>&#8216;s <code>enumerateObjectsUsingBlock:</code> or <code>UIView</code>&#8216;s <code>animateWithDuration:animations:</code>).</p>
<p class="bottom">But the coolest thing you can do with Blocks is to use them to create chucks of work that can be executed in parallel.  The hard part is breaking a job out into independent chunks of work, but once you have that implementation is trivial in Objective-C.  You just use <code>NSArray</code>&#8216;s <code>enumerateObjectsWithOptions:usingBlock:</code> with the <code>NSEnumerationConcurrent</code> option to do work on the chunks concurrently, like this:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span>myArray enumerateObjectsWithOptions<span style="color: #002200;">:</span>NSEnumerationConcurrent
                          usingBlock<span style="color: #002200;">:^</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span> obj, NSUInteger idx, <span style="color: #a61390;">BOOL</span> <span style="color: #002200;">*</span>stop<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">//do chunk of work on obj</span>
<span style="color: #002200;">&#125;</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>And if you really need to some concurrent kung-fu, Objective-C provides <code>NSOperationQueue</code> along with Grand Central Dispatch to really kick some ass.  If you can forgive the Charlie Sheen reference, I like to think of Blocks as the tiger blood of concurrent programming.  Winners use Blocks.</p>
<p>Good references: Drew McCormack&#8217;s <a href="http://www.macresearch.org/cocoa-scientists-xxxii-10-uses-blocks-cobjective-c">10 Uses for Blocks</a>; Make Ash&#8217;s <a href="http://www.mikeash.com/pyblog/friday-qa-2008-12-26.html">part 1</a> and <a href="http://www.mikeash.com/pyblog/friday-qa-2009-08-14-practical-blocks.html">part 2</a>; Mike Clark&#8217;s <a href="http://pragmaticstudio.com/blog/2010/7/28/ios4-blocks-1">basic blocks</a> and <a href="http://pragmaticstudio.com/blog/2010/9/15/ios4-blocks-2">advanced blocks</a>.</p>
<h3>UIAutomation</h3>
<p>UIAutomation is not much to look at.  And it actually starts looking pretty bad as a functional testing tool when compared to <a href="http://seleniumhq.org/">Selenium</a> or <a href="http://www.gorillalogic.com/flexmonkey">FlexMonkey</a> or <a href="http://www.gorillalogic.com/flexmonkey">FoneMonkey</a>.  But it&#8217;s there, it works, it&#8217;s full-baked, and it appears to be Apple&#8217;s way forward.  Automation is so powerful, particularly on big apps developed by big teams.  UIAutomation&#8217;s integration with Instruments is it&#8217;s achilles heel right now, so hopefully Apple brings out an update that is happily command-line-runnable soon.  Until then, it&#8217;s still super awesome to have an automation tool.</p>
<h2>The Bad</h2>
<p>There&#8217;s a lot of not-good-yet stuff in Objective-C development.  For the most part, I&#8217;m optimistic that much of stuff will get fixed over time.</p>
<h3>The Community</h3>
<p>With the exception of <a href="http://stackoverflow.com/questions/tagged/objective-c">Stack Overflow</a> (and that&#8217;s a major exception because it&#8217;s just awesome), the Objective-C community is just getting started, for the obvious reason that it&#8217;s so new on the radar.  The number of books, bloggers, libraries, frameworks, et cetera is just not there yet.  From my own personal experience, I find it very analogous to the Ruby community circa Rails 1.0.  A few, well known, super dev bloggers, a few good books, a few widely used frameworks (<a href="http://three20.info/">Three20</a>, <a href="http://www.cocos2d-iphone.org/">cocos2d</a>), and a lot of eager wannabes (like me then with ruby, and me now with iOS) <i class="quiet">[Note to self: Am I just a career wannabe?  Discuss.]</i>  But look at ruby now&#8230;the community got so big that they are now experiencing contraction (the Rails/Merb merge is the big example).</p>
<p>I&#8217;m always googling while writing code, but with Objective-C I often find myself coming up empty.  A lot of first time never-been-done-before stuff is going on in Objective-C and iOS, which is certainly exciting, but not without its challenges to day-to-day development.  I don&#8217;t doubt things will improve in the future, but right now I find the community lacking.</p>
<h3>Tools</h3>
<p>Much like the community, the tool chain is just getting started.  There&#8217;s a ton of Makefiles out there, along with piles of bash scripts.  And I swear people are still jumping to the command line in gdb.  Of course, this is all the C/C++ heritage, but come on people!  Who uses this stuff and actually likes it?  It&#8217;s not like <a href="http://ant.apache.org/">Ant</a> and <a href="http://maven.apache.org/">Maven</a> were forged by the gods, but Makefiles?  Save me.</p>
<p>And what&#8217;s up with <i>Other Linker Flags</i>?  This whole thing to totally idiotic.  Why can&#8217;t we invent some new packaging format that knows it must be added with <code>-lstdc++</code> or <code>-ObjC</code>?  Super lame.</p>
<p>I&#8217;ve been getting pretty dirty with <a href="http://rake.rubyforge.org/">Rake</a>, <a href="http://ant.apache.org/ivy/">Ivy</a>, and <a href="http://nexus.sonatype.org/">Sonatype&#8217;s Nexus</a> to bring some serenity to the situation.  Definitely a work in progress, but it&#8217;s getting there.</p>
<h2>The Ugly</h2>
<p>The ugly parts are rather unfortunate, especially given the beauty of Apple&#8217;s hardware. It&#8217;s also a little hard for non-programmers to see, because the user-facing bits of the software can be made so beautiful too. But I would certainly argue that the beauty of the UI is done despite of some evil ugly warts in the code. Maybe I&#8217;m being a little too Zen, or maybe I&#8217;m a hopeless romantic, or maybe I&#8217;m just being naive, but it sure would be great if inner beauty at the code level eventually lead to beauty at the surface.</p>
<h3>Memory Management</h3>
<p>Everyone loves to hate on Objective-C&#8217;s lack of garbage collection on iOS. I&#8217;m on the bandwagon on this one too, because memory management just plain sucks. The constant dance of 	<code>retain</code>, <code>release</code>, and <code>autorelease</code> is unrelenting, and <b>so hateful!</b> For an alternate data point, with the release of Gingerbread, Android has a new <a href="http://developer.android.com/sdk/android-2.3-highlights.html#DeveloperApis">concurrent garbage collector</a>. Come on Apple, give us some love.</p>
<p>Writing leak free code on the first pass is basically impossible, especially on a big app. So now I&#8217;ve had to expand my toolbox to include tools like Clang and Leaks and UIAutomation to find and fix all my memory bugs. It&#8217;s just ugly plain and simple.</p>
<h3>Xcode</h3>
<p>By far the ugliest part of Objective-C is the Xcode IDE. <b>By far!</b>  I&#8217;ve used the question, &quot;Tell me about some of the warts of iOS development?&quot;, a couple of times in recent interviews. It turns out to be a great differentiator between professional developers that are familiar with team development and professional tools and people that know how to code. All the candidates I&#8217;ve asked can easily point to memory management as one of the ugly parts, but almost no one says the Xcode.</p>
<p>If you&#8217;ve ever used a real IDE (<a href="http://www.eclipse.org/">Eclipse</a>, <a href="http://www.jetbrains.com/idea/">IntelliJ</a>, <a href="http://www.microsoft.com/visualstudio/en-us/">Visual Studio</a>), then you are probably familiar with various code-level helpers like generate getters/setters, rename class, automatic code formatting on save, just to give a few examples. Xcode doesn&#8217;t generate getters or setters from ivars. Nor does it do class rename, only file rename, how pathetic is that?  It doesn&#8217;t do any intelligent code formatting either on save or otherwise. And these are just my code-level gripes&#8230;</p>
<p>You are also probably familiar with the various development tooling integrations that put the <b>I</b> in Integrated Development Environment.  Things like using source control, viewing graphical diffs, running unit tests, running scripts, debugging with variable watches,  writing tickets, submitting bug reports, doing code reviews, and more.  Nope, Xcode doesn&#8217;t do any of those things either.</p>
<p>In the end, I give Xcode a &quot;syntax highlighter plus&quot; rating, which puts it just above vanilla <a href="http://www.vim.org/">Vim</a> but below a super tricked out Vim setup.</p>
<h2>Conclusion</h2>
<p>I&#8217;m an iOS developer by choice, but just because I&#8217;m splashing around in the mud like a happy pig right now doesn&#8217;t mean I like being dirty all the time. <i class="quiet">[For more about &quot;the mud&quot;, read my rablings <a href="http://saturnboy.com/2010/11/to-the-mountaintop/">here</a>.]</i> The ugly bits do have one positive effect: my value (and hence my salary) are improved. By setting the bar high, either intentionally or unintentionally, Apple is effectively keeping the riffraff out.</p>
<p>Hopefully, GC will arrive once all the iOS hardware gets multiple cores.  Or maybe ARC might be decent. And <a href="http://www.jetbrains.com/cidr/">JetBrains CIDR</a> (Objective-C IDE from the makers of IntelliJ) will turn out to be awesome. Onward and upward.</p>
<p><b>Update 2013-04-17:</b> Yes, ARC is pretty sweet. Yes, JetBrains <a href="http://www.jetbrains.com/objc/">AppCode</a> is the real deal.  And yes, Xcode is still an ugly wart on iOS development.</p>
]]></content:encoded>
			<wfw:commentRss>http://saturnboy.com/2011/03/objective-c-good-bad-ugly/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Nibless iOS App</title>
		<link>http://saturnboy.com/2011/03/nibless-ios-app/</link>
		<comments>http://saturnboy.com/2011/03/nibless-ios-app/#comments</comments>
		<pubDate>Wed, 09 Mar 2011 05:06:08 +0000</pubDate>
		<dc:creator>justin</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[Xcode]]></category>

		<guid isPermaLink="false">http://saturnboy.com/?p=1758</guid>
		<description><![CDATA[I always believe in the right tool for the job, so I&#8217;m not an Interface Builder hater per se, but I find it a little pathetic how infrequently IB is the right tool for the job. In addition, I&#8217;ve found IB to be at its worst whenever the development team is bigger than one. Don&#8217;t [...]]]></description>
			<content:encoded><![CDATA[<p>I always believe in the right tool for the job, so I&#8217;m not an Interface Builder hater per se, but I find it a little pathetic how infrequently IB is the right tool for the job.  In addition, I&#8217;ve found IB to be at its worst whenever the development team is bigger than one.  Don&#8217;t panic, just go nibless.</p>
<p>It&#8217;s very easy to go <i>completely</i> nibless (and a little stylish in my opinion) and eschew Interface Builder altogether.  For all you Flexers out there, this is the moral equivalent of eschewing Flash Builder&#8217;s Design View.  Honestly, who really uses Design View to build an app beside Adobe evangelists doing 30 minute demos?</p>
<h3>A Completely Nibless App</h3>
<p class="bottom">Here&#8217;s how you can build a completely nibless app in five simple steps.  First, you&#8217;ll want to start with a fresh <i>Window-based Application</i> in Xcode.  Then follow these steps:</p>
<ol>
<li>Remove <code>MainWindow</code> from your app&#8217;s <code>Info.plist</code></li>
<li>Delete all <code>.xib</code> files</li>
<li>Edit <code>AppDelegate.h</code> remove all the <code>IBOutlet</code> keywords</li>
<li>Edit <code>main.m</code>, import <code>AppDelegate.h</code>, and set the <code>UIApplicationMain</code>&#8216;s 4<sup>th</sup> parameter to <code>@"AppDelegate"</code></li>
<li>Edit <code>AppDelegate.m</code> to instantiate the main <code>UIWindow</code> in code</li>
</ol>
<p class="bottom">Here&#8217;s what <code>main.m</code> should look like after you&#8217;ve completed Step 4:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &lt;UIKit/UIKit.h&gt;</span>
<span style="color: #6e371a;">#import &quot;AppDelegate.h&quot;</span>
&nbsp;
<span style="color: #a61390;">int</span> main<span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> argc, <span style="color: #a61390;">char</span> <span style="color: #002200;">*</span>argv<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
    <span style="color: #400080;">NSAutoreleasePool</span> <span style="color: #002200;">*</span>pool <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSAutoreleasePool</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">int</span> retVal <span style="color: #002200;">=</span> UIApplicationMain<span style="color: #002200;">&#40;</span>argc, argv, <span style="color: #a61390;">nil</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;AppDelegate&quot;</span><span style="color: #002200;">&#41;</span>;
    <span style="color: #002200;">&#91;</span>pool release<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">return</span> retVal;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p class="bottom">And here&#8217;s what the <code>application:didFinishLaunchingWithOptions:</code> method in <code>AppDelegate.m</code> should look like:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>application<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIApplication <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>application didFinishLaunchingWithOptions<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>launchOptions <span style="color: #002200;">&#123;</span>
    self.window <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIWindow alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIScreen mainScreen<span style="color: #002200;">&#93;</span> applicationFrame<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>self.window makeKeyAndVisible<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">return</span> <span style="color: #a61390;">YES</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Now you can go about you view-creation business in a nibless fashion.  I&#8217;d probably create a <code>UIViewController</code> subclass as my root view, construct my UI in <code>loadView</code>, and wire everything together in the <code>AppDelegate</code>.</p>
<h3>Nibless App Xcode Template</h3>
<p>But don&#8217;t do any of that stuff!  Instead you can just use this handy-dandy Xcode template to do all the work.  On of the very few things Xcode gets right is its template system.  It is very easy to create a custom template for Xcode (see <a href="http://blog.carbonfive.com/2009/05/19/custom-xcode-template-for-iphone-development/">here</a> and <a href="http://www.sunetos.com/items/2011/01/21/creating-an-xcode-project-template-with-ghunit-and-ocmock/">here</a> for the basics).</p>
<p class="bottom">Install the <code>NiblessApp</code> custom template in Xcode:</p>
<ol>
<li>Download <a href="http://saturnboy.com/proj/ios/nibless_app_template.tgz">nibless_app_template.tgz</a></li>
<li><code>cd ~/Library/Application Support/Developer/Shared/Xcode/Project Templates/</code> (you might need to create the <code>Project Templates</code> folder if it doesn&#8217;t already exist)</li>
<li><code>tar xzvf /path/to/nibless_app_template.tgz</code></li>
<li>Quit Xcode and restart</li>
</ol>
<h5>Files</h5>
<ul>
<li><a href="http://saturnboy.com/proj/ios/nibless_app_template.tgz">nibless_app_template.tgz</a> &#8211; a completely nibless iPhone custom template for Xcode</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://saturnboy.com/2011/03/nibless-ios-app/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Stack and Queue extensions to NSMutableArray</title>
		<link>http://saturnboy.com/2011/02/stack-queue-nsmutablearray/</link>
		<comments>http://saturnboy.com/2011/02/stack-queue-nsmutablearray/#comments</comments>
		<pubDate>Tue, 22 Feb 2011 04:20:27 +0000</pubDate>
		<dc:creator>justin</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[Objective-C]]></category>

		<guid isPermaLink="false">http://saturnboy.com/?p=1745</guid>
		<description><![CDATA[This is all out there on the web, but I&#8217;m putting everything here all in one place so as not to forget. Just a simple Stack and Queue implementation as a category on NSMutableArray. It works and it doesn&#8217;t suck, and that&#8217;s all I&#8217;ll say about it. Stack A very basic Stack implementation with just [...]]]></description>
			<content:encoded><![CDATA[<p>This is all out there on the web, but I&#8217;m putting everything here all in one place so as not to forget.  Just a simple Stack and Queue implementation as a category on <code>NSMutableArray</code>.  It works and it doesn&#8217;t suck, and that&#8217;s all I&#8217;ll say about it.</p>
<h3>Stack</h3>
<p class="bottom">A very basic Stack implementation with just <code>push</code>, <code>pop</code>, and <code>peek</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> <span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">&#40;</span>Stack<span style="color: #002200;">&#41;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> push<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>item;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> pop;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> peek;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@implementation</span> <span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">&#40;</span>Stack<span style="color: #002200;">&#41;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> push<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>item <span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>self addObject<span style="color: #002200;">:</span>item<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> pop <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">id</span> item <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>self count<span style="color: #002200;">&#93;</span> <span style="color: #002200;">!=</span> <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        item <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self lastObject<span style="color: #002200;">&#93;</span> retain<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#91;</span>self removeLastObject<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
    <span style="color: #a61390;">return</span> item;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> peek <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">id</span> item <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>self count<span style="color: #002200;">&#93;</span> <span style="color: #002200;">!=</span> <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        item <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self lastObject<span style="color: #002200;">&#93;</span> retain<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
    <span style="color: #a61390;">return</span> item;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>No real magic above, just Objective-C&#8217;s annoying <code>retain</code> &#038; <code>autorelease</code> dance to get an item out of <code>NSMutableArray</code> correctly.</p>
<h3>Queue</h3>
<p class="bottom">Much like Stack above, here&#8217;s a very basic Queue implementation with just <code>enqueue</code>, <code>dequeue</code>, and <code>peek</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> <span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">&#40;</span>Queue<span style="color: #002200;">&#41;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> enqueue<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>item;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> dequeue;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> peek;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@implementation</span> <span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">&#40;</span>Queue<span style="color: #002200;">&#41;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> enqueue<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>item <span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>self addObject<span style="color: #002200;">:</span>item<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> dequeue <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">id</span> item <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>self count<span style="color: #002200;">&#93;</span> <span style="color: #002200;">!=</span> <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        item <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span> retain<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#91;</span>self removeObjectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
    <span style="color: #a61390;">return</span> item;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> peek <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">id</span> item <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>self count<span style="color: #002200;">&#93;</span> <span style="color: #002200;">!=</span> <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        item <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span> retain<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
    <span style="color: #a61390;">return</span> item;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>Note that we need the same <code>retain</code> &#038; <code>autorelease</code> dance as above.</p>
<h5>Files</h5>
<ul>
<li><a href="http://saturnboy.com/proj/ios/stack.tgz">stack.tgz</a></li>
<li><a href="http://saturnboy.com/proj/ios/queue.tgz">queue.tgz</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://saturnboy.com/2011/02/stack-queue-nsmutablearray/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Drag-and-Drop Revisited</title>
		<link>http://saturnboy.com/2010/12/drag-and-drop-revisited/</link>
		<comments>http://saturnboy.com/2010/12/drag-and-drop-revisited/#comments</comments>
		<pubDate>Wed, 15 Dec 2010 04:02:54 +0000</pubDate>
		<dc:creator>justin</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[drag-and-drop]]></category>
		<category><![CDATA[flex4]]></category>
		<category><![CDATA[skinning]]></category>
		<category><![CDATA[ux]]></category>

		<guid isPermaLink="false">http://saturnboy.com/?p=1668</guid>
		<description><![CDATA[It&#8217;s time to revisit drag-and-drop in Flex 4. For a long time now, my previous Drag-and-Drop in Flex 4 post has been the most popular post on this blog. I get something like 1,200 pageview/month on that one post. This time around, I&#8217;ll try to expand on the thinking that goes on before I write [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s time to revisit drag-and-drop in Flex 4.  For a long time now, my previous <a href="http://saturnboy.com/2009/08/drag-and-drop-flex-4/">Drag-and-Drop in Flex 4</a> post has been the most popular post on this blog.  I get something like 1,200 pageview/month on that one post.  This time around, I&#8217;ll try to expand on the thinking that goes on <i>before</i> I write the code to add drag-and-drop functionality to an app.</p>
<h3>UX 101</h3>
<p>It is impossible to develop an RIA without some basic understanding of interaction design.  As they say in one of my favorite movies, &quot;Many men have tried.  They tried and failed?  They tried and died.&quot;  So a short UX primer on drag-and-drop goes like this&#8230;</p>
<p>Users are typically familiar with drag-and-drop for two basic actions: moving-stuff-around and putting-stuff-into.  For example, we move windows around the desktop or move songs around in a playlist or move icons around in a folder.  Also, we put stuff into the trash or put a picture into an album or put an attachment into an email.  At a fundmental level, users have an understanding of these actions because they mimic the physical world.  Performing these actions via a touch interface (rather than a mouse) is even better, but that&#8217;s a story for another day.</p>
<p class="bottom">Given these two basic actions, I&#8217;m going to design a sample app that exercises them both.  This is what I came up with:</p>
<div class="span-14 last">
<img src="http://saturnboy.com/proj/flex4/drag_drop/colors/diagrams/drag-drop-diagram1.png" alt="diagram 1" title="diagram 1" width="490" height="327" />
</div>
<p>Our sample app has three drag actions.  In #1, you can drag to put items from the palette into the list.  In #2a and #2b, you can drag items from the list into a target box (that preforms some action).  In #3, you can drag items around inside the list to reorder them.  I&#8217;ll break them down one by one.</p>
<h3>#1 &mdash; Drag List Item to Another List</h3>
<p class="bottom">Dragging from one list and dropping on another is super easy in Flex 4.  Here&#8217;s a snippet:</p>

<div class="wp_syntax"><div class="code"><pre class="mxml" style="font-family:monospace;"><span style="color: #000000;"><span style="color: #7400FF;">&lt;s:List</span> id=<span style="color: #ff0000;">&quot;sourceList&quot;</span> dragEnabled=<span style="color: #ff0000;">&quot;true&quot;</span><span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:ArrayList</span> source=<span style="color: #ff0000;">&quot;['A','B','C','D']&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:List</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:List</span> id=<span style="color: #ff0000;">&quot;targetList&quot;</span> dropEnabled=<span style="color: #ff0000;">&quot;true&quot;</span><span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:ArrayList</span><span style="color: #7400FF;">&gt;</span><span style="color: #7400FF;">&lt;/s:ArrayList</span><span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:List</span><span style="color: #7400FF;">&gt;</span></span></pre></div></div>

<p>We set <code>dragEnable=true</code> on the source list and <code>dropEnable=true</code> on the target list.  For bi-directional drag-and-drop, just set both attributes true for both lists.  In a feat of awesomeness, Flex 4 does everything else for you.  You don&#8217;t need to listen for any events or write any handlers.  And you even get some nice eye candy for free, because the target list glows when you drag an item over it.</p>
<blockquote style="color:#333"><p><b>Trick 1</b> &mdash; When the drop target is an empty list, you <i>must</i> given it an empty <code>dataProvider</code>.  In the above example, I just use an empty <code>ArrayList</code> pair of tags in MXML, but I could just as easily say <code>dataProvider=&quot;{new ArrayList()}&quot;</code>, or even do everything in Actionscript.</p></blockquote>
<blockquote style="color:#333"><p><b>Trick 2</b> &mdash; The source list accepts a drop&mdash;if and only if&mdash;the item being dropped is compatible with the items in the target <code>dataProvider</code>.  For example, you can <i>not</i> drop a <code>Foo</code> object on a list of <code>Bar</code> objects.  Setting the target list to have an empty <code>dataProvider</code> (à la Trick 1), accepts anything.</p></blockquote>
<h3>#2 &mdash; Drag List Item to <code>UIComponent</code> Target</h3>
<p class="bottom">Again, to use a list as the drag source, we just set <code>dragEnable=true</code>.  But this time the target is some non-<code>List</code> <code>UIComponent</code>, so we need to do what we <a href="http://saturnboy.com/2009/08/drag-and-drop-flex-4/">always do</a> to enable drop functionality on a component&mdash;namely, attach handlers to the <code>dragEnter</code> and <code>dragDrop</code> events.  Here&#8217;s a snippet:</p>

<div class="wp_syntax"><div class="code"><pre class="mxml" style="font-family:monospace;"><span style="color: #000000;"><span style="color: #7400FF;">&lt;s:List</span> id=<span style="color: #ff0000;">&quot;sourceList&quot;</span> dragEnabled=<span style="color: #ff0000;">&quot;true&quot;</span><span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:ArrayList</span> source=<span style="color: #ff0000;">&quot;['A','B','C','D']&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:List</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Panel</span> id=<span style="color: #ff0000;">&quot;targetA&quot;</span> </span>
<span style="color: #000000;">         dragEnter=<span style="color: #ff0000;">&quot;dragEnterHandler(event)&quot;</span></span>
<span style="color: #000000;">         dragDrop=<span style="color: #ff0000;">&quot;dragDropHandler(event)&quot;</span><span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:Panel</span><span style="color: #7400FF;">&gt;</span></span></pre></div></div>

<p class="bottom">And here are the handlers:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> dragEnterHandler<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:DragEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
    DragManager.<span style="color: #006600;">acceptDragDrop</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #006600;">currentTarget</span> as IUIComponent<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> dragDropHandler<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:DragEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #808080; font-style: italic;">//do something with dropped item here</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>As always, we <i>accept</i> the drop in the <code>dragEnter</code> handler via the static <code>acceptDragDrop()</code> method on the <code>DragManager</code>.  And once the object is dropped, we can do whatever we want with it in the <code>dragDrop</code> handler.</p>
<h3>#3 &mdash; Drag List Item to Reorder Items</h3>
<p class="bottom">Finally, we want to be able to reorder the items in a list.  Again, the spark <code>List</code> does all the work for us.  We just set both <code>dragEnable=true</code> and <code>dropEnable=true</code> on the list we want to reorder.  We must also set the magical <code>dragMoveEnabled=true</code>, which has numerous ramifications. Here is the snippet:</p>

<div class="wp_syntax"><div class="code"><pre class="mxml" style="font-family:monospace;"><span style="color: #000000;"><span style="color: #7400FF;">&lt;s:List</span> id=<span style="color: #ff0000;">&quot;reorderList&quot;</span></span>
<span style="color: #000000;">        dragEnabled=<span style="color: #ff0000;">&quot;true&quot;</span></span>
<span style="color: #000000;">        dropEnabled=<span style="color: #ff0000;">&quot;true&quot;</span></span>
<span style="color: #000000;">        dragMoveEnabled=<span style="color: #ff0000;">&quot;true&quot;</span><span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:ArrayList</span> source=<span style="color: #ff0000;">&quot;['A','B','C','D']&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:List</span><span style="color: #7400FF;">&gt;</span></span></pre></div></div>

<p>The code is trivial, but what&#8217;s going on with <code>dragMoveEnabled=true</code>?  Well, <code>dragMoveEnable</code>, which defaults to <code>false</code>, controls whether or not an object is <i>moved</i> (aka removed from the source and added to the target) or <i>copied</i> (aka added to the target leaving the source untouched).  So to reorder the items in a list, we need the <i>move</i> behavior, thus we set <code>dragMoveEnabled=true</code>.</p>
<blockquote style="color:#333"><p><b>Trick 3</b> &mdash; If the source list is <code>dragMoveEnabled=true</code> and the target is a non-<code>List</code> <code>UIComponent</code>, you will see some seemingly <i>odd</i> behavior.  Every time to drop an object on your component, it will be deleted from the source list.  This is because you are only seeing the first half of the <i>move</i> (the item is removed from the source), and not the second half (the item is added to the target) because the target is not a <code>List</code>.</p></blockquote>
<blockquote style="color:#333"><p><b>Trick 4</b> &mdash; As an alternative to <code>dragMoveEnabled=true</code>, you can make a call to  <code>DragManager.showFeedback(DragManager.MOVE)</code> in the <code>dragEnter</code> handler to control whether or not an object is <i>moved</i> or <i>copied</i>.  Do <i>NOT</i> use both <code>showFeedback</code> and <code>dragMoveEnabled</code> at the same time.</p></blockquote>
<h3>Eye candy</h3>
<p>What&#8217;s a Flex app without eye candy?  But with drag-and-drop, I&#8217;d say most of the eye candy is more of a UX necessity than anything else.  It&#8217;s used to indicate potential drop targets, or highlight a target that is ready to receive a drop, etc.</p>
<h5>Glow On Enter</h5>
<p class="bottom">With list to list drag-and-drop, we get a glow on the target list for free, which tells the user that the target list is ready to receive the drop.  But when dragging to a non-<code>List</code> <code>UIComponent</code>, we need to add the glow ourselves:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> dragEnterHandler<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:DragEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
    myGlow.<span style="color: #006600;">alpha</span> = <span style="color: #cc66cc;">1</span>;
    DragManager.<span style="color: #006600;">acceptDragDrop</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #006600;">currentTarget</span> as IUIComponent<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> dragExitHandler<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:DragEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
    myGlow.<span style="color: #006600;">alpha</span> = <span style="color: #cc66cc;">0</span>;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> dragDropHandler<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:DragEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
    myGlow.<span style="color: #006600;">alpha</span> = <span style="color: #cc66cc;">0</span>;
    <span style="color: #808080; font-style: italic;">//do something with dropped item here</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p class="bottom">In the <code>dragEnter</code> handler, we turn the glow on, and we turn the glow off on either a <code>dragExit</code> or <code>dragDrop</code> event.  And we need to add a simple <code>GlowFilter</code> to the target component:</p>

<div class="wp_syntax"><div class="code"><pre class="mxml" style="font-family:monospace;"><span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Panel</span> id=<span style="color: #ff0000;">&quot;targetA&quot;</span> </span>
<span style="color: #000000;">         dragEnter=<span style="color: #ff0000;">&quot;dragEnterHandler(event)&quot;</span></span>
<span style="color: #000000;">         dragDrop=<span style="color: #ff0000;">&quot;dragDropHandler(event)&quot;</span><span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:filters</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:GlowFilter</span> id=<span style="color: #ff0000;">&quot;myGlow&quot;</span> blurX=<span style="color: #ff0000;">&quot;16&quot;</span> blurY=<span style="color: #ff0000;">&quot;16&quot;</span></span>
<span style="color: #000000;">                color=<span style="color: #ff0000;">&quot;#3399ff&quot;</span> alpha=<span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:filters</span><span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:Panel</span><span style="color: #7400FF;">&gt;</span></span></pre></div></div>

<p>No magic, just use MXML to add a <code>GlowFilter</code> to the target component, which in this snippet is a spark <code>Panel</code> component.</p>
<h5>Custom Drag Proxy</h5>
<p>Customizing the drag proxy (what an item looks like while dragging) is fun, but <a href="http://saturnboy.com/2009/08/drag-and-drop-flex-4/">I covered that previously</a>, so there&#8217;s no need to revisit it here.  On the other hand, I haven&#8217;t discussed how to customizing an item dragged from a spark <code>List</code> yet.  It is not only fun, but also quite a bit easier.</p>
<p class="bottom">Flex 4 makes is super easy to create a custom drag proxy by simply adding a magic <code>dragging</code> state to the <code>List</code>&#8216;s <code>itemRenderer</code>.  For this snippet, imagine I have a trivial <code>ItemRenderer</code> that just shows a centered label with black text, but when it&#8217;s being dragged I want to turn the text red:</p>

<div class="wp_syntax"><div class="code"><pre class="mxml" style="font-family:monospace;"><span style="color: #000000;">&lt;?xml version=<span style="color: #ff0000;">&quot;1.0&quot;</span> encoding=<span style="color: #ff0000;">&quot;utf-8&quot;</span>?<span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:ItemRenderer</span> ...<span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:states</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:State</span> name=<span style="color: #ff0000;">&quot;normal&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:State</span> name=<span style="color: #ff0000;">&quot;hovered&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:State</span> name=<span style="color: #ff0000;">&quot;selected&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:State</span> name=<span style="color: #ff0000;">&quot;dragging&quot;</span><span style="color: #7400FF;">/&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:states</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Label</span> left=<span style="color: #ff0000;">&quot;0&quot;</span> right=<span style="color: #ff0000;">&quot;0&quot;</span> top=<span style="color: #ff0000;">&quot;0&quot;</span> bottom=<span style="color: #ff0000;">&quot;0&quot;</span></span>
<span style="color: #000000;">            text=<span style="color: #ff0000;">&quot;{data.label}&quot;</span> textAlign=<span style="color: #ff0000;">&quot;center&quot;</span> verticalAlign=<span style="color: #ff0000;">&quot;middle&quot;</span></span>
<span style="color: #000000;">            color=<span style="color: #ff0000;">&quot;#000000&quot;</span></span>
<span style="color: #000000;">            color.hovered=<span style="color: #ff0000;">&quot;#0000ff&quot;</span></span>
<span style="color: #000000;">            color.dragging=<span style="color: #ff0000;">&quot;#ff0000&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:ItemRenderer</span><span style="color: #7400FF;">&gt;</span></span></pre></div></div>

<p>As you can see from the above code, customizing the drag indicator is a <i>huge</i> nothing.  Just add the magic <code>dragging</code> state to the list of states, and use Flex 4&#8242;s inline state syntax to do whatever you want.  In this snippet, we set <code>color.dragging=&quot;#ff0000&quot;</code> to turn the text red when its being dragged.  The sample app below does quite a few more things to customize the dragged item, so check out that code too.</p>
<h5>Custom Drop Indicator</h5>
<p>When a user drags an item over a target spark <code>List</code>, a line appears to show the user where the item will be inserted.  This line is known as the <i>drop indicator</i>.  To change things up, we will make a custom drop indicator that is a dashed line.</p>
<blockquote style="color:#333"><p><b>Trick 5</b> &mdash; Alas, Flex 4 does not have direct support for dashed strokes, so we need to fake it.  The easy way to make a dashed line in Flex 4 is to use <code>BitmapFill</code>.  First, we create an image that is a tileable pattern of squares.  Then, we make a <code>Rect</code> and set its fill to <code>BitmapFill</code> and <code>fillMode=&quot;repeat&quot;</code>.</p></blockquote>
<p class="bottom">The <code>dropIndicator</code> component is found in the <code>fx:Declarations</code> block of the default spark <code>ListSkin</code>, so we must create a custom <code>List</code> skin with our custom <code>dropIndicator</code> component.  Here&#8217;s a snippet from our custom skin&#8217;s <code>fx:Declarations</code> block:</p>

<div class="wp_syntax"><div class="code"><pre class="mxml" style="font-family:monospace;"><span style="color: #000000;"><span style="color: #7400FF;">&lt;fx:Declarations</span><span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;fx:Component</span> id=<span style="color: #ff0000;">&quot;dropIndicator&quot;</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Group</span> minWidth=<span style="color: #ff0000;">&quot;1&quot;</span> minHeight=<span style="color: #ff0000;">&quot;1&quot;</span> maxWidth=<span style="color: #ff0000;">&quot;1&quot;</span> maxHeight=<span style="color: #ff0000;">&quot;1&quot;</span><span style="color: #7400FF;">&gt;</span></span>
            <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Rect</span> left=<span style="color: #ff0000;">&quot;0&quot;</span> right=<span style="color: #ff0000;">&quot;0&quot;</span> top=<span style="color: #ff0000;">&quot;0&quot;</span> bottom=<span style="color: #ff0000;">&quot;0&quot;</span><span style="color: #7400FF;">&gt;</span></span>
                <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:fill</span><span style="color: #7400FF;">&gt;</span></span>
                    <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:BitmapFill</span> source=<span style="color: #ff0000;">&quot;@Embed('dash.png')&quot;</span></span>
<span style="color: #000000;">                            fillMode=<span style="color: #ff0000;">&quot;repeat&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
                <span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:fill</span><span style="color: #7400FF;">&gt;</span></span>
            <span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:Rect</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:Group</span><span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/fx:Component</span><span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/fx:Declarations</span><span style="color: #7400FF;">&gt;</span></span></pre></div></div>

<p>As described above, we have a <code>Rect</code> with a <code>BitmapFill</code>, and an image <code>dash.png</code> that is a tileable pattern of squares.  But the real magic is in the <code>Group</code> wrapper, by setting all of <code>minWidth</code>, <code>maxWidth</code>, <code>minHeight</code>, <code>maxHeight</code> to 1px we ensure we get a one pixel line as our drop indicator independent of the layout applied to our <code>List</code>.  This is because <code>HorizontalLayout</code> ignore min and max height, and <code>VerticalLayout</code> ignore min and max width.  So if we take a 1px view of our pattern of squares, we see dashes.</p>
<h3>Conclusion</h3>
<p class="bottom">Here&#8217;s the complete drag-and-drop sample (view source enabled):</p>
<div id="flashcontent-drag-drop-colors">
Flash is required.  <a href="http://www.adobe.com/go/getflashplayer">Get it here!</a>
</div>
<p>Hopefully, I covered the full spectrum of drag-and-drop functionality that the average Flex app (or even above average) is ever likely to need.  If you want more, check out the links below, or just leave me a comment.</p>
<h5>Useful Links</h5>
<ul>
<li>My first post on <a href="http://saturnboy.com/2009/08/drag-and-drop-flex-4/">drag-and-drop in Flex 4</a> covered a lot of drag proxy stuff.</li>
<li>Flex After Dark has a solid <a href="http://www.flexafterdark.com/tutorials/Flex-Drag-and-Drop">drag-and-drop tutorial</a> that is definitely worth reading if you are implementing drag-and-drop functionality in your app.</li>
<li>Evtim, aka The Godfather, as I like to call him, has a <a href="http://evtimmy.com/2010/01/drag-and-drop-skinning-in-spark/">cool post</a> on customizing both the drag indicator and the drop indicator of the spark <code>List</code> component.</li>
</ul>
<h5>Files</h5>
<ul>
<li><a href="http://saturnboy.com/proj/flex4/drag_drop/colors/DragDropColors.html">DragDropColors</a> (<a href="http://saturnboy.com/proj/flex4/drag_drop/colors/srcview/DragDropColors.zip">download</a>)</li>
</ul>
<div>
<script type="text/javascript">
swfobject.embedSWF('http://saturnboy.com/proj/flex4/drag_drop/colors/DragDropColors.swf', 'flashcontent-drag-drop-colors', '510', '380', '10.1.0', 'playerProductInstall.swf', false, { bgColor:'#f8f8f8', base:'.' });
</script>
</div>
]]></content:encoded>
			<wfw:commentRss>http://saturnboy.com/2010/12/drag-and-drop-revisited/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Testing Private Methods in Java</title>
		<link>http://saturnboy.com/2010/11/testing-private-methods-in-java/</link>
		<comments>http://saturnboy.com/2010/11/testing-private-methods-in-java/#comments</comments>
		<pubDate>Fri, 19 Nov 2010 03:09:28 +0000</pubDate>
		<dc:creator>justin</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://saturnboy.com/?p=1648</guid>
		<description><![CDATA[I&#8217;ve been writing a lot of Java lately, and a lot of tests. We always write tests, right? Alas, no cool record-and-playback stuff like FlexMonkey or FoneMonkey, just plain old JUnit 4 tests with plently of Hamcrest goodness. Suddenly, I realized that I really needed to test some private methods. So, a quick google for [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been writing a lot of Java lately, and a lot of tests.  We <i>always</i> write tests, right?  Alas, no cool record-and-playback stuff like <a href="http://www.gorillalogic.com/flexmonkey">FlexMonkey</a> or <a href="http://www.gorillalogic.com/fonemonkey">FoneMonkey</a>, just plain old <a href="http://www.junit.org/">JUnit 4</a> tests with plently of <a href="http://code.google.com/p/hamcrest/">Hamcrest</a> goodness.</p>
<p class="bottom">Suddenly, I realized that I really needed to test some private methods.  So, a quick google for &#8220;testing private methods java&#8221; brings up a <a href="http://www.artima.com/suiterunner/privateP.html">good article</a> by Bill Venners.  He lists all possible options to test private methods:</p>
<ol>
<li>Don&#8217;t test private methods</li>
<li>Give the methods package access</li>
<li>Use a nested test class</li>
<li>Use reflection</li>
</ol>
<p>Basically, the only real one is #4, use reflection.  Bill didn&#8217;t give me the exact code I needed, so lots of googling later I realized that the world is filled with opinionated people (like me), and boy do they love to talk about #1.  I just wanted some code, not a lecture, so I had to write my own code.  Here is that code for anyone else that just wants to test private methods.</p>
<p class="bottom">Imagine you have a class <code>MyClass</code> and it has a private method, <code>myMethod()</code>.  Sorta like this:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyClass <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> myMethod<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> s<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> s<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p class="bottom">Then you could use reflection to invoke the method like this:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">MyClass myClass <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">Method</span> method <span style="color: #339933;">=</span> MyClass.<span style="color: #000000; font-weight: bold;">class</span>.<span style="color: #006633;">getDeclaredMethod</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;myMethod&quot;</span>, <span style="color: #003399;">String</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
method.<span style="color: #006633;">setAccessible</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">String</span> output <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#41;</span> method.<span style="color: #006633;">invoke</span><span style="color: #009900;">&#40;</span>myClass, <span style="color: #0000ff;">&quot;some input&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The real magic is <code>setAccessible(true)</code> which allows the private method to be called outside the class.  And shazam, I can now test all my private methods.  I was really hoping JUnit 4 would provide some additional facilities specifically for testing private methods, but not luck.</p>
<h3>A Better Example</h3>
<p class="bottom">Here&#8217;s a more complete example.  Suppose we have a <code>NovelWriter</code> class that in a feat of API cleanliness only exposes the <code>writeNovel()</code> method.  It happens to have a few private utility methods that we&#8217;d like to test:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> NovelWriter <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> writeNovel<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">//...the magic goes here...</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> shout<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> s<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> s.<span style="color: #006633;">toUpperCase</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">replaceAll</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\\</span>.&quot;</span>, <span style="color: #0000ff;">&quot;!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> List<span style="color: #339933;">&lt;</span>Integer<span style="color: #339933;">&gt;</span> countLetters<span style="color: #009900;">&#40;</span>List<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> words<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        List<span style="color: #339933;">&lt;</span>Integer<span style="color: #339933;">&gt;</span> out <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>Integer<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> word <span style="color: #339933;">:</span> words<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            out.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span> word.<span style="color: #006633;">replaceAll</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;[^A-Za-z]+&quot;</span>,<span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">length</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">return</span> out<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>I won&#8217;t get into all the details, but it seems easy to imagine a clean API that has private helper methods.  Furthermore, it seems very logical to me to want to bring all methods, both public and private, so I can be sure they are being exercised to the fullest.</p>
<p class="bottom">Our JUnit 4 + Hamcrest test class:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> NovelWriterTest <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> NovelWriter novelWriter<span style="color: #339933;">;</span>
&nbsp;
    @BeforeClass
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> beforeClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        novelWriter <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> NovelWriter<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Test
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> privateShout<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">NoSuchMethodException</span>,
            <span style="color: #003399;">InvocationTargetException</span>, <span style="color: #003399;">IllegalAccessException</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #003399;">String</span> input <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;This is magic.&quot;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003399;">Method</span> method <span style="color: #339933;">=</span> NovelWriter.<span style="color: #000000; font-weight: bold;">class</span>.<span style="color: #006633;">getDeclaredMethod</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;shout&quot;</span>, <span style="color: #003399;">String</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        method.<span style="color: #006633;">setAccessible</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">String</span> output <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#41;</span> method.<span style="color: #006633;">invoke</span><span style="color: #009900;">&#40;</span>novelWriter, input<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        assertThat<span style="color: #009900;">&#40;</span>output, notNullValue<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        assertThat<span style="color: #009900;">&#40;</span>output, is<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;THIS IS MAGIC!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Test
    @SuppressWarnings<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;unchecked&quot;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> privateCountLetters<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">NoSuchMethodException</span>,
            <span style="color: #003399;">InvocationTargetException</span>, <span style="color: #003399;">IllegalAccessException</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        List<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> input <span style="color: #339933;">=</span> <span style="color: #003399;">Arrays</span>.<span style="color: #006633;">asList</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Foo&quot;</span>, <span style="color: #0000ff;">&quot;Foobar123&quot;</span>, <span style="color: #0000ff;">&quot;Foo Bar Baz&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003399;">Method</span> method <span style="color: #339933;">=</span> NovelWriter.<span style="color: #000000; font-weight: bold;">class</span>.<span style="color: #006633;">getDeclaredMethod</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;countLetters&quot;</span>, <span style="color: #003399;">List</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        method.<span style="color: #006633;">setAccessible</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        List<span style="color: #339933;">&lt;</span>Integer<span style="color: #339933;">&gt;</span> output <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>List<span style="color: #339933;">&lt;</span>Integer<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#41;</span> method.<span style="color: #006633;">invoke</span><span style="color: #009900;">&#40;</span>novelWriter, input<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        assertThat<span style="color: #009900;">&#40;</span>output, notNullValue<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        assertThat<span style="color: #009900;">&#40;</span>output.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, is<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        assertThat<span style="color: #009900;">&#40;</span>output, hasItems<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">6</span>, <span style="color: #cc66cc;">9</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The nice thing about using the Reflection API like this is that it really doesn&#8217;t get too messy.  I&#8217;m not inspecting anything at runtime, because I know exactly the return type and the types of all the parameters.  I&#8217;m just invoking the method with known inputs, followed by a simple cast on the output type.  And as you can see in the second test above, <code>privateCountLetters()</code>, it&#8217;s not a problem to use generics because we&#8217;re not doing any inspection only invocation.</p>
<p>Happy testing.</p>
<h5>Files</h5>
<ul>
<li><a href="http://saturnboy.com/proj/java/novel.tgz">novel.tgz</a> &ndash; download the complete Eclipse project</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://saturnboy.com/2010/11/testing-private-methods-in-java/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
