<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.seiji.me/~d/styles/itemcontent.css"?><rss 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/" version="2.0">

<channel>
	<title>seiji blog</title>
	
	<link>http://blog.seiji.me</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Wed, 03 Mar 2010 14:50:33 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.seiji.me/seiji" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="seiji" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Test</title>
		<link>http://blog.seiji.me/465.html</link>
		<comments>http://blog.seiji.me/465.html#comments</comments>
		<pubDate>Wed, 03 Mar 2010 14:48:02 +0000</pubDate>
		<dc:creator>seiji</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.seiji.me/465.html</guid>
		<description><![CDATA[test

edit
]]></description>
			<content:encoded><![CDATA[<p>test</p>
<p><span id="more-465"></span></p>
<p>edit</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.seiji.me/465.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>4ビットマイコン (大人の科学マガジンvol24)</title>
		<link>http://blog.seiji.me/460.html</link>
		<comments>http://blog.seiji.me/460.html#comments</comments>
		<pubDate>Fri, 02 Oct 2009 15:48:08 +0000</pubDate>
		<dc:creator>seiji</dc:creator>
				<category><![CDATA[Hardware]]></category>

		<guid isPermaLink="false">http://blog.seiji.me/?p=460</guid>
		<description><![CDATA[4ビットマイコン (大人の科学マガジンvol24)を買った。

	

	



	
	
こんな感じで入ってる。


	
	
簡単に組み立てできる。


プログラムメモリ:40byte
データメモリ:8byte
レジスタ: 8個
入力: キーパッド
出力: 2進LED, 数字LED, スピーカー

各アドレスに値を入れたり、プログラムを実行したり、電子音を鳴らしたりできる。なかなかおもしろい。
]]></description>
			<content:encoded><![CDATA[<p>4ビットマイコン (大人の科学マガジンvol24)を買った。</p>
<p>
	<br />
<a href="http://www.flickr.com/photos/31821868@N05/3974639920" title="View 'photo' on Flickr.com"><img src="http://farm3.static.flickr.com/2547/3974639920_20c26c4388.jpg" alt="photo" border="0" width="375" height="500" /></a><br />
	
</p>
<p><span id="more-460"></span>
<p>
	<br />
	<a href="http://www.flickr.com/photos/31821868@N05/3974634926" title="View 'photo' on Flickr.com"><img src="http://farm3.static.flickr.com/2472/3974634926_d853a3f064.jpg" alt="photo" border="0" width="500" height="375" /></a><br />
こんな感じで入ってる。
</p>
<p>
	<br />
	<a href="http://www.flickr.com/photos/31821868@N05/3974804984" title="View 'photo' on Flickr.com"><img src="http://farm3.static.flickr.com/2449/3974804984_dba5c064aa.jpg" alt="photo" border="0" width="500" height="375" /></a><br />
簡単に組み立てできる。
</p>
<ul>
<li>プログラムメモリ:40byte</li>
<li>データメモリ:8byte</li>
<li>レジスタ: 8個</li>
<li>入力: キーパッド</li>
<li>出力: 2進LED, 数字LED, スピーカー</li>
</ul>
<p>各アドレスに値を入れたり、プログラムを実行したり、電子音を鳴らしたりできる。なかなかおもしろい。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.seiji.me/460.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MacBookのHD交換</title>
		<link>http://blog.seiji.me/437.html</link>
		<comments>http://blog.seiji.me/437.html#comments</comments>
		<pubDate>Sun, 28 Dec 2008 13:12:34 +0000</pubDate>
		<dc:creator>seiji</dc:creator>
				<category><![CDATA[Apple]]></category>
		<category><![CDATA[Mac]]></category>
		<category><![CDATA[HD交換]]></category>
		<category><![CDATA[MacBook]]></category>

		<guid isPermaLink="false">http://blog.seiji.me/?p=437</guid>
		<description><![CDATA[HDの残り容量が少なくなってきたので、120Gのものから500Gのものに交換しました。

	
	
	



秋葉原のドスパラでWestern DigitalのWD5000BEVTが8,980円で売っていたので買ってきました。

ディスクユーティリティで復元を行い、HDを付け替え。
起動に時間がかかりましたが、問題なく交換できました。
Before



After




MacBookのHD交換はすぐ交換できるので楽です。
これでしばらくは容量を気にしなくてもよさそうです。
]]></description>
			<content:encoded><![CDATA[<p>HDの残り容量が少なくなってきたので、120Gのものから500Gのものに交換しました。</p>
<p>
	<br />
	<img src="http://blog.seiji.me/wp-content/uploads/2008/12/img-0023.jpg" alt="IMG_0023.JPG" border="0" width="200" height="266" /><br />
	
</p>
<p><span id="more-437"></span>
<p>
秋葉原のドスパラでWestern Digitalの<a href="http://www.wdc.com/jp/products/Products.asp?DriveID=506">WD5000BEVT</a>が8,980円で売っていたので買ってきました。
</p>
<p>ディスクユーティリティで復元を行い、HDを付け替え。<br />
起動に時間がかかりましたが、問題なく交換できました。</p>
<h4>Before</h4>
<p>
<img src="http://blog.seiji.me/wp-content/uploads/2008/12/before.jpg" alt="before.jpg" border="0" width="380" height="189" /><br />
</p>
<h4>After</h4>
<p>
<br />
<img src="http://blog.seiji.me/wp-content/uploads/2008/12/smb-1.jpg" alt="smb-1.jpg" border="0" width="380" height="206" /><br />
</p>
<p>MacBookのHD交換はすぐ交換できるので楽です。</p>
<p>これでしばらくは容量を気にしなくてもよさそうです。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.seiji.me/437.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Making a CGRect</title>
		<link>http://blog.seiji.me/421.html</link>
		<comments>http://blog.seiji.me/421.html#comments</comments>
		<pubDate>Fri, 26 Dec 2008 12:45:09 +0000</pubDate>
		<dc:creator>seiji</dc:creator>
				<category><![CDATA[Apple]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[CGRect]]></category>

		<guid isPermaLink="false">http://blog.seiji.me/?p=421</guid>
		<description><![CDATA[
	

	
CGRectには様々な作成方法があります。それをまとめておきます。

下のようなCGRectを作成したいと思います。

出力
HelloWorld[52159:20b] {{0, 0}, {320, 480}}



作成方法1
CGRect rect = CGRectMake(0, 0, 320, 480);

作成方法2
CGRect rect = {{0,0}, {320, 480}};

作成方法3
CGSize size = CGSizeMake(320, 480);
CGRect rect = {{0,0}, size};

作成方法4
CGPoint point = CGPointMake(0, 0);
CGSize size = CGSizeMake(320, 480);
CGRect rect = {point, size};

作成方法5
CGSize size = CGSizeMake(320, 480);
CGRect rect = {{0}, size};

作成方法6
CGSize size = CGSizeMake(320, 480);
CGRect rect = {{}, size};

他にもいろいろあるかとは思いますが、構造で定義できるのは便利ですね。
]]></description>
			<content:encoded><![CDATA[<p>
	<br />
<img src="http://blog.seiji.me/wp-content/uploads/2008/12/making-cgrect1.png" alt="Making CGRect.png" border="0" width="210" height="181" /><br />
	</p>
<p>CGRectには様々な作成方法があります。それをまとめておきます。
</p>
<p>下のようなCGRectを作成したいと思います。
</p>
<h4>出力</h4>
<pre class="console"><code>HelloWorld[52159:20b] {{0, 0}, {320, 480}}
</code></pre>
<p></p>
<p><span id="more-421"></span><br />
<h4>作成方法1</h4>
<pre><code>CGRect rect = CGRectMake(0, 0, 320, 480);
</code></pre>
<h4>作成方法2</h4>
<pre><code>CGRect rect = {{0,0}, {320, 480}};
</code></pre>
<h4>作成方法3</h4>
<pre><code>CGSize size = CGSizeMake(320, 480);
CGRect rect = {{0,0}, size};
</code></pre>
<h4>作成方法4</h4>
<pre><code>CGPoint point = CGPointMake(0, 0);
CGSize size = CGSizeMake(320, 480);
CGRect rect = {point, size};
</code></pre>
<h4>作成方法5</h4>
<pre><code>CGSize size = CGSizeMake(320, 480);
CGRect rect = {{0}, size};
</code></pre>
<h4>作成方法6</h4>
<pre><code>CGSize size = CGSizeMake(320, 480);
CGRect rect = {{}, size};
</code></pre>
<p>他にもいろいろあるかとは思いますが、構造で定義できるのは便利ですね。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.seiji.me/421.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>^NSStringFrom.* functions</title>
		<link>http://blog.seiji.me/405.html</link>
		<comments>http://blog.seiji.me/405.html#comments</comments>
		<pubDate>Thu, 25 Dec 2008 10:54:55 +0000</pubDate>
		<dc:creator>seiji</dc:creator>
				<category><![CDATA[Apple]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[NSStringFromCGRect]]></category>

		<guid isPermaLink="false">http://blog.seiji.me/?p=405</guid>
		<description><![CDATA[NSStringFromから続くFunctionは非常に便利です。特にCGRect, CGPoint, CGSizeを出力したい場合も展開して出力するのは面倒です。

	

	

 NSStringFromCGRect, NSStringFromClassを使い次のように出力してみます。
// CGRect
NSLog(@"%@", NSStringFromCGRect([self.view bounds]));
// Class
NSLog(@"%@", NSStringFromClass([[[UIApplication sharedApplication] delegate] class]));

出力
2008-12-25 19:44:29.628 HelloWorld[39711:20b] {{0, 0}, {320, 460}}
2008-12-25 19:44:29.629 HelloWorld[39711:20b] HelloWorldAppDelegate
]]></description>
			<content:encoded><![CDATA[<p>NSStringFromから続くFunctionは非常に便利です。特にCGRect, CGPoint, CGSizeを出力したい場合も展開して出力するのは面倒です。</p>
<p>
	<br />
<img src="http://blog.seiji.me/wp-content/uploads/2008/12/uikit-function-reference.jpg" alt="UIKit Function Reference — デベロッパドキュメント.jpg" border="0" width="224" height="193" /><br />
	</p>
<p><span id="more-405"></span>
<p> NSStringFromCGRect, NSStringFromClassを使い次のように出力してみます。</p>
<pre><code>// CGRect
NSLog(@"%@", NSStringFromCGRect([self.view bounds]));
// Class
NSLog(@"%@", NSStringFromClass([[[UIApplication sharedApplication] delegate] class]));
</code></pre>
<h4>出力</h4>
<pre class="console"><code>2008-12-25 19:44:29.628 HelloWorld[39711:20b] {{0, 0}, {320, 460}}
2008-12-25 19:44:29.629 HelloWorld[39711:20b] HelloWorldAppDelegate</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.seiji.me/405.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>UINavigationBarを見えなくする-iPhone App</title>
		<link>http://blog.seiji.me/399.html</link>
		<comments>http://blog.seiji.me/399.html#comments</comments>
		<pubDate>Wed, 24 Dec 2008 16:26:39 +0000</pubDate>
		<dc:creator>seiji</dc:creator>
				<category><![CDATA[Apple]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[UINavigationBar]]></category>
		<category><![CDATA[UINavigationController]]></category>

		<guid isPermaLink="false">http://blog.seiji.me/?p=399</guid>
		<description><![CDATA[UINavigationBarを見えなくする。

	

	

UINavigationControllerを使いたいんだけど、UINavigationBarが邪魔な時。下の一行を追加すればよいです。
self.navigationController.navigationBar.frame = CGRectZero;
]]></description>
			<content:encoded><![CDATA[<p>UINavigationBarを見えなくする。</p>
<p>
	<br />
<img src="http://blog.seiji.me/wp-content/uploads/2008/12/iphone-2.png" alt="iPhone シミュレータ-2.png" border="0" width="230" height="183" /><br />
	
</p>
<p>UINavigationControllerを使いたいんだけど、UINavigationBarが邪魔な時。下の一行を追加すればよいです。</p>
<pre><code>self.navigationController.navigationBar.frame = CGRectZero;</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.seiji.me/399.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mac Heist is giving away Free Apps for Christmas</title>
		<link>http://blog.seiji.me/396.html</link>
		<comments>http://blog.seiji.me/396.html#comments</comments>
		<pubDate>Sat, 20 Dec 2008 03:38:44 +0000</pubDate>
		<dc:creator>seiji</dc:creator>
				<category><![CDATA[Apple]]></category>
		<category><![CDATA[Mac]]></category>
		<category><![CDATA[Software-Mac]]></category>
		<category><![CDATA[Mac Heist]]></category>

		<guid isPermaLink="false">http://blog.seiji.me/?p=396</guid>
		<description><![CDATA[Mac HeistでMac Appsを配布しています。現在Synergy, Enigmo2がDLできるようです。
Christmasの日にもいくつか公開されるようですね。

	

	
	
		Mac Giving Tree &#8211; MacHeist
	
]]></description>
			<content:encoded><![CDATA[<p>Mac HeistでMac Appsを配布しています。現在Synergy, Enigmo2がDLできるようです。</p>
<p>Christmasの日にもいくつか公開されるようですね。</p>
<p>
	<br />
<a href="http://givingtree.macheist.com/"><img src="http://blog.seiji.me/wp-content/uploads/2008/12/mac-giving-tree.png" alt="Mac Giving Tree.png" border="0" width="400" height="296" /></a><br />
	<br />
	<q><br />
		<cite><a href="http://givingtree.macheist.com/">Mac Giving Tree &#8211; MacHeist</a></cite><br />
	</q></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.seiji.me/396.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>OCUnitのTestCase作成</title>
		<link>http://blog.seiji.me/389.html</link>
		<comments>http://blog.seiji.me/389.html#comments</comments>
		<pubDate>Thu, 18 Dec 2008 07:06:49 +0000</pubDate>
		<dc:creator>seiji</dc:creator>
				<category><![CDATA[Apple]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[OCUnit]]></category>

		<guid isPermaLink="false">http://blog.seiji.me/?p=389</guid>
		<description><![CDATA[OCUnitとはObjective-C用のUnitTest用Frameworkです。

今回はこのOCUnitを試してみたいと思います。-setUp, -tearDownなどJUnitなどのxUnitを使用した経験がある方なら非常に親しみやすいと言えます。

以下は全てX-Code上で行います。
UnitTest対象
UnitTestの対象として例えば以下のようなクラスを作成します。
Person.h
#import 

@interface Person : NSObject {
	NSString *displayName;
}
@property (copy) NSString *displayName;

- (id)initWithName:(NSString *)displayName;

@end

Person.m
#import "Person.h"

@implementation Person

@synthesize displayName;

- (id)initWithName:(NSString *)aName {
	if ((self = [super init])!= nil) {
		self.displayName = aName;
	}
	return self;
}

- (void) dealloc {
	[displayName release];
	[super dealloc];
}

@end
新規ターゲットの作成

ターゲットを選択し、新規ターゲットの作成を選択
Cocoa &#62;&#62; Unit Test Bundleを選択し、次へ
名前を例えばUnitTestsにして完了

ターゲットUnitTestsの情報の変更

ビルド &#62;&#62; リンク &#62;&#62;他のリンクフラグのCocoaをFoundationに変更する
ビルド &#62;&#62; ユーザー定義 で以下の項目以外を削除する。

GCC_C_LANGUAGE_STANDARD
GCC_WARN_ABOUT_RETURN_TYPE
GCC_WARN_UNUSED_VARIABLE



Person.mのターゲット修正

Person.mのターゲットの中にUnitTestsを追加する

新規TestCaseの作成

ファイル &#62;&#038;gt 新規ファイルを選択
Cocoa Touch Classes &#62;&#62; NSObject subclassを選択し、次へ
名前を例えばPersonTest、ターゲットをUnitTestsにして完了

TestCaseの実装
PersonTest.h
#import 

@class Person;
@interface PersonTest : SenTestCase [...]]]></description>
			<content:encoded><![CDATA[<p>OCUnitとはObjective-C用のUnitTest用Frameworkです。
</p>
<p>今回はこのOCUnitを試してみたいと思います。-setUp, -tearDownなどJUnitなどのxUnitを使用した経験がある方なら非常に親しみやすいと言えます。</p>
<p><span id="more-389"></span>
<p>以下は全てX-Code上で行います。</p>
<h3>UnitTest対象</h3>
<p>UnitTestの対象として例えば以下のようなクラスを作成します。</p>
<h4>Person.h</h4>
<pre><code>#import <foundation/Foundation.h>

@interface Person : NSObject {
	NSString *displayName;
}
@property (copy) NSString *displayName;

- (id)initWithName:(NSString *)displayName;

@end
</code></pre>
<h4>Person.m</h4>
<pre><code>#import "Person.h"

@implementation Person

@synthesize displayName;

- (id)initWithName:(NSString *)aName {
	if ((self = [super init])!= nil) {
		self.displayName = aName;
	}
	return self;
}

- (void) dealloc {
	[displayName release];
	[super dealloc];
}

@end</code></pre>
<h3>新規ターゲットの作成</h3>
<ol>
<li>ターゲットを選択し、新規ターゲットの作成を選択</li>
<li>Cocoa &gt;&gt; Unit Test Bundleを選択し、次へ</li>
<li>名前を例えばUnitTestsにして完了</li>
</ol>
<h3>ターゲットUnitTestsの情報の変更</h3>
<ol>
<li>ビルド &gt;&gt; リンク &gt;&gt;他のリンクフラグのCocoaをFoundationに変更する</li>
<li>ビルド &gt;&gt; ユーザー定義 で以下の項目以外を削除する。
<ul>
<li>GCC_C_LANGUAGE_STANDARD</li>
<li>GCC_WARN_ABOUT_RETURN_TYPE</li>
<li>GCC_WARN_UNUSED_VARIABLE</li>
</ul>
</li>
</ol>
<h3>Person.mのターゲット修正</h3>
<ol>
<li>Person.mのターゲットの中にUnitTestsを追加する</li>
</ol>
<h3>新規TestCaseの作成</h3>
<ol>
<li>ファイル &gt;&#038;gt 新規ファイルを選択</li>
<li>Cocoa Touch Classes &gt;&gt; NSObject subclassを選択し、次へ</li>
<li>名前を例えばPersonTest、ターゲットをUnitTestsにして完了</li>
</ol>
<h3>TestCaseの実装</h3>
<h4>PersonTest.h</h4>
<pre><code>#import <senTestingKit/SenTestingKit.h>

@class Person;
@interface PersonTest : SenTestCase {
	Person *person;
}

@end</code></pre>
<h4>PersonTest.m</h4>
<pre><code>#import "PersonTest.h"
#import "Person.h"

@implementation PersonTest

- (void)setUp {
	person = [[Person alloc] init];
}

- (void)testCreatePerson {
	STAssertNotNil(person, @"Couldn't create Person");
}

- (void)testSetDisplayName {
    NSString *displayName = @"Seiji";
    person.displayName = displayName;
    STAssertEqualObjects(displayName, person.displayName, @"Couldn't set person.displayName");
}

- (void)tearDown {
	[person release];
}

@end</code></pre>
<h3>TestCaseの実行</h3>
<p>UnitTestsを選択し、Buildを行うことでTestCaseが実行される。想定結果が違うものであればBuildの時点でErrorが表示されることになります。</p>
<h6>References</h6>
<ul>
<li><a href="http://developer.apple.com/tools/unittest.html">Test Driving Your Code with OCUnit</a></li>
<li><a href="http://www.stanford.edu/class/cs193p/cgi-bin/index.php">Lecture 19 &#8211; Unit Testing, Objective-C Runtime Fun and Localization</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.seiji.me/389.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>NSInvocationOperation,delegatePattern-CS193P</title>
		<link>http://blog.seiji.me/387.html</link>
		<comments>http://blog.seiji.me/387.html#comments</comments>
		<pubDate>Sat, 13 Dec 2008 07:25:44 +0000</pubDate>
		<dc:creator>seiji</dc:creator>
				<category><![CDATA[Apple]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[CS193P]]></category>
		<category><![CDATA[delegatePattern]]></category>
		<category><![CDATA[NSInvocationOperation]]></category>
		<category><![CDATA[Objective-C]]></category>

		<guid isPermaLink="false">http://blog.seiji.me/?p=387</guid>
		<description><![CDATA[NSInvocationOperation,delegatePattern
Lecture12迄の内容を基に&#8221;Presence&#8221;アプリケーションを拡張していきます。
AssignmentPresence3になります。


	
	
		iPhone Application Programming
	



今迄、作成したソースはhttp://public.me.com/seijit/iPhone/CS193Pから

今回の目標
前回迄は全てMainThreadで通信を行っていました。その為、待ち時間が長く感じられ、ストレスを受けていたのではないかと思います。今回はそのストレスを軽減する為に別Threadで通信を行い、最後にMainThreadの処理を返すように実装を行います。アプリケーションとしては下の図のように自分のStatusをUpdateする画面を用意し、そこからSendできるようにします。



今回は全てを記述するのは大変なので、要点を絞って記載します。

NSInvocationOperationの作成
ViewがLoadされる際にSelector(synchronousLoadsTwitterUsers)を指定してNSInvocationOperationを作成し、それをNSInvocationOperationQueueにAddします。その中で通信部分の処理を行っています。
また、作成時に指定したSelector内部ではSelector(didFinishLoadingTwitterUsersWithResults)を指定してMainThreadに返しています。

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
	if ([myData count] == 0) {
		[self showLoadingIndicators];
		[self beginLoadingTwitterUsers];
	}
}
- (void)beginLoadingTwitterUsers {
	[myData removeAllObjects];

	NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(synchronousLoadsTwitterUsers) object:nil];
	[operationQueue addOperation:operation];
	[operation release];
}
- (void)synchronousLoadsTwitterUsers {
	NSBundle *bundle = [NSBundle mainBundle];
	NSArray *twitterUsers = [NSArray arrayWithContentsOfFile:[bundle pathForResource:@"TwitterUsers" ofType:@"plist"]];

	for (NSString *userName in twitterUsers){
		Person *person = [[Person alloc] initWithPersonName:userName];
		if (person)
			[myData addObject:person];
		[person release];
	}
	[self performSelectorOnMainThread:@selector(didFinishLoadingTwitterUsersWithResults) withObject:nil [...]]]></description>
			<content:encoded><![CDATA[<h3>NSInvocationOperation,delegatePattern</h3>
<p>Lecture12迄の内容を基に&#8221;Presence&#8221;アプリケーションを拡張していきます。<br />
AssignmentPresence3になります。<br />
<br />
<img src="http://blog.seiji.me/wp-content/uploads/2008/11/cs193p-cocoa-programming-announcements.jpg" alt="CS193P - Cocoa Programming | Announcements.jpg" border="0" width="430" height="89" /><br />
	<br />
	<q><br />
		<cite><a href="http://www.stanford.edu/class/cs193p/cgi-bin/index.php">iPhone Application Programming</a></cite><br />
	</q>
</p>
<p><span id="more-387"></span>
<p>
今迄、作成したソースは<a href="http://public.me.com/seijit/iPhone/CS193P">http://public.me.com/seijit/iPhone/CS193P</a>から
</p>
<h3>今回の目標</h3>
<p>前回迄は全てMainThreadで通信を行っていました。その為、待ち時間が長く感じられ、ストレスを受けていたのではないかと思います。今回はそのストレスを軽減する為に別Threadで通信を行い、最後にMainThreadの処理を返すように実装を行います。アプリケーションとしては下の図のように自分のStatusをUpdateする画面を用意し、そこからSendできるようにします。</p>
<p><img src="http://blog.seiji.me/wp-content/uploads/2008/12/presence-part3-1-1.jpg" alt="Presence-Part3-1-1.jpg" border="0" width="400" />
</p>
<p>
今回は全てを記述するのは大変なので、要点を絞って記載します。
</p>
<h3><img src="http://blog.seiji.me/wp-content/uploads/2008/11/xcode.png" alt="picture.png" border="0" width="30" height="30" />NSInvocationOperationの作成</h3>
<p>ViewがLoadされる際にSelector(synchronousLoadsTwitterUsers)を指定してNSInvocationOperationを作成し、それをNSInvocationOperationQueueにAddします。その中で通信部分の処理を行っています。</p>
<p>また、作成時に指定したSelector内部ではSelector(didFinishLoadingTwitterUsersWithResults)を指定してMainThreadに返しています。
</p>
<pre><code>- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
	if ([myData count] == 0) {
		[self showLoadingIndicators];
		[self beginLoadingTwitterUsers];
	}
}
- (void)beginLoadingTwitterUsers {
	[myData removeAllObjects];

	NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(synchronousLoadsTwitterUsers) object:nil];
	[operationQueue addOperation:operation];
	[operation release];
}
- (void)synchronousLoadsTwitterUsers {
	NSBundle *bundle = [NSBundle mainBundle];
	NSArray *twitterUsers = [NSArray arrayWithContentsOfFile:[bundle pathForResource:@"TwitterUsers" ofType:@"plist"]];

	for (NSString *userName in twitterUsers){
		Person *person = [[Person alloc] initWithPersonName:userName];
		if (person)
			[myData addObject:person];
		[person release];
	}
	[self performSelectorOnMainThread:@selector(didFinishLoadingTwitterUsersWithResults) withObject:nil waitUntilDone:NO];
}
- (void)didFinishLoadingTwitterUsersWithResults {
	[self hideLoadingIndicators];
	[self.tableView reloadData];
    [self.tableView flashScrollIndicators];
}</code></pre>
<h3><img src="http://blog.seiji.me/wp-content/uploads/2008/11/xcode.png" alt="picture.png" border="0" width="30" height="30" />delegete Patternの使用</h3>
<p>NavigationItemのRightBarButtonItemをクリックするとStatusComposeする画面をModallyに出すようにしています。</p>
<pre><code>
- (void)viewDidLoad {
    [super viewDidLoad];
	UIBarButtonItem *addButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:self action:@selector(presentModally) ];
	self.navigationItem.rightBarButtonItem = addButton;
	[addButton release];
}
- (void)presentModally {
	StatusComposeViewController *statusController = [[StatusComposeViewController alloc]initWithNibName:@"StatusCompose" bundle:nil];
	statusController.delegate = self;
	[self presentModalViewController:statusController animated:YES];
	[statusController release];
}
</code></pre>
<p>この場合、このActionは今回で言うとPersonListViewControllerに実装するわけです。ではこの画面を消すActionはどこに書けばいいのでしょうか？</p>
<p>単純にStatusComposeViewControllerに下のように実装しても消すことはできます。</p>
<pre><code>
[self.navigationController dismissModalViewControllerAnimated:YES];
</code></pre>
<p>しかし、PresentとDismissするのは同じContorollerからするのが望ましいと言われています。別の操作を行うのにもこの方が操作をしやすいでしょう。</p>
<p>ここでdelegatePatternを使用します。</p>
<h4>Protocolの作成</h4>
<pre><code>@class StatusComposeViewController;

@protocol StatusComposeViewDelegate <nsobject>
@optional
- (void)statusComposeViewControllerDidFinish:(StatusComposeViewController *)controller;
- (void)statusComposeViewControllerDidCancel:(StatusComposeViewController *)controller;
@end</code></pre>
<p>今回のdelegateの為のProtocolを作成します。</p>
<h4>Propertyの追加</h4>
<pre><code>
@interface StatusComposeViewController : UIViewController <uitextFieldDelegate> {
	// 略
	id <statusComposeViewDelegate> delegate;
}
	// 略
@property (nonatomic, assign) id <statusComposeViewDelegate> delegate;
	// 略
@end
</code></pre>
<p>StatusComposeViewControllerにStatusComposeViewDelegateを満たしたid型の属性を追加します。</p>
<h4>StatusComposeViewDelegateの実装</h4>
<p> StatusComposeViewDelegateのメソッド(statusComposeViewControllerDidFinish:, statusComposeViewControllerDidCancel: )を実装します。</p>
<p>これでStatusComposeViewController側でStatusComposeViewDelegateで実装されたPersonListViewControllerのメソッド(i.e [self.delegate statusComposeViewControllerDidFinish:])を呼べばよいわけです。</p>
<p>これでAssignmentPresence3を終了としたいと思います。</p>
<p>今回作成したソースは<a href="http://public.me.com/seijit/iPhone/CS193P/Lecture12">http://public.me.com/seijit/iPhone/CS193P/Lecture12</a>のPresence5.tar.gzです</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.seiji.me/387.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>UITableViewDelegate, UITableViewDataSource-CS193P</title>
		<link>http://blog.seiji.me/384.html</link>
		<comments>http://blog.seiji.me/384.html#comments</comments>
		<pubDate>Thu, 11 Dec 2008 04:43:39 +0000</pubDate>
		<dc:creator>seiji</dc:creator>
				<category><![CDATA[Apple]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[CS193P]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[UITableViewDataSource]]></category>
		<category><![CDATA[UITableViewDelegate]]></category>

		<guid isPermaLink="false">http://blog.seiji.me/?p=384</guid>
		<description><![CDATA[UITableViewDelegate, UITableViewDataSource
前回plistの読込み、UITableViewCellAccessory-CS193Pの続きになります。
AssignmentPresence2の後半にあたります。


	
	
		iPhone Application Programming
	



今迄、作成したソースはhttp://public.me.com/seijit/iPhone/CS193Pから

今回の目標
PersonDetailViewControllerを修正し、下のアプリケーションを完成させるのが目的です。List,Detail共にTableViewを使用しています。ListがUITableViewStylePlain, Detailの方がUITableViewStyleGroupedとStyleを変えて表示をしています。


PersonDetail.xibの変更
PersonDetail.xibをWクリックし、InterfaceBuilderを起動します。ここではViewを大幅に変更します。
View上にUITableViewのみを配置し、AttributesInspectorでTableViewのStyleを&#8221;Grouped&#8221;にします。


ConnectionsInspectorでPersonDetailViewControllerのviewをTableViewに、TableViewのdataSource,delegateをPersonDetailViewControllerに接続します。



PersonList.xibでは同じTableViewを使っているのにdataSource,delegateの接続は行いませんでした。この違いはPersonLisrViewControllerはTableViewControllerのSubClassに対して、PersonDetailViewControllerはUIViewControllerのSubClassだからです。
PersonDetailViewControllerの修正

PersonDetailViewControllerはPersonListViewControllerとは違った形でTableViewを表示させます。ProtocolであるUITableViewDelegate, UITableViewDataSourceの実装を行います。
PersonDetailViewController
PersonDetailViewController.h
#import &#60;UIKit/UIKit.h&#62;

@class Person;
@interface PersonDetailViewController : UIViewController &#60;UITableViewDelegate, UITableViewDataSource&#62;  {
	Person* person;
}
@property (nonatomic, assign) Person* person;

@end


ProtocolであるUITableViewDelegate, UITableViewDataSourceの宣言をしています。
PersonDetailViewController.m
#import "PersonDetailViewController.h"
#import "Person.h"
#import "PersonText.h"

@implementation PersonDetailViewController
@synthesize person;

// 略
@end


UITableViewDelegate, UITableViewDataSourceのメソッドを実装します。

- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
	return [person.textArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	static NSString *CellIdentifier = @"MyIdentifer";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    [...]]]></description>
			<content:encoded><![CDATA[<h3>UITableViewDelegate, UITableViewDataSource</h3>
<p>前回<a href="http://blog.seiji.me/374.html">plistの読込み、UITableViewCellAccessory-CS193P</a>の続きになります。<br />
AssignmentPresence2の後半にあたります。<br />
<br />
<img src="http://blog.seiji.me/wp-content/uploads/2008/11/cs193p-cocoa-programming-announcements.jpg" alt="CS193P - Cocoa Programming | Announcements.jpg" border="0" width="430" height="89" /><br />
	<br />
	<q><br />
		<cite><a href="http://www.stanford.edu/class/cs193p/cgi-bin/index.php">iPhone Application Programming</a></cite><br />
	</q>
</p>
<p><span id="more-384"></span>
<p>
今迄、作成したソースは<a href="http://public.me.com/seijit/iPhone/CS193P">http://public.me.com/seijit/iPhone/CS193P</a>から
</p>
<h3>今回の目標</h3>
<p>PersonDetailViewControllerを修正し、下のアプリケーションを完成させるのが目的です。List,Detail共にTableViewを使用しています。ListがUITableViewStylePlain, Detailの方がUITableViewStyleGroupedとStyleを変えて表示をしています。</p>
<p><img src="http://blog.seiji.me/wp-content/uploads/2008/12/app-11.png" alt="app-1.png" border="0" width="400" height="391" />
</p>
<h3><img src="http://blog.seiji.me/wp-content/uploads/2008/11/picture.png" alt="picture.png" border="0" width="30" height="30" />PersonDetail.xibの変更</h3>
<p>PersonDetail.xibをWクリックし、InterfaceBuilderを起動します。ここではViewを大幅に変更します。</p>
<p>View上にUITableViewのみを配置し、AttributesInspectorでTableViewのStyleを&#8221;Grouped&#8221;にします。</p>
<p><a href="http://blog.seiji.me/wp-content/uploads/2008/12/1-2.png"><img src="http://blog.seiji.me/wp-content/uploads/2008/12/1-2.png" alt="ピクチャ 1-2.png" border="0" width="400" /></a>
</p>
<p>ConnectionsInspectorでPersonDetailViewControllerのviewをTableViewに、TableViewのdataSource,delegateをPersonDetailViewControllerに接続します。</p>
<p><img src="http://blog.seiji.me/wp-content/uploads/2008/12/table-view-connections-1.png" alt="Table View Connections-1.png" border="0" width="316" height="186" />
</p>
<p>
PersonList.xibでは同じTableViewを使っているのにdataSource,delegateの接続は行いませんでした。この違いはPersonLisrViewControllerはTableViewControllerのSubClassに対して、PersonDetailViewControllerはUIViewControllerのSubClassだからです。</p>
<h3><img src="http://blog.seiji.me/wp-content/uploads/2008/11/xcode.png" alt="xcode.png" border="0" width="30" height="30" />PersonDetailViewControllerの修正</h3>
<p>
PersonDetailViewControllerはPersonListViewControllerとは違った形でTableViewを表示させます。ProtocolであるUITableViewDelegate, UITableViewDataSourceの実装を行います。</p>
<h4>PersonDetailViewController</h4>
<h5>PersonDetailViewController.h</h5>
<pre><code>#import &lt;UIKit/UIKit.h&gt;

@class Person;
@interface PersonDetailViewController : UIViewController &lt;UITableViewDelegate, UITableViewDataSource&gt;  {
	Person* person;
}
@property (nonatomic, assign) Person* person;

@end
</code></pre>
<p>
ProtocolであるUITableViewDelegate, UITableViewDataSourceの宣言をしています。</p>
<h5>PersonDetailViewController.m</h5>
<pre><code>#import "PersonDetailViewController.h"
#import "Person.h"
#import "PersonText.h"

@implementation PersonDetailViewController
@synthesize person;

// 略
@end
</code></pre>
<p>
UITableViewDelegate, UITableViewDataSourceのメソッドを実装します。
</p>
<pre><code>- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
	return [person.textArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	static NSString *CellIdentifier = @"MyIdentifer";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }
    // Set up the cell...
	PersonText *personText = [person.textArray objectAtIndex:indexPath.row];
	UILabel *textField = [[UILabel alloc] initWithFrame:CGRectMake(20, 6, personText.size.width, personText.size.height)];
	[textField setText:[NSString stringWithCString:[personText.textString UTF8String] encoding:NSUTF8StringEncoding]];
	[textField setBackgroundColor:[UIColor clearColor]];
	[textField setLineBreakMode:UILineBreakModeWordWrap];
	[textField setFont:personText.font];
	[textField setHighlightedTextColor:[UIColor whiteColor]];
	[textField setNumberOfLines:0];
	[cell addSubview:textField];
	[textField release];

    return cell;
}</code></pre>
<p>行の数とUITableViewCellの中身の表示を行うメソッドです。UILabelを作成し、UITableViewCellのSubViewとして追加しています。</p>
<pre><code>
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    // Return the displayed title for the specified section.
    return @"Statuses";
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
	PersonText *personText = [person.textArray objectAtIndex:indexPath.row];
	int padding = 10;
	return personText.size.height + padding;
}
</code></pre>
<p>Sectionのヘッダ名を返すメソッドと、各UITableViewCellの高さを返すメソッドです。ここで動的にUITabelViewCellの高さを変化させています。</p>
<h4>PersonListViewController</h4>
<p>最後にPersonListのUITableViewCellをクリックした際のメソッドを変更しておきます。ここでPersonDetailViewControllerを作成し、UINavigationContollerのStackに追加しています。</p>
<h5>PersonListViewController.m</h5>
<pre><code>- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.
	PersonDetailViewController *detailViewController = [[PersonDetailViewController alloc] initWithNibName:@"PersonDetail" bundle:nil];
	int personIndex = [indexPath indexAtPosition: [indexPath length] - 1];
	detailViewController.person = [myData objectAtIndex:personIndex];
	[self.navigationController pushViewController:detailViewController animated:YES];
	[detailViewController release];
}</code></pre>
<p>これでAssignmentPresence2ができました。</p>
<p>今回作成したソースは<a href="http://public.me.com/seijit/iPhone/CS193P/Lecture9">http://public.me.com/seijit/iPhone/CS193P/Lecture9</a>のPresence4.tar.gzです。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.seiji.me/384.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss><!-- Dynamic page generated in 0.678 seconds. --><!-- Cached page generated by WP-Super-Cache on 2010-09-03 14:28:31 -->
