<?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>forever 5 years old blog &#187; Programming</title>
	<atom:link href="http://www.f5yo.net/archives/tag/programming/feed" rel="self" type="application/rss+xml" />
	<link>http://www.f5yo.net</link>
	<description>今年も 5 歳になりました</description>
	<lastBuildDate>Wed, 29 Apr 2009 01:32:39 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Google Chrome に Livedoor Reader 風ピン機能を追加する</title>
		<link>http://www.f5yo.net/archives/146</link>
		<comments>http://www.f5yo.net/archives/146#comments</comments>
		<pubDate>Tue, 24 Mar 2009 03:50:15 +0000</pubDate>
		<dc:creator>ihara</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Greasemonkey]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://www.f5yo.net/?p=146</guid>
		<description><![CDATA[Google Reader に Livedoor Reader 風ピン機能を追加するに引き続き、Google Chrome、エクステンション機能登場という記事があったので、そのグリモンを Google Chrome で動 [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.f5yo.net/archives/145">Google Reader に Livedoor Reader 風ピン機能を追加する</a>に引き続き、<a href="http://journal.mycom.co.jp/news/2009/03/24/015/index.html">Google Chrome、エクステンション機能登場</a>という記事があったので、そのグリモンを Google Chrome で動かせないかやってみました。</p>

<p>やり方は <a href="http://dev.chromium.org/developers/design-documents/extensions/howto">Chrome Extension HOWTO</a> にある通りなのですが、順を追って書いてみると、まず c:\myextension フォルダを作成して、その中に manifest.json ファイルを作成します。内容はこんな感じ。id とか適当ですが、グリモンでいう @include を matches で指定して、JS のファイル名を js で指定しております。</p>


<div class="wp_syntax"><div class="code"><pre class="txt" style="font-family:monospace;">{
  &quot;format_version&quot;: 1,
  &quot;id&quot;: &quot;00123456789ABCDEF0123456789ABCDEF0123456&quot;,
  &quot;version&quot;: &quot;1.0&quot;,
  &quot;name&quot;: &quot;Pin Extension for Google Reader&quot;,
  &quot;description&quot;: &quot;Pin Extension for Google Reader&quot;,
  &quot;content_scripts&quot;: [
    {
      &quot;matches&quot;: [&quot;http://www.google.com/reader/view/*&quot;, &quot;https://www.google.com/reader/view/*&quot;, &quot;http://www.google.co.jp/reader/view/*&quot;, &quot;https://www.google.co.jp/reader/view/*&quot;],
      &quot;js&quot;: [&quot;GoogleReaderPin.user.js&quot;]
    }
  ]
}</pre></div></div>




<p>で、次は実際の JS ファイルの作成なんですが、<a href="http://userscripts.org/scripts/show/17714">グリモンで動かしているそのもの</a>を持ってきて c:\myextension フォルダ内に置きます。</p>

<p>で、後は Google Chrome の起動オプションに &#8211;enable-extensions &#8211;load-extension=&#8221;c:\myextension&#8221; を指定して起動。<a href="http://www.google.com/reader/view/">Google Reader</a> 行ってみると i でピン立てて o で一気に開けとるがな。素晴らしい。</p>

<p>やばい、早く Mac 版が欲しい、そう思ったお昼時でした。</p>]]></content:encoded>
			<wfw:commentRss>http://www.f5yo.net/archives/146/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Google Reader に Livedoor Reader 風ピン機能を追加する</title>
		<link>http://www.f5yo.net/archives/145</link>
		<comments>http://www.f5yo.net/archives/145#comments</comments>
		<pubDate>Sun, 22 Mar 2009 01:51:07 +0000</pubDate>
		<dc:creator>ihara</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Greasemonkey]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://www.f5yo.net/?p=145</guid>
		<description><![CDATA[昔作った Google Reader に Livedoor Reader 風ピン機能を追加する Greasemonkey スクリプトです。自分では今も便利に使っております。

i でエントリにピンを立てて、o でピン [...]]]></description>
			<content:encoded><![CDATA[<p>昔作った Google Reader に Livedoor Reader 風ピン機能を追加する Greasemonkey スクリプトです。自分では今も便利に使っております。</p>

<p>i でエントリにピンを立てて、o でピンが立っているエントリを全て開きます。</p>

<a href="http://userscripts.org/scripts/show/17714">Pin Extension for Google Reader</a>

<p>先日サイトの作りを変えたときに参照するページをなくしちゃったので追加しておきます。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.f5yo.net/archives/145/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>libzebra を使って Objective-C でバーコードを読み取る</title>
		<link>http://www.f5yo.net/archives/143</link>
		<comments>http://www.f5yo.net/archives/143#comments</comments>
		<pubDate>Fri, 20 Mar 2009 04:29:05 +0000</pubDate>
		<dc:creator>ihara</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[barcode]]></category>
		<category><![CDATA[Objective-C]]></category>

		<guid isPermaLink="false">http://www.f5yo.net/?p=143</guid>
		<description><![CDATA[Obejctvie-C ほとんど関係ありませんが。
iPhone のアプリケーションでバーコードを読み取りたいなーと思って調べていると、Zebra っちゅうのがあったので（もうすぐ名前が変わるみたい）、これ使って実現で [...]]]></description>
			<content:encoded><![CDATA[<p>Obejctvie-C ほとんど関係ありませんが。</p>
<p>iPhone のアプリケーションでバーコードを読み取りたいなーと思って調べていると、<a href="http://zebra.sourceforge.net/">Zebra</a> っちゅうのがあったので（もうすぐ名前が変わるみたい）、これ使って実現できないかやってみました。</p>

MacPorts には zebra が用意されていないようなので、自分で<a href="http://sourceforge.net/project/showfiles.php?group_id=189236">ダウンロード</a>してきてコンパイルします。

<pre language="txt">
[Masahiro@masahiro.local:~/Documents] $ tar xzvf zebra-0.6.tar.gz
[Masahiro@masahiro.local:~/Documents] $ ./zebra-0.6
[Masahiro@masahiro.local:~/Documents/zebra-0.6] $ ./configure
...
configure: error: test for video support failed!
rebuild your kernel to include video4linux support or
configure --disable-video to skip building video support.
See `config.log' for more details.
</pre>

<p>なんつって怒られてしまいました。video4linux を無効にして再チャレンジ。</p>


<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">./configure --disable-video
...
No package 'ImageMagick++' found</pre></div></div>




<p>ImageMagick 入ってないと。MacPorts で ImageMagick インストールして再々チャレンジ。</p>


<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">./configure --disable-video
...
No package 'gtk+-2.0' found</pre></div></div>




<p>きりがないわ、っちゅうことで ./configure -h でオプション見て必要なさそうなものを外します。</p>


<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">./configure --prefix=/Users/Masahiro/tmp/libzebra --disable-video --without-gtk --without-qt --without-xv --without-xshm --without-x --without-python
...
please verify that the detected configuration matches your expectations:
------------------------------------------------------------------------
X                 --with-x=no
pthreads          --enable-pthread=yes
v4l               --enable-video=no
        =&gt; zebracam video scanner will *NOT* be built
jpeg              --with-jpeg=no
        =&gt; JPEG image conversions will *NOT* be supported
Magick++          --with-imagemagick=yes
Python            --with-python=no
GTK+              --with-gtk=no
        =&gt; the GTK+ widget will *NOT* be built
Qt4               --with-qt=no
        =&gt; the Qt4 widget will *NOT* be built</pre></div></div>




<p>自分のホームの tmp/libzebra に入れておくことにして、make &#038; make install でインストール。</p>

<p>で、次は Objective-C でこのライブラリを使ってバーコードを読み取るコードを書いてみます。テストで使うバーコードは<a href="http://apps.sourceforge.net/mediawiki/zebra/index.php?title=Troubleshooting#Size.2FResolution">ここ</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;">#import &lt;zebra.h&gt;</span>
&nbsp;
<span style="color: #6e371a;">#define fourcc(a, b, c, d) \</span>
    <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>uint32_t<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#40;</span>a<span style="color: #002200;">&#41;</span> | <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>uint32_t<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#40;</span>b<span style="color: #002200;">&#41;</span> &lt;&lt; <span style="color: #2400d9;">8</span><span style="color: #002200;">&#41;</span> | \
    <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>uint32_t<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#40;</span>c<span style="color: #002200;">&#41;</span> &lt;&lt; <span style="color: #2400d9;">16</span><span style="color: #002200;">&#41;</span> | <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>uint32_t<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#40;</span>d<span style="color: #002200;">&#41;</span> &lt;&lt; <span style="color: #2400d9;">24</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>
&nbsp;
<span style="color: #a61390;">void</span> dataHandler<span style="color: #002200;">&#40;</span>zebra_image_t<span style="color: #002200;">*</span> zimage, <span style="color: #a61390;">const</span> <span style="color: #a61390;">void</span><span style="color: #002200;">*</span> userdata<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
    NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;succeeded to scan barcode&quot;</span><span style="color: #002200;">&#41;</span>;
    <span style="color: #a61390;">const</span> zebra_symbol_t<span style="color: #002200;">*</span> symbol <span style="color: #002200;">=</span> zebra_image_first_symbol<span style="color: #002200;">&#40;</span>zimage<span style="color: #002200;">&#41;</span>;
    zebra_symbol_type_t type <span style="color: #002200;">=</span> zebra_symbol_get_type<span style="color: #002200;">&#40;</span>symbol<span style="color: #002200;">&#41;</span>;
    <span style="color: #a61390;">const</span> <span style="color: #a61390;">char</span><span style="color: #002200;">*</span> data <span style="color: #002200;">=</span> zebra_symbol_get_data<span style="color: #002200;">&#40;</span>symbol<span style="color: #002200;">&#41;</span>;
    NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;type: %d, data: %s&quot;</span>, type, data<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">void</span> cleanupHandler<span style="color: #002200;">&#40;</span>zebra_image_t<span style="color: #002200;">*</span> zimage<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
    NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;cleanup called&quot;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
CGImageRef createCGImageFromData<span style="color: #002200;">&#40;</span><span style="color: #400080;">NSData</span><span style="color: #002200;">*</span> data<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
    CGImageRef imageRef <span style="color: #002200;">=</span> <span style="color: #a61390;">NULL</span>;
    CGImageSourceRef sourceRef <span style="color: #002200;">=</span> CGImageSourceCreateWithData<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>CFDataRef<span style="color: #002200;">&#41;</span>data, <span style="color: #a61390;">NULL</span><span style="color: #002200;">&#41;</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>sourceRef<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        imageRef <span style="color: #002200;">=</span> CGImageSourceCreateImageAtIndex<span style="color: #002200;">&#40;</span>sourceRef, <span style="color: #2400d9;">0</span>, <span style="color: #a61390;">NULL</span><span style="color: #002200;">&#41;</span>;
        CFRelease<span style="color: #002200;">&#40;</span>sourceRef<span style="color: #002200;">&#41;</span>;
    <span style="color: #002200;">&#125;</span>
    <span style="color: #a61390;">return</span> imageRef;
<span style="color: #002200;">&#125;</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: #11740a; font-style: italic;">//</span>
    <span style="color: #11740a; font-style: italic;">// ファイルから画像を読み込みビットマップに変換する</span>
    <span style="color: #11740a; font-style: italic;">//</span>
    <span style="color: #400080;">NSData</span><span style="color: #002200;">*</span> data <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;">NSData</span> alloc<span style="color: #002200;">&#93;</span> initWithContentsOfFile<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;/Users/Masahiro/Desktop/Zbar.114.png&quot;</span><span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
    CGImageRef image <span style="color: #002200;">=</span> createCGImageFromData<span style="color: #002200;">&#40;</span>data<span style="color: #002200;">&#41;</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span>image<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;faild to execute createCGImageFromData&quot;</span><span style="color: #002200;">&#41;</span>;
        <span style="color: #a61390;">exit</span><span style="color: #002200;">&#40;</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#41;</span>;
    <span style="color: #002200;">&#125;</span>
    <span style="color: #a61390;">int</span> width <span style="color: #002200;">=</span> CGImageGetWidth<span style="color: #002200;">&#40;</span>image<span style="color: #002200;">&#41;</span>;
    <span style="color: #a61390;">int</span> height <span style="color: #002200;">=</span> CGImageGetHeight<span style="color: #002200;">&#40;</span>image<span style="color: #002200;">&#41;</span>;
    CGRect rect <span style="color: #002200;">=</span> CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">0</span>, width, height<span style="color: #002200;">&#41;</span>;
    <span style="color: #a61390;">int</span> bitsPerComponent <span style="color: #002200;">=</span> <span style="color: #2400d9;">8</span>;
    <span style="color: #a61390;">int</span> bytesPerRow <span style="color: #002200;">=</span> width;
    <span style="color: #a61390;">int</span> bitmapByteCount <span style="color: #002200;">=</span> bytesPerRow <span style="color: #002200;">*</span> height;
    <span style="color: #a61390;">void</span><span style="color: #002200;">*</span> bitmap <span style="color: #002200;">=</span> <span style="color: #a61390;">malloc</span><span style="color: #002200;">&#40;</span>bitmapByteCount<span style="color: #002200;">&#41;</span>;
    CGColorSpaceRef colorspace <span style="color: #002200;">=</span> CGColorSpaceCreateDeviceGray<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
    CGContextRef c <span style="color: #002200;">=</span> CGBitmapContextCreate<span style="color: #002200;">&#40;</span>bitmap,
                                           width,
                                           height,
                                           bitsPerComponent,
                                           bytesPerRow,
                                           colorspace,
                                           kCGImageAlphaNone<span style="color: #002200;">&#41;</span>;
    CGColorSpaceRelease<span style="color: #002200;">&#40;</span>colorspace<span style="color: #002200;">&#41;</span>;
    CGContextDrawImage<span style="color: #002200;">&#40;</span>c, rect, image<span style="color: #002200;">&#41;</span>;
    CGContextRelease<span style="color: #002200;">&#40;</span>c<span style="color: #002200;">&#41;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">//</span>
    <span style="color: #11740a; font-style: italic;">// バーコードを読み取る</span>
    <span style="color: #11740a; font-style: italic;">//</span>
    zebra_image_scanner_t<span style="color: #002200;">*</span> scanner <span style="color: #002200;">=</span> zebra_image_scanner_create<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
    zebra_image_scanner_set_data_handler<span style="color: #002200;">&#40;</span>scanner, dataHandler, <span style="color: #a61390;">NULL</span><span style="color: #002200;">&#41;</span>;
    zebra_image_t<span style="color: #002200;">*</span> zimage <span style="color: #002200;">=</span> zebra_image_create<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
    zebra_image_set_format<span style="color: #002200;">&#40;</span>zimage, fourcc<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">'Y'</span>, <span style="color: #bf1d1a;">'8'</span>, <span style="color: #bf1d1a;">'0'</span>, <span style="color: #bf1d1a;">'0'</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
    zebra_image_set_size<span style="color: #002200;">&#40;</span>zimage, width, height<span style="color: #002200;">&#41;</span>;
    zebra_image_set_data<span style="color: #002200;">&#40;</span>zimage, bitmap, bitmapByteCount, cleanupHandler<span style="color: #002200;">&#41;</span>;
    zebra_scan_image<span style="color: #002200;">&#40;</span>scanner, zimage<span style="color: #002200;">&#41;</span>;
    zebra_image_destroy<span style="color: #002200;">&#40;</span>zimage<span style="color: #002200;">&#41;</span>;
    zebra_image_scanner_destroy<span style="color: #002200;">&#40;</span>scanner<span style="color: #002200;">&#41;</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></pre></div></div>




<p>エラー処理丁寧に入れていませんが、こんな感じ。Xcode でビルドするときに気をつけないといけないのは、ヘッダのパス（/Users/Masahiro/tmp/libzebra/include）を追加することと、ライブラリを追加すること（今回はスタティックライブラリを使うことにして /Users/Masahiro/tmp/libzebra/lib/libzebra.a）。Xcode でリンクするライブラリの追加はファイルをドラッグ＆ドロップなんですね。それくらいっすかね。ああ、あとプロジェクトをテスト用に Foundation Tool で作っていまして、CGImage を扱うために ApplicationServices フレームワークを追加してあげないといけませんでした。普通に Cocoa Application だったりすると必要ないのかもしれません。</p>

<p>で、実行してみると、</p>


<div class="wp_syntax"><div class="code"><pre class="txt" style="font-family:monospace;">2009-03-20 13:18:51.084 ObjC Test[98413:10b] succeeded to scan barcode
2009-03-20 13:18:51.086 ObjC Test[98413:10b] type: 13, data: 9876543210128
2009-03-20 13:18:51.087 ObjC Test[98413:10b] cleanup called</pre></div></div>




<p>こんな感じで、タイプが 13（zebra.h の zebra_symbol_type_e で ZEBRA_EAN13）、バーコードが 9876543210128 と読み取れております。素晴らしい。</p>]]></content:encoded>
			<wfw:commentRss>http://www.f5yo.net/archives/143/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>デリゲートって何？</title>
		<link>http://www.f5yo.net/archives/141</link>
		<comments>http://www.f5yo.net/archives/141#comments</comments>
		<pubDate>Thu, 05 Mar 2009 13:42:13 +0000</pubDate>
		<dc:creator>ihara</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Objective-C]]></category>

		<guid isPermaLink="false">http://www.f5yo.net/?p=141</guid>
		<description><![CDATA[今日は「非形式プロトコル」で出てきた、デリゲートについてです。
「詳解 Objective-C 2.0」の「14-01 デリゲート」を読んでみるとよくわかります。オブジェクト指向プログラミングでいう「委譲」っちゅうやつ [...]]]></description>
			<content:encoded><![CDATA[<p>今日は「<a href="http://www.f5yo.net/archives/140">非形式プロトコル</a>」で出てきた、デリゲートについてです。</p>
<p>「詳解 Objective-C 2.0」の「14-01 デリゲート」を読んでみるとよくわかります。オブジェクト指向プログラミングでいう「<a href="http://ja.wikipedia.org/wiki/%E5%A7%94%E8%AD%B2">委譲</a>」っちゅうやつですね。もしくは、コールバック関数って感じで、デフォルトの実装を提供してもしなくても良いし、それの代わりに処理を行うものが指定されていれば、それを呼び出すという。</p>
<p>その話は良い本を読むなりしましょうというわけで、簡単に例だけ書いてみました。</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>
&nbsp;
<span style="color: #a61390;">@interface</span> MyObject1 <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span>
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">id</span> delegate;
<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>hello;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setDelegate<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>anObject;
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> MyObject1
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>hello <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>delegate <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Hello&quot;</span><span style="color: #002200;">&#41;</span>;
    <span style="color: #002200;">&#125;</span> <span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>delegate respondsToSelector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>hello<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #002200;">&#91;</span>delegate hello<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setDelegate<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>anObject <span style="color: #002200;">&#123;</span>
    delegate <span style="color: #002200;">=</span> anObject;
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #a61390;">@interface</span> MyObject2 <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>hello;
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> MyObject2
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>hello <span style="color: #002200;">&#123;</span>
    NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;こんにちは&quot;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@end</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;
    MyObject1<span style="color: #002200;">*</span> myObject1 <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>MyObject1 alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
    <span style="color: #11740a; font-style: italic;">// 以下 2 行を有効にすると、myObject1 は myObject2 に処理を移譲し、&quot;こんにちは&quot; が表示される</span>
    <span style="color: #11740a; font-style: italic;">// MyObject2* myObject2 = [[[MyObject2 alloc] init] autorelease];</span>
    <span style="color: #11740a; font-style: italic;">// [myObject1 setDelegate:myObject2];</span>
    <span style="color: #002200;">&#91;</span>myObject1 hello<span style="color: #002200;">&#93;</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></pre></div></div>



<p>デリゲート（myObject2）を指定しなければ myObject1 の hello メソッドが呼び出されて &#8220;Hello&#8221; と表示されるし、デリゲートを指定すればその hello メソッドが呼び出されて &#8220;こんにちは&#8221; が表示される、という感じ。</p>
<p>こんなもんスよね。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.f5yo.net/archives/141/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>非形式プロトコルって何？</title>
		<link>http://www.f5yo.net/archives/140</link>
		<comments>http://www.f5yo.net/archives/140#comments</comments>
		<pubDate>Wed, 04 Mar 2009 14:06:35 +0000</pubDate>
		<dc:creator>ihara</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Objective-C]]></category>

		<guid isPermaLink="false">http://www.f5yo.net/?p=140</guid>
		<description><![CDATA[「プロトコルって何？」の続きで、非公式プロトコルって何？です。
「詳解 Objective-C 2.0」の「11-03 非形式プロトコル」を読んでみると、非形式プロトコルは @protocol ではなくて NSObje [...]]]></description>
			<content:encoded><![CDATA[<p>「<a href="http://www.f5yo.net/archives/138">プロトコルって何？</a>」の続きで、非公式プロトコルって何？です。</p>
<p>「詳解 Objective-C 2.0」の「11-03 非形式プロトコル」を読んでみると、非形式プロトコルは @protocol ではなくて NSObject のカテゴリとして宣言する、カテゴリなのでコンパイル時に適合しているかどうかはチェックされない、とのこと。デリゲート（デリゲートって何？をやらないと）で使われることが多いそうですが、何のメリットがあるのかさっぱりわがんね。</p>
<p>というわけで、「<a href="http://journal.mycom.co.jp/column/objc/010/index.html">非形式プロトコル &#8211; もう1つのプロトコル</a>」と「<a href="http://journal.mycom.co.jp/column/objc/011/index.html">2つのプロトコルの使い分け</a>」を読んでみました。なるほど、デリゲートで呼び出されるメソッドは数は多いけど実際に全てを使わないといけいないことはそれほどなく、プロトコルで宣言してしまうとそれらを実装する必要が出てきてしまう（といっても警告出るだけだと思いますが）、なので非形式プロトコルにして空の実装がたくさんできてしまうようなことは避けましょう、と。うおー！とはならないですが、そんなもんなんですね。</p>
<p>「<a href="http://journal.mycom.co.jp/column/objc/011/index.html">2つのプロトコルの使い分け</a>」の最後にあるように、
<ul>
	<li>すべてのメソッドを実装する必要があり、インスタンス化をする前にチェックを行いたい場合は、形式プロトコル</li>
	<li>任意のメソッドだけ実装すればよくて、すでにインスタンス化されたオブジェクトを指定する場合は、非形式プロトコル </li>
</ul>
って覚えます。
</p>
<p>うーんソースが書けんかった。</p>]]></content:encoded>
			<wfw:commentRss>http://www.f5yo.net/archives/140/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>プロトコルって何？</title>
		<link>http://www.f5yo.net/archives/138</link>
		<comments>http://www.f5yo.net/archives/138#comments</comments>
		<pubDate>Sun, 01 Mar 2009 07:54:07 +0000</pubDate>
		<dc:creator>ihara</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Objective-C]]></category>

		<guid isPermaLink="false">http://www.f5yo.net/?p=138</guid>
		<description><![CDATA[今日はプロトコルって何？というわけで、「詳解 Objective-C 2.0」の「11-01 プロトコルの概念」「11-02 Objective-C におけるプロトコルの宣言」あたりを読んでみると、

	プロトコル  [...]]]></description>
			<content:encoded><![CDATA[<p>今日はプロトコルって何？というわけで、「詳解 Objective-C 2.0」の「11-01 プロトコルの概念」「11-02 Objective-C におけるプロトコルの宣言」あたりを読んでみると、
<ul>
	<li>プロトコル == Java でいうインタフェース == C++ でいう純粋仮想クラス</li>
	<li>「採用」はプロトコルを指定してクラスのインタフェースを宣言すること</li>
	<li>「適合」はプロトコルに含まれるメソッドを実装すること</li>
</ul>
って感じ。以上、なんですが、ちょっと試してみます。</p>
<p>以下だと MyObject1 に hello メソッドは用意されていない旨の警告を出しながらコンパイルは成功します。</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>
&nbsp;
<span style="color: #a61390;">@interface</span> MyObject1 <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span>
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> MyObject1
<span style="color: #a61390;">@end</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;
    MyObject1<span style="color: #002200;">*</span> myObject1 <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>MyObject1 alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>myObject1 hello<span style="color: #002200;">&#93;</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></pre></div></div>



<p>そこを、以下のように Greetings プロトコルを宣言して採用しておくと、こっちは MyObject1 が Greetings プロトコルを実装していない旨の警告を出しながらコンパイルは成功します。どっちにしろ Objective-C ではコンパイルは通ると。</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>
&nbsp;
<span style="color: #a61390;">@protocol</span> Greetings
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>hello;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>goodby;
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #a61390;">@interface</span> MyObject1 <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span> &lt;Greetings&gt;
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> MyObject1
<span style="color: #a61390;">@end</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;
    MyObject1<span style="color: #002200;">*</span> myObject1 <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>MyObject1 alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>myObject1 hello<span style="color: #002200;">&#93;</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></pre></div></div>



<p>以下のように hello と goodby を実装してあげると、無事警告はなくなります。</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>
&nbsp;
<span style="color: #a61390;">@protocol</span> Greetings
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>hello;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>goodby;
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #a61390;">@interface</span> MyObject1 <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span> &lt;Greetings&gt;
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> MyObject1
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>hello <span style="color: #002200;">&#123;</span>
    NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;hello&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>goodby <span style="color: #002200;">&#123;</span>
    NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;goodby&quot;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@end</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;
    MyObject1<span style="color: #002200;">*</span> myObject1 <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>MyObject1 alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>myObject1 hello<span style="color: #002200;">&#93;</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></pre></div></div>



<p>にゃるほど。</p>
<p>最後、NSObject の conformsToProtocol: を使うとオブジェクトがプロトコルに適合しているかどうか調べられるというわけでやってみます。</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>
&nbsp;
<span style="color: #a61390;">@protocol</span> Greetings
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>hello;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>goodby;
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #a61390;">@interface</span> MyObject1 <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span> &lt;Greetings&gt;
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> MyObject1
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>hello <span style="color: #002200;">&#123;</span>
    NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;hello&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>goodby <span style="color: #002200;">&#123;</span>
    NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;goodby&quot;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@end</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;
    MyObject1<span style="color: #002200;">*</span> myObject1 <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>MyObject1 alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>myObject1 conformsToProtocol<span style="color: #002200;">:</span><span style="color: #a61390;">@protocol</span><span style="color: #002200;">&#40;</span>Greetings<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;myObject1 conforms to Greetings protocol&quot;</span><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></pre></div></div>



<p>無事 &#8220;myObject1 conforms to Greetings protocol&#8221; が表示されます。じゃあ MyObject1 の hello メソッドの実装をコメントアウトして同じことをやってみたらどうなるのと思って試してみると、同じように &#8220;myObject1 conforms to Greetings protocol&#8221; と表示されとる。。じゃあ「採用」と「適合」の違いは？</p>
<p>「詳解 Objective-C 2.0」の「11-02 Objective-C におけるプロトコルの宣言」の「必須機能とオプションの指定」には、@required と @optional とでプロトコルを採用したクラスで実装必須なメソッドとそうでないメソッドを指定できるとのことなので、上の動きを見てみると、conformsToProtocol: よりも adoptsToProtocol: が名前として合っていて、プロトコルで @required と @optional を指定しない場合は @optional が指定されたのと同じ、が正しいような感じがしますが。。</p>
<p>というわけで、まだよく理解できていないんだなこれは。アガガ。</p>
<p>[2009/03/02 追記]<br />
「<a href="http://journal.mycom.co.jp/column/objc/009/index.html">プロトコルが必要とされた背景とは? &#8211; なぜあえて静的な型を?</a>」を読んでみると「オブジェクトがこのプロトコルを採用することを、プロトコルに適合している、という。」とのことなので、採用 == 適合、という理解で良いか。
</p>
<p>[2009/03/04 追記]<br />
@required と @optional については、@optional を指定するとメソッドの実装がなくてもコンパイラに怒られない（警告が出ない）だけですね。]]></content:encoded>
			<wfw:commentRss>http://www.f5yo.net/archives/138/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>カテゴリって何？</title>
		<link>http://www.f5yo.net/archives/136</link>
		<comments>http://www.f5yo.net/archives/136#comments</comments>
		<pubDate>Fri, 27 Feb 2009 12:40:44 +0000</pubDate>
		<dc:creator>ihara</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Objective-C]]></category>

		<guid isPermaLink="false">http://www.f5yo.net/?p=136</guid>
		<description><![CDATA[今日はカテゴリって何？というわけで、手元に書籍がないので「カテゴリ &#8211; 動的なメソッドの追加によるクラスの拡張」を読んでみました。
カテゴリを使うと、直接的には「クラスが持つメソッドをカテゴリに分けて管理す [...]]]></description>
			<content:encoded><![CDATA[<p>今日はカテゴリって何？というわけで、手元に書籍がないので「<a href="http://journal.mycom.co.jp/column/objc/008/index.html">カテゴリ &#8211; 動的なメソッドの追加によるクラスの拡張</a>」を読んでみました。</p>
<p>カテゴリを使うと、直接的には「クラスが持つメソッドをカテゴリに分けて管理することができる」、仕組み的には「Objective-C が提供する、動的に既存のクラスにメソッドを追加する機能により実現している」とのこと。
</p>
<p>早速試してみます。そのままやると上記の例のままになっちゃうので、異なるカテゴリ名で、同名のメソッドを追加してみます。</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>
&nbsp;
<span style="color: #a61390;">@interface</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">&#40;</span>Greetings1<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>hello;
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">&#40;</span>Greetings1<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>hello <span style="color: #002200;">&#123;</span>
    NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Hello! in Greetings1&quot;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #a61390;">@interface</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">&#40;</span>Greetings2<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>hello;
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">&#40;</span>Greetings2<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>hello <span style="color: #002200;">&#123;</span>
    NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Hello! in Greetings2&quot;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@end</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: #11740a; font-style: italic;">//</span>
    <span style="color: #11740a; font-style: italic;">// NSString の拡張メソッド hello を呼び出す</span>
    <span style="color: #11740a; font-style: italic;">//</span>
    <span style="color: #400080;">NSString</span><span style="color: #002200;">*</span> myNSString <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> initWithUTF8String<span style="color: #002200;">:</span><span style="color: #bf1d1a;">&quot;hoge&quot;</span><span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>myNSString hello<span style="color: #002200;">&#93;</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></pre></div></div>



<p>Greetings1 と Greetings2 カテゴリで hello メソッドを追加してみました。ビルドして実行すると、&#8221;Hello! in Greetings2&#8243; と、後ろで定義した方が呼び出されている模様。</p>
<p>nm でオブジェクトファイルに含まれるシンボルを見てみるとこんな感じ。-[NSString(Greetings1) hello] と -[NSString(Greetings2) hello] が存在しています。
</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="text" style="font-family:monospace;">[maihara@B-2008-202.local:~/Documents/ObjC Test] $ nm build/ObjC\ Test.build/Debug/ObjC\ Test.build/Objects-normal/i386/ObjC\ Test.o
00000000 t -[NSString(Greetings1) hello]
00000027 t -[NSString(Greetings2) hello]
00000000 A .objc_category_name_NSString_Greetings1
00000000 A .objc_category_name_NSString_Greetings2
         U .objc_class_name_NSAutoreleasePool
         U .objc_class_name_NSString
         U _NSLog
         U ___CFConstantStringClassReference
0000004e T _main
         U _objc_msgSend</pre></td></tr></table></div>



<p>シンボルは異なるので同名のメソッドは存在することができて、呼び出すと最後に定義されたものが呼び出されるのか。同じクラスの異なるカテゴリで同じメソッド名のものを提供するとどれが呼び出されるかわからないからいかんヨ、ということですかね。</p>
<p>
[2009/02/28 追記]<br />
「詳解 Objective-C 2.0」の「09-02 既存クラスへのカテゴリの追加」の「既存メソッドの上書き」を読んでみると、同名のメソッドが複数のカテゴリで定義されていると、どの定義が有効になるかは保証されていない、とのことでした。なるほど。
</p>]]></content:encoded>
			<wfw:commentRss>http://www.f5yo.net/archives/136/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>@selector って何？</title>
		<link>http://www.f5yo.net/archives/78</link>
		<comments>http://www.f5yo.net/archives/78#comments</comments>
		<pubDate>Sat, 21 Feb 2009 09:43:03 +0000</pubDate>
		<dc:creator>ihara</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Objective-C]]></category>

		<guid isPermaLink="false">http://www.f5yo.net/?p=78</guid>
		<description><![CDATA[iPhone デベロッパーズクックブックを読んでいこうとしているのですが、そもそも Objective-C 自体を理解できていないので、わからないところを調べながら進めます。
というわけで、@selector って何？ [...]]]></description>
			<content:encoded><![CDATA[<p>iPhone デベロッパーズクックブックを読んでいこうとしているのですが、そもそも Objective-C 自体を理解できていないので、わからないところを調べながら進めます。</p>
<p>というわけで、@selector って何？と思って「詳解 Objective-C 2.0」の「07-02 メッセージ送信の仕組み」の「セレクタと SEL 型」と「メッセージ探索」を読んでみました。</p>
<p>そもそも、セレクタ == メッセージセレクタ == メソッド名。@selector はそのセレクタがコンパイル時に変換される内部表現を、プログラム上で扱えるようにするコンパイラ指示子とのこと。Objective-C では実行するメソッドは実行時に動的に決定されるということで、これを使うと動的にメソッドを呼び分けられるということです。</p>
<p>早速試してみます。</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>
&nbsp;
<span style="color: #a61390;">@interface</span> MyObject1 <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span> <span style="color: #002200;">&#123;</span>
<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>hello;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>goodby;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>goodMorning<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>firstName lastName<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>lastName;
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> MyObject1
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>hello <span style="color: #002200;">&#123;</span>
    NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;hello&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>goodby <span style="color: #002200;">&#123;</span>
    NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;goodby&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>goodMorning<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>firstName lastName<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>lastName <span style="color: #002200;">&#123;</span>
    NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;good morning, %@ %@&quot;</span>, lastName, firstName<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@end</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: #11740a; font-style: italic;">//</span>
    <span style="color: #11740a; font-style: italic;">// 現在の時間によりメソッドを呼び分ける</span>
    <span style="color: #11740a; font-style: italic;">//</span>
    MyObject1<span style="color: #002200;">*</span> myObject1 <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>MyObject1 alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">time_t</span> now <span style="color: #002200;">=</span> <span style="color: #a61390;">time</span><span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>;
    <span style="color: #a61390;">struct</span> <span style="color: #a61390;">tm</span><span style="color: #002200;">*</span> t <span style="color: #002200;">=</span> <span style="color: #a61390;">localtime</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&amp;</span>now<span style="color: #002200;">&#41;</span>;
    <span style="color: #a61390;">SEL</span> method <span style="color: #002200;">=</span> t<span style="color: #002200;">-</span>&gt;tm_hour &lt; <span style="color: #2400d9;">12</span> ? <span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>hello<span style="color: #002200;">&#41;</span> <span style="color: #002200;">:</span> <span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>goodby<span style="color: #002200;">&#41;</span>;
    <span style="color: #002200;">&#91;</span>myObject1 performSelector<span style="color: #002200;">:</span>method<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">//</span>
    <span style="color: #11740a; font-style: italic;">// goodMorning メソッドを実装しているか調べる</span>
    <span style="color: #11740a; font-style: italic;">//</span>
    <span style="color: #400080;">NSString</span><span style="color: #002200;">*</span> msg <span style="color: #002200;">=</span> <span style="color: #a61390;">NULL</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>myObject1 respondsToSelector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>goodMorning<span style="color: #002200;">:</span>lastName<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        msg <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithCString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">&quot;goodMorning:lastName: is implemented&quot;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span> <span style="color: #a61390;">else</span> <span style="color: #002200;">&#123;</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><span style="color: #bf1d1a;">&quot;goodMorning:lastName: is NOT implemented&quot;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
    NSLog<span style="color: #002200;">&#40;</span>msg<span style="color: #002200;">&#41;</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></pre></div></div>



<p>前の方では時間によって MyObject の hello メソッドと goodby メソッドを呼び分け、後ろの方では MyObject に goodMorning:lastName: メソッドが実装されているか調べています。</p>
<p>この例だといまいちこのメソッド呼び出しの動的な側面が表現できていないと思いますが、メソッドが実装されてさえいればそのオブジェクトが何であれ関係ないところは、メッセージの送信先を動的に変えたいところとかで便利そうですね。なるほど。</p>
<p>と思って iPhone デベロッパーズクックブックに戻ってみたら @selector でてきてなかったや。キャハ。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.f5yo.net/archives/78/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
