<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss1japanesefull.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns="http://purl.org/rss/1.0/"><channel rdf:about="http://blog.boreal-kiss.com"><title>boreal-kiss.com</title><link>http://blog.boreal-kiss.com</link><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rdf+xml" href="http://feeds.feedburner.com/bbk" /><description>Flash, Flex, AIRのプログラミングメモ。最近はCocoa, Cocoa touchに傾倒中。</description><dc:language>ja</dc:language><dc:date>2011-04-13T11:15:48-07:00</dc:date><sy:updatePeriod xmlns:sy="http://purl.org/rss/1.0/modules/syndication/">hourly</sy:updatePeriod><sy:updateFrequency xmlns:sy="http://purl.org/rss/1.0/modules/syndication/">1</sy:updateFrequency><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/bbk" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="bbk" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><items><rdf:Seq><rdf:li rdf:resource="http://blog.boreal-kiss.com/?p=583" /><rdf:li rdf:resource="http://blog.boreal-kiss.com/?p=570" /><rdf:li rdf:resource="http://blog.boreal-kiss.com/?p=513" /><rdf:li rdf:resource="http://blog.boreal-kiss.com/?p=461" /><rdf:li rdf:resource="http://blog.boreal-kiss.com/?p=440" /></rdf:Seq></items></channel><item rdf:about="http://blog.boreal-kiss.com/?p=583"><title>Xcode 4: プロジェクトテンプレートにサブグループを追加する</title><link>http://blog.boreal-kiss.com/2011/04/09/subgroups-on-an-xcode-4-project-template/</link><dc:subject>未分類</dc:subject><dc:subject>iPhone</dc:subject><dc:subject>Xcode</dc:subject><dc:creator>borealkiss</dc:creator><dc:date>2011-04-08T23:49:52-07:00</dc:date><content:encoded xmlns:content="http://purl.org/rss/1.0/modules/content/"><![CDATA[<p>テンプレートにサブグループ(グループ内グループ)を追加する方法は、Appleのデフォルトテンプレートには実践している例がなく、さらにテンプレートの仕様についても何も情報がないため長いこと謎だった。が、判明。例えば以下のようなディレクトリ構造のままテンプレートにSomeClass.mファイルを追加したいとする。</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">Classes/SomeClass/SomeClass.m</pre></div></div>

