<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~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/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Mac Developer Tips</title>
	
	<link>http://MacDeveloperTips.com</link>
	<description>Tips, tools and code for iPhone and Mac developers.</description>
	<lastBuildDate>Thu, 12 Aug 2010 23:45:27 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/MacDeveloperTips" /><feedburner:info uri="macdevelopertips" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><feedburner:emailServiceId>MacDeveloperTips</feedburner:emailServiceId><feedburner:feedburnerHostname>http://feedburner.google.com</feedburner:feedburnerHostname><feedburner:browserFriendly>Welcome to Mac Developer Tips!</feedburner:browserFriendly><item>
		<title>Using XOR to Toggle an Integer Between 1 and 0</title>
		<link>http://feedproxy.google.com/~r/MacDeveloperTips/~3/cA_b7_JtyFA/using-xor-to-toggle-an-integer-between-1-and-0.html</link>
		<comments>http://MacDeveloperTips.com/c/using-xor-to-toggle-an-integer-between-1-and-0.html#comments</comments>
		<pubDate>Thu, 12 Aug 2010 23:39:58 +0000</pubDate>
		<dc:creator>john</dc:creator>
				<category><![CDATA[C]]></category>

		<guid isPermaLink="false">http://MacDeveloperTips.com/?p=277</guid>
		<description><![CDATA[If you ever come upon a need to toggle an integer value between 1 and 0, consider using the bitwise exclusive-OR (^) operator in C to get the job done. In a recent application I wrote a method with one parameter, an integer, that is expected to be 1 or 0. In creating a demo [...]]]></description>
				<content:encoded><![CDATA[<p>If you ever come upon a need to toggle an integer value between 1 and 0, consider using the bitwise exclusive-OR (<strong>^</strong>) operator in C to get the job done.</p>
<p>In a recent application I wrote a method with one parameter, an integer, that is expected to be 1 or 0. In creating a demo of the application I wanted to pass in alternating values of 1 and 0 as part of a test for a specific use case. Instead of using an if statement in the calling method to decide when to send a 1 or 0, I wrote something similar to the code below:<br />
<span id="more-277"></span></p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>someMethod
<span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">static</span> <span style="color: #a61390;">int</span> x <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Toggles 1 to 0 and 0 to 1 using xor</span>
  x <span style="color: #002200;">^=</span> <span style="color: #2400d9;">1</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Call the method that expects a 1 or 0</span>
  <span style="color: #002200;">&#91;</span>methodCallHere toggle<span style="color: #002200;">:</span>x<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>More often than not, exclusive-OR (^), AND (&#038;) and inclusive-OR (|) are consider a bit-level operations (see this post on <a href="http://macdevelopertips.com/c/bitfields-in-c.html">bitfields</a>). However, there are times when these operators work equally well with integer values.</p>
<img src="http://feeds.feedburner.com/~r/MacDeveloperTips/~4/cA_b7_JtyFA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://MacDeveloperTips.com/c/using-xor-to-toggle-an-integer-between-1-and-0.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://MacDeveloperTips.com/c/using-xor-to-toggle-an-integer-between-1-and-0.html</feedburner:origLink></item>
		<item>
		<title>Bitfields in C</title>
		<link>http://feedproxy.google.com/~r/MacDeveloperTips/~3/_QoP16zWQuQ/bitfields-in-c.html</link>
		<comments>http://MacDeveloperTips.com/c/bitfields-in-c.html#comments</comments>
		<pubDate>Mon, 09 Aug 2010 12:00:40 +0000</pubDate>
		<dc:creator>john</dc:creator>
				<category><![CDATA[C]]></category>

		<guid isPermaLink="false">http://MacDeveloperTips.com/?p=281</guid>
		<description><![CDATA[Given space is often at a premium when writing applications for mobile devices, I want to show you how to use bitfields to manage a series of values that need only on/off status. The upside is that you can store a surprising number of status values within a single integer, 32 to be exact. The [...]]]></description>
				<content:encoded><![CDATA[<p>Given space is often at a premium when writing applications for mobile devices, I want to show you how to use bitfields to manage a series of values that need only on/off status. The upside is that you can store a surprising number of status values within a single integer, 32 to be exact. </p>
<p>The code that follows is pure C. Given that Objective-C is a superset of C, don&#8217;t forget that you can leverage all that C has to offer, beyond working with objects.<br />
<span id="more-281"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">struct</span>
<span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">unsigned</span> preLoaded<span style="color: #002200;">:</span> <span style="color: #2400d9;">1</span>;
  <span style="color: #a61390;">unsigned</span> saveState<span style="color: #002200;">:</span> <span style="color: #2400d9;">1</span>;
  <span style="color: #a61390;">unsigned</span> saveLoginName<span style="color: #002200;">:</span> <span style="color: #2400d9;">1</span>;
<span style="color: #002200;">&#125;</span> statusFlags;</pre></td></tr></table></div>

<p>I&#8217;ve defines three bitfields, all which will be saved within an unsigned integer. Accessing the bits is as easy as this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Setting bits</span>
statusFlags.preLoaded <span style="color: #002200;">=</span> <span style="color: #2400d9;">1</span>;
statusFlags.saveState <span style="color: #002200;">=</span> statusFlags.saveLoginName <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Get bits</span>
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>statusFlags.saveLoginName <span style="color: #002200;">==</span> <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>
  ... <span style="color: #a61390;">do</span> something here...</pre></td></tr></table></div>

<p>Using the sizeof() function, let&#8217;s compare the size of an integer to the size of the structure we created:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Setting bits</span>
statusFlags.preLoaded <span style="color: #002200;">=</span> <span style="color: #2400d9;">1</span>;
statusFlags.saveState <span style="color: #002200;">=</span> statusFlags.saveLoginName <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Get bits</span>
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>statusFlags.saveLoginName <span style="color: #002200;">==</span> <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;saveLoginName bit is on.&quot;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #a61390;">else</span>
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;saveLoginName bit is off.&quot;</span><span style="color: #002200;">&#41;</span>;  
&nbsp;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Sizeof int: %d&quot;</span>, <span style="color: #a61390;">sizeof</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>; 
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Sizeof statusFlags: %d&quot;</span>, <span style="color: #a61390;">sizeof</span><span style="color: #002200;">&#40;</span>statusFlags<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;</pre></td></tr></table></div>

<p>The output looks as follows, which verifies that the bitfields are maintained within a single unsigned integer.</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2008/12/bit1.png"/></p>
<p>What&#8217;s interesting is that we now know that an integer is 4 bytes, this would imply that we can store up to 32 separate flags within our structure. To verify this the case, let&#8217;s create a new structure that looks as follows:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">struct</span>
<span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">unsigned</span> preLoaded<span style="color: #002200;">:</span> <span style="color: #2400d9;">1</span>;
  <span style="color: #a61390;">unsigned</span> saveState<span style="color: #002200;">:</span> <span style="color: #2400d9;">1</span>;
  <span style="color: #a61390;">unsigned</span> saveLoginName<span style="color: #002200;">:</span> <span style="color: #2400d9;">1</span>;
  <span style="color: #a61390;">unsigned</span> buffer<span style="color: #002200;">:</span> <span style="color: #2400d9;">28</span>;
  <span style="color: #a61390;">unsigned</span> savePassword<span style="color: #002200;">:</span> <span style="color: #2400d9;">1</span>;
<span style="color: #002200;">&#125;</span> statusFlagsExtended;</pre></td></tr></table></div>

<p>In this structure, &#8216;buffer&#8217; is nothing more than a means for me to soak up 28 bits such that I can add one more bit on the end. Let&#8217;s take a look at the sizeof this structure with the code below:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Setting bits</span>
statusFlags.preLoaded <span style="color: #002200;">=</span> <span style="color: #2400d9;">1</span>;
statusFlags.saveState <span style="color: #002200;">=</span> statusFlags.saveLoginName <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Get bits</span>
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>statusFlags.saveLoginName <span style="color: #002200;">==</span> <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;saveLoginName bit is on.&quot;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #a61390;">else</span>
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;saveLoginName bit is off.&quot;</span><span style="color: #002200;">&#41;</span>;  
&nbsp;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Sizeof int: %d&quot;</span>, <span style="color: #a61390;">sizeof</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>; 
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Sizeof statusFlags: %d&quot;</span>, <span style="color: #a61390;">sizeof</span><span style="color: #002200;">&#40;</span>statusFlags<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Sizeof statusFlagsExtended: %d&quot;</span>, <span style="color: #a61390;">sizeof</span><span style="color: #002200;">&#40;</span>statusFlagsExtended<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;</pre></td></tr></table></div>

<p>As expected, the structure &#8216;statusFlagsExtended&#8217; is also stored as an integer.</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2008/12/bit2.png"/></p>
<img src="http://feeds.feedburner.com/~r/MacDeveloperTips/~4/_QoP16zWQuQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://MacDeveloperTips.com/c/bitfields-in-c.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://MacDeveloperTips.com/c/bitfields-in-c.html</feedburner:origLink></item>
		<item>
		<title>Introduction to Protocols</title>
		<link>http://feedproxy.google.com/~r/MacDeveloperTips/~3/4cTf-dnXSZs/introduction-to-protocols.html</link>
		<comments>http://MacDeveloperTips.com/cocoa/introduction-to-protocols.html#comments</comments>
		<pubDate>Sun, 26 Apr 2009 14:02:02 +0000</pubDate>
		<dc:creator>john</dc:creator>
				<category><![CDATA[Cocoa]]></category>

		<guid isPermaLink="false">http://MacDeveloperTips.com/?p=270</guid>
		<description><![CDATA[What follows is a quick introduction to working with protocols. This is good background information to understand as protocols are common in various Cocoa frameworks. A protocol is means to define a list of required and/or optional methods that a class implements. If a class adopts a protocol, it must implement all required methods in [...]]]></description>
				<content:encoded><![CDATA[<p>What follows is a quick introduction to working with protocols. This is good background information to understand as protocols are common in various Cocoa frameworks. A protocol is means to define a list of required and/or optional methods that a class implements. If a class adopts a protocol, it must implement all required methods in the protocols it adopts. </p>
<p>Cocoa uses protocols to support interprocess communication through Objective-C messages. In addition, since Objective-C does not support multiple inheritance, you can achieve similar functionality with protocols, as a class can adopt more than one protocol.<br />
<span id="more-270"></span></p>
<p>A good example of a protocol is <em>NSCoding</em>, which has two required methods that a class must implement. This protocol is used to enable classes to be encoded and decoded, that is, archiving of objects by writing to permanent storage.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@protocol</span> <span style="color: #2a6f76;">NSCoding</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>encodeWithCoder<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSCoder</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>aCoder;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>initWithCoder<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSCoder</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>aDecoder;
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<p>To adopt a protocol, enclose the name of the protocol in <> as below:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Interface</span>
<span style="color: #a61390;">@interface</span> SomeClass <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span> &lt;NSCoding&gt; 
<span style="color: #002200;">&#123;</span>
  ...
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// Implementation</span>
<span style="color: #a61390;">@implementation</span> SomeClass
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>encodeWithCoder<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSCoder</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>aCoder
<span style="color: #002200;">&#123;</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>initWithCoder<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSCoder</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>aDecoder
<span style="color: #002200;">&#123;</span>
  ...
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p><strong>Defining a Protocol</strong></p>
<p>You can create both required an optional methods within a protocol. What follows is a definion of a protocol named &#8216;Fubar&#8217;:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@protocol</span> Fubar
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>send<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>data;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>receive;
@optional
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>status;
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<p>To use the protocol, as with the example above, specify the protocol in the interface and write the required methods in the class implementation:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Interface</span>
<span style="color: #a61390;">@interface</span> AnotherClass <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span> &lt;Fubar&gt; 
<span style="color: #002200;">&#123;</span>
  ...
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// Implementation</span>
<span style="color: #a61390;">@implementation</span> AnotherClass
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>send<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>data
<span style="color: #002200;">&#123;</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>receive
<span style="color: #002200;">&#123;</span>
  ...
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// Optional methods</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>status
<span style="color: #002200;">&#123;</span>
  ...
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<p>If you are from a Java background, protocols should look familiar as they are analogous to an interface.</p>
<img src="http://feeds.feedburner.com/~r/MacDeveloperTips/~4/4cTf-dnXSZs" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://MacDeveloperTips.com/cocoa/introduction-to-protocols.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://MacDeveloperTips.com/cocoa/introduction-to-protocols.html</feedburner:origLink></item>
		<item>
		<title>Basics of Notifications</title>
		<link>http://feedproxy.google.com/~r/MacDeveloperTips/~3/2V6_2f1PBvA/basics-of-notifications.html</link>
		<comments>http://MacDeveloperTips.com/cocoa/basics-of-notifications.html#comments</comments>
		<pubDate>Wed, 15 Apr 2009 03:40:38 +0000</pubDate>
		<dc:creator>john</dc:creator>
				<category><![CDATA[Cocoa]]></category>

		<guid isPermaLink="false">http://MacDeveloperTips.com/?p=269</guid>
		<description><![CDATA[What follows is a brief guide to working with Notifications in Cocoa. I&#8217;ll cover the basics, including registering an observer and posting notifications, just enough to start using notifications in your apps. There is an instance of NSNotificationCenter available to every running application. This class acts as an intermediary to facilitate communication between objects that [...]]]></description>
				<content:encoded><![CDATA[<p>What follows is a brief guide to working with Notifications in Cocoa. I&#8217;ll cover the basics, including registering an observer and posting notifications,  just enough to start using notifications in your apps.</p>
<p>There is an instance of <em>NSNotificationCenter</em> available to every running application. This class acts as an intermediary to facilitate communication between objects that are interested in being notified at some point in the future (these objects are known as the  <em>observers</em>) and a <em>poster</em> that posts to the notification center, resulting in all observers (registered for a specific notification) being called.<br />
<span id="more-269"></span></p>
<p>To give you an idea of where you might use notifications, consider how you might handle downloading of data in a background thread. I recently used notifications in this scenario as I wanted to be notified when a web-service call completed. Upon receiving a notification, I then proceeded to populate a view with the data retrieved, or with an error message if the data access failed. </p>
<p>The 30,000 foot view consists of two steps:</p>
<p><strong>Step #1</strong><br />
Register an observer, which requests that a selector to be called when a specific notification is posted.</p>
<p><strong>Step #2</strong><br />
Post a notification, which will result in all registered observers being called.</p>
<p>For example:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Register observer to be notified when download of data is complete</span>
<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNotificationCenter</span> defaultCenter<span style="color: #002200;">&#93;</span> addObserver<span style="color: #002200;">:</span>self 
                                   selector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>downloadDataComplete<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> 
                                   name<span style="color: #002200;">:</span>NOTIF_DataComplete object<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>This code registers an observer for a notification with the name <em>NOTIF_DataComplete</em> (more on that below). When a notification is posted with the same name, the method <em>downloadDataComplete</em> will be called. </p>
<p>Posting a notification with the name <em>NOTIF_DataComplete</em> would look as follows:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Post notification that the download is complete  </span>
<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNotificationCenter</span> defaultCenter<span style="color: #002200;">&#93;</span> 
                    postNotificationName<span style="color: #002200;">:</span>NOTIF_DataComplete object<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>Let&#8217;s show how this might like in a working example.</p>
<p><strong>Class Registering as Observer</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//  SomeClass.h</span>
&nbsp;
<span style="color: #6e371a;">#import &lt;Foundation/Foundation.h&gt;</span>
&nbsp;
<span style="color: #a61390;">@interface</span> SomeClass <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span> 
<span style="color: #002200;">&#123;</span>
&nbsp;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// For name of notification</span>
<span style="color: #a61390;">extern</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span> <span style="color: #a61390;">const</span> NOTIF_DataComplete;
&nbsp;
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<p>Below is the code for the implementation of <em>SomeClass</em>. Notice on line 6 the name of the notification is defined. On line 40 we register as an observer for the named notification, specifying the selector <em>@selector(downloadDataComplete:)</em>. Once the notification is posted, the method on line 26 is called.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//  SomeClass.m</span>
&nbsp;
<span style="color: #6e371a;">#import &quot;SomeClass.h&quot;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// Name of notification</span>
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span> <span style="color: #a61390;">const</span> NOTIF_DataComplete <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;DataComplete&quot;</span>;
&nbsp;
<span style="color: #6e371a;">#pragma mark -</span>
<span style="color: #6e371a;">#pragma mark Private Interface</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Private interface definitions
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/</span>
<span style="color: #a61390;">@interface</span> SomeClass <span style="color: #002200;">&#40;</span>private<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> dataDownloadComplete<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSNotification</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>notif;
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> SomeClass
&nbsp;
<span style="color: #6e371a;">#pragma mark -</span>
<span style="color: #6e371a;">#pragma mark Private Methods</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">/*---------------------------------------------------------------------------
* Notifications of data downloads 
*--------------------------------------------------------------------------*/</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>downloadDataComplete<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSNotification</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>notif 
<span style="color: #002200;">&#123;</span>
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Received Notification - Data has been downloaded&quot;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #6e371a;">#pragma mark -</span>
<span style="color: #6e371a;">#pragma mark Initialization</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">/*---------------------------------------------------------------------------
* Initialization
*--------------------------------------------------------------------------*/</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> init
<span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// Other initialization code here...</span>
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Register observer to be called when download of data is complete</span>
  <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNotificationCenter</span> defaultCenter<span style="color: #002200;">&#93;</span> addObserver<span style="color: #002200;">:</span>self 
                         selector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>downloadDataComplete<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> 
                         name<span style="color: #002200;">:</span>NOTIF_DataComplete object<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>; 
&nbsp;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #6e371a;">#pragma mark -</span>
<span style="color: #6e371a;">#pragma mark Cleanup</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">/*---------------------------------------------------------------------------
* Cleanup 
*--------------------------------------------------------------------------*/</span> 
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>dealloc 
<span style="color: #002200;">&#123;</span>
  <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNotificationCenter</span> defaultCenter<span style="color: #002200;">&#93;</span> removeObserver<span style="color: #002200;">:</span>self<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>super dealloc<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<p><strong>Class That Posts Notification</strong></p>
<p>To keep things simple, I&#8217;ll add a few lines to the <em>applicationDidFinishLaunching</em> method to show you how posting a notification might look:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>applicationDidFinishLaunching<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIApplication <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>application 
<span style="color: #002200;">&#123;</span>
  <span style="color: #002200;">&#91;</span>window makeKeyAndVisible<span style="color: #002200;">&#93;</span>;
&nbsp;
  SomeClass <span style="color: #002200;">*</span>someclass <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>SomeClass alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// ...</span>
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Post a notification that the data has been downloaded  </span>
  <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNotificationCenter</span> defaultCenter<span style="color: #002200;">&#93;</span> 
            postNotificationName<span style="color: #002200;">:</span>NOTIF_DataComplete object<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>I create an instance of SomeClass and follow this by posting of a notification of the name <em>NOTIF_DataComplete</em>. This example is a little contrived, however, I&#8217;m sure you get the idea. Although a short example, it shows just how easy it is to register an observer and post notifications.</p>
<p>There are various other nuances to working with notifications that can be quite helpful when the time comes that you need more robust support for communication among objects. For example, you could specify that you are interested in receiving a notification by name (as above), however, only if that notification has a specific object affiliated with it. This would allow you to use the same notification name, however, only notify certain objects. One use of this could be if you have multiple downloads and want to use notifications to be notify a specific object that it&#8217;s download is complete.</p>
<p>If you are working with multiple threads, which would be the case if you need to fire off multiple downloads in the background, you can look into distributed notifications, which allow notifications to be delivered to a particular thread.</p>
<img src="http://feeds.feedburner.com/~r/MacDeveloperTips/~4/2V6_2f1PBvA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://MacDeveloperTips.com/cocoa/basics-of-notifications.html/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		<feedburner:origLink>http://MacDeveloperTips.com/cocoa/basics-of-notifications.html</feedburner:origLink></item>
		<item>
		<title>Gotcha: GCC and Unused Values</title>
		<link>http://feedproxy.google.com/~r/MacDeveloperTips/~3/qOA4sxO-xIU/gotcha-gcc-and-unused-values.html</link>
		<comments>http://MacDeveloperTips.com/xcode/gotcha-gcc-and-unused-values.html#comments</comments>
		<pubDate>Mon, 06 Apr 2009 12:55:57 +0000</pubDate>
		<dc:creator>john</dc:creator>
				<category><![CDATA[Xcode]]></category>

		<guid isPermaLink="false">http://MacDeveloperTips.com/?p=267</guid>
		<description><![CDATA[Subtle typos and the problems they bring, it&#8217;s enough to drive one nuts. Let me share a recent experience that will shed some light on my most recent experience. Below is a short snippet of code that show something similar to what I was recently attempting to do. It&#8217;s nothing more than a variable definition, [...]]]></description>
				<content:encoded><![CDATA[<p>Subtle typos and the problems they bring, it&#8217;s enough to drive one nuts. Let me share a recent experience that will shed some light on my most recent experience. Below is a short snippet of code that show something similar to what I was recently attempting to do. It&#8217;s nothing more than a variable definition, and in one place incrementing the variable, in another I decrementing.<br />
<span id="more-267"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;">NSInteger x <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Increment x</span>
x <span style="color: #002200;">+=</span> <span style="color: #2400d9;">1</span>;
&nbsp;
...
&nbsp;
<span style="color: #11740a; font-style: italic;">// Decrement x</span>
x <span style="color: #002200;">-+</span> <span style="color: #2400d9;">1</span>;</pre></td></tr></table></div>

<p>One small problem here, there is a typo in the code for decrementing x, which is easy to do given the + and = are on the say key.</p>
<p>It took some time to track down this problem as the compiler does not provide a warning for operations such as this that don&#8217;t have any effect.</p>
<p>Let&#8217;s add a compiler option so we receive a warning when we write a statement has no effect.</p>
<p><strong>Option #1: User Defined GCC Options</strong></p>
<p>- Make sure the Active SDK is set to one of the Simulator options<br />
- Expand the Target option in the project settings<br />
- Right click on the target<br />
- Choose the Build settings<br />
- Scroll to the bottom to the user defined settings<br />
- Add an entry: GCC_WARN_UNUSED_VALUE and set the value to YES</p>
<p><strong>Option #2: GCC Warning Options</strong></p>
<p>- Make sure the Active SDK is set to one of the Device options<br />
- Expand the Target option in the project settings<br />
- Right click on the target<br />
- Choose the Build settings<br />
- Scroll to the section GCC 4.0 &#8211; Warnings<br />
- Select the entry <em>Unused Values</em></p>
<p>Either of the above will suffice, it just depends on what you have set as the Active SDK.</p>
<p>Upon making this change, for statements that have no effect, you will now get a warning generated by the compiler:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2009/03/unused-value1.png" alt="" /></p>
<p>In an upcoming post I&#8217;ll show you how to set this as an option for all project templates.</p>
<img src="http://feeds.feedburner.com/~r/MacDeveloperTips/~4/qOA4sxO-xIU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://MacDeveloperTips.com/xcode/gotcha-gcc-and-unused-values.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://MacDeveloperTips.com/xcode/gotcha-gcc-and-unused-values.html</feedburner:origLink></item>
		<item>
		<title>Xcode Code Completion Macros</title>
		<link>http://feedproxy.google.com/~r/MacDeveloperTips/~3/sEhJtFfOgkU/xcode-code-completion-macros.html</link>
		<comments>http://MacDeveloperTips.com/xcode/xcode-code-completion-macros.html#comments</comments>
		<pubDate>Thu, 26 Mar 2009 21:17:36 +0000</pubDate>
		<dc:creator>john</dc:creator>
				<category><![CDATA[Xcode]]></category>

		<guid isPermaLink="false">http://MacDeveloperTips.com/?p=266</guid>
		<description><![CDATA[In the previous post I described the basics for working with code completion in Xcode. In this post I will show how you can use built-in text macros to insert various code fragments. As an example, begin by entering ifelse into Xcode and follow this by pressing Control . (control period) and you&#8217;ll see the [...]]]></description>
				<content:encoded><![CDATA[<p>In the <a href="http://macdevelopertips.com/xcode/xcode-code-completion.html" target="_blank">previous post</a> I described the basics for working with code completion in Xcode. In this post I will show how you can use built-in text macros to insert various code fragments.</p>
<p>As an example, begin by entering <strong>ifelse</strong> into Xcode and follow this by pressing <strong>Control .</strong> (control period) and you&#8217;ll see the following code block inserted:<br />
<span id="more-266"></span></p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2009/03/ifelse.png" alt="" /></p>
<p>At this point you can add code into the placeholder fields, moving from one field to the next using <strong>Control /</strong> (control forward-slash).</p>
<p>If you enter a code snippet in which there is more than one possible match, you can cycle through the options by pressing <strong>F5</strong> which will bring up a list of options in a popup window, or pressing <strong>Command .</strong> will cycle through the choices inline in your code.</p>
<p>For example, if you enter <strong>if</strong> and follow this by pressing F5, you will be shown the window below:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2009/03/if.png" alt="" /></p>
<p><strong>List of Available Macros</strong></p>
<p>The <a href="http://developer.apple.com/documentation/developertools/Conceptual/XcodeWorkspace/100-The_Text_Editor/chapter_5_section_8.html" target="_blank">Xcode documentation</a> includes a list of macros available for C, C++ and Objective-C. Some of the macros I use on a regular basis are shown below:</p>
<ul>
<li>nss -&gt; NSString</li>
<li>nsms -&gt; NSMutableString</li>
<li>nsd -&gt; NSDictionary</li>
<li>nsmd -&gt; NSMutableDictionary</li>
<li>a -&gt; array (press Control-. to toggle through options)</li>
<li>log -&gt; NSLog</li>
<li>if -&gt; if else (press Control-. to toggle through options)</li>
</ul>
<p><strong>Text Macros from the Menu System</strong><br />
Xcode includes a menu of text macros for C, C++, HTML among others. You can find these macros in the <strong>Edit</strong> menu under <strong>Insert Text Macro</strong>.</p>
<img src="http://feeds.feedburner.com/~r/MacDeveloperTips/~4/sEhJtFfOgkU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://MacDeveloperTips.com/xcode/xcode-code-completion-macros.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://MacDeveloperTips.com/xcode/xcode-code-completion-macros.html</feedburner:origLink></item>
		<item>
		<title>Xcode Code Completion</title>
		<link>http://feedproxy.google.com/~r/MacDeveloperTips/~3/To4UuclvgYE/xcode-code-completion.html</link>
		<comments>http://MacDeveloperTips.com/xcode/xcode-code-completion.html#comments</comments>
		<pubDate>Thu, 05 Mar 2009 14:26:35 +0000</pubDate>
		<dc:creator>john</dc:creator>
				<category><![CDATA[Xcode]]></category>

		<guid isPermaLink="false">http://MacDeveloperTips.com/?p=265</guid>
		<description><![CDATA[What follows is a quick review of how I use code completion in Xcode. Chances are that options and features exist beyond what I&#8217;ll cover here, so comments and suggestions are welcome. Let&#8217;s say I want to insert a CGRectMake method. I can begin by typing CG and pressing F5 (or Option-esc), which will popup [...]]]></description>
				<content:encoded><![CDATA[<p>What follows is a quick review of how I use code completion in Xcode. Chances are that options and features exist beyond what I&#8217;ll cover here, so comments and suggestions are welcome.</p>
<p>Let&#8217;s say I want to insert a CGRectMake method. I can begin by typing <strong>CG</strong> and pressing F5 (or Option-esc), which will popup a list of possible matches:<br />
<span id="more-265"></span></p>
<p><img width="500" src="http://iPhoneDeveloperTips.com/wp-content/uploads/2009/03/codecompl2.png" /></p>
<p>If I choose an entry from this list, Xcode takes care of the rest. If I continue to type characters (instead of hitting F5), Xcode will further narrow the available matches (assuming there is more than one). Again, at any point I can press F5 to bring up the matching list.</p>
<p>As an alternative to using F5 for a list of suggestions, I can hit tab at any point and Xcode will insert the matched text currently displayed. For example, in the figure below I entered <strong>CGRe</strong> and Xcode inserted the suggestion shown below:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2009/03/codecompl1.png" /></p>
<p>If I&#8217;d like to go with this suggestion, pressing the <strong>Tab</strong> key will insert the code.</p>
<p><strong>Filling in Placeholder Fields</strong><br />
The next step is to fill in any required fields that Xcode has recognized. If there are parameters, Xcode will highlight the first field as shown:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2009/03/codecompl3.png" /></p>
<p>Enter the value you want in the field and press <strong>Control /</strong> to move to the next field.<br />
<strong><br />
Code Sense Preference Settings</strong><br />
If you have any trouble getting these suggestions to work, I&#8217;ve included a screenshot of the Code Sense settings that I have configured in Xcode:</p>
<p><img width="550" src="http://iPhoneDeveloperTips.com/wp-content/uploads/2009/03/codecompl0.png" /></p>
<p>In the next post I&#8217;ll show how you can use various built-in macros to quickly insert blocks of code.</p>
<img src="http://feeds.feedburner.com/~r/MacDeveloperTips/~4/To4UuclvgYE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://MacDeveloperTips.com/xcode/xcode-code-completion.html/feed</wfw:commentRss>
		<slash:comments>11</slash:comments>
		<feedburner:origLink>http://MacDeveloperTips.com/xcode/xcode-code-completion.html</feedburner:origLink></item>
		<item>
		<title>Safari 4 Beta</title>
		<link>http://feedproxy.google.com/~r/MacDeveloperTips/~3/VkKERdPcxag/safari-4-beta.html</link>
		<comments>http://MacDeveloperTips.com/general/safari-4-beta.html#comments</comments>
		<pubDate>Mon, 02 Mar 2009 03:05:27 +0000</pubDate>
		<dc:creator>john</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Safari]]></category>

		<guid isPermaLink="false">http://MacDeveloperTips.com/?p=264</guid>
		<description><![CDATA[A little off topic, but a good tip none the less&#8230; Apple recently released Safari 4 beta and if you haven&#8217;t given it a try, I highly recommend you do. The primary reason for passing on this tip is that my web browsing experience with Firefox over the past few months has been extremely frustrating [...]]]></description>
				<content:encoded><![CDATA[<p>A little off topic, but a good tip none the less&#8230;</p>
<p>Apple recently released <a target="_blank" href="http://www.apple.com/safari/download/">Safari 4 beta</a> and if you haven&#8217;t given it a try, I highly recommend you do.<br />
<span id="more-264"></span></p>
<p>The primary reason for passing on this tip is that my web browsing experience with Firefox over the past few months has been extremely frustrating (read slow!). From the moment I started the Safari 4 beta it was clear the performance was superior to Firefox. I realize there may be more to this than simply a faster browser, as my system could use a good house cleaning. However, I continue to go between both browsers and Safari is no doubt my new browser of choice for the bulk of my time on the net.</p>
<p>There is one default setting that I immediately had to change in Safari, and that&#8217;s the location of the tabs. If you haven&#8217;t tried the new browser, the figure below shows how the tab bar is now on top. </p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2009/03/safari1.png" /></p>
<p>I just couldn&#8217;t get used to this. Here&#8217;s how to change the tabbar to the &#8220;normal&#8221; location &#8211; from a terminal enter the following:</p>
<pre>
defaults write com.apple.Safari DebugSafari4TabBarIsOnTop -bool NO
</pre>
<p>You can find a list of additional configuration changes available in Safari 4 <a  target="_blank" href="http://swedishcampground.com/safari-4-hidden-preferences">here</a>. Beyond the tips, the <a  target="_blank" href="http://swedishcampground.com/about">About page</a> is worth a visit, an interesting bit of trivia to be had&#8230;</p>
<img src="http://feeds.feedburner.com/~r/MacDeveloperTips/~4/VkKERdPcxag" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://MacDeveloperTips.com/general/safari-4-beta.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://MacDeveloperTips.com/general/safari-4-beta.html</feedburner:origLink></item>
		<item>
		<title>Objective-C Object as a C Structure</title>
		<link>http://feedproxy.google.com/~r/MacDeveloperTips/~3/z3dJGp8YeqY/objective-c-object-as-a-c-structure.html</link>
		<comments>http://MacDeveloperTips.com/objective-c/objective-c-object-as-a-c-structure.html#comments</comments>
		<pubDate>Sun, 01 Mar 2009 21:06:46 +0000</pubDate>
		<dc:creator>john</dc:creator>
				<category><![CDATA[Objective-C]]></category>

		<guid isPermaLink="false">http://MacDeveloperTips.com/?p=263</guid>
		<description><![CDATA[Okay, so figuring out how to unwind an Objective-C object into its base representation goes against all that is object-oriented programming, however, it&#8217;s interesting none-the-less. In Objective-C there is a directive, @defs(), that outputs (at compile time) the list of instance variables for a class. We can use this list to create a C structure [...]]]></description>
				<content:encoded><![CDATA[<p>Okay, so figuring out how to unwind an Objective-C object into its base representation goes against all that is object-oriented programming, however, it&#8217;s interesting none-the-less. </p>
<p>In Objective-C there is a directive, @defs(), that outputs (at compile time) the list of instance variables for a class. We can use this list to create a C structure with the variables of a specified class.<br />
<span id="more-263"></span></p>
<p>For instance, if we have a class named TestClass, here is how one might create a structure using the @defs directive:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">struct</span> testClassStructure
<span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">@defs</span><span style="color: #002200;">&#40;</span>TestClass<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span> <span style="color: #002200;">*</span>testClassAsStruct;</pre></td></tr></table></div>

<p>Before we look at how to use this structure to access the variables of TestClass, let&#8217;s look at a simple interface and implementation of TestClass:</p>
<p><strong>TestClass Interface</strong></p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//  TestClass.h</span>
<span style="color: #11740a; font-style: italic;">//</span>
<span style="color: #6e371a;">#import &lt;UIKit/UIKit.h&gt;</span>
&nbsp;
<span style="color: #a61390;">@interface</span> TestClass <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span> 
<span style="color: #002200;">&#123;</span>
  <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>testString;
  <span style="color: #a61390;">int</span>      testInteger;
  <span style="color: #a61390;">BOOL</span>     testBoolean;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>testString;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>readonly<span style="color: #002200;">&#41;</span> <span style="color: #a61390;">int</span> testInteger;
<span style="color: #a61390;">@property</span> <span style="color: #a61390;">BOOL</span> testBoolean;
&nbsp;
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<p><strong>TestClass Implementation</strong></p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//  TestClass.m</span>
<span style="color: #11740a; font-style: italic;">//  </span>
<span style="color: #6e371a;">#import &quot;TestClass.h&quot;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">/*-------------------------------------
* TestClass implementation
*-------------------------------------*/</span>
<span style="color: #a61390;">@implementation</span> TestClass
&nbsp;
<span style="color: #a61390;">@synthesize</span> testString;
<span style="color: #a61390;">@synthesize</span> testInteger;
<span style="color: #a61390;">@synthesize</span> testBoolean;
&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>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super init<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> 
  <span style="color: #002200;">&#123;</span>
    testString <span style="color: #002200;">=</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> initWithString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Fubar&quot;</span><span style="color: #002200;">&#93;</span>;
    testInteger <span style="color: #002200;">=</span> <span style="color: #2400d9;">99</span>;
    testBoolean <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;
  <span style="color: #002200;">&#125;</span>
  <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>dealloc 
<span style="color: #002200;">&#123;</span>
  <span style="color: #002200;">&#91;</span>testString release<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>super dealloc<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<p>Pretty simple stuff. Our interface defines three instance variables, a string, an integer and a boolean. The implementation file, upon initialization of a TestClass object sets defaults values for each instance variable.</p>
<p>So, let&#8217;s look at how to use a C structure to read/write the instance variables of TestClass:</p>
<p><strong>AppDelegate Interface</strong></p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//  UntitledAppDelegate.h</span>
&nbsp;
<span style="color: #6e371a;">#import &lt;UIKit/UIKit.h&gt;</span>
&nbsp;
<span style="color: #a61390;">@interface</span> UntitledAppDelegate <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span> &lt;UIApplicationDelegate&gt; 
<span style="color: #002200;">&#123;</span>
    UIWindow <span style="color: #002200;">*</span>window;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> IBOutlet UIWindow <span style="color: #002200;">*</span>window;
&nbsp;
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<p><strong>AppDelegate Implementation</strong></p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//  UntitledAppDelegate.m</span>
&nbsp;
<span style="color: #6e371a;">#import &quot;UntitledAppDelegate.h&quot;</span>
<span style="color: #6e371a;">#import &quot;testClass.h&quot;</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> UntitledAppDelegate
&nbsp;
<span style="color: #a61390;">@synthesize</span> window;
&nbsp;
<span style="color: #a61390;">struct</span> testClassStructure
<span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">@defs</span><span style="color: #002200;">&#40;</span>TestClass<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span> <span style="color: #002200;">*</span>testClassAsStruct;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>applicationDidFinishLaunching<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIApplication <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>application 
<span style="color: #002200;">&#123;</span> 
&nbsp;
  TestClass <span style="color: #002200;">*</span>tmp;
&nbsp;
  tmp <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>TestClass alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
&nbsp;
  testClassAsStruct <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">struct</span> testClassStructure <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> tmp;
&nbsp;
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;testString: %@&quot;</span>, testClassAsStruct<span style="color: #002200;">-</span>&gt;testString<span style="color: #002200;">&#41;</span>;
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;testInteger: %d&quot;</span>, testClassAsStruct<span style="color: #002200;">-</span>&gt;testInteger<span style="color: #002200;">&#41;</span>;
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;testBoolean: %s&quot;</span>, testClassAsStruct<span style="color: #002200;">-</span>&gt;testBoolean <span style="color: #002200;">==</span> <span style="color: #a61390;">YES</span> ? <span style="color: #bf1d1a;">&quot;Yes&quot;</span> <span style="color: #002200;">:</span> <span style="color: #bf1d1a;">&quot;No&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
  testClassAsStruct<span style="color: #002200;">-</span>&gt;testString <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;A new string&quot;</span>;
  testClassAsStruct<span style="color: #002200;">-</span>&gt;testInteger <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
  testClassAsStruct<span style="color: #002200;">-</span>&gt;testBoolean <span style="color: #002200;">=</span> <span style="color: #a61390;">NO</span>;
&nbsp;
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;testString: %@&quot;</span>, testClassAsStruct<span style="color: #002200;">-</span>&gt;testString<span style="color: #002200;">&#41;</span>;
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;testInteger: %d&quot;</span>, testClassAsStruct<span style="color: #002200;">-</span>&gt;testInteger<span style="color: #002200;">&#41;</span>;
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;testBoolean: %s&quot;</span>, testClassAsStruct<span style="color: #002200;">-</span>&gt;testBoolean <span style="color: #002200;">==</span> <span style="color: #a61390;">YES</span> ? <span style="color: #bf1d1a;">&quot;Yes&quot;</span> <span style="color: #002200;">:</span> <span style="color: #bf1d1a;">&quot;No&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
  <span style="color: #002200;">&#91;</span>tmp release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>dealloc 
<span style="color: #002200;">&#123;</span>
  <span style="color: #002200;">&#91;</span>window release<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>super dealloc<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<p>And of course, the output:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2008/12/cstructasobject.png"/></p>
<p>There you have it. An inside look at how to work with an Objective-C object using a C structure. </p>
<img src="http://feeds.feedburner.com/~r/MacDeveloperTips/~4/z3dJGp8YeqY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://MacDeveloperTips.com/objective-c/objective-c-object-as-a-c-structure.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<feedburner:origLink>http://MacDeveloperTips.com/objective-c/objective-c-object-as-a-c-structure.html</feedburner:origLink></item>
		<item>
		<title>Open Source Screen Capture Tool</title>
		<link>http://feedproxy.google.com/~r/MacDeveloperTips/~3/HSRkCEuBg0k/open-source-screen-capture-tool.html</link>
		<comments>http://MacDeveloperTips.com/tools-utilities/open-source-screen-capture-tool.html#comments</comments>
		<pubDate>Mon, 23 Feb 2009 14:56:17 +0000</pubDate>
		<dc:creator>john</dc:creator>
				<category><![CDATA[Tools/Utilities]]></category>

		<guid isPermaLink="false">http://MacDeveloperTips.com/?p=262</guid>
		<description><![CDATA[I&#8217;ve been on the hunt for an open source screen cature tool for the Mac. I finally found what I was looking for, a tool that in which you can specify the exact size of the capture window. Let me explain&#8230; In the process of creating screenshots to accompany applications submitted to the App Store, [...]]]></description>
				<content:encoded><![CDATA[<p>I&#8217;ve been on the hunt for an open source screen cature tool for the Mac. I finally found what I was looking for, a tool that in which you can specify the exact size of the capture window. Let me explain&#8230;</p>
<p>In the process of creating screenshots to accompany applications submitted to the App Store, you can include up to 5 images. Although the images can be anything you like, typically the images consist of screenshots of an application running in the iPhone simulator. When working with the simulator there are two sizes used most frequently, 320w x 460h (Apple recommends not to show the status bar across the top) and 480w x 320h (landscape mode).<br />
<span id="more-262"></span></p>
<p>Until I found <a target="_blank" href="http://www.chimoosoft.com/products/captureme/">Capture Me</a>, I would use the built-in screen capture, command-shift-4, to capture screenshots. Problem is, there was no means to specify the exact image size to capture, so this approach requires either a very steady hand and a good eye, or capturing an area obviously larger than needed and cropping in Preview or another tool.</p>
<p>To use Capture Me, you specify the size of the area to capture and then drag a semi-transparent window over the capture area. Clicking inside the capture area creates a screenshot. You can save screenshots to the desktop (filename will be automatically generated) or have the screenshot copied to the clipboard, or both! </p>
<p>Capture Me also offers a screen recording feature, however, I haven&#8217;t given this a go as of yet.</p>
<p><strong>Additional Features:</strong><br />
Here are a few things that I think would make for some nice additions to Capture Me:</p>
<p>#1 &#8211; Specify the location to save the file</p>
<p>#2 &#8211; Specify the filename for the screenshot. For example, when capturing/uploading screenshots to the App Store I use the filenames &#8220;screenshot1.jpg&#8221; &#8220;screenshot2.jpg&#8221; etc. If I could tell Capture Me to use this same format, it would save me the trouble of renaming the files.</p>
<p>#3 &#8211; The third feature is a little harder to describe, so let me explain how I use the tool which should help clarify what I am after. As I mentioned above, I use Capture Me to create screenshots of the iPhone simulator, what this entails is dragging Capture Me over the simulator and clicking inside the Capture Me window. In order to create the next screenshot, I need to click/drag the Capture Me window and move it to the side, click on the iPhone simulator to setup the next screenshot, then drag the Capture Me window back over the simulator for the next capture. </p>
<p>If I could tell the Capture Me window to move to the left/right the same width of the area I am capturing (+/- a few pixles) I could quickly capture a series of screenshots with ease. Even better if one keyboard shortcut would tell Capture Me to slide to the left/right, and one shortcut to tell Capture Me to slide it back to it&#8217;s original location.</p>
<p>I hope to get some time over the coming weeks and/or months to poke around the Capture Me code and see how far I can get to add the features above. If you beat me to the punch, drop me a note, and count me in as a beta tester.</p>
<p>Many thanks to <a target="_blank" href="http://www.chimoosoft.com/">Chimoosoft</a> for creating, and open sourcing, <a  target="_blank" href="http://www.chimoosoft.com/products/captureme/">Capture Me</a>.</p>
<img src="http://feeds.feedburner.com/~r/MacDeveloperTips/~4/HSRkCEuBg0k" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://MacDeveloperTips.com/tools-utilities/open-source-screen-capture-tool.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://MacDeveloperTips.com/tools-utilities/open-source-screen-capture-tool.html</feedburner:origLink></item>
	</channel>
</rss>
