<?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>MRo Blog &#187; Objective C</title>
	<atom:link href="http://blog.mro.name/tag/objective-c/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.mro.name</link>
	<description>Marcus Rohrmoser mobile Software</description>
	<lastBuildDate>Wed, 23 Jun 2010 11:32:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>XCode: missing &#8220;deprecated&#8221; warnings</title>
		<link>http://blog.mro.name/2010/06/xcode-missing-deprecated-warnings/</link>
		<comments>http://blog.mro.name/2010/06/xcode-missing-deprecated-warnings/#comments</comments>
		<pubDate>Tue, 22 Jun 2010 10:55:56 +0000</pubDate>
		<dc:creator>mro</dc:creator>
				<category><![CDATA[Articles in english]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[alloc]]></category>
		<category><![CDATA[compiler]]></category>
		<category><![CDATA[deprecated]]></category>
		<category><![CDATA[gcc]]></category>
		<category><![CDATA[initWithFrame]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Objective C]]></category>
		<category><![CDATA[UITableViewCell]]></category>
		<category><![CDATA[warning]]></category>
		<category><![CDATA[XCode]]></category>

		<guid isPermaLink="false">http://blog.mro.name/?p=1889</guid>
		<description><![CDATA[when developing for long-term use, you want to use APIs that aren&#8217;t likely to be removed soon, a.k.a. &#8220;deprecated&#8221;. So, don&#8217;t use downward compatible calls below a point you really aim for. XCode helps with compiler warnings about &#8220;deprecated&#8221; calls &#8211; if &#8220;Project -&#62; Edit Project Settings -&#62; GCC_WARN_ABOUT_DEPRECATED_FUNCTIONS&#8221; is set: But be careful, this [...]]]></description>
			<content:encoded><![CDATA[<p>when developing for long-term use, you want to use APIs that aren&#8217;t <a href="http://en.wikipedia.org/wiki/Deprecation">likely to be removed soon, a.k.a. &#8220;deprecated&#8221;</a>.</p>
<p>So, don&#8217;t use downward compatible calls below a point you really aim for.</p>
<p>XCode helps with compiler warnings about &#8220;deprecated&#8221; calls &#8211; if &#8220;Project -&gt; Edit Project Settings -&gt; GCC_WARN_ABOUT_DEPRECATED_FUNCTIONS&#8221; is set:</p>
<div id="attachment_1890" class="wp-caption aligncenter" style="width: 395px"><a href="http://blog.mro.name/wp-content/uploads/2010/06/Bildschirmfoto-2010-06-22-um-12.39.48.png"><img class="size-full wp-image-1890 " title="Compiler setting: Warn about deprecated calls" src="http://blog.mro.name/wp-content/uploads/2010/06/Bildschirmfoto-2010-06-22-um-12.39.48.png" alt="Compiler setting: Warn about deprecated calls" width="385" height="150" /></a><p class="wp-caption-text">Compiler setting: Warn about deprecated calls</p></div>
<p>But be careful, this complains e.g. here only once while <code><a href="http://developer.apple.com/iphone/library/documentation/uikit/reference/UITableViewCell_Class/DeprecationAppendix/AppendixADeprecatedAPI.html#//apple_ref/doc/uid/TP40006938-CH3-SW32">initWithFrame:</a></code> is also deprecated (compiling for iPhone OS 3.0):</p>
<div id="attachment_1891" class="wp-caption aligncenter" style="width: 310px"><a href="http://blog.mro.name/wp-content/uploads/2010/06/Bildschirmfoto-2010-06-22-um-12.41.57.png"><img class="size-medium wp-image-1891  " title="No &quot;deprecated&quot; warning on initXY" src="http://blog.mro.name/wp-content/uploads/2010/06/Bildschirmfoto-2010-06-22-um-12.41.57-300x14.png" alt="" width="300" height="14" /></a><p class="wp-caption-text">No &quot;deprecated&quot; warning on initXY</p></div>
<p>As <a href="http://stackoverflow.com/questions/2135514/deprecated-not-triggering-compiler-warning-with-subclass/2136130#comment-2076316">Kevin Ballard pointed out at Stackoverflow</a>, this is because <code>[AnyClass alloc]</code> returns a type <code>id</code> &#8211; which doesn&#8217;t know about it&#8217;s interface.</p>
<p>To get this kind of compiler warnings, you have to type-cast the <code>[AnyClass alloc]</code> like this:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#40;</span>AnyClass<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#91;</span>AnyClass alloc<span style="color: #002200;">&#93;</span> initXY<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>Maybe time for a macro?</p>
<p>The macro could look like</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// http://blog.mro.name/2010/06/xcode-missing-deprecated-warnings/</span>
<span style="color: #6e371a;">#define alloc(c)	((c*)[c alloc])</span></pre></div></div>

<p>Migrate your codebase via XCode &#8220;Edit -&gt; Find in Project&#8221; with search pattern <code>\[\s*([^\[\]]+)\s+alloc\s*\]</code> and replacement pattern <code>alloc(\1)</code>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mro.name/2010/06/xcode-missing-deprecated-warnings/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>iPhone Unit Test Coverage</title>
		<link>http://blog.mro.name/2010/03/iphone-unit-test-coverage/</link>
		<comments>http://blog.mro.name/2010/03/iphone-unit-test-coverage/#comments</comments>
		<pubDate>Thu, 25 Mar 2010 11:11:21 +0000</pubDate>
		<dc:creator>mro</dc:creator>
				<category><![CDATA[Articles in english]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[Coverage]]></category>
		<category><![CDATA[CoverStory]]></category>
		<category><![CDATA[gcov]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Objective C]]></category>
		<category><![CDATA[SenTestingKit]]></category>
		<category><![CDATA[Unit Test]]></category>

		<guid isPermaLink="false">http://blog.mro.name/?p=1707</guid>
		<description><![CDATA[have a look at the CoverStory Howto, download CoverStory, open (with XCode) the XCode Project you want to measure, run the script EnableGCov.scpt linked to from the howto, run your tests and see a linker error, &#8211; dead end for the time being. http://groups.google.com/group/coverstory-discuss/browse_thread/thread/fbcbf5ed61d8d02b#]]></description>
			<content:encoded><![CDATA[<ol>
<li>have a look at the <a href="http://code.google.com/p/coverstory/wiki/UsingCoverstory">CoverStory Howto</a>,</li>
<li>download CoverStory,</li>
<li>open (with XCode) the XCode Project you want to measure,</li>
<li>run the script EnableGCov.scpt linked to from the howto,</li>
<li>run your tests and see a <a href="http://lists.apple.com/archives/xcode-users/2009/Aug/msg00436.html">linker error</a>, &#8211; dead end for the time being. <a href="http://groups.google.com/group/coverstory-discuss/browse_thread/thread/fbcbf5ed61d8d02b#">http://groups.google.com/group/coverstory-discuss/browse_thread/thread/fbcbf5ed61d8d02b#</a></li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://blog.mro.name/2010/03/iphone-unit-test-coverage/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>NSCachedURLResponse / NSKeyedUnarchiver pain</title>
		<link>http://blog.mro.name/2010/01/nscachedurlresponse-nskeyedunarchiver-pain/</link>
		<comments>http://blog.mro.name/2010/01/nscachedurlresponse-nskeyedunarchiver-pain/#comments</comments>
		<pubDate>Wed, 27 Jan 2010 18:42:00 +0000</pubDate>
		<dc:creator>mro</dc:creator>
				<category><![CDATA[Articles in english]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[NSCachedURLResponse]]></category>
		<category><![CDATA[NSKeyedArchiver]]></category>
		<category><![CDATA[NSKeyedUnarchiver]]></category>
		<category><![CDATA[NSURLCache]]></category>
		<category><![CDATA[Objective C]]></category>

		<guid isPermaLink="false">http://blog.mro.name/?p=1667</guid>
		<description><![CDATA[as the iPhone SDK comes with a rather dysfunctional NSURLCache — Apple suggests to implement it from scratch yourself in the code examples about caching — I went for just this. Until I came across the [NSKeyedUnarchiver unarchiveObjectWithData:...] not restoring userInfo, storagePolicy and data of NSCachedURLResponse. Couldn&#8217;t believe it and spent almost the whole day verifying [...]]]></description>
			<content:encoded><![CDATA[<p>as the iPhone SDK comes with a rather dysfunctional <a href="http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSURLCache_Class/Reference/Reference.html">NSURLCache</a> — Apple suggests to implement it from scratch yourself in the <a href="http://developer.apple.com/iphone/library/samplecode/URLCache/index.html#//apple_ref/doc/uid/DTS40008061">code examples about caching</a> — I went for just this.</p>
<p>Until I came across the <code>[<a href="http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSKeyedUnarchiver_Class/Reference/Reference.html">NSKeyedUnarchiver</a> unarchiveObjectWithData:...]</code> not restoring <code>userInfo</code>, <code>storagePolicy</code> and <code>data</code> of <code><a href="http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSCachedURLResponse_Class/Reference/Reference.html">NSCachedURLResponse</a></code>.</p>
<p>Couldn&#8217;t believe it and spent almost the whole day verifying that the error is not within my code but really the unarchiver treats those fields transient.</p>
<p>See yourself:</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;">void</span><span style="color: #002200;">&#41;</span>testiPhoneSDK312NSKeyedArchiver
<span style="color: #002200;">&#123;</span>
<span style="color: #11740a; font-style: italic;">//  prepare a NSCachedURLResponse</span>
    <span style="color: #400080;">NSCachedURLResponse</span> <span style="color: #002200;">*</span>src <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
    <span style="color: #002200;">&#123;</span>
        <span style="color: #400080;">NSURL</span> <span style="color: #002200;">*</span>i_url <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURL</span> URLWithString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;http://www.url.example/path/file.html&quot;</span><span style="color: #002200;">&#93;</span>;
        <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>i_mime <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;text/html&quot;</span>;
        NSInteger expectedContentLength <span style="color: #002200;">=</span> <span style="color: #2400d9;">112</span>;
        <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>i_encoding <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;iso-8859-2&quot;</span>;
&nbsp;
        <span style="color: #400080;">NSURLResponse</span> <span style="color: #002200;">*</span>i_response <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURLResponse</span> alloc<span style="color: #002200;">&#93;</span> initWithURL<span style="color: #002200;">:</span>i_url
            MIMEType<span style="color: #002200;">:</span>i_mime
            expectedContentLength<span style="color: #002200;">:</span>expectedContentLength
            textEncodingName<span style="color: #002200;">:</span>i_encoding<span style="color: #002200;">&#93;</span>;
        <span style="color: #400080;">NSData</span> <span style="color: #002200;">*</span>i_data <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Hello, world!&quot;</span> dataUsingEncoding<span style="color: #002200;">:</span>NSISOLatin2StringEncoding<span style="color: #002200;">&#93;</span>;
        NSURLCacheStoragePolicy i_storage <span style="color: #002200;">=</span>  NSURLCacheStorageAllowed;
        <span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>i_userInfo <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithObject<span style="color: #002200;">:</span>
        <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDate</span> dateWithTimeIntervalSince1970<span style="color: #002200;">:</span><span style="color: #2400d9;">13</span><span style="color: #002200;">&#93;</span> forKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;era&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
        <span style="color: #400080;">NSCachedURLResponse</span> <span style="color: #002200;">*</span>src <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSCachedURLResponse</span> alloc<span style="color: #002200;">&#93;</span> initWithResponse<span style="color: #002200;">:</span>i_response
            data<span style="color: #002200;">:</span>i_data
            userInfo<span style="color: #002200;">:</span>i_userInfo
            storagePolicy<span style="color: #002200;">:</span>i_storage<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">//      ensure it's all in place:</span>
        STAssertEqualObjects<span style="color: #002200;">&#40;</span>i_url, src.response.URL, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;&quot;</span><span style="color: #002200;">&#41;</span>;
        STAssertEqualObjects<span style="color: #002200;">&#40;</span>i_mime, src.response.MIMEType, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;&quot;</span><span style="color: #002200;">&#41;</span>;
        STAssertEquals<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>expectedContentLength, <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>src.response.expectedContentLength, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;&quot;</span><span style="color: #002200;">&#41;</span>;
        STAssertEqualObjects<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;file.html&quot;</span>, src.response.suggestedFilename, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;&quot;</span><span style="color: #002200;">&#41;</span>;
        STAssertEqualObjects<span style="color: #002200;">&#40;</span>i_encoding, src.response.textEncodingName, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
        STAssertEqualObjects<span style="color: #002200;">&#40;</span>i_data, src.data, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;&quot;</span><span style="color: #002200;">&#41;</span>;
        STAssertEqualObjects<span style="color: #002200;">&#40;</span>i_userInfo, src.userInfo, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;&quot;</span><span style="color: #002200;">&#41;</span>;
        STAssertEquals<span style="color: #002200;">&#40;</span> 0u, src.storagePolicy, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;&quot;</span><span style="color: #002200;">&#41;</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">//  archive + unarchive:</span>
    <span style="color: #400080;">NSCachedURLResponse</span> <span style="color: #002200;">*</span>dst <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSKeyedUnarchiver</span> unarchiveObjectWithData<span style="color: #002200;">:</span>
      <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSKeyedArchiver</span> archivedDataWithRootObject<span style="color: #002200;">:</span>src<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">//  check whether src == dst</span>
    STAssertEqualObjects<span style="color: #002200;">&#40;</span>src.response.URL, dst.response.URL, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;&quot;</span><span style="color: #002200;">&#41;</span>;
    STAssertEqualObjects<span style="color: #002200;">&#40;</span>src.response.MIMEType, dst.response.MIMEType, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;&quot;</span><span style="color: #002200;">&#41;</span>;
    STAssertEquals<span style="color: #002200;">&#40;</span>src.response.expectedContentLength, dst.response.expectedContentLength, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;&quot;</span><span style="color: #002200;">&#41;</span>;
    STAssertEqualObjects<span style="color: #002200;">&#40;</span>src.response.suggestedFilename, dst.response.suggestedFilename, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;&quot;</span><span style="color: #002200;">&#41;</span>;
    STAssertEqualObjects<span style="color: #002200;">&#40;</span>src.response.textEncodingName, dst.response.textEncodingName, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">//  !!!!!!!!!!</span>
<span style="color: #11740a; font-style: italic;">//  sad information loss after unarchiving:</span>
    STAssertNil<span style="color: #002200;">&#40;</span> dst.data, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;&quot;</span> <span style="color: #002200;">&#41;</span>;
    STAssertNil<span style="color: #002200;">&#40;</span> dst.userInfo, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;&quot;</span> <span style="color: #002200;">&#41;</span>;
    STAssertEquals<span style="color: #002200;">&#40;</span> 2u , dst.storagePolicy, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;&quot;</span> <span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>When my NSURLCache replacement is ready I think about publishing it a github — interested anyone?</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mro.name/2010/01/nscachedurlresponse-nskeyedunarchiver-pain/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Binary Search NSArray</title>
		<link>http://blog.mro.name/2010/01/binary-search-nsarray/</link>
		<comments>http://blog.mro.name/2010/01/binary-search-nsarray/#comments</comments>
		<pubDate>Tue, 12 Jan 2010 22:02:07 +0000</pubDate>
		<dc:creator>mro</dc:creator>
				<category><![CDATA[Articles in english]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[Binary]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[gist]]></category>
		<category><![CDATA[git]]></category>
		<category><![CDATA[github]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[NSArray]]></category>
		<category><![CDATA[Objective C]]></category>
		<category><![CDATA[OS X]]></category>
		<category><![CDATA[Search]]></category>

		<guid isPermaLink="false">http://blog.mro.name/?p=1652</guid>
		<description><![CDATA[Though CFArray comes with binary search capability, NSArray does not &#8211; at least not within the iPhone SDK. The indexOfObject:inSortedRange:options:usingComparator: can&#8217;t be found. Plus the CFArrayBSearchValues doesn&#8217;t tell you whether the key actually is part of the list or not. That&#8217;s what the Java JDK does, so let&#8217;s implement some category methods -&#40;NSInteger&#41;binarySearch:&#40;id&#41;key; -&#40;NSInteger&#41;binarySearch:&#40;id&#41;key usingSelector:&#40;SEL&#41;comparator; [...]]]></description>
			<content:encoded><![CDATA[<p>Though <a href="http://developer.apple.com/mac/library/DOCUMENTATION/CoreFoundation/Reference/CFArrayRef/index.html">CFArray</a> comes with <a href="http://en.wikipedia.org/wiki/Binary_search_algorithm#Iterative">binary search</a> capability, <a href="http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/NSArray.html">NSArray</a> does not &#8211; at least not within the iPhone SDK. The <a href="http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/NSArray.html#//apple_ref/occ/instm/NSArray/indexOfObject:inSortedRange:options:usingComparator:">indexOfObject:inSortedRange:options:usingComparator:</a> can&#8217;t be found.</p>
<p>Plus the <a href="http://developer.apple.com/mac/library/DOCUMENTATION/CoreFoundation/Reference/CFArrayRef/Reference/reference.html#//apple_ref/doc/uid/20001192-CH201-F10956">CFArrayBSearchValues</a> doesn&#8217;t tell you whether the key actually is part of the list or not. That&#8217;s what the <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/Arrays.html#binarySearch(T[],%20T,%20java.util.Comparator)">Java JDK</a> does, so let&#8217;s implement some <a href="http://developer.apple.com/mac/library/documentation/General/Conceptual/DevPedia-CocoaCore/Category.html">category</a> methods</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>NSInteger<span style="color: #002200;">&#41;</span>binarySearch<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>key;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>binarySearch<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>key usingSelector<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">SEL</span><span style="color: #002200;">&#41;</span>comparator;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>binarySearch<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>key usingSelector<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">SEL</span><span style="color: #002200;">&#41;</span>comparator inRange<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">NSRange</span><span style="color: #002200;">&#41;</span>range;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>binarySearch<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>key usingFunction<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSInteger <span style="color: #002200;">&#40;</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span>, <span style="color: #a61390;">id</span>, <span style="color: #a61390;">void</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>comparator context<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>context;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>binarySearch<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>key usingFunction<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSInteger <span style="color: #002200;">&#40;</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span>, <span style="color: #a61390;">id</span>, <span style="color: #a61390;">void</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>comparator context<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>context inRange<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">NSRange</span><span style="color: #002200;">&#41;</span>range;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>binarySearch<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>key usingDescriptors<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>sortDescriptors;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>binarySearch<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>key usingDescriptors<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>sortDescriptors inRange<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">NSRange</span><span style="color: #002200;">&#41;</span>range;</pre></div></div>

<p>ourselves, 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>NSInteger<span style="color: #002200;">&#41;</span>binarySearch<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>key usingFunction<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSInteger <span style="color: #002200;">&#40;</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span>, <span style="color: #a61390;">id</span>, <span style="color: #a61390;">void</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>comparator context<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>context inRange<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">NSRange</span><span style="color: #002200;">&#41;</span>range
<span style="color: #002200;">&#123;</span>
    NSLogD<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;[NSArray(MroBinarySearch) binarySearch:%@ usingFunction:]&quot;</span>, key<span style="color: #002200;">&#41;</span>;
    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>self.count <span style="color: #002200;">==</span> <span style="color: #2400d9;">0</span> || key <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span> || comparator <span style="color: #002200;">==</span> <span style="color: #a61390;">NULL</span><span style="color: #002200;">&#41;</span>
        <span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>self binarySearch<span style="color: #002200;">:</span>key usingSelector<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> inRange<span style="color: #002200;">:</span>range<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">//	check overflow?</span>
    NSInteger min <span style="color: #002200;">=</span> range.location;
    NSInteger max <span style="color: #002200;">=</span> range.location <span style="color: #002200;">+</span> range.length <span style="color: #002200;">-</span> <span style="color: #2400d9;">1</span>;
&nbsp;
    <span style="color: #a61390;">while</span> <span style="color: #002200;">&#40;</span>min <span style="color: #002200;">&amp;</span>lt; <span style="color: #002200;">=</span> max<span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
        <span style="color: #11740a; font-style: italic;">// http://googleresearch.blogspot.com/2006/06/extra-extra-read-all-about-it-nearly.html</span>
        <span style="color: #a61390;">const</span> NSInteger mid <span style="color: #002200;">=</span> min <span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span>max <span style="color: #002200;">-</span> min<span style="color: #002200;">&#41;</span> <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span>;
        <span style="color: #a61390;">switch</span> <span style="color: #002200;">&#40;</span>comparator<span style="color: #002200;">&#40;</span>key, <span style="color: #002200;">&#91;</span>self objectAtIndex<span style="color: #002200;">:</span>mid<span style="color: #002200;">&#93;</span>, context<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>
        <span style="color: #002200;">&#123;</span>
            <span style="color: #a61390;">case</span> NSOrderedSame<span style="color: #002200;">:</span>
                <span style="color: #a61390;">return</span> mid;
            <span style="color: #a61390;">case</span> NSOrderedDescending<span style="color: #002200;">:</span>
                min <span style="color: #002200;">=</span> mid <span style="color: #002200;">+</span> <span style="color: #2400d9;">1</span>;
                <span style="color: #a61390;">break</span>;
            <span style="color: #a61390;">case</span> NSOrderedAscending<span style="color: #002200;">:</span>
                max <span style="color: #002200;">=</span> mid <span style="color: #002200;">-</span> <span style="color: #2400d9;">1</span>;
                <span style="color: #a61390;">break</span>;
        <span style="color: #002200;">&#125;</span>
    <span style="color: #002200;">&#125;</span>
    <span style="color: #a61390;">return</span> <span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>min <span style="color: #002200;">+</span> <span style="color: #2400d9;">1</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>See the <a href="http://gist.github.com/275631/">full interface + implementation + testcase without html encoding dirt at github</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mro.name/2010/01/binary-search-nsarray/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CoreData generic findManyByKey</title>
		<link>http://blog.mro.name/2009/12/coredata-generic-findmanybykey/</link>
		<comments>http://blog.mro.name/2009/12/coredata-generic-findmanybykey/#comments</comments>
		<pubDate>Wed, 09 Dec 2009 03:32:34 +0000</pubDate>
		<dc:creator>mro</dc:creator>
				<category><![CDATA[Articles in english]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[CoreData]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[NSEntityDescription]]></category>
		<category><![CDATA[NSManagedObject]]></category>
		<category><![CDATA[NSManagedObjectContext]]></category>
		<category><![CDATA[NSPredicate]]></category>
		<category><![CDATA[Objective C]]></category>

		<guid isPermaLink="false">http://blog.mro.name/?p=1583</guid>
		<description><![CDATA[The base for many of my SELECT-ish queries when querying by exact match is one generic method I created in some category methods on NSManagedObjectContext: -&#40;NSArray*&#41;entityName:&#40;NSString*&#41;entityName findManyByRelation:&#40;NSDictionary*&#41;dict &#123; // TODO handle dict nil and emptyness NSMutableArray *arr = &#91;&#91;NSMutableArray alloc&#93; initWithCapacity:dict.count&#93;; for&#40;NSString *key in dict&#41; &#123; NSExpression *left = &#91;NSExpression expressionForKeyPath:key&#93;; NSExpression *right = &#91;NSExpression [...]]]></description>
			<content:encoded><![CDATA[<p>The base for many of my <a href="http://en.wikipedia.org/wiki/Select_%28SQL%29">SELECT</a>-ish queries when querying by exact match is one generic method I created in some <a href="http://developer.apple.com/iphone/library/DOCUMENTATION/Cocoa/Conceptual/ObjectiveC/Articles/ocCategories.html#//apple_ref/doc/uid/TP30001163-CH20-SW1">category methods</a> on <a href="http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/CoreDataFramework/Classes/NSManagedObjectContext_Class/NSManagedObjectContext.html">NSManagedObjectContext</a>:</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: #400080;">NSArray</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>entityName<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>entityName findManyByRelation<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>dict
<span style="color: #002200;">&#123;</span>
<span style="color: #11740a; font-style: italic;">//  TODO handle dict nil and emptyness</span>
    <span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span>arr <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableArray</span> alloc<span style="color: #002200;">&#93;</span> initWithCapacity<span style="color: #002200;">:</span>dict.count<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>key <span style="color: #a61390;">in</span> dict<span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
        <span style="color: #400080;">NSExpression</span> <span style="color: #002200;">*</span>left <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSExpression</span> expressionForKeyPath<span style="color: #002200;">:</span>key<span style="color: #002200;">&#93;</span>;
        <span style="color: #400080;">NSExpression</span> <span style="color: #002200;">*</span>right <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSExpression</span> expressionForConstantValue<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>dict objectForKey<span style="color: #002200;">:</span>key<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
        <span style="color: #400080;">NSComparisonPredicate</span> <span style="color: #002200;">*</span>cp <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSComparisonPredicate</span> alloc<span style="color: #002200;">&#93;</span> initWithLeftExpression<span style="color: #002200;">:</span>left
            rightExpression<span style="color: #002200;">:</span>right modifier<span style="color: #002200;">:</span>NSDirectPredicateModifier type<span style="color: #002200;">:</span>NSEqualToPredicateOperatorType options<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#91;</span>arr addObject<span style="color: #002200;">:</span>cp<span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#91;</span>cp release<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
    <span style="color: #400080;">NSPredicate</span> <span style="color: #002200;">*</span>pred <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>arr.count <span style="color: #002200;">==</span> <span style="color: #2400d9;">1</span><span style="color: #002200;">&#41;</span>
        pred <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>arr 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>;    <span style="color: #11740a; font-style: italic;">// why do I have to retain here?</span>
    <span style="color: #a61390;">else</span>
        pred <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSCompoundPredicate</span> alloc<span style="color: #002200;">&#93;</span> initWithType<span style="color: #002200;">:</span>NSAndPredicateType subpredicates<span style="color: #002200;">:</span>arr<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #400080;">NSFetchRequest</span> <span style="color: #002200;">*</span>fr <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSFetchRequest</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
    fr.entity <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSEntityDescription</span> entityForName<span style="color: #002200;">:</span>entityName inManagedObjectContext<span style="color: #002200;">:</span>self<span style="color: #002200;">&#93;</span>;
    fr.predicate <span style="color: #002200;">=</span> pred;
    <span style="color: #002200;">&#91;</span>arr release<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>pred release<span style="color: #002200;">&#93;</span>;
<span style="color: #11740a; font-style: italic;">//	NSLog(@&quot;predicate effective: %@&quot;, fr.predicate);</span>
&nbsp;
    <span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span>err <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
    <span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>ps <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self executeFetchRequest<span style="color: #002200;">:</span>fr error<span style="color: #002200;">:&amp;</span>amp;err<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>err <span style="color: #002200;">!=</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span>
        <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSException</span> <span style="color: #a61390;">raise</span><span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;DB Error&quot;</span> format<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Fetch problem %@&quot;</span>, fr.predicate.predicateFormat<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>fr release<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">return</span> ps;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>For example it serves under the hood of it&#8217;s sibling helpers</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: #400080;">NSManagedObject</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>entityName<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>entityName findByPrimaryKey<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>dict
<span style="color: #002200;">&#123;</span>
    <span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>ps <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self entityName<span style="color: #002200;">:</span>entityName findManyByRelation<span style="color: #002200;">:</span>dict<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>ps <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span> || ps.count <span style="color: #002200;">==</span> <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>
        <span style="color: #a61390;">return</span> <span style="color: #a61390;">nil</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>ps.count <span style="color: #002200;">==</span> <span style="color: #2400d9;">1</span><span style="color: #002200;">&#41;</span>
        <span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>ps objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSException</span> <span style="color: #a61390;">raise</span><span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;DB Error&quot;</span> format<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Multiple hits for %@&quot;</span>, dict<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">return</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSManagedObject</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span><span style="color: #2400d9;">1</span><span style="color: #002200;">/</span><span style="color: #2400d9;">0</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>and</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: #400080;">NSManagedObject</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>entityName<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>entityName selectOrInsertWithKey<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>dict
<span style="color: #002200;">&#123;</span>
    <span style="color: #400080;">NSManagedObject</span> <span style="color: #002200;">*</span>o <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self entityName<span style="color: #002200;">:</span>entityName findByPrimaryKey<span style="color: #002200;">:</span>dict<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>o <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
        <span style="color: #400080;">NSEntityDescription</span> <span style="color: #002200;">*</span>ed <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSEntityDescription</span> entityForName<span style="color: #002200;">:</span>entityName inManagedObjectContext<span style="color: #002200;">:</span>self<span style="color: #002200;">&#93;</span>;
        o <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSManagedObject</span> alloc<span style="color: #002200;">&#93;</span> initWithEntity<span style="color: #002200;">:</span>ed insertIntoManagedObjectContext<span style="color: #002200;">:</span>self<span style="color: #002200;">&#93;</span>;
        <span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>key <span style="color: #a61390;">in</span> dict<span style="color: #002200;">&#41;</span>
            <span style="color: #002200;">&#91;</span>o setValue<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>dict objectForKey<span style="color: #002200;">:</span>key<span style="color: #002200;">&#93;</span> forKey<span style="color: #002200;">:</span>key<span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#91;</span>o autorelease<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
    <span style="color: #a61390;">return</span> o;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Those methods again are called by custom convenience wrappers in the <a href="http://developer.apple.com/iPhone/library/documentation/General/Conceptual/DevPedia-CocoaCore/MVC.html">model classes</a> loosely following the <a href="http://martinfowler.com/eaaCatalog/activeRecord.html">ActiveRecord Pattern</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mro.name/2009/12/coredata-generic-findmanybykey/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>NSDateFormatter case sensitive trap</title>
		<link>http://blog.mro.name/2009/12/nsdateformatter-case-sensitive-trap/</link>
		<comments>http://blog.mro.name/2009/12/nsdateformatter-case-sensitive-trap/#comments</comments>
		<pubDate>Thu, 03 Dec 2009 14:05:24 +0000</pubDate>
		<dc:creator>mro</dc:creator>
				<category><![CDATA[Articles in english]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[NSDateFormatter]]></category>
		<category><![CDATA[Objective C]]></category>
		<category><![CDATA[OS X]]></category>
		<category><![CDATA[SenTestingKit]]></category>
		<category><![CDATA[Unicode]]></category>

		<guid isPermaLink="false">http://blog.mro.name/?p=1427</guid>
		<description><![CDATA[Though NSDateFormatter behaves slightly different than documented, the following might even be correct, as strange as it might look (mind the last two lines): -&#40;void&#41;testNSDateFormatterTrap &#123; NSDateFormatter *lower = &#91;&#91;&#91;NSDateFormatter alloc&#93; init&#93; autorelease&#93;; lower.dateFormat = @&#34;yyyy-MM-dd HH:mm:SS ZZZ&#34;; &#160; NSDateFormatter *upper = &#91;&#91;&#91;NSDateFormatter alloc&#93; init&#93; autorelease&#93;; upper.dateFormat = @&#34;YYYY-MM-dd HH:mm:SS ZZZ&#34;; &#160; lower.timeZone = upper.timeZone [...]]]></description>
			<content:encoded><![CDATA[<p>Though <a href="http://blog.mro.name/2009/08/nsdateformatter-http-header/">NSDateFormatter behaves slightly different than documented</a>, the following might even be correct, as strange as it might look (mind the last two lines):</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;">void</span><span style="color: #002200;">&#41;</span>testNSDateFormatterTrap
<span style="color: #002200;">&#123;</span>
    <span style="color: #400080;">NSDateFormatter</span> <span style="color: #002200;">*</span>lower <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDateFormatter</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
    lower.dateFormat <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;yyyy-MM-dd HH:mm:SS ZZZ&quot;</span>;
&nbsp;
    <span style="color: #400080;">NSDateFormatter</span> <span style="color: #002200;">*</span>upper <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDateFormatter</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
    upper.dateFormat <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;YYYY-MM-dd HH:mm:SS ZZZ&quot;</span>;
&nbsp;
    lower.timeZone <span style="color: #002200;">=</span> upper.timeZone <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSTimeZone</span> timeZoneForSecondsFromGMT<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #400080;">NSDate</span> <span style="color: #002200;">*</span>d <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>lower dateFromString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;1970-01-01 00:00:00 +0000&quot;</span><span style="color: #002200;">&#93;</span>;
    STAssertEqualObjects<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;1970-01-01 00:00:00 +0000&quot;</span>, <span style="color: #002200;">&#91;</span>lower stringFromDate<span style="color: #002200;">:</span>d<span style="color: #002200;">&#93;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;lower iso wrong&quot;</span><span style="color: #002200;">&#41;</span>;
    STAssertEqualObjects<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;1970-01-01 00:00:00 +0000&quot;</span>, <span style="color: #002200;">&#91;</span>upper stringFromDate<span style="color: #002200;">:</span>d<span style="color: #002200;">&#93;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;upper iso wrong&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
    d <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>d addTimeInterval<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">-</span><span style="color: #2400d9;">60</span><span style="color: #002200;">*</span><span style="color: #2400d9;">60</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
    STAssertEqualObjects<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;1969-12-31 23:00:00 +0000&quot;</span>, <span style="color: #002200;">&#91;</span>lower stringFromDate<span style="color: #002200;">:</span>d<span style="color: #002200;">&#93;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;lower iso wrong&quot;</span><span style="color: #002200;">&#41;</span>;
    STAssertEqualObjects<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;1970-12-31 23:00:00 +0000&quot;</span>, <span style="color: #002200;">&#91;</span>upper stringFromDate<span style="color: #002200;">:</span>d<span style="color: #002200;">&#93;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;upper iso wrong&quot;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>The <a href="http://unicode.org/reports/tr35/tr35-6.html#Date_Field_Symbol_Table">Unicode Format Pattern Documentation</a> explains the difference of the upper- and lowercase year format &#8211; but frankly I don&#8217;t get the &#8220;Year of week of year&#8221; idea.</p>
<p>But that subtracting one hour in fact <strong>adds almost a whole year</strong> &#8211; that&#8217;s odd to me.</p>
<p>So I rather stay away from the uppercase form &#8211; be it correct or buggy.</p>
<p>Seen with iPhone SDK 3.1.2 and XCode 3.2.1 on Snow Leopard.</p>
<p><strong>Update:</strong></p>
<p>I think I got it! Uppercase YYYY makes sense only in combination with a calendar week &#8211; and not months or quarters.</p>
<p>Look at January 1st 2010. It belongs to calendar week 53 of 2009. Week 1/2010 starts on Jan 4th.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mro.name/2009/12/nsdateformatter-case-sensitive-trap/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CocoaTouch, CoreData and binary String Search</title>
		<link>http://blog.mro.name/2009/10/cocoatouch-coredata-and-binary-string-search/</link>
		<comments>http://blog.mro.name/2009/10/cocoatouch-coredata-and-binary-string-search/#comments</comments>
		<pubDate>Fri, 23 Oct 2009 18:27:59 +0000</pubDate>
		<dc:creator>mro</dc:creator>
				<category><![CDATA[Articles in english]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[Binary]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[CoreData]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[NSPredicate]]></category>
		<category><![CDATA[Objective C]]></category>
		<category><![CDATA[Search]]></category>

		<guid isPermaLink="false">http://blog.mro.name/?p=1321</guid>
		<description><![CDATA[The query optimiser for NSPredicate queries ontop CoreData/SQLite on the iPhone is a bit rudimentary (cough) and so I had to optimise myself to get binary-search enabled quick results: +&#40;NSPredicate*&#41;findBySearchTerm:&#40;NSString*&#41;rawTerm within:&#40;BOOL&#41;within context:&#40;NSManagedObjectContext*&#41;context &#123; NSSet *tokens = &#91;MovieM indexTokens:rawTerm&#93;; if&#40;tokens == nil &#124;&#124; tokens.count &#38;lt; = 0&#41; return &#91;NSPredicate predicateWithFormat:@&#34;FALSEPREDICATE&#34;&#93;; NSMutableArray *preds = &#91;NSMutableArray arrayWithCapacity:tokens.count&#93;; &#160; [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://en.wikipedia.org/wiki/Query_optimizer">query optimiser</a> for <a href="http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSPredicate_Class/Reference/NSPredicate.html">NSPredicate</a> queries ontop CoreData/SQLite on the iPhone is a bit rudimentary (cough) and so I had to optimise myself to get binary-search enabled quick results:</p>
<p><span id="more-1321"></span></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: #400080;">NSPredicate</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>findBySearchTerm<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>rawTerm within<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>within context<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSManagedObjectContext</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>context
<span style="color: #002200;">&#123;</span>
    <span style="color: #400080;">NSSet</span> <span style="color: #002200;">*</span>tokens <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>MovieM indexTokens<span style="color: #002200;">:</span>rawTerm<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>tokens <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span> || tokens.count <span style="color: #002200;">&amp;</span>lt; <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>
        <span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSPredicate</span> predicateWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;FALSEPREDICATE&quot;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span>preds <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableArray</span> arrayWithCapacity<span style="color: #002200;">:</span>tokens.count<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>within <span style="color: #002200;">==</span> <span style="color: #a61390;">NO</span> <span style="color: #002200;">&amp;</span>amp;<span style="color: #002200;">&amp;</span>amp; context <span style="color: #002200;">!=</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
	<span style="color: #11740a; font-style: italic;">// As queries aren't optimised by default we do it ourselves:</span>
	<span style="color: #11740a; font-style: italic;">// 1st: find matching entries from the IndexKey table - leveraging it's index:</span>
        <span style="color: #400080;">NSFetchRequest</span> <span style="color: #002200;">*</span>fr <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSFetchRequest</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
        fr.entity <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSEntityDescription</span> entityForName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;IndexKey&quot;</span> inManagedObjectContext<span style="color: #002200;">:</span>context<span style="color: #002200;">&#93;</span>;
        <span style="color: #400080;">NSMutableSet</span> <span style="color: #002200;">*</span>result <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
        <span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span>error <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
        <span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>token <span style="color: #a61390;">in</span> tokens<span style="color: #002200;">&#41;</span>
        <span style="color: #002200;">&#123;</span>
	    <span style="color: #11740a; font-style: italic;">// BETWEEN uses the table-index while BEGINSWITH does not:</span>
            fr.predicate <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSPredicate</span> predicateWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;key BETWEEN {%@, %@}&quot;</span>, token, <span style="color: #002200;">&#91;</span>MovieM upperBoundSearchString<span style="color: #002200;">:</span>token<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
            <span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>keys <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>context executeFetchRequest<span style="color: #002200;">:</span>fr error<span style="color: #002200;">:&amp;</span>amp;error<span style="color: #002200;">&#93;</span>;
            <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>error <span style="color: #002200;">!=</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span>
                NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Oops: %@&quot;</span>, error<span style="color: #002200;">&#41;</span>;
	    <span style="color: #11740a; font-style: italic;">// turn IndexKey entries to movies (join up):</span>
            <span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>movs <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>keys valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;movie&quot;</span><span style="color: #002200;">&#93;</span>;
	    <span style="color: #11740a; font-style: italic;">// aggregate the results for each token:</span>
            <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>result <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span>
                result <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableSet</span> setWithArray<span style="color: #002200;">:</span>movs<span style="color: #002200;">&#93;</span>;
            <span style="color: #a61390;">else</span>
                <span style="color: #002200;">&#91;</span>result intersectSet<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSSet</span> setWithArray<span style="color: #002200;">:</span>movs<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#125;</span>
        <span style="color: #002200;">&#91;</span>fr release<span style="color: #002200;">&#93;</span>;
        <span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSPredicate</span> predicateWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;SELF IN %@&quot;</span>, result<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #400080;">NSPredicate</span> <span style="color: #002200;">*</span>template <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>within<span style="color: #002200;">&#41;</span>
        template <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSPredicate</span> predicateWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;ANY index.key CONTAiNS $searchTerm&quot;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">else</span>
        template <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSPredicate</span> predicateWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;ANY index.key BEGINSWITH $searchTerm&quot;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>token <span style="color: #a61390;">in</span> tokens<span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
        <span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>params <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithObject<span style="color: #002200;">:</span>token forKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;searchTerm&quot;</span><span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#91;</span>preds addObject<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>template predicateWithSubstitutionVariables<span style="color: #002200;">:</span>params<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
    <span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSCompoundPredicate</span> andPredicateWithSubpredicates<span style="color: #002200;">:</span>preds<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Helpers herein are</p>
<ul>
<li><code>[MovieM indexTokens:rawTerm]</code> folds diacritics and uppercase and cuts at whitespace or interpunction,</li>
<li><code>[MovieM upperBoundSearchString:token]</code> which was inspired by <a href="http://developer.apple.com/mac/library/samplecode/DerivedProperty/listing8.html">Apple Sample Code &#8220;DerivedProperty&#8221;</a>.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.mro.name/2009/10/cocoatouch-coredata-and-binary-string-search/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cocoa wrapped regex.h</title>
		<link>http://blog.mro.name/2009/09/cocoa-wrapped-regexh/</link>
		<comments>http://blog.mro.name/2009/09/cocoa-wrapped-regexh/#comments</comments>
		<pubDate>Tue, 01 Sep 2009 08:55:51 +0000</pubDate>
		<dc:creator>mro</dc:creator>
				<category><![CDATA[Articles in english]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Objective C]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[YouTube]]></category>

		<guid isPermaLink="false">http://blog.mro.name/?p=1229</guid>
		<description><![CDATA[Strange enough there&#8217;s no regular expression class in the iPhone SDK. Update: iOS 4 brings NSRegularExpression. My simple wrapper around the regex.h C API is not safe for unicode matching patterns but does the job e.g. for parsing URLs. If you need more, have a look at RegexKitLite. My simple wrapper has the interface: // [...]]]></description>
			<content:encoded><![CDATA[<p>Strange enough there&#8217;s no regular expression class in the iPhone SDK.</p>
<p>Update: iOS 4 brings <code><a href="http://developer.apple.com/iphone/library/documentation/Foundation/Reference/NSRegularExpression_Class/Reference/Reference.html">NSRegularExpression</a></code>.</p>
<p>My simple wrapper around the <a href="http://developer.apple.com/iphone/library/documentation/System/Conceptual/ManPages_iPhoneOS/man3/regex.3.html">regex.h C API</a> is not safe for unicode matching patterns but does the job e.g. for parsing URLs. If you need more, have a look at <a href="http://regexkit.sourceforge.net/RegexKitLite/">RegexKitLite</a>. My simple wrapper has the interface:</p>
<p><span id="more-1229"></span></p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//  RegExp.h</span>
<span style="color: #11740a; font-style: italic;">//</span>
<span style="color: #11740a; font-style: italic;">//  Created by Marcus Rohrmoser on 26.08.09.</span>
<span style="color: #11740a; font-style: italic;">//  Copyright 2009 Marcus Rohrmoser. All rights reserved.</span>
&nbsp;
<span style="color: #6e371a;">#import &amp;lt; Foundation/Foundation.h &amp;gt;</span>
<span style="color: #6e371a;">#import &amp;lt; regex.h &amp;gt;</span>
&nbsp;
<span style="color: #6e371a;">#define REGEXP_NSERROR_DOMAIN   @&quot;regex.h&quot;</span>
<span style="color: #6e371a;">#define NSRegExpPatternKey      @&quot;NSRegExpPatternKey&quot;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">/** Very simple Objective-C Wrapper around the regex.h functions.
 */</span>
<span style="color: #a61390;">@interface</span> RegExp <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span>
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">@private</span>
    regex_t <span style="color: #002200;">*</span> c_regex;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">/** Compile a RegExp.
 @param pattern See regex.h / regcomp for details. ASCII encodeable characters only!
 @param error in case of trouble: REGEXP_NSERROR_DOMAIN, errocode from regcomp
 and userinfo NSLocalizedDescriptionKey and NSRegExpPatternKey.
 @return the regexp or nil in case of error.
 */</span>
<span style="color: #002200;">+</span><span style="color: #002200;">&#40;</span>RegExp<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>RegExpWithString<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>pattern error<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSError</span><span style="color: #002200;">**</span><span style="color: #002200;">&#41;</span>error;
&nbsp;
<span style="color: #11740a; font-style: italic;">/**
 Perform a match.
 @param string text to search
 @return nil (no match) or the complete match plus all matching groups.
 */</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>match<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><span style="color: #a61390;">string</span>;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>with implementation</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//  RegExp.m</span>
<span style="color: #11740a; font-style: italic;">//</span>
<span style="color: #11740a; font-style: italic;">//  Created by Marcus Rohrmoser on 26.08.09.</span>
<span style="color: #11740a; font-style: italic;">//  Copyright 2009 Marcus Rohrmoser. All rights reserved.</span>
&nbsp;
<span style="color: #6e371a;">#import &quot;RegExp.h&quot;</span>
&nbsp;
<span style="color: #6e371a;">#define ENCODING NSASCIIStringEncoding</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">/* Currently uses stringWithCString - which is bashed at
 + http://lists.apple.com/archives/cocoa-dev/2004/Nov/msg01643.html, but why?
 + What's wrong with it?
 + */</span>
<span style="color: #a61390;">@implementation</span> RegExp
&nbsp;
<span style="color: #6e371a;">#pragma mark Internal Helpers</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>initWithParsedPattern<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>regex_t<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>value_
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super init<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>
        c_regex <span style="color: #002200;">=</span> value_;
    <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: #400080;">NSError</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>errorFromRegExpError<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>regex_t<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>regex pattern<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>pattern code<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>code
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">const</span> <span style="color: #a61390;">size_t</span> errbuf_size <span style="color: #002200;">=</span> <span style="color: #2400d9;">1000</span>;
    <span style="color: #a61390;">char</span> <span style="color: #002200;">*</span> errbuf <span style="color: #002200;">=</span> <span style="color: #a61390;">calloc</span><span style="color: #002200;">&#40;</span>errbuf_size, <span style="color: #a61390;">sizeof</span><span style="color: #002200;">&#40;</span>errbuf<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
    regerror<span style="color: #002200;">&#40;</span>code, regex, errbuf, errbuf_size<span style="color: #002200;">&#41;</span>;
<span style="color: #11740a; font-style: italic;">//	regfree(regex);</span>
<span style="color: #11740a; font-style: italic;">//	free(regex);</span>
&nbsp;
    <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>msg <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithCString<span style="color: #002200;">:</span>errbuf encoding<span style="color: #002200;">:</span>NSASCIIStringEncoding<span style="color: #002200;">&#93;</span>;
    <span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span>error <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSError</span> errorWithDomain<span style="color: #002200;">:</span>REGEXP_NSERROR_DOMAIN code<span style="color: #002200;">:</span>code
        userInfo<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithObjectsAndKeys<span style="color: #002200;">:</span>
            pattern,  NSRegExpPatternKey,
            msg,  NSLocalizedDescriptionKey, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">free</span><span style="color: #002200;">&#40;</span>errbuf<span style="color: #002200;">&#41;</span>;
    <span style="color: #a61390;">return</span> error;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #6e371a;">#pragma mark Public Interface</span>
&nbsp;
<span style="color: #002200;">+</span><span style="color: #002200;">&#40;</span>RegExp<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>RegExpWithString<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>value_ error<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSError</span><span style="color: #002200;">**</span><span style="color: #002200;">&#41;</span>error
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>value_ <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span>
        <span style="color: #a61390;">return</span> <span style="color: #a61390;">nil</span>;
    <span style="color: #a61390;">const</span> <span style="color: #a61390;">char</span> <span style="color: #002200;">*</span> pat_c <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>value_ cStringUsingEncoding<span style="color: #002200;">:</span>ENCODING<span style="color: #002200;">&#93;</span>;
    regex_t <span style="color: #002200;">*</span> regex <span style="color: #002200;">=</span> <span style="color: #a61390;">calloc</span><span style="color: #002200;">&#40;</span><span style="color: #2400d9;">1</span>, <span style="color: #a61390;">sizeof</span><span style="color: #002200;">&#40;</span>regex_t<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
    <span style="color: #a61390;">const</span> <span style="color: #a61390;">int</span> ec <span style="color: #002200;">=</span> regcomp<span style="color: #002200;">&#40;</span>regex, pat_c, REG_EXTENDED<span style="color: #002200;">&#41;</span>;
    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>ec <span style="color: #002200;">!=</span> <span style="color: #2400d9;">0</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>error <span style="color: #002200;">!=</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span>
            <span style="color: #002200;">*</span>error <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>RegExp errorFromRegExpError<span style="color: #002200;">:</span>regex pattern<span style="color: #002200;">:</span>value_ code<span style="color: #002200;">:</span>ec<span style="color: #002200;">&#93;</span>;
        regfree<span style="color: #002200;">&#40;</span>regex<span style="color: #002200;">&#41;</span>;
        <span style="color: #a61390;">free</span><span style="color: #002200;">&#40;</span>regex<span style="color: #002200;">&#41;</span>;
        <span style="color: #a61390;">return</span> <span style="color: #a61390;">nil</span>;
    <span style="color: #002200;">&#125;</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>RegExp alloc<span style="color: #002200;">&#93;</span> initWithParsedPattern<span style="color: #002200;">:</span>regex<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: #400080;">NSArray</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>match<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><span style="color: #a61390;">string</span>
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">string</span> <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span>
        <span style="color: #a61390;">return</span> <span style="color: #a61390;">nil</span>;
    <span style="color: #a61390;">const</span> <span style="color: #a61390;">char</span> <span style="color: #002200;">*</span> c_str <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #a61390;">string</span> cStringUsingEncoding<span style="color: #002200;">:</span>ENCODING<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>c_str <span style="color: #002200;">==</span> <span style="color: #a61390;">NULL</span><span style="color: #002200;">&#41;</span>
        <span style="color: #a61390;">return</span> <span style="color: #a61390;">nil</span>;
    <span style="color: #a61390;">const</span> <span style="color: #a61390;">int</span> count <span style="color: #002200;">=</span> <span style="color: #2400d9;">1</span> <span style="color: #002200;">+</span> c_regex<span style="color: #002200;">-&amp;</span>gt;re_nsub;
    regmatch_t match<span style="color: #002200;">&#91;</span>count<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">const</span> <span style="color: #a61390;">int</span> ec <span style="color: #002200;">=</span> regexec<span style="color: #002200;">&#40;</span>c_regex, c_str, count, match, <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>;
    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>ec <span style="color: #002200;">!=</span> <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>
        <span style="color: #a61390;">return</span> <span style="color: #a61390;">nil</span>;
    <span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span>arr <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableArray</span> arrayWithCapacity<span style="color: #002200;">:</span>count<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> i <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; i <span style="color: #002200;">&amp;</span>lt; count; i<span style="color: #002200;">++</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>match<span style="color: #002200;">&#91;</span>i<span style="color: #002200;">&#93;</span>.rm_so <span style="color: #002200;">==</span> <span style="color: #002200;">-</span><span style="color: #2400d9;">1</span> <span style="color: #002200;">&amp;</span>amp;<span style="color: #002200;">&amp;</span>amp; match<span style="color: #002200;">&#91;</span>i<span style="color: #002200;">&#93;</span>.rm_eo <span style="color: #002200;">==</span> <span style="color: #002200;">-</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#41;</span>
            <span style="color: #002200;">&#91;</span>arr addObject<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNull</span> null<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
        <span style="color: #a61390;">else</span>
        <span style="color: #002200;">&#123;</span>
            <span style="color: #a61390;">const</span> <span style="color: #a61390;">char</span> <span style="color: #002200;">*</span> start <span style="color: #002200;">=</span> <span style="color: #002200;">&amp;</span>amp;c_str<span style="color: #002200;">&#91;</span> match<span style="color: #002200;">&#91;</span>i<span style="color: #002200;">&#93;</span>.rm_so <span style="color: #002200;">&#93;</span>;
            <span style="color: #a61390;">const</span> <span style="color: #a61390;">int</span> length <span style="color: #002200;">=</span> match<span style="color: #002200;">&#91;</span>i<span style="color: #002200;">&#93;</span>.rm_eo <span style="color: #002200;">-</span> match<span style="color: #002200;">&#91;</span>i<span style="color: #002200;">&#93;</span>.rm_so;
            <span style="color: #002200;">&#91;</span>arr addObject<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> alloc<span style="color: #002200;">&#93;</span> initWithBytes<span style="color: #002200;">:</span>start length<span style="color: #002200;">:</span>length encoding<span style="color: #002200;">:</span>ENCODING<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#125;</span>
    <span style="color: #002200;">&#125;</span>
    <span style="color: #a61390;">return</span> arr;
<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>
    regfree<span style="color: #002200;">&#40;</span>c_regex<span style="color: #002200;">&#41;</span>;
    <span style="color: #a61390;">free</span><span style="color: #002200;">&#40;</span>c_regex<span style="color: #002200;">&#41;</span>;
    <span style="color: #002200;">&#91;</span>super dealloc<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@end</span></pre></div></div>

<p>and an accompanying testcase</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//  RegExpTC.m</span>
<span style="color: #11740a; font-style: italic;">//</span>
<span style="color: #11740a; font-style: italic;">//  Created by Marcus Rohrmoser on 26.08.09.</span>
<span style="color: #11740a; font-style: italic;">//  Copyright 2009 Marcus Rohrmoser. All rights reserved.</span>
&nbsp;
<span style="color: #6e371a;">#include &quot;TargetConditionals.h&quot;</span>
<span style="color: #6e371a;">#if !TARGET_IPHONE_SIMULATOR</span>
<span style="color: #6e371a;">#warning TestCase ignored when not building for Simulator</span>
<span style="color: #6e371a;">#else</span>
&nbsp;
<span style="color: #6e371a;">#import &quot;../Classes/RegExp.h&quot;</span>
&nbsp;
<span style="color: #6e371a;">#import &amp;lt; SenTestingKit/SenTestingKit.h &amp;gt;</span>
&nbsp;
<span style="color: #a61390;">@interface</span> RegExpTC <span style="color: #002200;">:</span> SenTestCase
<span style="color: #002200;">&#123;</span>
&nbsp;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> RegExpTC
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>testYouTube
<span style="color: #002200;">&#123;</span>
    <span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span>error <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
    RegExp <span style="color: #002200;">*</span>pat <span style="color: #002200;">=</span>
<span style="color: #002200;">&#91;</span>RegExp RegExpWithString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;http.+<span style="color: #2400d9;">\.</span>youtube<span style="color: #2400d9;">\.</span>com/(v/|watch<span style="color: #2400d9;">\?</span>v=)([A-Za-z0-9._%-]+)&quot;</span> error<span style="color: #002200;">:&amp;</span>amp;error<span style="color: #002200;">&#93;</span>;
    STAssertNil<span style="color: #002200;">&#40;</span>error, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;pattern compile&quot;</span><span style="color: #002200;">&#41;</span>;
    STAssertNotNil<span style="color: #002200;">&#40;</span>pat, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;pattern compile&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
    <span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>match <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>pat match<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;http://www.youtube.com/v/61wkfmWzLq4&amp;amp;hl=de
&amp;amp;fs=1&amp;amp;rel=0&amp;amp;color1=0x3a3a3a&amp;amp;color2=0x999999&amp;amp;hd=1&amp;amp;border=1&quot;</span><span style="color: #002200;">&#93;</span>;
    STAssertNotNil<span style="color: #002200;">&#40;</span>match, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;must match&quot;</span><span style="color: #002200;">&#41;</span>;
    STAssertEquals<span style="color: #002200;">&#40;</span>3u, match.count, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;one matching subexpression&quot;</span><span style="color: #002200;">&#41;</span>;
    STAssertEqualObjects<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;v/&quot;</span>, <span style="color: #002200;">&#91;</span>match objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;snippet&quot;</span><span style="color: #002200;">&#41;</span>;
    STAssertEqualObjects<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;61wkfmWzLq4&quot;</span>, <span style="color: #002200;">&#91;</span>match objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">2</span><span style="color: #002200;">&#93;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;snippet&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// /youtube.com/watch?v=([A-Za-z0-9._%-]*)[&amp;amp;w;=+_-]*/</span>
&nbsp;
    match <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>pat match<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;http://www.youtube.com/watch?v=61wkfmWzLq4&quot;</span><span style="color: #002200;">&#93;</span>;
    STAssertNotNil<span style="color: #002200;">&#40;</span>match, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;must match&quot;</span><span style="color: #002200;">&#41;</span>;
    STAssertEquals<span style="color: #002200;">&#40;</span>3u, match.count, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;one matching subexpression&quot;</span><span style="color: #002200;">&#41;</span>;
    STAssertEqualObjects<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;watch?v=&quot;</span>, <span style="color: #002200;">&#91;</span>match objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;snippet&quot;</span><span style="color: #002200;">&#41;</span>;
    STAssertEqualObjects<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;61wkfmWzLq4&quot;</span>, <span style="color: #002200;">&#91;</span>match objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">2</span><span style="color: #002200;">&#93;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;snippet&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
    match <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>pat match<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;http://www.youtube.com/v/61wkfmWzLq4&amp;amp;hl=de&amp;amp;fs=1&amp;amp;&quot;</span><span style="color: #002200;">&#93;</span>;
    STAssertNotNil<span style="color: #002200;">&#40;</span>match, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;must match&quot;</span><span style="color: #002200;">&#41;</span>;
    STAssertEqualObjects<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;v/&quot;</span>, <span style="color: #002200;">&#91;</span>match objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;snippet&quot;</span><span style="color: #002200;">&#41;</span>;
    STAssertEqualObjects<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;61wkfmWzLq4&quot;</span>, <span style="color: #002200;">&#91;</span>match objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">2</span><span style="color: #002200;">&#93;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;snippet&quot;</span><span style="color: #002200;">&#41;</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>testCocoaOk
<span style="color: #002200;">&#123;</span>
    <span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span>error <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
&nbsp;
    STAssertNil<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>RegExp RegExpWithString<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> error<span style="color: #002200;">:&amp;</span>amp;error<span style="color: #002200;">&#93;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;pattern nil&quot;</span><span style="color: #002200;">&#41;</span>;
    STAssertNil<span style="color: #002200;">&#40;</span>error, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;pattern must compile ok&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
    RegExp <span style="color: #002200;">*</span>reg <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>RegExp RegExpWithString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;http.+<span style="color: #2400d9;">\.</span>youtube<span style="color: #2400d9;">\.</span>com/v/([^<span style="color: #2400d9;">\&amp;</span>amp;]+)<span style="color: #2400d9;">\&amp;</span>amp;.*&quot;</span>
error<span style="color: #002200;">:&amp;</span>amp;error<span style="color: #002200;">&#93;</span>;
    STAssertNil<span style="color: #002200;">&#40;</span>error, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;pattern must compile ok&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
    <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>str <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;http://www.youtube.com/v/61wkfmWzLq4&amp;amp;hl=de&amp;amp;fs=1
&amp;amp;rel=0&amp;amp;color1=0x3a3a3a&amp;amp;color2=0x999999&amp;amp;hd=1&amp;amp;border=1&quot;</span>;
    <span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span> match <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>reg match<span style="color: #002200;">:</span>str<span style="color: #002200;">&#93;</span>;
    STAssertNotNil<span style="color: #002200;">&#40;</span>match, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;must match&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
    STAssertEquals<span style="color: #002200;">&#40;</span>2u, match.count, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;two matches expected&quot;</span><span style="color: #002200;">&#41;</span>;
    STAssertEqualObjects<span style="color: #002200;">&#40;</span>str, <span style="color: #002200;">&#91;</span>match objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;complete match&quot;</span><span style="color: #002200;">&#41;</span>;
    STAssertEqualObjects<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;61wkfmWzLq4&quot;</span>, <span style="color: #002200;">&#91;</span>match objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;first matching group&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
    STAssertNil<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>reg match<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;must not match&quot;</span><span style="color: #002200;">&#41;</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>testCocoaFail
<span style="color: #002200;">&#123;</span>
    <span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span>error <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
    RegExp <span style="color: #002200;">*</span>reg <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>RegExp RegExpWithString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;&quot;</span> error<span style="color: #002200;">:&amp;</span>amp;error<span style="color: #002200;">&#93;</span>;
    STAssertNil<span style="color: #002200;">&#40;</span>reg, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;pattern must fail&quot;</span><span style="color: #002200;">&#41;</span>;
    STAssertNotNil<span style="color: #002200;">&#40;</span>error, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;pattern must fail&quot;</span><span style="color: #002200;">&#41;</span>;
    STAssertEqualObjects<span style="color: #002200;">&#40;</span>REGEXP_NSERROR_DOMAIN, error.domain, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;domain check&quot;</span><span style="color: #002200;">&#41;</span>;
    STAssertEquals<span style="color: #002200;">&#40;</span>REG_EMPTY, error.code, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;code check&quot;</span><span style="color: #002200;">&#41;</span>;
    STAssertEqualObjects<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;empty (sub)expression&quot;</span>, error.localizedDescription, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;description check&quot;</span><span style="color: #002200;">&#41;</span>;
    STAssertEqualObjects<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;&quot;</span>, <span style="color: #002200;">&#91;</span>error.userInfo valueForKey<span style="color: #002200;">:</span>NSRegExpPatternKey<span style="color: #002200;">&#93;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;pattern info&quot;</span><span style="color: #002200;">&#41;</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>testRaw
<span style="color: #002200;">&#123;</span>
    regex_t regex;
    <span style="color: #a61390;">const</span> <span style="color: #a61390;">char</span> <span style="color: #002200;">*</span> pattern <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">&quot;http.+<span style="color: #2400d9;">\.</span>youtube<span style="color: #2400d9;">\.</span>com/v/([^<span style="color: #2400d9;">\&amp;</span>amp;]+)<span style="color: #2400d9;">\&amp;</span>amp;.*&quot;</span>;
    STAssertTrue<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span> <span style="color: #002200;">==</span> regcomp<span style="color: #002200;">&#40;</span><span style="color: #002200;">&amp;</span>amp;regex, pattern, REG_EXTENDED<span style="color: #002200;">&#41;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;RegExp compile&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
    regmatch_t <span style="color: #002200;">*</span> match <span style="color: #002200;">=</span> <span style="color: #a61390;">calloc</span><span style="color: #002200;">&#40;</span><span style="color: #2400d9;">1</span> <span style="color: #002200;">+</span> regex.re_nsub, <span style="color: #a61390;">sizeof</span><span style="color: #002200;">&#40;</span>regmatch_t<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
    <span style="color: #a61390;">const</span> <span style="color: #a61390;">char</span> <span style="color: #002200;">*</span> <span style="color: #a61390;">string</span> <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">&quot;http://www.youtube.com/v/61wkfmWzLq4&amp;amp;hl=de&amp;amp;fs=1&amp;amp;rel=0
&amp;amp;color1=0x3a3a3a&amp;amp;color2=0x999999&amp;amp;hd=1&amp;amp;border=1&quot;</span>;
&nbsp;
    STAssertTrue<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span> <span style="color: #002200;">==</span> regexec<span style="color: #002200;">&#40;</span><span style="color: #002200;">&amp;</span>amp;regex, <span style="color: #a61390;">string</span>, <span style="color: #2400d9;">1</span> <span style="color: #002200;">+</span> regex.re_nsub, match, <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;RegExp Match&quot;</span><span style="color: #002200;">&#41;</span>;
    STAssertTrue<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">1</span> <span style="color: #002200;">==</span> regex.re_nsub, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Matching groups count&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
    STAssertTrue<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span> <span style="color: #002200;">==</span> match<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span>.rm_so, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Match 0 start&quot;</span><span style="color: #002200;">&#41;</span>;
    STAssertTrue<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">127</span> <span style="color: #002200;">==</span> match<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span>.rm_eo, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Match 0 start&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
    STAssertTrue<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">25</span> <span style="color: #002200;">==</span> match<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span>.rm_so, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Match 1 start&quot;</span><span style="color: #002200;">&#41;</span>;
    STAssertTrue<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">36</span> <span style="color: #002200;">==</span> match<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span>.rm_eo, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Match 1 start&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
    <span style="color: #a61390;">free</span><span style="color: #002200;">&#40;</span>match<span style="color: #002200;">&#41;</span>;
    regfree<span style="color: #002200;">&#40;</span><span style="color: #002200;">&amp;</span>amp;regex<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span>
<span style="color: #6e371a;">#endif</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.mro.name/2009/09/cocoa-wrapped-regexh/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>NSDateFormatter &amp; Http Header</title>
		<link>http://blog.mro.name/2009/08/nsdateformatter-http-header/</link>
		<comments>http://blog.mro.name/2009/08/nsdateformatter-http-header/#comments</comments>
		<pubDate>Tue, 18 Aug 2009 18:53:02 +0000</pubDate>
		<dc:creator>mro</dc:creator>
				<category><![CDATA[Articles in english]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[NSDate]]></category>
		<category><![CDATA[NSDateFormatter]]></category>
		<category><![CDATA[NSHTTPURLResponse]]></category>
		<category><![CDATA[Objective C]]></category>
		<category><![CDATA[RFC1123]]></category>
		<category><![CDATA[SenTestingKit]]></category>

		<guid isPermaLink="false">http://blog.mro.name/?p=1210</guid>
		<description><![CDATA[Ever tried to get e.g. the &#8220;Last-Modified&#8221; HTTP response header field into a NSDate object? That&#8217;s no real fun, because this standard formatting isn&#8217;t digested by default, the required format string doesn&#8217;t quite work as documented. Let&#8217;s concentrate on &#8220;Full Date&#8221; according to RFC 1123: NSString *src = @&#34;Fri, 14 Aug 2009 14:45:31 GMT&#34;; The [...]]]></description>
			<content:encoded><![CDATA[<p>Ever tried to get e.g. the <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.29">&#8220;Last-Modified&#8221; HTTP response header field</a> into a <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/Reference/Reference.html">NSDate</a> object? That&#8217;s no real fun, because</p>
<ol>
<li>this standard formatting isn&#8217;t <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/Reference/Reference.html#//apple_ref/doc/uid/20000188-dateWithString_">digested by default</a>,</li>
<li>the required format string doesn&#8217;t quite work as documented.</li>
</ol>
<p><span id="more-1210"></span><br />
Let&#8217;s concentrate on <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3.1">&#8220;Full Date&#8221; according to RFC 1123</a>:</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>src <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Fri, 14 Aug 2009 14:45:31 GMT&quot;</span>;</pre></div></div>

<p>The <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html#//apple_ref/doc/uid/20000447-SW32">NSDateFormatter</a> switched to <a href="http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns">Unicode Format strings</a> with OSX 10.4 (<a href="http://developer.apple.com/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html#//apple_ref/doc/uid/TP40002369-SW5">see the reference</a>), so I use</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSDateFormatter</span> <span style="color: #002200;">*</span>df <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDateFormatter</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
df.timeZone <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSTimeZone</span> timeZoneWithAbbreviation<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;GMT&quot;</span><span style="color: #002200;">&#93;</span>;
df.dateFormat <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;EEE',' dd MMM yyyy HH':'mm':'ss 'GMT'&quot;</span>;</pre></div></div>

<p><strong>Caution</strong>: &#8216;<code>zzz</code>&#8216; as timezone format should give the same results, but doesn&#8217;t. It says <code>...GMT+00:00</code>.</p>
<p>As the textual parts are in english, I add</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">df.locale <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSLocale</span> alloc<span style="color: #002200;">&#93;</span> initWithLocaleIdentifier<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;en_US&quot;</span><span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>to be independent of the user&#8217;s current locale.</p>
<p>That&#8217;s it!</p>
<p><strong>Update:</strong></p>
<p>If <code>[NSDateFormatter dateFromString:...]</code> and <code>[NSDateFormatter stringFromDate:...]</code> are threadsafe, a category-implementation could look like:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import </span>
&nbsp;
<span style="color: #11740a; font-style: italic;">/** Category on NSDate to support rfc1123 formatted date strings.
 http://blog.mro.name/2009/08/nsdateformatter-http-header/ and
 http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3.1
 */</span>
<span style="color: #a61390;">@interface</span> <span style="color: #400080;">NSDate</span> <span style="color: #002200;">&#40;</span>NSDateRFC1123<span style="color: #002200;">&#41;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">/**
 Convert a RFC1123 'Full-Date' string
 (http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3.1)
 into NSDate.
 */</span>
<span style="color: #002200;">+</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDate</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>dateFromRFC1123<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>value_;
&nbsp;
<span style="color: #11740a; font-style: italic;">/**
 Convert NSDate into a RFC1123 'Full-Date' string
 (http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3.1).
 */</span>
<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>rfc1123String;
&nbsp;
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> <span style="color: #400080;">NSDate</span> <span style="color: #002200;">&#40;</span>NSDateRFC1123<span style="color: #002200;">&#41;</span>
&nbsp;
<span style="color: #002200;">+</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDate</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>dateFromRFC1123<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>value_
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>value_ <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span>
        <span style="color: #a61390;">return</span> <span style="color: #a61390;">nil</span>;
    <span style="color: #a61390;">static</span> <span style="color: #400080;">NSDateFormatter</span> <span style="color: #002200;">*</span>rfc1123 <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>rfc1123 <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
        rfc1123 <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDateFormatter</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
        rfc1123.locale <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSLocale</span> alloc<span style="color: #002200;">&#93;</span> initWithLocaleIdentifier<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;en_US&quot;</span><span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
        rfc1123.timeZone <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSTimeZone</span> timeZoneWithAbbreviation<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;GMT&quot;</span><span style="color: #002200;">&#93;</span>;
        rfc1123.dateFormat <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;EEE',' dd MMM yyyy HH':'mm':'ss z&quot;</span>;
    <span style="color: #002200;">&#125;</span>
    <span style="color: #400080;">NSDate</span> <span style="color: #002200;">*</span>ret <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>rfc1123 dateFromString<span style="color: #002200;">:</span>value_<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>ret <span style="color: #002200;">!=</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span>
        <span style="color: #a61390;">return</span> ret;
&nbsp;
    <span style="color: #a61390;">static</span> <span style="color: #400080;">NSDateFormatter</span> <span style="color: #002200;">*</span>rfc850 <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>rfc850 <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
        rfc850 <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDateFormatter</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
        rfc850.locale <span style="color: #002200;">=</span> rfc1123.locale;
        rfc850.timeZone <span style="color: #002200;">=</span> rfc1123.timeZone;
        rfc850.dateFormat <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;EEEE',' dd'-'MMM'-'yy HH':'mm':'ss z&quot;</span>;
    <span style="color: #002200;">&#125;</span>
    ret <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>rfc850 dateFromString<span style="color: #002200;">:</span>value_<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>ret <span style="color: #002200;">!=</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span>
        <span style="color: #a61390;">return</span> ret;
&nbsp;
    <span style="color: #a61390;">static</span> <span style="color: #400080;">NSDateFormatter</span> <span style="color: #002200;">*</span><span style="color: #a61390;">asctime</span> <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: #a61390;">asctime</span> <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
        <span style="color: #a61390;">asctime</span> <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDateFormatter</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
        <span style="color: #a61390;">asctime</span>.locale <span style="color: #002200;">=</span> rfc1123.locale;
        <span style="color: #a61390;">asctime</span>.timeZone <span style="color: #002200;">=</span> rfc1123.timeZone;
        <span style="color: #a61390;">asctime</span>.dateFormat <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;EEE MMM d HH':'mm':'ss yyyy&quot;</span>;
    <span style="color: #002200;">&#125;</span>
    <span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span><span style="color: #a61390;">asctime</span> dateFromString<span style="color: #002200;">:</span>value_<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: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>rfc1123String
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">static</span> <span style="color: #400080;">NSDateFormatter</span> <span style="color: #002200;">*</span>df <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>df <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
        df <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDateFormatter</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
        df.locale <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSLocale</span> alloc<span style="color: #002200;">&#93;</span> initWithLocaleIdentifier<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;en_US&quot;</span><span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
        df.timeZone <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSTimeZone</span> timeZoneWithAbbreviation<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;GMT&quot;</span><span style="color: #002200;">&#93;</span>;
        df.dateFormat <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;EEE',' dd MMM yyyy HH':'mm':'ss 'GMT'&quot;</span>;
    <span style="color: #002200;">&#125;</span>
    <span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>df stringFromDate<span style="color: #002200;">:</span>self<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">//////////////////////////////////////////////////////////</span>
<span style="color: #11740a; font-style: italic;">// A testcase to try things out.</span>
<span style="color: #11740a; font-style: italic;">//////////////////////////////////////////////////////////</span>
&nbsp;
<span style="color: #6e371a;">#import </span>
&nbsp;
<span style="color: #11740a; font-style: italic;">/**
 http://blog.mro.name/2009/08/nsdateformatter-http-header/ and
 http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3.1
 */</span>
<span style="color: #a61390;">@interface</span> NSDateFormatterTC <span style="color: #002200;">:</span> SenTestCase
<span style="color: #002200;">&#123;</span>
&nbsp;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> NSDateFormatterTC
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>testAsctime
<span style="color: #002200;">&#123;</span>
    STAssertEqualObjects<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Sun, 06 Nov 1994 08:49:37 GMT&quot;</span>,
        <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDate</span> dateFromRFC1123<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Sun Nov  6 08:49:37 1994&quot;</span><span style="color: #002200;">&#93;</span> rfc1123String<span style="color: #002200;">&#93;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;fail&quot;</span><span style="color: #002200;">&#41;</span>;
    STAssertEqualObjects<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Wed, 16 Nov 1994 08:49:37 GMT&quot;</span>,
        <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDate</span> dateFromRFC1123<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Wed Nov 16 08:49:37 1994&quot;</span><span style="color: #002200;">&#93;</span> rfc1123String<span style="color: #002200;">&#93;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;fail&quot;</span><span style="color: #002200;">&#41;</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>testRFC850
<span style="color: #002200;">&#123;</span>
    STAssertEqualObjects<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Sun, 06 Nov 1994 08:49:37 GMT&quot;</span>,
        <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDate</span> dateFromRFC1123<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Sunday, 06-Nov-94 08:49:37 GMT&quot;</span><span style="color: #002200;">&#93;</span> rfc1123String<span style="color: #002200;">&#93;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;fail&quot;</span><span style="color: #002200;">&#41;</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>testRFC1123
<span style="color: #002200;">&#123;</span>
    <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;Fri, 14 Aug 2009 14:45:31 GMT&quot;</span>;
    STAssertEqualObjects<span style="color: #002200;">&#40;</span>s, <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDate</span> dateFromRFC1123<span style="color: #002200;">:</span>s<span style="color: #002200;">&#93;</span> rfc1123String<span style="color: #002200;">&#93;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;fail&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
    s <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Sun, 06 Nov 1994 08:49:37 GMT&quot;</span>;
    STAssertEqualObjects<span style="color: #002200;">&#40;</span>s, <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDate</span> dateFromRFC1123<span style="color: #002200;">:</span>s<span style="color: #002200;">&#93;</span> rfc1123String<span style="color: #002200;">&#93;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;fail&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// a strange asymmetry:</span>
    s <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Sat, 01 Jan 0001 00:00:00 GMT&quot;</span>;
    STAssertEqualObjects<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Sat, 01 Jan 0001 01:27:48 GMT&quot;</span>,
        <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDate</span> dateFromRFC1123<span style="color: #002200;">:</span>s<span style="color: #002200;">&#93;</span> rfc1123String<span style="color: #002200;">&#93;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;fail&quot;</span><span style="color: #002200;">&#41;</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>testRawRFC1123
<span style="color: #002200;">&#123;</span>
    <span style="color: #400080;">NSDateFormatter</span> <span style="color: #002200;">*</span>rfc1123 <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDateFormatter</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
    rfc1123.locale <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSLocale</span> alloc<span style="color: #002200;">&#93;</span> initWithLocaleIdentifier<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;en_US&quot;</span><span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
    rfc1123.timeZone <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSTimeZone</span> timeZoneWithAbbreviation<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;GMT&quot;</span><span style="color: #002200;">&#93;</span>;
    rfc1123.dateFormat <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;EEE',' dd MMM yyyy HH':'mm':'ss 'GMT'&quot;</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;Fri, 14 Aug 2009 14:45:31 GMT&quot;</span>;
    STAssertEqualObjects<span style="color: #002200;">&#40;</span>s, <span style="color: #002200;">&#91;</span>rfc1123 stringFromDate<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>rfc1123 dateFromString<span style="color: #002200;">:</span>s<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;fail&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
    s <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Sun, 06 Nov 1994 08:49:37 GMT&quot;</span>;
    STAssertEqualObjects<span style="color: #002200;">&#40;</span>s, <span style="color: #002200;">&#91;</span>rfc1123 stringFromDate<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>rfc1123 dateFromString<span style="color: #002200;">:</span>s<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;fail&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
    s <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Sat, 01 Jan 0001 08:00:00 GMT&quot;</span>;
    STAssertEqualObjects<span style="color: #002200;">&#40;</span>s, <span style="color: #002200;">&#91;</span>rfc1123 stringFromDate<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>rfc1123 dateFromString<span style="color: #002200;">:</span>s<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;fail&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
    <span style="color: #002200;">&#91;</span>rfc1123 release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@end</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.mro.name/2009/08/nsdateformatter-http-header/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Unit Testing / iPhone</title>
		<link>http://blog.mro.name/2009/07/unit-testing-iphone/</link>
		<comments>http://blog.mro.name/2009/07/unit-testing-iphone/#comments</comments>
		<pubDate>Sun, 26 Jul 2009 13:39:44 +0000</pubDate>
		<dc:creator>mro</dc:creator>
				<category><![CDATA[Articles in english]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Objective C]]></category>
		<category><![CDATA[OCUnit]]></category>
		<category><![CDATA[SenTestingKit]]></category>
		<category><![CDATA[Unit Test]]></category>
		<category><![CDATA[XCode]]></category>

		<guid isPermaLink="false">http://blog.mro.name/?p=1194</guid>
		<description><![CDATA[Having Eclipse &#38; JUnit in mind I missed unit testing quite a bit while developing with XCode for iPhone. As a first shot, I set SenTestingKit up as explained by it&#8217;s author and it works really nicely. One thing I still miss is Step&#8217;n'Trace debugging the tests. Other intros to the topic from Apple about [...]]]></description>
			<content:encoded><![CDATA[<p>Having Eclipse &amp; JUnit in mind I missed unit testing quite a bit while developing with XCode for iPhone.</p>
<p>As a first shot, I <a href="http://www.sente.ch/s/?p=535&amp;lang=en">set SenTestingKit up as explained by it&#8217;s author</a> and it works really nicely. One thing I still miss is Step&#8217;n'Trace debugging the tests.</p>
<p>Other intros to the topic from Apple about <a href="http://developer.apple.com/tools/unittest.html">OCUnit (2005)</a> and <a href="http://developer.apple.com/documentation/developertools/Conceptual/UnitTesting/Articles/CreatingTests.html#//apple_ref/doc/uid/TP40002171-125425">SenTestingKit (2008)</a> are ok but not as good as from the author of SenTestingKit.</p>
<p>Didn&#8217;t examine other Unit Test frameworks like e.g. the <a href="http://code.google.com/p/google-toolbox-for-mac/wiki/iPhoneUnitTesting">one from Google</a> yet.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mro.name/2009/07/unit-testing-iphone/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>NSURLCache Joke / iPhone</title>
		<link>http://blog.mro.name/2009/07/nsurlcache-joke-iphone/</link>
		<comments>http://blog.mro.name/2009/07/nsurlcache-joke-iphone/#comments</comments>
		<pubDate>Tue, 07 Jul 2009 12:08:18 +0000</pubDate>
		<dc:creator>mro</dc:creator>
				<category><![CDATA[Articles in english]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[NSURLCache]]></category>
		<category><![CDATA[Objective C]]></category>

		<guid isPermaLink="false">http://blog.mro.name/2009/07/nsurlcache-joke-iphone/</guid>
		<description><![CDATA[Did you ever wonder why Apple&#8217;s own Demo App URLCache doesn&#8217;t use the NSURLCache class, but rather reimplements disk caching instead? Well, it looks like NSURLCache promises disk-caching, but doesn&#8217;t keep this promise.]]></description>
			<content:encoded><![CDATA[<p>Did you ever wonder why Apple&#8217;s own Demo App <a href="https://developer.apple.com/iphone/library/samplecode/URLCache/index.html">URLCache</a> doesn&#8217;t use the <a href="http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSURLCache_Class/Reference/Reference.html">NSURLCache</a> class, but rather reimplements disk caching instead? Well, it looks like NSURLCache promises disk-caching, but doesn&#8217;t keep this promise.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mro.name/2009/07/nsurlcache-joke-iphone/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>NSURLConnection gzip magic</title>
		<link>http://blog.mro.name/2009/06/nsurlconnection-gzip-magic/</link>
		<comments>http://blog.mro.name/2009/06/nsurlconnection-gzip-magic/#comments</comments>
		<pubDate>Sun, 28 Jun 2009 13:12:31 +0000</pubDate>
		<dc:creator>mro</dc:creator>
				<category><![CDATA[Articles in english]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[compression]]></category>
		<category><![CDATA[gzip]]></category>
		<category><![CDATA[htaccess]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[NSURLConnection]]></category>
		<category><![CDATA[Objective C]]></category>
		<category><![CDATA[wget]]></category>

		<guid isPermaLink="false">http://blog.mro.name/?p=1064</guid>
		<description><![CDATA[For quite some time I ranted about not being able to use compressed network communcation out-of-the-box on the iPhone. Despite being undocumented (or I just overlooked the hint), NSURLConnection does gzip decompression transparently! That&#8217;s how to use it: ensure the webserver sends gzipped content, use e.g. wget to verify: $ wget --header='Accept-Encoding: gzip' \ --server-response [...]]]></description>
			<content:encoded><![CDATA[<p>For quite some time I ranted about not being able to use compressed network communcation out-of-the-box on the iPhone.</p>
<p>Despite being undocumented (or I just overlooked the hint), <a href="http://developer.apple.com/documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html">NSURLConnection</a> does <a href="http://en.wikipedia.org/wiki/Gzip">gzip</a> decompression transparently!</p>
<p>That&#8217;s how to use it:</p>
<p><span id="more-1064"></span></p>
<ol>
<li>ensure the webserver sends gzipped content, use e.g. <a href="http://www.gnu.org/software/wget/">wget</a> to verify:

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">wget</span> <span style="color: #660033;">--header</span>=<span style="color: #ff0000;">'Accept-Encoding: gzip'</span> \
    <span style="color: #660033;">--server-response</span> http:<span style="color: #000000; font-weight: bold;">//</span>example.com<span style="color: #000000; font-weight: bold;">/</span>demo.xmlz</pre></div></div>

<p>It should (in case of a gzipped xml document) look like</p>

<div class="wp_syntax"><div class="code"><pre class="log" style="font-family:monospace;">HTTP/1.1 200 OK
...
Content-Type: text/xml
Content-Encoding: gzip
...</pre></div></div>

</li>
<li>If your webserver doesn&#8217;t support transparent compression, you can still upload gzipped content and tell the server to send the correct response headers by setting up a  <a href="http://en.wikipedia.org/wiki/Htaccess"><code>.htaccess</code></a> file:

<div class="wp_syntax"><div class="code"><pre class="apache" style="font-family:monospace;">...
<span style="color: #00007f;">AddType</span> text/xml .xml .xmlz
<span style="color: #00007f;">AddEncoding</span> gzip .gz .xmlz
&nbsp;
<span style="color: #adadad; font-style: italic;"># inspired by</span>
<span style="color: #adadad; font-style: italic;"># http://betterexplained.com/articles/</span>
<span style="color: #adadad; font-style: italic;">#  how-to-optimize-your-site-with-gzip-compression/</span>
<span style="color: #adadad; font-style: italic;"># compress all text &amp;amp; html:</span>
AddOutputFilterByType DEFLATE text/html text/plain text/xml
...</pre></div></div>

</li>
<li>to verify from within your app, log the response header in the NSURLConnection callbacks:

<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;">void</span><span style="color: #002200;">&#41;</span>connection<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
  didReceiveResponse<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSURLResponse</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>response
<span style="color: #002200;">&#123;</span>
    NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;didReceiveResponse %@: %@&quot;</span>, <span style="color: #002200;">&#91;</span>response URL<span style="color: #002200;">&#93;</span>,
      <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSHTTPURLResponse</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>response allHeaderFields<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
    buffer <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableData</span> dataWithCapacity<span style="color: #002200;">:</span><span style="color: #2400d9;">1024</span><span style="color: #002200;">*</span><span style="color: #2400d9;">1024</span><span style="color: #002200;">&#93;</span> retain<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>connection<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
  didReceiveData<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSData</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>dat
<span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>buffer appendData<span style="color: #002200;">:</span>dat<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>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>
    NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;connectionDidFinishLoading %d bytes&quot;</span>, <span style="color: #002200;">&#91;</span>buffer length<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
    <span style="color: #002200;">&#91;</span>buffer release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>It should look like</p>

<div class="wp_syntax"><div class="code"><pre class="log" style="font-family:monospace;">2009-06-28 14:31:09.722 DemoApp[3981:20b] \
  didReceiveResponse http://example.com/demo.xmlz: {
...
&quot;Content-Type&quot; = &quot;text/xml&quot;;
&quot;Content-Encoding&quot; = gzip;
&quot;Content-Length&quot; = 123042;
}
...
2009-06-28 14:31:11.619 DemoApp[3981:20b] \
  connectionDidFinishLoading 602979 bytes</pre></div></div>

<p>As you can see we received way more bytes than went over the wire.</li>
<li>enjoy lightning fast network communication!</li>
</ol>
<p>P.S.: It seems not to be necessary setting the request header yourself:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSMutableURLRequest</span> <span style="color: #002200;">*</span>request <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableURLRequest</span> requestWithURL<span style="color: #002200;">:</span>url
    cachePolicy<span style="color: #002200;">:</span>NSURLRequestReloadIgnoringLocalCacheData
    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;">// set explicitly:</span>
<span style="color: #002200;">&#91;</span>request setValue<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;gzip&quot;</span> forHTTPHeaderField<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Accept-Encoding&quot;</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.mro.name/2009/06/nsurlconnection-gzip-magic/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CoreData &#8211; Hello World.</title>
		<link>http://blog.mro.name/2009/06/coredata-hello-world/</link>
		<comments>http://blog.mro.name/2009/06/coredata-hello-world/#comments</comments>
		<pubDate>Sun, 21 Jun 2009 16:40:55 +0000</pubDate>
		<dc:creator>mro</dc:creator>
				<category><![CDATA[Artikel auf deutsch]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[CoreData]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Objective C]]></category>

		<guid isPermaLink="false">http://blog.mro.name/?p=988</guid>
		<description><![CDATA[CoreData ist im iPhone OS 3.0 ganz frisch dazugekommen und riecht auch noch ein wenig nach Plastik. Leider ist der Beispielcode auf den Apple auch im iPhone CoreData Tutorial immer wieder verweist (Locations Sample Project) dem Umzug der OS 3.0 beta um Opfer gefallen. Also los geht&#8217;s zum ersten Schritt: 1. Ein leeres Projekt Nachdem [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://developer.apple.com/iphone/library/referencelibrary/GettingStarted/GettingStartedWithCoreData/index.html#//apple_ref/doc/uid/TP40005316">CoreData</a> ist im iPhone OS 3.0 ganz frisch dazugekommen und riecht auch noch ein wenig nach Plastik.</p>
<p>Leider ist der Beispielcode auf den Apple auch im <a href="http://developer.apple.com/iphone/library/documentation/DataManagement/Conceptual/iPhoneCoreData01/Introduction/Introduction.html">iPhone CoreData Tutorial</a> immer wieder verweist (<a href="http://developer.apple.com/iphone/prerelease/library/samplecode/Locations/index.html">Locations Sample Project</a>) dem Umzug der OS 3.0 beta um Opfer gefallen.</p>
<p>Also los geht&#8217;s zum ersten Schritt:</p>
<p><span id="more-988"></span><br />
1. Ein leeres Projekt</p>
<p>Nachdem iPhone SDK 3.0 installiert ist: &#8220;XCode -&gt; File -&gt; New Project&#8221; mit &#8220;Use Core Data for storage&#8221; Häkchen&#8221;:</p>
<div id="attachment_989" class="wp-caption aligncenter" style="width: 410px"> <img class="aligncenter wp-image-989" title="xcode-new-coredata-project" src="http://blog.mro.name/wp-content/2009/06/bild-5.png" alt="Neues Projekt mit CoreData Unterstützung" width="400" /><p class="wp-caption-text">Neues Projekt mit CoreData Unterstützung</p></div>
<p>Der Application Delegate enthält dann gleich eine ganze Menge Code zur Verwaltung der Datenquelle, u.a. mit</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSURL</span> <span style="color: #002200;">*</span>storeUrl <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURL</span> fileURLWithPath<span style="color: #002200;">:</span>
<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self applicationDocumentsDirectory<span style="color: #002200;">&#93;</span>
stringByAppendingPathComponent<span style="color: #002200;">:</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;CoreDataDemo.sqlite&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>den Namen der sqlite DB Datei, in die gespeichert werden soll.</p>
<p>2. Das Schema</p>
<div id="attachment_993" class="wp-caption aligncenter" style="width: 410px"> <img class="aligncenter wp-image-993" title="new-coredata-schema" src="http://blog.mro.name/wp-content/2009/06/bild-6.png" alt="Ein neues CoreData Schema" width="400" /> <p class="wp-caption-text">Ein neues CoreData Schema</p></div>
<p>und sollte so heißen wie die DB oben.</p>
<p>3. Zugriffe</p>
<p>Siehe <a href="http://developer.apple.com/documentation/Cocoa/Conceptual/CoreData/Articles/cdFetching.html#//apple_ref/doc/uid/TP40002484">Core Data Programming Guide &#8211; Fetching Managed Objects</a>.</p>
<p>Damit die Beispiel-Zugriffe Sinn ergeben, muß das Schema eine Entity &#8220;Employee&#8221; mit Attributen &#8220;lastName&#8221; (String) und &#8220;salary&#8221; (Numeric) enthalten.</p>
<p>Dann sollte der erste Einstieg eigentlich klappen.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mro.name/2009/06/coredata-hello-world/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Objective C geht bei den Buchverkäufen</title>
		<link>http://blog.mro.name/2009/03/objective-c-geht-bei-oreilly/</link>
		<comments>http://blog.mro.name/2009/03/objective-c-geht-bei-oreilly/#comments</comments>
		<pubDate>Mon, 02 Mar 2009 22:05:19 +0000</pubDate>
		<dc:creator>mro</dc:creator>
				<category><![CDATA[Artikel auf deutsch]]></category>
		<category><![CDATA[seenontheweb]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Objective C]]></category>

		<guid isPermaLink="false">http://blog.mro.name/?p=579</guid>
		<description><![CDATA[ab wie Schmitts Katze. Sieh an, sieh an.]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.heise.de/newsticker/O-Reilly-Absatzzahlen-C-ist-Spitzenreiter--/zoom/133763/0">ab wie Schmitts Katze</a>. Sieh an, sieh an.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mro.name/2009/03/objective-c-geht-bei-oreilly/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