<p>この場合、Definitionsパートに次のように記述すればディレクトリ構造を保持したままテンプレートに追加することができる。</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Definitions<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dict<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Classes/SomeClass/SomeClass.m<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dict<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Group<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;array<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Classes<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>SomeClass<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/array<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Path<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Classes/SomeClass/SomeClass.m<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dict<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dict<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Nodesパートは関連するKeyネームを追加するだけで特に目新しい変更はない。</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Nodes<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;array<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Classes/SomeClass/SomeClass.m<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/array<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>サブグループを追加したテンプレートの一例はgithubに置いてある。テンプレートの詳細については以下のリンク先を参照ください。</p>
<ul>
<li><a href="https://github.com/borealkiss/Minimal-Template">https://github.com/borealkiss/Minimal-Template</a></li>
<li><a href="http://blog.boreal-kiss.net/2011/03/11/a-minimal-project-template-for-xcode-4/">A minimal project template for Xcode 4</a></li>
</ul>
]]></content:encoded><description>テンプレートにサブグループ(グループ内グループ)を追加する方法は、Appleのデフォルトテンプレートには実践している例がなく、さらにテンプレートの仕様についても何も情報がないため長いこと謎だった。が、判明。例えば以下のよ [...]</description><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blog.boreal-kiss.com/2011/04/09/subgroups-on-an-xcode-4-project-template/feed/</wfw:commentRss><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">0</slash:comments></item><item rdf:about="http://blog.boreal-kiss.com/?p=570"><title>Minimal Template: Xcode 4用プロジェクトテンプレート</title><link>http://blog.boreal-kiss.com/2011/03/12/a-minimal-project-template-for-xcode-4/</link><dc:subject>未分類</dc:subject><dc:subject>iPhone</dc:subject><dc:subject>Xcode</dc:subject><dc:creator>borealkiss</dc:creator><dc:date>2011-03-12T01:17:19-08:00</dc:date><content:encoded xmlns:content="http://purl.org/rss/1.0/modules/content/"><![CDATA[<p>Xcode 4用のユーザーテンプレートの作り方がだいぶわかってきたのでまとめた。余裕ができたら日本語化する予定。</p>
<ul>
<li><a href="http://blog.boreal-kiss.net/2011/03/11/a-minimal-project-template-for-xcode-4/">A minimal project template for Xcode 4</a></li>
</ul>
<h1>テンプレートを改変をする際に注意すべきこと</h1>
<p>TemplateInfo.plist (新規プロジェクト作成時にXcode 4が最初に読み込む設定ファイル)の改変には細心の注意を。不正なデータ構造を残したままXcode 4に読み込ませると最悪の場合、無関係なディレクトリを消去してしまう等、作業環境に悪影響を与える可能性がある。詳細は上記ブログ記事を参照ください。</p>
<h1>Downloads</h1>
<p>iPhone用のサンプルは以下に置いてある。</p>
<ul>
<li><a href="https://github.com/borealkiss/Minimal-Template">https://github.com/borealkiss/Minimal-Template</a></li>
</ul>
]]></content:encoded><description>Xcode 4用のユーザーテンプレートの作り方がだいぶわかってきたのでまとめた。余裕ができたら日本語化する予定。 A minimal project template for Xcode 4 テンプレートを改変をする際に [...]</description><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blog.boreal-kiss.com/2011/03/12/a-minimal-project-template-for-xcode-4/feed/</wfw:commentRss><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">0</slash:comments></item><item rdf:about="http://blog.boreal-kiss.com/?p=513"><title>Grand Central Dispatch: ストライドを用いてforループのイテレーション回数を最適化する</title><link>http://blog.boreal-kiss.com/2011/03/08/grand-central-dispatch-stride-loop/</link><dc:subject>未分類</dc:subject><dc:subject>DesignPatterns</dc:subject><dc:subject>GCD</dc:subject><dc:subject>Objective-C</dc:subject><dc:creator>borealkiss</dc:creator><dc:date>2011-03-07T12:54:27-08:00</dc:date><content:encoded xmlns:content="http://purl.org/rss/1.0/modules/content/"><![CDATA[<h1>イントロダクション</h1>
<p>Grand Central Dispatch (GCD)にはforループをマシンのコア数に応じて並列処理してくれる便利な関数 dispatch_apply(size_t, dispatch_queue_t, void) が存在する。</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">void</span> dispatch_apply<span style="color: #002200;">&#40;</span>
	<span style="color: #a61390;">size_t</span> iterations,
	dispatch_queue_t queue,
	<span style="color: #a61390;">void</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">^</span>block<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">size_t</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>引数は前から順番に、forループのイテレーション回数、タスク送信先のキュー、各イテレーションで行われるタスク、である。この関数はforループの各イテレーションごとにキューにタスクを送っている。したがって、もしタスクの処理時間がキューにタスクを送信するためにかかる時間(オーバーヘッド)より小さくなるような場合には注意が必要となる。普通にforループをまわす時よりも処理が遅くなる可能性があるからだ。具体例を見てみよう。</p>
<h1>実行環境</h1>
<p>Mac OS X 2.4 GHz Intel Core 2 Duo (CPU2個)</p>
<h1>forループ vs. GCD その1</h1>
<p>以下のような「何もしない」blockをforループでまわして処理にかかった時間を計測してみる。</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">void</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">^</span>block<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> i<span style="color: #002200;">&#41;</span> <span style="color: #002200;">=</span> <span style="color: #002200;">^</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> i<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#123;</span>
&nbsp;
<span style="color: #002200;">&#125;</span>;</pre></div></div>

<p>比較するのは普通のforループ処理を行う関数 loop_normal(int, void)</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">static</span> <span style="color: #a61390;">void</span> loop_normal<span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> count, <span style="color: #a61390;">void</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">^</span>block<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#123;</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&lt;count; i<span style="color: #002200;">++</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#123;</span>
		block<span style="color: #002200;">&#40;</span>i<span style="color: #002200;">&#41;</span>;
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>とGCDを用いた場合の関数 loop_normal_gcd(int, void) である。</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">static</span> <span style="color: #a61390;">void</span> loop_normal_gcd<span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> count, <span style="color: #a61390;">void</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">^</span>block<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">void</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">^</span>block_gcd<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">size_t</span> i<span style="color: #002200;">&#41;</span> <span style="color: #002200;">=</span> <span style="color: #002200;">^</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">size_t</span> i<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#123;</span>
		block<span style="color: #002200;">&#40;</span>i<span style="color: #002200;">&#41;</span>;
	<span style="color: #002200;">&#125;</span>;
