<?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>if (programmer == ramble) [self blog]</title>
	<atom:link href="http://newwavedigitalmedia.com/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://newwavedigitalmedia.com</link>
	<description>A place to ramble about programming and life.</description>
	<lastBuildDate>Mon, 02 Aug 2010 01:39:26 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Single binding for calcuated values</title>
		<link>http://newwavedigitalmedia.com/?p=132</link>
		<comments>http://newwavedigitalmedia.com/?p=132#comments</comments>
		<pubDate>Sun, 01 Aug 2010 06:37:43 +0000</pubDate>
		<dc:creator>Scott Andrew</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[bindings]]></category>
		<category><![CDATA[Cocoa]]></category>

		<guid isPermaLink="false">http://newwavedigitalmedia.com/?p=132</guid>
		<description><![CDATA[<p>One of the tricks I have recently learned was how to bind a single object to multiple properties to create a calculated binding. This comes in handy if you have a field that or an image that relies on the value of more than one property and you would rather use bindings than manage the calculated <span style="color:#777"> . . . &#8594; Read More: <a href="http://newwavedigitalmedia.com/?p=132">Single binding for calcuated values</a></span>]]></description>
			<content:encoded><![CDATA[<p>One of the tricks I have recently learned was how to bind a single object to multiple properties to create a calculated binding. This comes in handy if you have a field that or an image that relies on the value of more than one property and you would rather use bindings than manage the calculated field by watching for values being changed by delegates or notification.</p>
<p>The key to doing this is to define a getter for the calculated value and a keysForValuesAffecting&lt;getter&gt; function to define what keys affect the value.</p>
<p>In this sample app we will create a first name, middle name, and last name field. The static full name field will display the full name as it is typed. There is also a light that will be green when we have both a first name and a last name and red if we don&#8217;t. The sample app looks like:</p>
<p><a href="http://newwavedigitalmedia.com/wp-content/uploads/2010/08/KeysAffectingValuesDemoScreenSnapz001.png"><img class="aligncenter size-full wp-image-134" title="KeysAffectingValuesDemoScreenSnapz001" src="http://newwavedigitalmedia.com/wp-content/uploads/2010/08/KeysAffectingValuesDemoScreenSnapz001.png" alt="" width="454" height="197" /></a></p>
<p>The KeyPathsForValuesAffecting sample project can be downloaded <a href="http://newwavedigitalmedia.com/wp-content/uploads/2010/08/KeyPathsForValuesAffectingDemo.zip">here</a>. </p>
<p>
<p><h3><strong>What is going on..</strong></h3>
<p>When you define a binding for a key path that doesn&#8217;t exist in an object, cocoa will call keysForValueAffecting&lt;getter&gt; on the class. This allows the user to define what keys are going to affect the getter we are going to supply. The function name requires the name of the getter at the end of the function name. So in our example we have a getter defined as &#8220;fullName,&#8221; we also supply  a keysForValueAffectingFullName class function that will tell cocoa we want the first, middle and last name fields.</p>
<p>
<p><h3><strong>Create new project</strong></h3>
<ol>
<li>Select New Project from the file menu or press Shift+Command+N</li>
<li>Select &#8220;Cocoa Application&#8221; press &#8220;Choose..&#8221;</li>
<li>Name the project KeyPathsForValuesAffectingDemo and press ok.</li>
<li>Once the project is created add the readimage.png and greenimage.png that are in this <a href="http://newwavedigitalmedia.com/wp-content/uploads/2010/08/redandgreenlight.zip">zip file</a>.</li>
</ol>
<p><a href="http://newwavedigitalmedia.com/wp-content/uploads/2010/06/Screen-shot-2010-08-01-at-3.23.30-PM.png"><img class="aligncenter size-full wp-image-137" title="Create New Project" src="http://newwavedigitalmedia.com/wp-content/uploads/2010/06/Screen-shot-2010-08-01-at-3.23.30-PM.png" alt="" width="744" height="608" /></a></p>
<p>
<p><h3><strong>Edit the MainWindow.xib</strong></h3>
<p>The next steps is to add our controls and bind their values to our application controller.  To open interface builder double click on the MainWindow.xib item, usually found in the resources folder.</p>
<p>Once interface builder is open you will want to double click the main window to edit it. Once the window is open add your text fields, edit fields and an image field. It should look similiar to this:<br />
<a href="http://newwavedigitalmedia.com/wp-content/uploads/2010/06/Interface-BuilderScreenSnapz002.png"><img src="http://newwavedigitalmedia.com/wp-content/uploads/2010/06/Interface-BuilderScreenSnapz002.png" alt="" title="Inteface Builder 01" width="454" height="197" class="aligncenter size-full wp-image-213" /></a></p>
<p>
<p><h3><strong>Binding the controls</strong></h3>
<p>Now we need to bind each control. We haven&#8217;t written the code yet, but lets get it done while we are in here.</p>
<p>We want to bind our our &#8220;First Name&#8221; text field to the firstName property in our application delegate. We also want to make it update as as the value changes not when the tab key is hit. So to do that follow the following steps:</p>
<ol>
<li>Select the &#8220;First  Name&#8221; edit field</li>
<li>Select the &#8220;Bindings&#8221; tab in the inspector or press command+4.</li>
<li>Expand the &#8220;Value&#8221; category.</li>
<li>Select the &#8220;Key Paths For Values Affecting Demo App Delegate&#8221; from the &#8220;Bind to:&#8221; popup.</li>
<li>Select the checkbox next to the &#8220;Bind to:&#8221; popup.</li>
<li>Set the &#8220;Model Key Path&#8221; to self.firstName</li>
<li>Check the Continuously Update Value box</li>
</ol>
<p><a href="http://newwavedigitalmedia.com/wp-content/uploads/2010/06/Interface-BuilderScreenSnapz003.png"><img src="http://newwavedigitalmedia.com/wp-content/uploads/2010/06/Interface-BuilderScreenSnapz003.png" alt="" title="Interface BuilderScreenSnapz003" width="287" height="774" class="aligncenter size-full wp-image-215" /></a><br />
Next we need to setup our &#8220;Middle Name&#8221; field in the same way:</p>
<ol>
<li>Select the &#8220;Middle  Name&#8221; edit field</li>
<li>Select the &#8220;Bindings&#8221; tab in the inspector or press command+4.</li>
<li>Expand the &#8220;Value&#8221; category.</li>
<li>Select the &#8220;Key Paths For Values Affecting Demo App Delegate&#8221; from the &#8220;Bind to:&#8221; popup.</li>
<li>Select the checkbox next to the &#8220;Bind to:&#8221; popup.</li>
<li>Set the &#8220;Model Key Path&#8221; to self.middleName</li>
<li>Check the Continuously Update Value box</li>
</ol>
<p>We then need to bind the &#8220;Last Name&#8221; field as we did the last two:</p>
<ol>
<li>Select the &#8220;Last  Name&#8221; edit field</li>
<li>Select the &#8220;Bindings&#8221; tab in the inspector or press command+4.</li>
<li>Expand the &#8220;Value&#8221; category.</li>
<li>Select the &#8220;Key Paths For Values Affecting Demo App Delegate&#8221; from the &#8220;Bind to:&#8221; popup.</li>
<li>Select the checkbox next to the &#8220;Bind to:&#8221; popup.</li>
<li>Set the &#8220;Model Key Path&#8221; to self.lastName</li>
<li>Check the Continuously Update Value box</li>
</ol>
<p>We added a field to show our full name. We need to bind that to our fullName key path.</p>
<ol>
<li>Select the text field to dislay the full name.</li>
<li>Select the &#8220;Bindings&#8221; tab in the inspector or press command+4.</li>
<li>Expand the &#8220;Value&#8221; category.</li>
<li>Select the &#8220;Key Paths For Values Affecting Demo App Delegate&#8221; from the &#8220;Bind to:&#8221; popup.</li>
<li>Select the checkbox next to the &#8220;Bind to:&#8221; popup.</li>
<li>Set the &#8220;Model Key Path&#8221; to self.fullName</li>
</ol>
<p>Lastly we need to setup our light image.</p>
<ol>
<li>Select the image</li>
<li>Select the &#8220;Attributes&#8221; tab in the inspector or press command+1</li>
<li>Set the image to &#8220;redlight&#8221;.</li>
<li>Set the border to &#8220;None&#8221;.</li>
<li>Set the scaling to &#8220;None&#8221;.</li>
<li>Press command+= to make the view as big as the image.</li>
<li>Align the image until it looks good.</li>
<li>Select the &#8220;Bindings&#8221; tab in the inspector or press command+4.</li>
<li>Expand the &#8220;Value&#8221; category.</li>
<li>Select the &#8220;Key Paths For Values Affecting Demo App Delegate&#8221; from the &#8220;Bind to:&#8221; popup.</li>
<li>Select the checkbox next to the &#8220;Bind to:&#8221; popup.</li>
<li>Set the &#8220;Model Key Path&#8221; to self.statusLight</li>
</ol>
<p>Save the xib file and go back to XCode.</p>
<p>
<p><h3><strong>KeyPathsForValuesAffectingDemoAppDelegate.h</strong></h3>
<p>We need to add first, middle and name NSString instance variables to our class. These are each properties that we will set with our bindings. We also will add two NSImage instance variables to our header to hold our green and red light images.</p>
<div class="codecolorer-container objc dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #a61390;">@interface</span> KeyPathsForValuesAffectingDemoAppDelegate <span style="color: #002200;">:</span> <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSObject_Class/"><span style="color: #400080;">NSObject</span></a> &lt;NSApplicationDelegate&gt; <span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSWindow_Class/"><span style="color: #400080;">NSWindow</span></a> <span style="color: #002200;">*</span>window;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a><span style="color: #002200;">*</span> firstName;<br />
&nbsp; &nbsp; <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a><span style="color: #002200;">*</span> middleName;<br />
&nbsp; &nbsp; <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a><span style="color: #002200;">*</span> lastName;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSImage_Class/"><span style="color: #400080;">NSImage</span></a><span style="color: #002200;">*</span> redLight;<br />
&nbsp; &nbsp; <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSImage_Class/"><span style="color: #400080;">NSImage</span></a><span style="color: #002200;">*</span> greenLight;<br />
<span style="color: #002200;">&#125;</span><br />
<br />
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>readwrite, copy<span style="color: #002200;">&#41;</span> <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a><span style="color: #002200;">*</span> firstName;<br />
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>readwrite, copy<span style="color: #002200;">&#41;</span> <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a><span style="color: #002200;">*</span> middleName;<br />
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>readwrite, copy<span style="color: #002200;">&#41;</span> <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a><span style="color: #002200;">*</span> lastName;<br />
<br />
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>assign<span style="color: #002200;">&#41;</span> IBOutlet <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSWindow_Class/"><span style="color: #400080;">NSWindow</span></a> <span style="color: #002200;">*</span>window;<br />
<br />
<span style="color: #a61390;">@end</span></div></div>
<p>
<p><h3><strong>KeyPathsForValuesAffectingDemoAppDelegate.m</strong></h3>
<p>The first thing we need to add to our implementation are the synthesizers for firstName, middleName and lastName.</p>
<div class="codecolorer-container objc dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #a61390;">@synthesize</span> firstName;<br />
<span style="color: #a61390;">@synthesize</span> middleName;<br />
<span style="color: #a61390;">@synthesize</span> lastName;</div></div>
<p>We don&#8217;t want to forget to release things in our dealloc.</p>
<div class="codecolorer-container objc dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><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><br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>firstName release<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>middleName release<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>lastName release<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>redLight release<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>greenLight release<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>super dealloc<span style="color: #002200;">&#93;</span>;<br />
<span style="color: #002200;">&#125;</span></div></div>
<p>Here is where the magic starts to happen. We need to tell cocoa what key affects our value for fullName. So we need to implement the keyPathsForValuesAffectingFullName. What we need to return is a set with the key paths to our first name, middle name, and last name so our getter get&#8217;s called anytime these values are changed.</p>
<div class="codecolorer-container objc dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #002200;">+</span><span style="color: #002200;">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSSet_Class/"><span style="color: #400080;">NSSet</span></a><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> keyPathsForValuesAffectingFullName <span style="color: #002200;">&#123;</span><br />
<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSSet_Class/"><span style="color: #400080;">NSSet</span></a> setWithObjects<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;self.firstName&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;self.middleName&quot;</span>,<br />
<span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;self.lastName&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;<br />
<span style="color: #002200;">&#125;</span></div></div>
<p>Now we can implement our fullName getter. This basically will just create our full name string from the first, middle and last names. This will get called anytime the first, middle or last name changes.</p>
<div class="codecolorer-container objc dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> fullName <span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSMutableString_Class/"><span style="color: #400080;">NSMutableString</span></a><span style="color: #002200;">*</span> <span style="color: #a61390;">string</span> <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSMutableString_Class/"><span style="color: #400080;">NSMutableString</span></a> <span style="color: #a61390;">string</span><span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>firstName <span style="color: #002200;">!=</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#91;</span><span style="color: #a61390;">string</span> appendString<span style="color: #002200;">:</span>firstName<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>middleName <span style="color: #002200;">!=</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>firstName <span style="color: #002200;">!=</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#91;</span><span style="color: #a61390;">string</span> appendString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot; &quot;</span><span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#91;</span><span style="color: #a61390;">string</span> appendString<span style="color: #002200;">:</span>middleName<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>lastName <span style="color: #002200;">!=</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>firstName <span style="color: #002200;">!=</span> <span style="color: #a61390;">nil</span> || middleName <span style="color: #002200;">!=</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#91;</span><span style="color: #a61390;">string</span> appendString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot; &quot;</span><span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#91;</span><span style="color: #a61390;">string</span> appendString<span style="color: #002200;">:</span>lastName<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #a61390;">return</span> <span style="color: #a61390;">string</span>;<br />
<span style="color: #002200;">&#125;</span></div></div>
<p>We have also added one for the status light. The light will be green if both the first and last name are not nil, red if anyone of them are nil.  So first thing we do is setup the key paths that affect the light.</p>
<div class="codecolorer-container objc dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #002200;">+</span><span style="color: #002200;">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSSet_Class/"><span style="color: #400080;">NSSet</span></a><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> keyPathsForValuesAffectingStatusLight <span style="color: #002200;">&#123;</span><br />
<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSSet_Class/"><span style="color: #400080;">NSSet</span></a> setWithObjects<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;self.firstName&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;self.lastName&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;<br />
<span style="color: #002200;">&#125;</span></div></div>
<p>Now we return the appropriate image depending on the state of the first and last name.</p>
<div class="codecolorer-container objc dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSImage_Class/"><span style="color: #400080;">NSImage</span></a><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> statusLight <span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSImage_Class/"><span style="color: #400080;">NSImage</span></a><span style="color: #002200;">*</span> image <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>firstName <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span> || lastName <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>redLight <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; redLight <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSImage_Class/"><span style="color: #400080;">NSImage</span></a> imageNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;redlight.png&quot;</span><span style="color: #002200;">&#93;</span> retain<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; image <span style="color: #002200;">=</span> redLight;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #a61390;">else</span> <span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>greenLight <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; greenLight <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSImage_Class/"><span style="color: #400080;">NSImage</span></a> imageNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;greenlight.png&quot;</span><span style="color: #002200;">&#93;</span> retain<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; image <span style="color: #002200;">=</span> greenLight;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #a61390;">return</span> image;<br />
<span style="color: #002200;">&#125;</span></div></div>
<p>
<p><h3><strong>Conclusion</strong></h3>
<p>keyPathsForValuesAffecting unlocks a lot of possible ways to use bindings beyond the usual one key path per-binding. This is a simple example of how to bind to a calculated value that is completely controlled by code. The other nice thing about this is that it is category friendly, so you can easily extend an object to support a calculated binding.</p>
<p>Download sample project is <a href="http://newwavedigitalmedia.com/wp-content/uploads/2010/08/KeyPathsForValuesAffectingDemo.zip">here</a>. </p>
]]></content:encoded>
			<wfw:commentRss>http://newwavedigitalmedia.com/?feed=rss2&amp;p=132</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>NSXMLElement &amp; NSXMLNode categories to share.</title>
		<link>http://newwavedigitalmedia.com/?p=104</link>
		<comments>http://newwavedigitalmedia.com/?p=104#comments</comments>
		<pubDate>Fri, 01 Jan 2010 19:37:01 +0000</pubDate>
		<dc:creator>Scott Andrew</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://newwavedigitalmedia.com/?p=104</guid>
		<description><![CDATA[<p>I have been working on an application that uses NSXML classes to parse HTML sites and massage them for use as an eBook. I created a couple of handy categories for NSXMLElement and NSXMLNode to make my life easier.</p>
<p>NSXMLNode</p>
<p>I found that when doing XPATH searches through the source. I usually was only looking for one node <span style="color:#777"> . . . &#8594; Read More: <a href="http://newwavedigitalmedia.com/?p=104">NSXMLElement &#038; NSXMLNode categories to share.</a></span>]]></description>
			<content:encoded><![CDATA[<p>I have been working on an application that uses NSXML classes to parse HTML sites and massage them for use as an eBook. I created a couple of handy categories for NSXMLElement and NSXMLNode to make my life easier.</p>
<p><strong>NSXMLNode</strong></p>
<p>I found that when doing XPATH searches through the source. I usually was only looking for one node (or the first node in the search). So i created a simple helper function:</p>
<code>-(<span style="color: #3366ff;">NSXMLNode</span><span style="color: #3366ff;">*</span>):singleNodeForXPath:(<span style="color: #3366ff;">NSString*</span>)xPath error:(<span style="color: #3366ff;">NSError**</span>)error</code>
<p>This is simple wrapper around the nodesForXPath call that returns the first node found in the search path. If no items are found then it returns nil. If multiple items are found it returns only the first one.</p>
<p>The other call is:</p>
<code>-(<span style="color: #3366ff;">NSString*</span>) stringValueForNodePathAtXPath:(<span style="color: #3366ff;">NSString*</span>)path error:(<span style="color: #3366ff;">NSError**</span>)error</code>
<p>This simply returns the string value for the first node found in an xPath search.</p>
<p>These just provide a simple way to do a quick search and return the first items found.</p>
<p><strong>NSXMLElement</strong></p>
<p>Since i am reconstructing elements and possibly manipulating the tree, I needed a way to do a shallow copy of a node so that it has no children. So i have added:</p>
<code>-(<span style="color: #3366ff;">NSXMLElement*</span>) shallowCopy</code>
<p>This will copy the element and all its attributes as a standalone node, no children and no parents.</p>
<p>You can download the source <a href="http://newwavedigitalmedia.com/wp-content/uploads/2010/01/XMLUtilities.zip">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://newwavedigitalmedia.com/?feed=rss2&amp;p=104</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>NWPickerField, a combo box for the iPhone.</title>
		<link>http://newwavedigitalmedia.com/?p=79</link>
		<comments>http://newwavedigitalmedia.com/?p=79#comments</comments>
		<pubDate>Thu, 08 Oct 2009 06:04:19 +0000</pubDate>
		<dc:creator>Scott Andrew</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[iPhone]]></category>

		<guid isPermaLink="false">http://newwavedigitalmedia.com/?p=79</guid>
		<description><![CDATA[<p>I was working on a project and needed a way to pick an expiration date for credit card processing and wanted to use the same control for choosing a state. The solution I came up with is a read-only picker field that uses the UIPickerView for input instead of the keyboard.</p>
<p>The idea was to have something <span style="color:#777"> . . . &#8594; Read More: <a href="http://newwavedigitalmedia.com/?p=79">NWPickerField, a combo box for the iPhone.</a></span>]]></description>
			<content:encoded><![CDATA[<p>I was working on a project and needed a way to pick an expiration date for credit card processing and wanted to use the same control for choosing a state. The solution I came up with is a read-only picker field that uses the UIPickerView for input instead of the keyboard.</p>
<p>The idea was to have something that looked like an edit control, since users know to click on it, but used a picker view instead of a keyboard for entry. So this control is a marriage of a UITextView subclass and a UIPickerView subclass.</p>
<p>I am releasing the class for all to use.  The code is <a title="hoted on github" href="http://github.com/scottandrew/NWPickerField">hosted on github</a> ready to be branched and used. If you use the control let me know.</p>
<p>To use the code its very simple:</p>
<ol>
<li>Add the
<div class="codecolorer-container text dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">NWPickerField</div></div>
<p>,</p>
<div class="codecolorer-container text dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">NWPickerView</div></div>
<p>,</p>
<div class="codecolorer-container text dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">String+NSArrayExtension,</div></div>
<p>and</p>
<div class="codecolorer-container text dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">downArrow.png</div></div>
<p>sources into your project.</li>
<li>Drag a UITextField onto a view in InterfaceBuilder.</li>
<li>Change its class to the
<div class="codecolorer-container text dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">CWPickerField</div></div>
<p>class.</li>
<li>Implement the
<div class="codecolorer-container text dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">NWPickerFieldDelegate</div></div>
<p>protocol, which mirrors the</p>
<div class="codecolorer-container text dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">UIPickerViewDelegate</div></div>
<p>.</li>
<li>Set the format string for the field when the view is loaded.</li>
</ol>
<p>The code can be</p>
<p><img class="aligncenter size-full wp-image-77" title="Picker shot 1" src="http://newwavedigitalmedia.com/wp-content/uploads/2009/10/Picker-shot-1.png" alt="Picker shot 1" width="386" height="742" /></p>
<p><img class="aligncenter size-full wp-image-76" title="picker shot 2" src="http://newwavedigitalmedia.com/wp-content/uploads/2009/10/picker-shot-2.png" alt="picker shot 2" width="386" height="742" /></p>
]]></content:encoded>
			<wfw:commentRss>http://newwavedigitalmedia.com/?feed=rss2&amp;p=79</wfw:commentRss>
		<slash:comments>24</slash:comments>
		</item>
		<item>
		<title>iPhoto2PBase source now available</title>
		<link>http://newwavedigitalmedia.com/?p=23</link>
		<comments>http://newwavedigitalmedia.com/?p=23#comments</comments>
		<pubDate>Mon, 13 Jul 2009 18:25:01 +0000</pubDate>
		<dc:creator>Scott Andrew</dc:creator>
				<category><![CDATA[Source Code]]></category>
		<category><![CDATA[iPhoto2PBase]]></category>

		<guid isPermaLink="false">http://newwavedigitalmedia.com/?p=23</guid>
		<description><![CDATA[<p>The iPhoto2PBase source is now available via github. The url is http://github.com/scottandrew/iPhoto2PBase/tree/master.</p>
<p>To download the source install git and enter the following into the console.</p>
<p>git <span style="color:#777"> . . . &#8594; Read More: <a href="http://newwavedigitalmedia.com/?p=23">iPhoto2PBase source now available</a></span>]]></description>
			<content:encoded><![CDATA[<p>The iPhoto2PBase source is now available via github. The url is http://github.com/scottandrew/iPhoto2PBase/tree/master.</p>
<p>To download the source install git and enter the following into the console.</p>
<p>git clone git://github.com/scottandrew/iPhoto2PBase.git</p>
]]></content:encoded>
			<wfw:commentRss>http://newwavedigitalmedia.com/?feed=rss2&amp;p=23</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>iPhoto2PBase now free!!!</title>
		<link>http://newwavedigitalmedia.com/?p=10</link>
		<comments>http://newwavedigitalmedia.com/?p=10#comments</comments>
		<pubDate>Sat, 11 Jul 2009 16:15:31 +0000</pubDate>
		<dc:creator>Scott Andrew</dc:creator>
				<category><![CDATA[iPhoto2PBase]]></category>

		<guid isPermaLink="false">http://newwavedigitalmedia.com/?p=10</guid>
		<description><![CDATA[<p>iPhoto2PBase is now free. (Product page coming soon)

<p class="wp-caption-text">iPhoto2PBase Screenshot</p>
iPhoto2BPase is an iPhoto plug-in that allows you to upload your photos to your PBase account.

The plug-in allows you to:</p>

Create new galleries (nested or at the root)
Upload unlimited number of photos
Edit the title and description of you photos.
See the status of your PBase account
Resize your images before <span style="color:#777"> . . . &#8594; Read More: <a href="http://newwavedigitalmedia.com/?p=10">iPhoto2PBase now free!!!</a></span>]]></description>
			<content:encoded><![CDATA[<p>iPhoto2PBase is now free. (Product page coming soon)<br />
<br/><br />
<div id="attachment_19" class="wp-caption aligncenter" style="width: 450px"><img src="http://newwavedigitalmedia.com/wp-content/uploads/2009/07/details_iphoto2pbase-2.02.gif" alt="iPhoto2PBase Screenshot" title="iPhoto2PBase Screenshot" width="440" height="298" class="size-full wp-image-19" /><p class="wp-caption-text">iPhoto2PBase Screenshot</p></div><br />
<br/>iPhoto2BPase is an iPhoto plug-in that allows you to upload your photos to your PBase account.<br />
<br/><br />
The plug-in allows you to:</p>
<ul>
<li>Create new galleries (nested or at the root)</li>
<li>Upload unlimited number of photos</li>
<li>Edit the title and description of you photos.</li>
<li>See the status of your PBase account</li>
<li>Resize your images before upload</li>
<li>Convert your photographs to JPEG</li>
<li>Use a photographs comments in iPhoto as the photo&#8217;s description in PBase.</li>
<li>Save login information in the system&#8217;s keychain.</li>
<li>Foreign language support for photo title and description.</li>
<li>Set most of the gallery attributes that are available in the web interface. (country, style, number of rows per page, and many more&#8230;)</li>
</ul>
<p><br/><br />
<a href="http://newwavedigitalmedia.com/downloads/iPhoto2PBase2.0.zip">Click here to download.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://newwavedigitalmedia.com/?feed=rss2&amp;p=10</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Time for reinvention.</title>
		<link>http://newwavedigitalmedia.com/?p=3</link>
		<comments>http://newwavedigitalmedia.com/?p=3#comments</comments>
		<pubDate>Fri, 10 Jul 2009 03:46:35 +0000</pubDate>
		<dc:creator>Scott Andrew</dc:creator>
				<category><![CDATA[Life]]></category>

		<guid isPermaLink="false">http://newwavedigitalmedia.com/?p=3</guid>
		<description><![CDATA[<p>As we get ready to release some new software later this year, its time for a re-invention. Watch for some cocoa tutorials and some open source code to come down the pipe.</p>
<p>If you are wondering where iPhoto2PBase is. It will be back as free and open source <span style="color:#777"> . . . &#8594; Read More: <a href="http://newwavedigitalmedia.com/?p=3">Time for reinvention.</a></span>]]></description>
			<content:encoded><![CDATA[<p>As we get ready to release some new software later this year, its time for a re-invention. Watch for some cocoa tutorials and some open source code to come down the pipe.</p>
<p>If you are wondering where iPhoto2PBase is. It will be back as free and open source software.</p>
<p>Stay tuned&#8230;.</p>
]]></content:encoded>
			<wfw:commentRss>http://newwavedigitalmedia.com/?feed=rss2&amp;p=3</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
