<?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>Sun Limited Mt. &#187; Objective-C</title>
	<atom:link href="http://blog.syuhari.jp/archives/category/objective-c/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.syuhari.jp</link>
	<description>I love iPhone, CakePHP and WordPress.</description>
	<lastBuildDate>Thu, 20 Oct 2011 19:36:15 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Objective-C でシングルトンパターン</title>
		<link>http://blog.syuhari.jp/archives/2178</link>
		<comments>http://blog.syuhari.jp/archives/2178#comments</comments>
		<pubDate>Wed, 14 Jul 2010 06:50:32 +0000</pubDate>
		<dc:creator>matsuura</dc:creator>
				<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[desing pattern]]></category>
		<category><![CDATA[singleton]]></category>

		<guid isPermaLink="false">http://www.syuhari.jp/blog/?p=2178</guid>
		<description><![CDATA[Objective-C でシングルトンパターンのクラスを作成するメモです。 シングルトンパターンとは、クラスのインスタンスがひとつしか生成されないことを保証するデザインパターンです。NSUserDefaults などがシングルトンなクラスです。 シングルトンなクラスを作成するときのポイントです。 静的インスタンスを生成して、ひとつだけ作成したインスタンスを入れておく アップルが推奨しているクラスファクトリーメソッド名（sharedInstance や sharedManager) を使用して、インスタンス未生成時のみインスタンスを生成する インスタンスがひとつしか生成されないことを保証するために [[Hoge alloc] init] とされた場合の対策をしておく retain, retainCount, copyWithZone, release, autorelease をオーバライドしてシングルトン状態が保持されるようにする で、作成したのが以下のようなコードです。これに必要なメソッドなどを適宜追加して使います。インスタンスを生成する処理を @synchronized(){} で囲うことにより同時に複数のスレッドから実行されたときにも、処理が同時に実行されないようにブロックされ、シングルトン状態を保持できます。 #import "History.h" @implementation History static History* sharedHistory = nil; + (History*)sharedManager { @synchronized(self) { if (sharedHistory == nil) { sharedHistory = [[self alloc] init]; } } return sharedHistory; } + (id)allocWithZone:(NSZone [...]]]></description>
			<content:encoded><![CDATA[<p>Objective-C でシングルトンパターンのクラスを作成するメモです。<br />
シングルトンパターンとは、クラスのインスタンスがひとつしか生成されないことを保証するデザインパターンです。NSUserDefaults などがシングルトンなクラスです。<br />
<span id="more-2178"></span><br />
シングルトンなクラスを作成するときのポイントです。</p>
<ol>
<li>静的インスタンスを生成して、ひとつだけ作成したインスタンスを入れておく</li>
<li>アップルが推奨しているクラスファクトリーメソッド名（sharedInstance や sharedManager) を使用して、インスタンス未生成時のみインスタンスを生成する</li>
<li>インスタンスがひとつしか生成されないことを保証するために [[Hoge alloc] init] とされた場合の対策をしておく</li>
<li>retain, retainCount, copyWithZone, release, autorelease をオーバライドしてシングルトン状態が保持されるようにする</li>
</ol>
<p>で、作成したのが以下のようなコードです。これに必要なメソッドなどを適宜追加して使います。インスタンスを生成する処理を @synchronized(){} で囲うことにより同時に複数のスレッドから実行されたときにも、処理が同時に実行されないようにブロックされ、シングルトン状態を保持できます。</p>
<pre class="cpp" name="code">
#import "History.h"

@implementation History

static History* sharedHistory = nil;

+ (History*)sharedManager {
	@synchronized(self) {
		if (sharedHistory == nil) {
			sharedHistory = [[self alloc] init];
		}
	}
	return sharedHistory;
}

+ (id)allocWithZone:(NSZone *)zone {
	@synchronized(self) {
		if (sharedHistory == nil) {
			sharedHistory = [super allocWithZone:zone];
			return sharedHistory;
		}
	}
	return nil;
}

- (id)copyWithZone:(NSZone*)zone {
	return self;  // シングルトン状態を保持するため何もせず self を返す
}

- (id)retain {
	return self;  // シングルトン状態を保持するため何もせず self を返す
}

- (unsigned)retainCount {
	return UINT_MAX;  // 解放できないインスタンスを表すため unsigned int 値の最大値 UINT_MAX を返す
}

- (void)release {
	// シングルトン状態を保持するため何もしない
}

- (id)autorelease {
	return self;  // シングルトン状態を保持するため何もせず self を返す
}

@end
</pre>
<p>実際にインスタンスを取得する際は以下のようになります。</p>
<pre class="cpp" name="code">
History* history = [History sharedManager];
</pre>
<p>参考サイト<br />
<a href="http://developer.apple.com/jp/documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaObjects/chapter_3_section_10.html" target="_blank" class="liexternal">Cocoa Fundamentals Guide: シングルトンインスタンスの作成</a></p>
<p>先月も今月もほとんどブログを更新していないかった。反省して更新するようにしたいです。</p>
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fblog.syuhari.jp%2Farchives%2F2178&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light&amp;height=80" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:80px;" allowTransparency="true"></iframe>]]></content:encoded>
			<wfw:commentRss>http://blog.syuhari.jp/archives/2178/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Objective-C でプロパティ名とインスタンス変数名を変える方法</title>
		<link>http://blog.syuhari.jp/archives/1831</link>
		<comments>http://blog.syuhari.jp/archives/1831#comments</comments>
		<pubDate>Sun, 27 Sep 2009 05:23:17 +0000</pubDate>
		<dc:creator>matsuura</dc:creator>
				<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[iPhone/iPod touch]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Mac]]></category>
		<category><![CDATA[property]]></category>
		<category><![CDATA[synthesize]]></category>

		<guid isPermaLink="false">http://www.syuhari.jp/blog/?p=1831</guid>
		<description><![CDATA[Objective-C でプロパティ名はほとんどの場合インスタンス名と同じです。しかし、何かしらの理由でインスタンス名を公開したくない場合、インスタンス名とプロパティ名を変えることができます。 例えば Book クラスに title というインスタンス変数があり、name というプロパティ名にしたい場合は以下のようにします。 @interface Book : NSObject { NSString* title; } @property NSString* name; @end インスタンス変数に title、プロパティ名に name を指定します。 次に @implementation で synthesize を指定する際に、以下のように @synthesize name=title; と指定するのがポイントです。これで name のアクセッサ（setName: , name) が作られます。そのアクセッサ内では title が使われます。また、name にアクセスする際には self.name とかならずアクセッサ経由でアクセスするか、title を使用します。 @implementation Book @synthesize name=title; - (void) hoge { NSLog("%@", self.name); } - [...]]]></description>
			<content:encoded><![CDATA[<p>Objective-C でプロパティ名はほとんどの場合インスタンス名と同じです。しかし、何かしらの理由でインスタンス名を公開したくない場合、インスタンス名とプロパティ名を変えることができます。</p>
<p>例えば Book クラスに title というインスタンス変数があり、name というプロパティ名にしたい場合は以下のようにします。</p>
<pre class="cpp" name="code">
@interface Book : NSObject {
  NSString* title;
}
@property NSString* name;
@end
</pre>
<p>インスタンス変数に title、プロパティ名に name を指定します。<br />
<span id="more-1831"></span><br />
次に @implementation で synthesize を指定する際に、以下のように @synthesize name=title; と指定するのがポイントです。これで name のアクセッサ（setName: , name) が作られます。そのアクセッサ内では title が使われます。また、name にアクセスする際には self.name とかならずアクセッサ経由でアクセスするか、title を使用します。</p>
<pre class="cpp" name="code">
@implementation Book

@synthesize name=title;

- (void) hoge {
  NSLog("%@", self.name);
}
- (void) dealloc {
  [super dealloc];
  self.name=nil;
}
@end
</pre>
<p>dealloc 時の注意点は self.name = nil; としてメモリ解放することです。</p>
<h4><a href="http://www.amazon.co.jp/%E5%85%A5%E9%96%80-Objective-C-2-0-Programmer%E2%80%99s-SELECTION/dp/4798119989%3FSubscriptionId%3D00ZZGWWEM6KCNNQ67M82%26tag%3D8109-22%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D4798119989" target="_blank" class="liexternal">入門 Objective-C 2.0 (Programmer’s SELECTION)</a></h4>
<p><a href="http://www.amazon.co.jp/%E5%85%A5%E9%96%80-Objective-C-2-0-Programmer%E2%80%99s-SELECTION/dp/4798119989%3FSubscriptionId%3D00ZZGWWEM6KCNNQ67M82%26tag%3D8109-22%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D4798119989" target="_blank" class="liimagelink"><img src="http://ecx.images-amazon.com/images/I/51SyK84c6wL._SL160_.jpg" border="0" width="Array" height="Array" alt="入門 Objective-C 2.0 (Programmer’s SELECTION)" align="left" /></a>Scott Knaster<br />
￥ 3,360<br />
大型本<br />
翔泳社<br />
<br clear="all"/></p>
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fblog.syuhari.jp%2Farchives%2F1831&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light&amp;height=80" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:80px;" allowTransparency="true"></iframe>]]></content:encoded>
			<wfw:commentRss>http://blog.syuhari.jp/archives/1831/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