&nbsp;
	dispatch_queue_t queue 
		<span style="color: #002200;">=</span> dispatch_get_global_queue<span style="color: #002200;">&#40;</span>DISPATCH_QUEUE_PRIORITY_DEFAULT, <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>;
	dispatch_apply<span style="color: #002200;">&#40;</span>count, queue, block_gcd<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>比較した結果が下図になる(横軸はイテレーション回数、縦軸が関数の処理にかかった時間)。これを見るとGCDを用いた場合の方が(格段に)遅くなっていることがわかるだろう。この原因は「何もしない」タスクを完了するための時間に比べてキュー送信のオーバーヘッドが大きいことによる。</p>
<p><img src="http://blog.boreal-kiss.com/wp/wp-content/uploads/2011/03/nostride.png" alt="for loop vs GCD" title="for loop vs GCD" width="488" height="340" class="aligncenter size-full wp-image-552" /></p>
<p>それではこのオーバーヘッドが無視できるような構造にするにはどうすればよいだろうか。ひとつの方法はループにストライドを導入することである。</p>
<h1>ストライドの導入</h1>
<p>この方式はストライドで定義される整数値分の回数のイテレーションを一回のイテレーションに押し込んでしまう方法だ。これによりforループのイテレーション回数が減り、かわりに一回のイテレーションにおけるタスク量を増やすことができる。具体的に見てみよう。まず普通のforループを行う関数 loop_normal(int, void) がある。これはcount回のイテレーションを行う。</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">static</span> <span style="color: #a61390;">void</span> loop_normal<span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> count, <span style="color: #a61390;">void</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">^</span>block<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#123;</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&lt;count; i<span style="color: #002200;">++</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#123;</span>
		block<span style="color: #002200;">&#40;</span>i<span style="color: #002200;">&#41;</span>;
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>次に上記関数と全く同じ処理を行う関数が以下の loop_stride(int, int, void) である。ただし整数 stride (stride < count) を導入することで関数内部のループ回数が (count / stride) になっていることに注目してもらいたい。イテレーション回数が減った分イテレーション一回のタスク量が増えていることもわかるだろう。</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">static</span> <span style="color: #a61390;">void</span> loop_stride<span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> count, <span style="color: #a61390;">int</span> stride, <span style="color: #a61390;">void</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">^</span>block<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">int</span> iMax <span style="color: #002200;">=</span> count <span style="color: #002200;">/</span> stride;
	<span style="color: #a61390;">int</span> remainder <span style="color: #002200;">=</span> count <span style="color: #002200;">%</span> stride;
&nbsp;
	<span style="color: #a61390;">void</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">^</span>block_stride<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> i<span style="color: #002200;">&#41;</span> <span style="color: #002200;">=</span> <span style="color: #002200;">^</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> i<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#123;</span>
		<span style="color: #a61390;">int</span> j <span style="color: #002200;">=</span> i <span style="color: #002200;">*</span> stride;
		<span style="color: #a61390;">int</span> jMax <span style="color: #002200;">=</span> j <span style="color: #002200;">+</span> stride;
&nbsp;
		<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>i <span style="color: #002200;">==</span> iMax <span style="color: #002200;">-</span> <span style="color: #2400d9;">1</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#123;</span>
			jMax <span style="color: #002200;">+=</span> remainder;
		<span style="color: #002200;">&#125;</span>
&nbsp;
		<span style="color: #a61390;">while</span> <span style="color: #002200;">&#40;</span>j &lt; jMax<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
			block<span style="color: #002200;">&#40;</span>j<span style="color: #002200;">&#41;</span>;
			j<span style="color: #002200;">++</span>;
		<span style="color: #002200;">&#125;</span>
	<span style="color: #002200;">&#125;</span>;
