<?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/"
	
	xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Get Set Games &#187; singleton</title>
	<atom:link href="http://getsetgames.com/tag/singleton/feed/" rel="self" type="application/rss+xml" />
	<link>http://getsetgames.com</link>
	<description></description>
	<lastBuildDate>Thu, 09 Sep 2010 15:15:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
<atom:link rel="hub" href="http://pubsubhubbub.appspot.com"/><atom:link rel="hub" href="http://superfeedr.com/hubbub"/><cloud domain='getsetgames.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<link rel="http://api.friendfeed.com/2008/03#sup" xmlns="http://www.w3.org/2005/Atom" type="application/json" href="http://friendfeed.com/api/public-sup.json#5c973c50be"/>		<item>
		<title>The Objective-C Singleton</title>
		<link>http://getsetgames.com/2009/08/30/the-objective-c-singleton/</link>
		<comments>http://getsetgames.com/2009/08/30/the-objective-c-singleton/#comments</comments>
		<pubDate>Sun, 30 Aug 2009 18:17:50 +0000</pubDate>
		<dc:creator>Derek van Vliet</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[objective-c]]></category>
		<category><![CDATA[singleton]]></category>

		<guid isPermaLink="false">http://getsetgames.com/?p=269</guid>
		<description><![CDATA[One of the most useful patterns that we have employed in our iPhone game is the singleton. For those who don&#8217;t know, singletons are a class that only gets instantiated once in your application&#8217;s run-time. They often take the form of manager or factory classes. A good example of a singleton is the web-based resource [...]]]></description>
			<content:encoded><![CDATA[<p>One of the most useful patterns that we have employed in our iPhone game is the singleton. For those who don&#8217;t know, singletons are a class that only gets instantiated once in your application&#8217;s run-time. They often take the form of manager or factory classes. A good example of a singleton is the <a href="http://getsetgames.com/2009/08/19/web-enabled-resource-manager-for-cocos2d/">web-based resource manager class</a> that we posted about recently.</p>
<p>We have been using singletons for a variety of things in our cocos2d-based game. Including:</p>
<ul>
<li>Resource management</li>
<li>Atlas sprite managers</li>
<li>User settings management</li>
<li>Score management</li>
</ul>
<p>Below is a template for the singletons that we use in objective-c.</p>
<p>MySingleton.h:</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> MySingleton <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span> <span style="color: #002200;">&#123;</span>
&nbsp;
<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">+</span><span style="color: #002200;">&#40;</span>MySingleton<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>sharedMySingleton;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>sayHello;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>MySingleton.m:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@implementation</span> MySingleton
<span style="color: #a61390;">static</span> MySingleton<span style="color: #002200;">*</span> _sharedMySingleton <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
&nbsp;
<span style="color: #002200;">+</span><span style="color: #002200;">&#40;</span>MySingleton<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>sharedMySingleton
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">@synchronized</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>MySingleton class<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span>_sharedMySingleton<span style="color: #002200;">&#41;</span>
			<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #a61390;">return</span> _sharedMySingleton;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #a61390;">return</span> <span style="color: #a61390;">nil</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">+</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>alloc
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">@synchronized</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>MySingleton class<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		NSAssert<span style="color: #002200;">&#40;</span>_sharedMySingleton <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Attempted to allocate a second instance of a singleton.&quot;</span><span style="color: #002200;">&#41;</span>;
		_sharedMySingleton <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super alloc<span style="color: #002200;">&#93;</span>;
		<span style="color: #a61390;">return</span> _sharedMySingleton;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #a61390;">return</span> <span style="color: #a61390;">nil</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>init <span style="color: #002200;">&#123;</span>
	self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super init<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>self <span style="color: #002200;">!=</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		<span style="color: #11740a; font-style: italic;">// initialize stuff here</span>
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>sayHello <span style="color: #002200;">&#123;</span>
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Hello World!&quot;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@end</span></pre></div></div>

<h2>Example Usage</h2>
<p>Using the methods of the singleton is then as easy as this:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>MySingleton sharedMySingleton<span style="color: #002200;">&#93;</span> sayHello<span style="color: #002200;">&#93;</span>;</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://getsetgames.com/2009/08/30/the-objective-c-singleton/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
	
	</item>
	</channel>
</rss>
