<?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>Moonshine Project Dev Blog</title>
	<atom:link href="http://blog.moonshine-project.com/en/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.moonshine-project.com/en</link>
	<description>iPhone app dev whatnots</description>
	<lastBuildDate>Sun, 25 Apr 2010 17:10:24 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Exploring NSDateFormatter using F-Script as a REPL</title>
		<link>http://blog.moonshine-project.com/en/2010/04/26/exploring-nsdateformatter-using-f-script-as-a-repl/</link>
		<comments>http://blog.moonshine-project.com/en/2010/04/26/exploring-nsdateformatter-using-f-script-as-a-repl/#comments</comments>
		<pubDate>Sun, 25 Apr 2010 16:33:01 +0000</pubDate>
		<dc:creator>ento</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.moonshine-project.com/en/?p=44</guid>
		<description><![CDATA[(here be preface)
Let&#8217;s start by instantiating a formatter and a date.

&#62; fmt := NSDateFormatter new.
&#160;
&#62; fmt
&#60;NSDateFormatter: 0x2004cec80&#62;
&#160;
&#62; now := NSDate date.
&#160;
&#62; now
2010-04-25 18:03:26 +0900

Note that assignment is :=, not =, and no angled brackets around method calls. Also, a sentence ends in a period, not a semicolon.
OK, what does the virgin formatter output when we [...]]]></description>
			<content:encoded><![CDATA[<p>(here be preface)</p>
<p>Let&#8217;s start by instantiating a formatter and a date.</p>

<div class="wp_syntax"><div class="code"><pre class="smalltalk" style="font-family:monospace;">&gt; <span style="color: #00007f;">fmt</span> <span style="color: #000066; font-weight:bold;">:=</span> <span style="color: #0000ff;">NSDateFormatter</span> new.
&nbsp;
&gt; fmt
&lt;<span style="color: #0000ff;">NSDateFormatter</span>: <span style="color: #00007f;">0x2004cec80</span>&gt;
&nbsp;
&gt; <span style="color: #00007f;">now</span> <span style="color: #000066; font-weight:bold;">:=</span> <span style="color: #0000ff;">NSDate</span> date.
&nbsp;
&gt; now
<span style="color: #7f0000;">2010</span>-<span style="color: #00007f;">04</span>-<span style="color: #00007f;">25</span> <span style="color: #7f0000;">18</span>:<span style="color: #00007f;">03</span>:<span style="color: #00007f;">26</span> +<span style="color: #00007f;">0900</span></pre></div></div>

<p>Note that assignment is :=, not =, and no angled brackets around method calls. Also, a sentence ends in a period, not a semicolon.</p>
<p>OK, what does the virgin formatter output when we feed it a date?</p>

<div class="wp_syntax"><div class="code"><pre class="smtalltalk" style="font-family:monospace;">&gt; fmt stringFromDate:now
''</pre></div></div>

<p>Hmm, after consulting the Xcode Developer Documentation, it seems that we need to set a format string to get a non-empty result. There are also shortcut methods provided to easily set the formats for the date part and the time part.</p>
<p>The argument to these methods is a constant, which we have easy access to in F-Script.</p>

<div class="wp_syntax"><div class="code"><pre class="smtalltalk" style="font-family:monospace;">&gt; NSDateFormatterNoStyle
0
&nbsp;
&gt; fmt setDateStyle:NSDateFormatterMediumStyle
&nbsp;
&gt; fmt dateFormat
'MMM d, yyyy'
&nbsp;
&gt; fmt stringFromDate:now
'Apr 25, 2010'</pre></div></div>

<p>There, the date. Let&#8217;s try setting the time part format.</p>

<div class="wp_syntax"><div class="code"><pre class="smtalltalk" style="font-family:monospace;">&gt; fmt setTimeStyle:NSDateFormatterMediumStyle
&nbsp;
&gt; fmt dateFormat
'MMM d, yyyy h:mm:ss a'
&nbsp;
&gt; fmt stringFromDate:now
'Apr 25, 2010 6:03:26 PM'</pre></div></div>

<p>With #setDateFormat:, we have more control over the formatting. You can even include arbitrary string. (Which I found out after reading the <a href="http://unicode.org/reports/tr35/tr35-4.html#Date_Format_Patterns">date format patterns specification</a> adopted by NSDateFormatter.)</p>

<div class="wp_syntax"><div class="code"><pre class="smtalltalk" style="font-family:monospace;">&gt; fmt setDateFormat:'''Year''YYYY'
&nbsp;
&gt; fmt dateFormat
''Year'YYYY'
&nbsp;
&gt; fmt stringFromDate:now
'Year2010'</pre></div></div>

<p>(Note: this is a draft. More details will be filled in afterwards.)</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.moonshine-project.com/en/2010/04/26/exploring-nsdateformatter-using-f-script-as-a-repl/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Implicit animation of custom CALayer properties</title>
		<link>http://blog.moonshine-project.com/en/2010/03/14/implicit-animation-of-custom-calayer-properties/</link>
		<comments>http://blog.moonshine-project.com/en/2010/03/14/implicit-animation-of-custom-calayer-properties/#comments</comments>
		<pubDate>Sun, 14 Mar 2010 04:00:41 +0000</pubDate>
		<dc:creator>ento</dc:creator>
				<category><![CDATA[dev]]></category>

		<guid isPermaLink="false">http://blog.moonshine-project.com/en/?p=20</guid>
		<description><![CDATA[Our quest through various frameworks for creating GUI on Cocoa Touch started out from the UIKit. Unsatisfied with its performance, we then moved on to OpenGL, which is the workhorse of virtually all of our image intensive apps.
However, since last week, I&#8217;ve been playing with Quartz and Core Animation to create a simple clock application [...]]]></description>
			<content:encoded><![CDATA[<p>Our quest through various frameworks for creating GUI on Cocoa Touch started out from the UIKit. Unsatisfied with its performance, we then moved on to OpenGL, which is the workhorse of virtually all of our image intensive apps.</p>
<p>However, since last week, I&#8217;ve been playing with Quartz and Core Animation to create a simple clock application and explore their capabilities.</p>
<p>Things have been going well until I tried to animate a clock hand by changing its angle. You can automatically animate a layer&#8217;s &#8220;animatable properties&#8221; like opacity, size, etc., just by changing its value. How do I go about applying this &#8220;implicit animation&#8221; feature to custom parameters like a clock hand&#8217;s angle?</p>
<p>After three hours of googling, I found that starting from iPhone OS 3.0, CALayer lets you specify which property its content depends on (via needsDisplayForKey:), and which action should be run when the property is changed (via actionForKey:). However, there was one more thing that needed to be taken care of, which I will explain later.</p>
<p>So lets get to the code. Code is worth a thousand words, I believe.</p>
<p>First off, creating the clock hand layer:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@implementation</span> ClockView
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>awakeFromNib <span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>self setupLayers<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>self start<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;">void</span><span style="color: #002200;">&#41;</span>start <span style="color: #002200;">&#123;</span>
    <span style="color: #400080;">NSTimer</span> <span style="color: #002200;">*</span>timer <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSTimer</span> scheduledTimerWithTimeInterval<span style="color: #002200;">:</span><span style="color: #2400d9;">1.0</span><span style="color: #002200;">/</span><span style="color: #2400d9;">60.0</span> 
        target<span style="color: #002200;">:</span>self
        selector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>tick<span style="color: #002200;">&#41;</span>
        userInfo<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span>
        repeats<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
    self.animationTimer <span style="color: #002200;">=</span> timer;
<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>setupLayers <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">/*
     Add layers for drawing each hand.
     Animation is handled by animating the angle key.
     */</span>
    <span style="color: #002200;">&#123;</span>
        HandLayer <span style="color: #002200;">*</span>hand <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>HandLayer alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
        hand.frame <span style="color: #002200;">=</span> self.frame;
        self.secondHand <span style="color: #002200;">=</span> hand;
        <span style="color: #002200;">&#91;</span>hand release<span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#91;</span>self.layer addSublayer<span style="color: #002200;">:</span>self.secondHand<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
    <span style="color: #11740a; font-style: italic;">// ...</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> tick <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// Tell each clock hand that its angle has changed.</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>That wasn&#8217;t particularly interesting. Here comes the climax of this post; the animation:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> HandLayer <span style="color: #002200;">:</span> CALayer <span style="color: #002200;">&#123;</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@property</span> CGFloat angle;
&nbsp;
<span style="color: #a61390;">@end</span>
&nbsp;
&nbsp;
<span style="color: #a61390;">@implementation</span> HandLayer
&nbsp;
@dynamic angle;
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>needsDisplayForKey<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>aKey <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// Changes in angle require redisplay</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>aKey isEqualToString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;angle&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #a61390;">return</span> <span style="color: #a61390;">YES</span>;
    <span style="color: #002200;">&#125;</span> <span style="color: #a61390;">else</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>super needsDisplayForKey<span style="color: #002200;">:</span>aKey<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</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>actionForKey<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> aKey <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>aKey isEqualToString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;angle&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #11740a; font-style: italic;">// Create a basic interpolation for &quot;angle&quot; animation</span>
        CABasicAnimation <span style="color: #002200;">*</span>theAnimation <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>CABasicAnimation
            animationWithKeyPath<span style="color: #002200;">:</span>aKey<span style="color: #002200;">&#93;</span>;
        <span style="color: #11740a; font-style: italic;">// Attention: the animation needs to know the fromValue</span>
        theAnimation.fromValue <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self presentationLayer<span style="color: #002200;">&#93;</span> valueForKey<span style="color: #002200;">:</span>aKey<span style="color: #002200;">&#93;</span>;
        <span style="color: #a61390;">return</span> theAnimation;
    <span style="color: #002200;">&#125;</span> <span style="color: #a61390;">else</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>super actionForKey<span style="color: #002200;">:</span>aKey<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</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>drawInContext<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGContextRef<span style="color: #002200;">&#41;</span>context <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// Draw a hand using the angle property</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Note the line that sets theAnimation.fromValue to the current angle of hand in display. It&#8217;s something not in the textbook. The reason I was able to figure it out is thanks to this <a href="http://www.omnigroup.com/blog/entry/Animating_CALayer_content">Omni Group&#8217;s blog post on &#8220;Animating CALayer content&#8221;</a>, which is from a time when you had to &#8220;whip up [...] a superclass&#8221; and implement your own mechanism of needsDisplayForKey:.</p>
<p>Finally, a video!</p>
<p><!-- Smart Youtube --><span class="youtube"><object width="425" height="355"><param name="movie" value="http://www.youtube.com/v/4rcE0kOrjVA&amp;rel=1&amp;color1=d6d6d6&amp;color2=f0f0f0&amp;border=&amp;fs=1&amp;hl=en&amp;autoplay=&amp;showinfo=0&amp;iv_load_policy=3&amp;showsearch=0" /><param name="allowFullScreen" value="true" /><embed wmode="transparent" src="http://www.youtube.com/v/4rcE0kOrjVA&amp;rel=1&amp;color1=d6d6d6&amp;color2=f0f0f0&amp;border=&amp;fs=1&amp;hl=en&amp;autoplay=&amp;showinfo=0&amp;iv_load_policy=3&amp;showsearch=0" type="application/x-shockwave-flash" allowfullscreen="true" width="425" height="355" ></embed><param name="wmode" value="transparent" /></object></span></p>
<p>Note: The timing function of the animation used in the video is kCAMediaTimingFunctionEaseIn, not the default linear one.</p>
<p>References:</p>
<ul>
<li><a href="http://developer.apple.com/mac/library/documentation/cocoa/conceptual/CoreAnimation_guide/Introduction/Introduction.html">Core Animation Programming Guide</a></li>
<li><a href="http://developer.apple.com/mac/library/documentation/GraphicsImaging/Reference/CALayer_class/Introduction/Introduction.html#//apple_ref/occ/clm/CALayer/needsDisplayForKey:">CALayer needsDisplayForKey:</a></li>
<li><a href="http://developer.apple.com/mac/library/documentation/GraphicsImaging/Reference/CALayer_class/Introduction/Introduction.html#//apple_ref/occ/instm/CALayer/actionForKey:">CALayer actionForKey:</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.moonshine-project.com/en/2010/03/14/implicit-animation-of-custom-calayer-properties/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Asynchronous unit testing with GHUnit and NSInvocation</title>
		<link>http://blog.moonshine-project.com/en/2009/11/22/asynchronous-unit-testing-with-ghunit-and-nsinvocation/</link>
		<comments>http://blog.moonshine-project.com/en/2009/11/22/asynchronous-unit-testing-with-ghunit-and-nsinvocation/#comments</comments>
		<pubDate>Sun, 22 Nov 2009 10:11:51 +0000</pubDate>
		<dc:creator>ento</dc:creator>
				<category><![CDATA[dev]]></category>

		<guid isPermaLink="false">http://blog.moonshine-project.com/en/?p=5</guid>
		<description><![CDATA[1. Decoupling the network access code
Suppose you have an app that calls a remote API to store the user&#8217;s rating of your content:

@implementation StarService
&#160;
- &#40;void&#41;star:&#40;NSUInteger&#41;pictureNumber count:&#40;NSUInteger&#41;count &#123;
	// create the request
	NSString *url = &#91;NSString stringWithFormat:@&#34;http://%@:%d/api/star/&#34;, serviceHostname, servicePort, nil&#93;;
	NSMutableURLRequest *theRequest=&#91;NSMutableURLRequest requestWithURL:&#91;NSURL URLWithString:url&#93;
											  cachePolicy:NSURLRequestUseProtocolCachePolicy
										  timeoutInterval:60.0&#93;;
	// ... setup the API parameters and HTTP headers ...
	StarRequestDelegate *requestDelegate = [...]]]></description>
			<content:encoded><![CDATA[<h2>1. Decoupling the network access code</h2>
<p>Suppose you have an app that calls a remote API to store the user&#8217;s rating of your content:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@implementation</span> StarService
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>star<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSUInteger<span style="color: #002200;">&#41;</span>pictureNumber count<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSUInteger<span style="color: #002200;">&#41;</span>count <span style="color: #002200;">&#123;</span>
	<span style="color: #11740a; font-style: italic;">// create the request</span>
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>url <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;http://%@:%d/api/star/&quot;</span>, serviceHostname, servicePort, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #400080;">NSMutableURLRequest</span> <span style="color: #002200;">*</span>theRequest<span style="color: #002200;">=</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableURLRequest</span> requestWithURL<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURL</span> URLWithString<span style="color: #002200;">:</span>url<span style="color: #002200;">&#93;</span>
											  cachePolicy<span style="color: #002200;">:</span>NSURLRequestUseProtocolCachePolicy
										  timeoutInterval<span style="color: #002200;">:</span><span style="color: #2400d9;">60.0</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #11740a; font-style: italic;">// ... setup the API parameters and HTTP headers ...</span>
	StarRequestDelegate <span style="color: #002200;">*</span>requestDelegate <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>StarRequestDelegate alloc<span style="color: #002200;">&#93;</span> initWithService<span style="color: #002200;">:</span>self delegate<span style="color: #002200;">:</span>serviceDelegate<span style="color: #002200;">&#93;</span>;
	<span style="color: #11740a; font-style: italic;">/* Starting API call! */</span>
	<span style="color: #400080;">NSURLConnection</span> <span style="color: #002200;">*</span>theConnection <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURLConnection</span> connectionWithRequest<span style="color: #002200;">:</span>aRequest delegate<span style="color: #002200;">:</span>requestDelegate<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
...
<span style="color: #11740a; font-style: italic;">// NSURLConnection delegate for the star API call</span>
<span style="color: #a61390;">@implementation</span> StarRequestDelegate
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>connectionDidFinishLoading<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSURLConnection</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>connection <span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>delegate <span style="color: #002200;">&amp;&amp;</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSObject</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>delegate respondsToSelector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>starService<span style="color: #002200;">:</span>didFinishStar<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		<span style="color: #11740a; font-style: italic;">/* API call returned normally, invoke the delegate's callback */</span>
		<span style="color: #002200;">&#91;</span>delegate starService<span style="color: #002200;">:</span>service didFinishStar<span style="color: #002200;">:</span>receivedData<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
	<span style="color: #002200;">&#91;</span>super connectionDidFinishLoading<span style="color: #002200;">:</span>connection<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>The first thing you want to do is to implement a fake version of the calling class to slice out the network dependency. Then you can use the fake service to develop other parts of the app without worrying about setting up the API server each time you do a test run.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@implementation</span> FakeStarService
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>star<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSUInteger<span style="color: #002200;">&#41;</span>pictureNumber count<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSUInteger<span style="color: #002200;">&#41;</span>count <span style="color: #002200;">&#123;</span>
	<span style="color: #11740a; font-style: italic;">/* invocation to call the delegate method directly */</span>
	<span style="color: #400080;">NSInvocation</span> <span style="color: #002200;">*</span>invocation;
	<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSInvocation</span> retainedInvocationWithTarget<span style="color: #002200;">:</span>serviceDelegate invocationOut<span style="color: #002200;">:&amp;</span>invocation<span style="color: #002200;">&#93;</span>
	 starService<span style="color: #002200;">:</span>self didFinishStar<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">/* wrap it around with another invocation to simulate network delay */</span>
	<span style="color: #400080;">NSInvocation</span> <span style="color: #002200;">*</span>delayInvocation;
	<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSInvocation</span> retainedInvocationWithTarget<span style="color: #002200;">:</span>invocation invocationOut<span style="color: #002200;">:&amp;</span>delayInvocation<span style="color: #002200;">&#93;</span>
	 performSelector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>invoke<span style="color: #002200;">&#41;</span> withObject<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> afterDelay<span style="color: #002200;">:</span>delay<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>delayInvocation performSelectorOnMainThread<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>invoke<span style="color: #002200;">&#41;</span> withObject<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> waitUntilDone<span style="color: #002200;">:</span><span style="color: #a61390;">NO</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Here I&#8217;m creating two NSInvocations to simulate a delayed API call. Wait, there must be more to creating an NSInvocation? You&#8217;re right. This code wouldn&#8217;t be possible without the excellent <a href="http://cocoawithlove.com/2008/03/construct-nsinvocation-for-any-message.html">ForwardedConstruction extension</a> for NSInvocation.</p>
<p>To use the extension on iPhone, download the code from the original article and add/modify the following parts:</p>

<div class="wp_syntax"><div class="code"><pre class="diff" style="font-family:monospace;">--- Downloads/NSInvocationForwardedConstruction/NSInvocation<span style="">&#40;</span>ForwardedConstruction<span style="">&#41;</span>.h	<span style="">2009</span>-05-04 <span style="">11</span>:<span style="">55</span>:<span style="">34.000000000</span> +0900
<span style="color: #888822;">+++ NSInvocation<span style="">&#40;</span>ForwardedConstruction<span style="">&#41;</span>.h	<span style="">2009</span>-<span style="">12</span>-02 <span style="">10</span>:<span style="">17</span>:<span style="">07.000000000</span> +0900</span>
<span style="color: #440088;">@@ -<span style="">11</span>,<span style="">7</span> +<span style="">11</span>,<span style="">7</span> @@</span>
 //  appreciated but not required.
 //
&nbsp;
<span style="color: #991111;">-#import &lt;Cocoa/Cocoa.h&gt;</span>
<span style="color: #00b000;">+#import &lt;UIKit/UIKit.h&gt;</span>
&nbsp;
 @interface NSInvocation <span style="">&#40;</span>ForwardedConstruction<span style="">&#41;</span>
&nbsp;
<span style="color: #440088;">@@ -<span style="">21</span>,<span style="">3</span> +<span style="">21</span>,<span style="">10</span> @@</span>
 	invocationOut:<span style="">&#40;</span>NSInvocation **<span style="">&#41;</span>invocationOut;
&nbsp;
 @end
<span style="color: #00b000;">+</span>
<span style="color: #00b000;">+#if <span style="">&#40;</span>TARGET_OS_IPHONE<span style="">&#41;</span></span>
<span style="color: #00b000;">+@interface NSObject <span style="">&#40;</span>ForwardedConstruction<span style="">&#41;</span></span>
<span style="color: #00b000;">+- <span style="">&#40;</span>NSString *<span style="">&#41;</span>className;</span>
<span style="color: #00b000;">++ <span style="">&#40;</span>NSString *<span style="">&#41;</span>className;</span>
<span style="color: #00b000;">+@end</span>
<span style="color: #00b000;">+#endif</span>
<span style="color: #888822;">--- Downloads/NSInvocationForwardedConstruction/NSInvocation+ForwardedConstruction.m	<span style="">2009</span>-05-04 <span style="">11</span>:<span style="">55</span>:<span style="">34.000000000</span> +0900</span>
<span style="color: #888822;">+++ NSInvocation<span style="">&#40;</span>ForwardedConstruction<span style="">&#41;</span>.m	<span style="">2009</span>-<span style="">12</span>-02 <span style="">10</span>:<span style="">17</span>:<span style="">43.000000000</span> +0900</span>
<span style="color: #440088;">@@ -<span style="">12</span>,<span style="">7</span> +<span style="">12</span>,<span style="">9</span> @@</span>
 //
&nbsp;
 #import &quot;NSInvocation<span style="">&#40;</span>ForwardedConstruction<span style="">&#41;</span>.h&quot;
<span style="color: #991111;">-#import &lt;objc/objc-runtime.h&gt;</span>
<span style="color: #00b000;">+//#import &lt;objc/objc-runtime.h&gt;</span>
<span style="color: #00b000;">+#import &lt;objc/runtime.h&gt;</span>
<span style="color: #00b000;">+#import &lt;objc/message.h&gt;</span>
&nbsp;
 //
 // InvocationProxy is a private class for receiving invocations via the
<span style="color: #440088;">@@ -<span style="">376</span>,<span style="">4</span> +<span style="">378</span>,<span style="">21</span> @@</span>
 	return invocationProxy;
 <span style="">&#125;</span>
&nbsp;
<span style="color: #00b000;">+@end </span>
<span style="color: #00b000;">+</span>
<span style="color: #00b000;">+#if <span style="">&#40;</span>TARGET_OS_IPHONE<span style="">&#41;</span></span>
<span style="color: #00b000;">+</span>
<span style="color: #00b000;">+@implementation NSObject <span style="">&#40;</span>ForwardedConstruction<span style="">&#41;</span></span>
<span style="color: #00b000;">+</span>
<span style="color: #00b000;">+- <span style="">&#40;</span>NSString *<span style="">&#41;</span>className</span>
<span style="color: #00b000;">+<span style="">&#123;</span></span>
<span style="color: #00b000;">+	return <span style="">&#91;</span>NSString stringWithUTF8String:class_getName<span style="">&#40;</span><span style="">&#91;</span>self class<span style="">&#93;</span><span style="">&#41;</span><span style="">&#93;</span>;</span>
<span style="color: #00b000;">+<span style="">&#125;</span></span>
<span style="color: #00b000;">++ <span style="">&#40;</span>NSString *<span style="">&#41;</span>className</span>
<span style="color: #00b000;">+<span style="">&#123;</span></span>
<span style="color: #00b000;">+	return <span style="">&#91;</span>NSString stringWithUTF8String:class_getName<span style="">&#40;</span>self<span style="">&#41;</span><span style="">&#93;</span>;</span>
<span style="color: #00b000;">+<span style="">&#125;</span></span>
<span style="color: #00b000;">+</span>
 @end
<span style="color: #00b000;">+</span>
<span style="color: #00b000;">+#endif</span></pre></div></div>

<h2>2. Choosing the right thread</h2>
<p><a href="http://github.com/gabriel/gh-unit">GHUnit</a> is a test framework for Objective-C (Mac OS X 10.5 and iPhone 2.x/3.x) with a pretty GUI test runner. It has the ability to run itself on a separate thread, which comes in handy when dealing with NSURLConnection related tests.</p>
<p>Why? Because for some internal working of NSURLConnection, invoking it from the main thread seems to be the most hassle-free way of using. By having the test framework running on a separate thread, we can keep the network related code on the main thread while enjoying a smooth testing UI.</p>
<p>So here is the working setup I have:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// test cases for the real service class.</span>
<span style="color: #11740a; font-style: italic;">// there's another similar class for testing the fake service.</span>
<span style="color: #a61390;">@implementation</span> HttpNetTest
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>shouldRunOnMainThread <span style="color: #002200;">&#123;</span>
	<span style="color: #11740a; font-style: italic;">/* Tell GHUnit to run on a separate thread */</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>test_send_star <span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>tester do_test_send_star<span style="color: #002200;">:</span>service<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> StarServiceTests
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>do_test_send_star<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>service <span style="color: #002200;">&#123;</span>
	<span style="color: #11740a; font-style: italic;">// setup an invocation and</span>
	<span style="color: #400080;">NSInvocation</span> <span style="color: #002200;">*</span>invocation;
	<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSInvocation</span> retainedInvocationWithTarget<span style="color: #002200;">:</span>service invocationOut<span style="color: #002200;">:&amp;</span>invocation<span style="color: #002200;">&#93;</span>
	  star<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span> count<span style="color: #002200;">:</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #11740a; font-style: italic;">/* invoke it on the main thread */</span>
	<span style="color: #002200;">&#91;</span>invocation performSelectorOnMainThread<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>invoke<span style="color: #002200;">&#41;</span> withObject<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> waitUntilDone<span style="color: #002200;">:</span><span style="color: #a61390;">NO</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">/* wait */</span>
	<span style="color: #a61390;">BOOL</span> notTimeout <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>AsyncTestHelper wait<span style="color: #002200;">:</span>service.delegate property<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>receivedDidFinishGetStarsCount<span style="color: #002200;">&#41;</span> atLeast<span style="color: #002200;">:</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">/* asserts */</span>
	GHAssertTrue<span style="color: #002200;">&#40;</span>notTimeout, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Should not timeout&quot;</span><span style="color: #002200;">&#41;</span>;
	GHAssertEquals<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>NSUInteger<span style="color: #002200;">&#41;</span><span style="color: #2400d9;">1</span>, <span style="color: #002200;">&#91;</span>service.delegate receivedDidFinishStarCount<span style="color: #002200;">&#93;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;delegate should receive star callback&quot;</span><span style="color: #002200;">&#41;</span>;		
<span style="color: #002200;">&#125;</span></pre></div></div>

<h2>3. Waiting for the API call to end</h2>
<p>The final snippet is a little helper for testing asynchronous operation. You might have noticed the line using it in the test case:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@implementation</span> StarServiceTests
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>do_test_send_star<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>service <span style="color: #002200;">&#123;</span>
	<span style="color: #11740a; font-style: italic;">// .. </span>
	<span style="color: #002200;">&#91;</span>AsyncTestHelper wait<span style="color: #002200;">:</span>service.delegate property<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>receivedDidFinishStarCount<span style="color: #002200;">&#41;</span> atLeast<span style="color: #002200;">:</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #11740a; font-style: italic;">// ..</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Basically the method just loops until the specified property value is equal or more than a certain threshold:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@implementation</span> AsyncTestHelper
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>wait<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>target property<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">SEL</span><span style="color: #002200;">&#41;</span>getter atLeast<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSUInteger<span style="color: #002200;">&#41;</span>count <span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">int</span> tried <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
	<span style="color: #a61390;">while</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>NSUInteger<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#91;</span>target performSelector<span style="color: #002200;">:</span>getter<span style="color: #002200;">&#93;</span>  <span style="color: #2400d9;">10</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
			<span style="color: #a61390;">return</span> FALSE;
		<span style="color: #002200;">&#125;</span>
		<span style="color: #002200;">&#91;</span><span style="color: #400080;">NSThread</span> sleepForTimeInterval<span style="color: #002200;">:</span><span style="color: #2400d9;">0.5</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
	<span style="color: #a61390;">return</span> TRUE;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Now you can get the pleasure of full green tests with iPhone network programming too.</p>
<p><img src="http://img.skitch.com/20091202-rdp1ea91iuxwcjtwpbndua1i9w.png" alt="ghunit_test_runner" width="287"></p>
<p>Finally, here&#8217;s a class diagram of all the classes mentioned above, produced with a little help from Xcode&#8217;s Core Data modeling interface.</p>
<p><img src="http://img.skitch.com/20091122-j6q7axd8up53qb1m1xdaxswrai.png"></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.moonshine-project.com/en/2009/11/22/asynchronous-unit-testing-with-ghunit-and-nsinvocation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hello world!</title>
		<link>http://blog.moonshine-project.com/en/2009/07/07/hello-world/</link>
		<comments>http://blog.moonshine-project.com/en/2009/07/07/hello-world/#comments</comments>
		<pubDate>Tue, 07 Jul 2009 07:07:46 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Welcome to Moonshine Project.
]]></description>
			<content:encoded><![CDATA[<p>Welcome to <a href="http://www.moonshine-project.com/">Moonshine Project</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.moonshine-project.com/en/2009/07/07/hello-world/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