&nbsp;
	<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&lt;iMax; i<span style="color: #002200;">++</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#123;</span>
		block_stride<span style="color: #002200;">&#40;</span>i<span style="color: #002200;">&#41;</span>;
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>ストライドを導入すればイテレーションの一回のタスク量を増やせることがわかった。それでは実際にGCDに放り込んで結果を見てみよう。</p>
<h1>forループ vs. GCD その2</h1>
<p>比較したのは普通のforループを行う関数 loop_normal(int, void)</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">static</span> <span style="color: #a61390;">void</span> loop_normal<span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> count, <span style="color: #a61390;">void</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">^</span>block<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#123;</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&lt;count; i<span style="color: #002200;">++</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#123;</span>
		block<span style="color: #002200;">&#40;</span>i<span style="color: #002200;">&#41;</span>;
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>とストライドを導入して一回のタスク量を増やした関数 loop_stride_gcd(int, int, void) である。</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">static</span> <span style="color: #a61390;">void</span> loop_stride_gcd<span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> count, <span style="color: #a61390;">int</span> stride, <span style="color: #a61390;">void</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">^</span>block<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">int</span> iMax <span style="color: #002200;">=</span> count <span style="color: #002200;">/</span> stride;
	<span style="color: #a61390;">int</span> remainder <span style="color: #002200;">=</span> count <span style="color: #002200;">%</span> stride;
&nbsp;
	<span style="color: #a61390;">void</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">^</span>block_gcd<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">size_t</span> i<span style="color: #002200;">&#41;</span> <span style="color: #002200;">=</span> <span style="color: #002200;">^</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">size_t</span> i<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#123;</span>
		<span style="color: #a61390;">int</span> j <span style="color: #002200;">=</span> i <span style="color: #002200;">*</span> stride;
		<span style="color: #a61390;">int</span> jMax <span style="color: #002200;">=</span> j <span style="color: #002200;">+</span> stride;
&nbsp;
		<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>i <span style="color: #002200;">==</span> iMax <span style="color: #002200;">-</span> <span style="color: #2400d9;">1</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#123;</span>
			jMax <span style="color: #002200;">+=</span> remainder;
		<span style="color: #002200;">&#125;</span>
&nbsp;
		<span style="color: #a61390;">while</span> <span style="color: #002200;">&#40;</span>j &lt; jMax<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
			block<span style="color: #002200;">&#40;</span>j<span style="color: #002200;">&#41;</span>;
			j<span style="color: #002200;">++</span>;
		<span style="color: #002200;">&#125;</span>
	<span style="color: #002200;">&#125;</span>;
&nbsp;
	dispatch_queue_t queue 
		<span style="color: #002200;">=</span> dispatch_get_global_queue<span style="color: #002200;">&#40;</span>DISPATCH_QUEUE_PRIORITY_DEFAULT, <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>;
	dispatch_apply<span style="color: #002200;">&#40;</span>iMax, queue, block_gcd<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>そして両者の引数に「何もしない」blockを渡し実行時間を比較した。</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">void</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">^</span>block<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> i<span style="color: #002200;">&#41;</span> <span style="color: #002200;">=</span> <span style="color: #002200;">^</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> i<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#123;</span>
&nbsp;
<span style="color: #002200;">&#125;</span>;</pre></div></div>

<p>ここではイテレーションの回数は100,000回に固定し、ストライドの値を変化させて両者を比較した。結果は以下の図になる(横軸はストライドの値、縦軸は関数の処理にかかった時間)。ストライドの値が大きい場合はGCDを用いた場合の方が処理が早くなっていることがわかる。これらはつまりキューにタスクを送る際のオーバーヘッドが全体として無視できる程度になっていることを意味する。</p>
<p><img src="http://blog.boreal-kiss.com/wp/wp-content/uploads/2011/03/stride.png" alt="for loop vs GCD (stride)" title="for loop vs GCD (stride)" width="488" height="340" class="aligncenter size-full wp-image-553" /></p>
<h1>まとめ</h1>
<p>GCDの恩恵を受けるためにはタスクは適度に重い(処理に時間がかかる)ものである必要があることがわかった。タスクが軽すぎる場合、GCDを利用することでかえって普通のforループよりも処理が遅くなる場合がある。これはGCDのオーバーヘッドによる。このような場合にはforループにストライドを導入することでイテレーション回数を減らし一回のタスクを重くすることで解決できる。</p>
]]></content:encoded><description>イントロダクション Grand Central Dispatch (GCD)にはforループをマシンのコア数に応じて並列処理してくれる便利な関数 dispatch_apply(size_t, dispatch_queue [...]</description><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blog.boreal-kiss.com/2011/03/08/grand-central-dispatch-stride-loop/feed/</wfw:commentRss><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">0</slash:comments></item><item rdf:about="http://blog.boreal-kiss.com/?p=461"><title>Grand Central Dispatch: forループ処理を並列化する</title><link>http://blog.boreal-kiss.com/2011/03/06/grand-central-dispatch/</link><dc:subject>未分類</dc:subject><dc:subject>Cocoa</dc:subject><dc:subject>DesignPatterns</dc:subject><dc:subject>GCD</dc:subject><dc:subject>Objective-C</dc:subject><dc:creator>borealkiss</dc:creator><dc:date>2011-03-05T08:58:06-08:00</dc:date><content:encoded xmlns:content="http://purl.org/rss/1.0/modules/content/"><![CDATA[<h1>Grand Central Dispatch</h1>
<p>OSX 10.6から導入されたGrand Central Dispatch (GCD)は複数タスクを処理するための便利な仕組みのことで、これを使うとマルチコアの恩恵を簡単に受けることができる。複数同時処理といった仕組みとしてスレッドがあるが、GCDの場合(スレッドを作成する時・制御する時のような)面倒くさい手順を一切踏まなくてよくなる。さらにシステム側でCPUを適切に利用する工夫がされているので(CPU1個なら1個、2個なら2個、8個なら8個勝手に使ってくれる)実行環境を気にしなくてよい、という利点が挙げられる。詳細は以下のドキュメントが非常に参考になる。</p>
<ul>
<li><a href="http://developer.apple.com/library/mac/#documentation/General/Conceptual/ConcurrencyProgrammingGuide/Introduction/Introduction.html">Concurrency Programming Guide</a></li>
</ul>
<p>さて今回、CPUが複数あるマシンでGCDの恩恵を受けているかどうかを簡単な計算で確認したので以下に結果を載せておく。GCDにはforループを簡単に並列化する仕組みがあるのでそれを利用した。なお計算を行ったマシンは2.4 GHz Intel Core 2 Duo。</p>
<h1>forループの並列化</h1>
<p>以下のようなforループがあるとする。</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><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&lt;count; i<span style="color: #002200;">++</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#123;</span>
	<span style="color: #11740a; font-style: italic;">//Do some work.</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>ループ内のi回目の処理が他と独立したタスクである場合GCDのdispatch queueを用いて並列処理が可能になる。以下は上記forループと同じ仕事を行う。</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//GCD way.</span>
<span style="color: #a61390;">void</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">^</span>block<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">size_t</span> i<span style="color: #002200;">&#41;</span> <span style="color: #002200;">=</span> <span style="color: #002200;">^</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">size_t</span> i<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#123;</span>
	<span style="color: #11740a; font-style: italic;">//Do some work.</span>
<span style="color: #002200;">&#125;</span>;
&nbsp;
dispatch_queue_t queue 
	<span style="color: #002200;">=</span> dispatch_get_global_queue<span style="color: #002200;">&#40;</span>DISPATCH_QUEUE_PRIORITY_DEFAULT, <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>;
dispatch_apply<span style="color: #002200;">&#40;</span>count, queue, block<span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>ここで</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">void</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">^</span>block<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">size_t</span> i<span style="color: #002200;">&#41;</span></pre></div></div>

<p>はblockと呼ばれるApple独自のC言語の拡張である。これは簡単に言うと一連の作業をまとめてオブジェクト化したものであり<sup> &dagger;<a href="http://blog.boreal-kiss.com/2011/03/06/grand-central-dispatch/#footnote_0_461" id="identifier_0_461" class="footnote-link footnote-identifier-link" title="仕組みとしてはNSInvocationに似ているがblockは特定のメソッドだけに固執しておらず、記述された内容をprocedualに実行できる">1</a></sup>、GCDとは親密な関係にある。block自体も簡単で便利な仕組みなので、興味があるひとは以下のドキュメントを参考にしてほしい。</p>
<ul>
<li><a href="http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Blocks/Articles/00_Introduction.html">Blocks Programming Topics</a></li>
</ul>
<p>さて上記の場合、blockに記述された内容がforループ内の処理に相当し、これをGCDのdispatch_apply()の引数として渡してシステム側で処理してもらっている。記述が簡単な点にも注目してもらいたい。</p>
<h1>計算結果</h1>
<p>普通のforループとdispatch queueを用いた場合を比較したものを載せておく。forループ内の1回の処理時間(t_task)を横軸、ループ終了までにかかった時間を縦軸にとってある。ここでループの回数(count)はt_taskの値ごとに異なっており</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">t_task x count <span style="color: #002200;">=</span> <span style="color: #2400d9;">10</span> sec.</pre></div></div>

<p>となるように設定してある。例えば図の一番左のバーの場合、ループ内の一回の処理時間を0.00001秒、ループの総数を1000000回としてある(全処理時間は0.00001 x 1000000 = 10秒)<sup> &dagger;<a href="http://blog.boreal-kiss.com/2011/03/06/grand-central-dispatch/#footnote_1_461" id="identifier_1_461" class="footnote-link footnote-identifier-link" title="縦軸のループ処理時間が10秒以上になっているものがあるのはループを行う際のオーバーヘッドの累積によるもの。">2</a></sup>。試行範囲に限って言えばdispatch queueを用いた方が2倍ほど早いことがわかる。2倍という数字はCPU2個使った並列化、ということだろう。設定によっては普通のforループの方が早くなるようなことがあるかと思われたがそのようなこともなかったので、並列化が可能であればGCDはどんどん使った方が良さそうだ。</p>
<p><img src="http://blog.boreal-kiss.com/wp/wp-content/uploads/2011/03/gcd.png" alt="" title="gcd" width="500" height="331" class="alignnone size-full wp-image-462" /></p>
<h1>確認用プログラム</h1>
<p>上記計算に使用したプログラム。ループ内の処理時間の変更は指定時間スリープさせることで実現させてある。gettimeofday_sec()はマイクロ秒測定用の関数で<a href="http://kzk9.net/column/time.html">C言語: 実行時間測定の方法</a>より拝借した。</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &lt;Foundation/Foundation.h&gt;</span>
<span style="color: #6e371a;">#include &lt;sys/time.h&gt;</span>
&nbsp;
<span style="color: #a61390;">double</span> gettimeofday_sec<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #a61390;">static</span> <span style="color: #a61390;">void</span> test_normal_loop<span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> count, NSTimeInterval sleep<span style="color: #002200;">&#41;</span>;
<span style="color: #a61390;">static</span> <span style="color: #a61390;">void</span> test_dispatch_que<span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> count, NSTimeInterval sleep<span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #a61390;">int</span> main <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> argc, <span style="color: #a61390;">const</span> <span style="color: #a61390;">char</span> <span style="color: #002200;">*</span> argv<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
    <span style="color: #400080;">NSAutoreleasePool</span> <span style="color: #002200;">*</span> pool <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSAutoreleasePool</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">const</span> NSTimeInterval TEN_SEC <span style="color: #002200;">=</span> <span style="color: #2400d9;">10.0</span>;
&nbsp;
	<span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> count <span style="color: #002200;">=</span> <span style="color: #2400d9;">10</span>; count &lt; 1e7; count<span style="color: #002200;">*=</span><span style="color: #2400d9;">10</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#123;</span>
		NSTimeInterval sleep <span style="color: #002200;">=</span> TEN_SEC <span style="color: #002200;">/</span> count;
		test_normal_loop<span style="color: #002200;">&#40;</span>count, sleep<span style="color: #002200;">&#41;</span>;
		test_dispatch_que<span style="color: #002200;">&#40;</span>count, sleep<span style="color: #002200;">&#41;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #002200;">&#91;</span>pool drain<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">return</span> <span style="color: #2400d9;">0</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">double</span> gettimeofday_sec<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">struct</span> timeval tv;
    gettimeofday<span style="color: #002200;">&#40;</span><span style="color: #002200;">&amp;</span>tv, <span style="color: #a61390;">NULL</span><span style="color: #002200;">&#41;</span>;
    <span style="color: #a61390;">return</span> tv.tv_sec <span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">double</span><span style="color: #002200;">&#41;</span>tv.tv_usec<span style="color: #002200;">*</span>1e<span style="color: #002200;">-</span>6;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">static</span> <span style="color: #a61390;">void</span> test_normal_loop<span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> count, NSTimeInterval sleep<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">double</span> t1 <span style="color: #002200;">=</span> gettimeofday_sec<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	<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&lt;count; i<span style="color: #002200;">++</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#123;</span>
		<span style="color: #002200;">&#91;</span><span style="color: #400080;">NSThread</span> sleepForTimeInterval<span style="color: #002200;">:</span>sleep<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #a61390;">double</span> t2 <span style="color: #002200;">=</span> gettimeofday_sec<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #a61390;">printf</span><span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">&quot;%f<span style="color: #2400d9;">\n</span>&quot;</span>, t2 <span style="color: #002200;">-</span> t1<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">static</span> <span style="color: #a61390;">void</span> test_dispatch_que<span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> count, NSTimeInterval sleep<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">double</span> t1 <span style="color: #002200;">=</span> gettimeofday_sec<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #a61390;">void</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">^</span>block<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">size_t</span> i<span style="color: #002200;">&#41;</span> <span style="color: #002200;">=</span> <span style="color: #002200;">^</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">size_t</span> i<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#123;</span>
		<span style="color: #002200;">&#91;</span><span style="color: #400080;">NSThread</span> sleepForTimeInterval<span style="color: #002200;">:</span>sleep<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>;
&nbsp;
	dispatch_queue_t queue 
		<span style="color: #002200;">=</span> dispatch_get_global_queue<span style="color: #002200;">&#40;</span>DISPATCH_QUEUE_PRIORITY_DEFAULT, <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>;
	dispatch_apply<span style="color: #002200;">&#40;</span>count, queue, block<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #a61390;">double</span> t2 <span style="color: #002200;">=</span> gettimeofday_sec<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #a61390;">printf</span><span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">&quot;%f<span style="color: #2400d9;">\n</span>&quot;</span>, t2 <span style="color: #002200;">-</span> t1<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<h1>Footnotes</h1><ol class="footnotes"><li id="footnote_0_461" class="footnote">仕組みとしてはNSInvocationに似ているがblockは特定のメソッドだけに固執しておらず、記述された内容をprocedualに実行できる</li><li id="footnote_1_461" class="footnote">縦軸のループ処理時間が10秒以上になっているものがあるのはループを行う際のオーバーヘッドの累積によるもの。</li></ol>]]></content:encoded><description>Grand Central Dispatch OSX 10.6から導入されたGrand Central Dispatch (GCD)は複数タスクを処理するための便利な仕組みのことで、これを使うとマルチコアの恩恵を簡単に受 [...]</description><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blog.boreal-kiss.com/2011/03/06/grand-central-dispatch/feed/</wfw:commentRss><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">0</slash:comments></item><item rdf:about="http://blog.boreal-kiss.com/?p=440"><title>Warning “…may not respond to…” を意図的に表示させない方法</title><link>http://blog.boreal-kiss.com/2011/02/06000026.html/</link><dc:subject>未分類</dc:subject><dc:subject>Cocoa</dc:subject><dc:subject>NSProxy</dc:subject><dc:subject>Objective-C</dc:subject><dc:creator>borealkiss</dc:creator><dc:date>2011-02-05T07:00:26-08:00</dc:date><content:encoded xmlns:content="http://purl.org/rss/1.0/modules/content/"><![CDATA[<p>例えば、NSProxyのサブクラスにメッセージのフォワーディングをさせたい時<sup> &dagger;<a href="http://blog.boreal-kiss.com/2011/02/06000026.html/#footnote_0_440" id="identifier_0_440" class="footnote-link footnote-identifier-link" title="メッセージフォワーディングの具体例は-[NSObject performSelector:withObject]に複数の引数を渡す &amp;#8211; boreal-kiss.com">1</a></sup>などにどうしてもこの警告が表示されてしまう。存在しないメソッドを呼んでいるので警告が出るのは当たり前だが放置しておくにはうっとおしい。この警告、実は簡単に消すことができる。</p>
<h1>Solution</h1>
<p>&#8220;&#8230;may not respond to&#8230;&#8221;が表示されるクラスのカテゴリを新規に作成し(インターフェイスのみ)、その中に&#8221;&#8230;may not respond to&#8230;&#8221;が表示されるメソッドを定義してやる。インターフェイスは&#8221;&#8230;may not respond to&#8230;&#8221;が表示されるファイルの一番上にでも書いておけばよいだろう。</p>
<h1>Example</h1>
<p>以下の例ではMyObjectクラスの-[MyClass doSomething]をメインスレッドで実行する際にNSProxyのフォワーディングを利用している。-[MyObject doSomethingOnMainThread]に注目してもらいたい。-[NSObject performSelectorOnMainThread:withObject:waitUntilDone:]をNSProxyのサブクラス(MyProxy)に送っているがMyProxy ()カテゴリのおかげで&#8221;&#8230;may not respond to&#8230;&#8221;の警告が表示されない。</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//</span>
<span style="color: #11740a; font-style: italic;">//  MyObject.m</span>
<span style="color: #11740a; font-style: italic;">//</span>
&nbsp;
<span style="color: #6e371a;">#import &quot;MyProxy.h&quot;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">//This is important!!</span>
<span style="color: #a61390;">@interface</span> MyProxy <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>doSomething;
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> MyObject 
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>doSomethingOnMainThread<span style="color: #002200;">&#123;</span>
	MyProxy <span style="color: #002200;">*</span>myProxy <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>MyProxy alloc<span style="color: #002200;">&#93;</span> initWithTarget<span style="color: #002200;">:</span>self<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">//No warnings!!</span>
	<span style="color: #002200;">&#91;</span>myProxy doSomething<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>doSomething<span style="color: #002200;">&#123;</span>
	<span style="color: #11740a; font-style: italic;">//Does something.</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//</span>
<span style="color: #11740a; font-style: italic;">//  MyProxy.h</span>
<span style="color: #11740a; font-style: italic;">//</span>
&nbsp;
<span style="color: #6e371a;">#import &lt;Foundation/Foundation.h&gt;</span>
&nbsp;
<span style="color: #a61390;">@interface</span> MyProxy <span style="color: #002200;">:</span> <span style="color: #400080;">NSProxy</span> <span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">id</span> _target;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>initWithTarget<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>aTarget;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//</span>
<span style="color: #11740a; font-style: italic;">//  MyProxy.m</span>
<span style="color: #11740a; font-style: italic;">//</span>
&nbsp;
<span style="color: #6e371a;">#import &quot;MyProxy.h&quot;</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> MyProxy
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>initWithTarget<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>aTarget<span style="color: #002200;">&#123;</span>
	_target <span style="color: #002200;">=</span> aTarget;
	<span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">//Override</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSMethodSignature</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>methodSignatureForSelector<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">SEL</span><span style="color: #002200;">&#41;</span>aSelector<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>_target methodSignatureForSelector<span style="color: #002200;">:</span>aSelector<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">//Override</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>forwardInvocation<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSInvocation</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>anInvocation<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>anInvocation performSelectorOnMainThread<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>invokeWithTarget<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span>
		withObject<span style="color: #002200;">:</span>_target waitUntilDone<span style="color: #002200;">:</span><span style="color: #a61390;">NO</span><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>dealloc<span style="color: #002200;">&#123;</span>
	_target <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
	<span style="color: #002200;">&#91;</span>super dealloc<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<h1>Reference</h1>
<ul>
<li><a href="http://stackoverflow.com/questions/1478371/how-to-disable-warning-in-xcode-on-specific-line">message &#8211; How to disable warning in xcode on specific line ? &#8211; Stack Overflow</a></li>
</ul>
<h1>Footnotes</h1><ol class="footnotes"><li id="footnote_0_440" class="footnote">メッセージフォワーディングの具体例は<a href="http://blog.boreal-kiss.com/2010/03/02000040.html">-[NSObject performSelector:withObject]に複数の引数を渡す &#8211; boreal-kiss.com</a></li></ol>]]></content:encoded><description>例えば、NSProxyのサブクラスにメッセージのフォワーディングをさせたい時 &amp;#8224;1などにどうしてもこの警告が表示されてしまう。存在しないメソッドを呼んでいるので警告が出るのは当たり前だが放置しておくにはうっ [...]</description><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blog.boreal-kiss.com/2011/02/06000026.html/feed/</wfw:commentRss><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">0</slash:comments></item></rdf:RDF>

