<?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>Blog @ Tim Isted &#187; Bindings</title>
	<atom:link href="http://www.timisted.net/blog/archive/tag/bindings-cocoa-programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.timisted.net/blog</link>
	<description>Cocoa Programming and Other Things</description>
	<lastBuildDate>Sat, 12 Dec 2009 17:43:49 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>A Bindable Custom NSView Subclass</title>
		<link>http://www.timisted.net/blog/archive/a-bindable-custom-nsview-subclass/</link>
		<comments>http://www.timisted.net/blog/archive/a-bindable-custom-nsview-subclass/#comments</comments>
		<pubDate>Thu, 27 Nov 2008 15:54:06 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Bindings]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Core Data]]></category>
		<category><![CDATA[Apple]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Framework]]></category>
		<category><![CDATA[Interface Builder]]></category>
		<category><![CDATA[NSView]]></category>
		<category><![CDATA[Xcode]]></category>

		<guid isPermaLink="false">http://www.timisted.net/blog/?p=259</guid>
		<description><![CDATA[In this post I&#8217;ll walk through writing a custom subclass of NSView that can bind to an NSArrayController to display information either from some generic array connected to the array controller, or from a Core Data object model.
The view we&#8217;ll be creating is a custom view to display a pie-chart; in the sample app the [...]]]></description>
			<content:encoded><![CDATA[<p>In this post I&#8217;ll walk through writing a custom subclass of NSView that can bind to an NSArrayController to display information either from some generic array connected to the array controller, or from a Core Data object model.</p>
<p>The view we&#8217;ll be creating is a custom view to display a pie-chart; in the sample app the pie-chart will be drawn from data entered in an NSTableView. The finished application looks like the following:<br />
<img class="size-full wp-image-235" title="customnsviewapp" src="http://www.timisted.net/blog/wp-content/uploads/2008/09/customnsviewapp.gif" alt="The finished sample application showing the custom NSView subclass bound to a Core Data entity" /><br />
<span id="more-259"></span><br />
If you&#8217;ve read previous posts on this site, you just might possibly have realized that I&#8217;m quite a big fan of Core Data. Unsurprisingly, therefore, the sample application is a Core Data document-based app. Its project and code can be downloaded here: <a href="http://www.timisted.net/blog/wp-content/uploads/2008/11/customnsview.zip">customnsview.zip</a>.</p>
<p>We&#8217;ll be looking at binding to an array controller&#8217;s &#8216;arrangedObjects&#8217; key, and also its &#8217;selectionIndexes&#8217; key to enable the user to change which object is selected by clicking in our view. The segments on our pie-chart have both a &#8216;name&#8217; and a numeric &#8216;value&#8217; so, in the same way that you can bind a model&#8217;s keys to different column tables in an NSTableView, we&#8217;ll make it possible to bind a model&#8217;s keys to the names and values on the pie-chart. This would allow us to bind, for example, a department&#8217;s employees&#8217; &#8216;fullNames&#8217; and their &#8217;salaries&#8217; to view relative salaries, or in a file-viewing application, &#8216;fileNames&#8217; and &#8216;fileSizes&#8217; etc.</p>
<h3>Basic Principles</h3>
<p>To be able to display our information in the view, the view needs to maintain its own store of the data. We&#8217;ll maintain one array of segment names and one array of segment values. When the view is told to display itself, it will sum the values and use the total to determine how big each segment should be relative to the others; the view will then draw each segment using various geometric functions and bezier paths.</p>
<h3>Creating our View</h3>
<p>We&#8217;ll begin by creating a very basic subclass of NSView to which we can add various functionality throughout this article.</p>
<h4>The View Subclass</h4>
<ul>
<li>Begin by creating a new Xcode &#8216;Cocoa Core Data Document-Based Application&#8217;, called &#8216;CustomNSView&#8217;.</li>
<li>In the new project&#8217;s browser, right-click (or control-click) on the Classes group and choose Add -&gt; New File&#8230;; in the window that appears, chose the &#8216;Objective-C NSView Subclass&#8217; and click Next.</li>
<li>Name the file &#8216;MyPieChartView.m&#8217; and make sure the &#8216;Also create &#8220;MyPieChartView.h&#8221; checkbox is ticked.</li>
<li>After clicking &#8216;Finish&#8217; to create the files, open up &#8216;MyPieChartView.m&#8217; and change the template <code>drawRect:</code> method with the one below; we&#8217;ll also override the &#8216;isFlipped&#8217; method to return YES so that our coordinate system has {0,0} set to the top left of the view:

<div class="wp_syntax"><div 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>drawRect<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">NSRect</span><span style="color: #002200;">&#41;</span>rect
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSColor</span> blackColor<span style="color: #002200;">&#93;</span> set<span style="color: #002200;">&#93;</span>;
	NSRectFill<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>self bounds<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>isFlipped
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> <span style="color: #a61390;">YES</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
</ul>
<p>Next we need to add an instance of our view to the Window of our document object.</p>
<ul>
<li>Open up MyDocument.xib in Interface Builder and delete the placeholder label from the Window.</li>
<li>Drag a custom view onto the Window, set it to resize with the window and use the identity inspector to change the class of the view to &#8216;MyPieChartView&#8217;. My interface looks like this:<br />
<img class="size-full wp-image-262" title="basic-interface-builder-win" src="http://www.timisted.net/blog/wp-content/uploads/2008/11/basic-interface-builder-win.gif" alt="The basic interface for the document" /></li>
</ul>
<p>If you build and run the application, you should find that each new document opens a window with our black box view showing.</p>
<h4>Adding the Arrays and Bindings</h4>
<p>At this point, we need to add a couple of arrays to the view that will hold the names and values of each pie segment.</p>
<ul>
<li>Open the MyPieChartView.h header file and add in two NSArrays for the names and values, together with KVC accessors:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> MyPieChartView <span style="color: #002200;">:</span> <span style="color: #400080;">NSView</span>
<span style="color: #002200;">&#123;</span>
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>_segmentNamesArray;
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>_segmentValuesArray;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>segmentNamesArray;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setSegmentNamesArray<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>newArray;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>segmentValuesArray;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setSegmentValuesArray<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>newArray;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

</li>
<li>We&#8217;ll be allowing other objects to bind to these arrays so we&#8217;ll need to add an <code>initialize:</code> method to our view that exposes them for binding. Whenever the arrays change, we need to get the view to redisplay itself so in the setter methods we need to call <code>[self setNeedsDisplayInRect:]</code>. We also need to release the stored arrays when the view is deallocated. In MyPieChartView.m, implement these methods:

<div class="wp_syntax"><div 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>initialize
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>self exposeBinding<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;segmentNamesArray&quot;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>self exposeBinding<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;segmentValuesArray&quot;</span><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: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>segmentNamesArray
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>_segmentNamesArray retain<span style="color: #002200;">&#93;</span> autorelease<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>setSegmentNamesArray<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>newArray
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>self willChangeValueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;segmentNamesArray&quot;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>_segmentNamesArray release<span style="color: #002200;">&#93;</span>;
	_segmentNamesArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>newArray copy<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>self didChangeValueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;segmentNamesArray&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>self setNeedsDisplayInRect<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>self visibleRect<span style="color: #002200;">&#93;</span><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: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>segmentValuesArray
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>_segmentValuesArray retain<span style="color: #002200;">&#93;</span> autorelease<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>setSegmentValuesArray<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>newArray
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>self willChangeValueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;segmentValuesArray&quot;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>_segmentValuesArray release<span style="color: #002200;">&#93;</span>;
	_segmentValuesArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>newArray copy<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>self didChangeValueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;segmentValuesArray&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>self setNeedsDisplayInRect<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>self visibleRect<span style="color: #002200;">&#93;</span><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>_segmentNamesArray release<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>_segmentValuesArray 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></pre></div></div>

</li>
</ul>
<p>With our arrays setup, we need to create a data model and some objects to which we can bind.</p>
<h3>The Data Model</h3>
<p>For the purposes of this demonstration, we need only a very simple data model together with an NSTableView in the interface to add and remove objects.</p>
<ul>
<li>Open MyDocument.xcdatamodel and add a new entity called &#8216;PieSegment&#8217;. Add two attributes, a String and a Decimal, called &#8216;name&#8217; and &#8216;amount&#8217; respectively. Give the &#8216;name&#8217; attribute a default string like &#8216;new segment&#8217; and &#8216;amount&#8217; a default value of 0.</li>
<li>Return to MyDocument.xib in Interface Builder and add a new NSArrayController called &#8217;segmentsArrayController&#8217; for the &#8216;PieSegment&#8217; entity, that prepares content. Bind its Managed Object Context to the File&#8217;s Owner managedObjectContext.</li>
<li>Add an NSTableView and two buttons to the document window; bind the columns of the table view to the &#8216;name&#8217; and &#8216;amount&#8217; path of the segmentsArrayController arrangedObjects key.</li>
<li>Add a decimal NSNumberFormatter to the &#8216;amount&#8217; column.</li>
<li>Connect the buttons to the array controller&#8217;s add: and remove: actions and make any other adjustments as you wish. My interface looks like this:<br />
<img class="aligncenter size-full wp-image-266" title="The revised interface for the CustomNSView application" src="http://www.timisted.net/blog/wp-content/uploads/2008/11/interface-redesigned.gif" alt="" /></li>
</ul>
<p>Build and Run the application to make sure that the table view and buttons work as expected.</p>
<h3>Setting up the Bindings and Displaying the Data</h3>
<p>Obviously, nothing is happening yet with our custom NSView since we haven&#8217;t bound anything to our names and values arrays, nor have we modified the view&#8217;s <code>drawRect:</code> method. We&#8217;ll rectify that now:</p>
<ul>
<li>Open the MyDocument.h header file and add in IBOutlets for the array controller and view:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> MyDocument <span style="color: #002200;">:</span> <span style="color: #400080;">NSPersistentDocument</span>
<span style="color: #002200;">&#123;</span>
	IBOutlet <span style="color: #400080;">NSArrayController</span> <span style="color: #002200;">*</span>segmentsArrayController;
	IBOutlet <span style="color: #400080;">NSView</span> <span style="color: #002200;">*</span>pieChartView;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

</li>
<li>Connect these outlets to the relevant view and controller in Interface Builder.</li>
<li>In MyDocument.m, modify the <code>windowControllerDidLoadNib:</code> method so that we bind the view to the array controller:

<div class="wp_syntax"><div 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>windowControllerDidLoadNib<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSWindowController</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>windowController
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>super windowControllerDidLoadNib<span style="color: #002200;">:</span>windowController<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>pieChartView bind<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;segmentNamesArray&quot;</span> toObject<span style="color: #002200;">:</span>segmentsArrayController withKeyPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;arrangedObjects.name&quot;</span> options<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>pieChartView bind<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;segmentValuesArray&quot;</span> toObject<span style="color: #002200;">:</span>segmentsArrayController withKeyPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;arrangedObjects.amount&quot;</span> options<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>In MyPieChartView.m, modify the <code>drawRect:</code> method with some very simple text display code that will just output the two arrays side-by-side:

<div class="wp_syntax"><div 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>drawRect<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">NSRect</span><span style="color: #002200;">&#41;</span>rect
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSColor</span> blackColor<span style="color: #002200;">&#93;</span> set<span style="color: #002200;">&#93;</span>;
	NSRectFill<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>self bounds<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #a61390;">NSPoint</span> midPoint <span style="color: #002200;">=</span> NSMakePoint<span style="color: #002200;">&#40;</span>NSMidX<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>self bounds<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>, NSMidY<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>self bounds<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>namesText <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self segmentNamesArray<span style="color: #002200;">&#93;</span> description<span style="color: #002200;">&#93;</span>;
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>valuesText <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self segmentValuesArray<span style="color: #002200;">&#93;</span> description<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>textAttributes <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithObjectsAndKeys<span style="color: #002200;">:</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSColor</span> whiteColor<span style="color: #002200;">&#93;</span>, NSForegroundColorAttributeName, <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSFont</span> systemFontOfSize<span style="color: #002200;">:</span><span style="color: #2400d9;">10</span><span style="color: #002200;">&#93;</span>, NSFontAttributeName, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	NSSize namesSize <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>namesText sizeWithAttributes<span style="color: #002200;">:</span>textAttributes<span style="color: #002200;">&#93;</span>;
	NSSize valuesSize <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>valuesText sizeWithAttributes<span style="color: #002200;">:</span>textAttributes<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">NSPoint</span> namesTextPoint <span style="color: #002200;">=</span> NSMakePoint<span style="color: #002200;">&#40;</span>midPoint.x <span style="color: #002200;">-</span> namesSize.width <span style="color: #002200;">-</span> <span style="color: #2400d9;">2</span>, midPoint.y <span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>namesSize.height <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
	<span style="color: #a61390;">NSPoint</span> valuesTextPoint <span style="color: #002200;">=</span> NSMakePoint<span style="color: #002200;">&#40;</span>midPoint.x <span style="color: #002200;">+</span> valuesSize.width <span style="color: #002200;">+</span> <span style="color: #2400d9;">2</span>, midPoint.y <span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>valuesSize.height <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>namesText drawAtPoint<span style="color: #002200;">:</span>namesTextPoint withAttributes<span style="color: #002200;">:</span>textAttributes<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>valuesText drawAtPoint<span style="color: #002200;">:</span>valuesTextPoint withAttributes<span style="color: #002200;">:</span>textAttributes<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
</ul>
<p style="text-align: center;">If you build and run the application, you&#8217;ll find that values added to the table view will show up in columns in the pie-chart view:<br />
<img class="aligncenter size-full wp-image-269" title="The view showing data with a basic textual display" src="http://www.timisted.net/blog/wp-content/uploads/2008/11/application-with-basic-disp.gif" alt="" /></p>
<h3>Displaying the Data as a Pie-Chart</h3>
<p>Whilst at this point we have a nicely-bound custom NSView subclass, we still need to use the provided data to display a pie-chart. There are many different ways to go about displaying such a chart; what follows is the path I decided to take. You may have different suggestions, in which case please feel free to make comments. The methods I use to calculate the sizes and shapes of the segments may not be the fastest, or least memory-intensive, but should be fairly easily understood by anyone with basic mathematical and geometric knowledge.</p>
<p>We&#8217;ll draw the segments of the &#8216;pie&#8217; using NSBezierPaths. Since it isn&#8217;t only the changing of data that can cause a view to be told to draw itself, we&#8217;ll &#8216;cache&#8217; these paths in an array held by the view, updating them only if the data changes. To make it easy to color each segment we&#8217;ll maintain a static class array of colors, generated randomly when needed, such that additional instances of our class can also use the same colors.</p>
<h4>Adding Drawing Information to our View</h4>
<p>We need to add an array pointer to MyPieChartView that will hold the segment paths. We also need a method to access this paths array, and a method that will regenerate it when necessary. We need a method that will produce a random color, and another method that will supply a color from a static colors array if given a zero-based index, generating it if necessary.</p>
<ul>
<li>Change the MyPieChartView.h header file by adding the new array pointer and methods:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> MyPieChartView <span style="color: #002200;">:</span> <span style="color: #400080;">NSView</span>
<span style="color: #002200;">&#123;</span>
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>_segmentNamesArray;
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>_segmentValuesArray;
&nbsp;
	<span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span>_segmentPathsArray;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>segmentNamesArray;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setSegmentNamesArray<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>newArray;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>segmentValuesArray;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setSegmentValuesArray<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>newArray;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>segmentPathsArray;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>generateDrawingInformation;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSColor</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>randomColor;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSColor</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>colorForIndex<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">unsigned</span><span style="color: #002200;">&#41;</span>index;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

</li>
<li>In MyPieChartView.m implement the <code>segmentPathsArray:</code> method and modify the <code>dealloc:</code> method to release the array:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>segmentPathsArray
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> _segmentPathsArray;
<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>_segmentNamesArray release<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>_segmentValuesArray release<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> _segmentPathsArray <span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#91;</span>_segmentPathsArray release<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>super dealloc<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>Implement the <code>randomColor:</code> and <code>colorForIndex:</code> methods like this:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSColor</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>randomColor
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">float</span> red <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>random<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">%</span>1000<span style="color: #002200;">&#41;</span><span style="color: #002200;">/</span><span style="color: #2400d9;">1000.0</span>;
	<span style="color: #a61390;">float</span> green <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>random<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">%</span>1000<span style="color: #002200;">&#41;</span><span style="color: #002200;">/</span><span style="color: #2400d9;">1000.0</span>;
	<span style="color: #a61390;">float</span> blue <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>random<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">%</span>1000<span style="color: #002200;">&#41;</span><span style="color: #002200;">/</span><span style="color: #2400d9;">1000.0</span>;
	<span style="color: #a61390;">float</span> alpha <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>random<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">%</span>1000<span style="color: #002200;">&#41;</span><span style="color: #002200;">/</span><span style="color: #2400d9;">1000.0</span>;
	<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSColor</span> colorWithCalibratedRed<span style="color: #002200;">:</span>red green<span style="color: #002200;">:</span>green blue<span style="color: #002200;">:</span>blue alpha<span style="color: #002200;">:</span>alpha<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: #400080;">NSColor</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>colorForIndex<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">unsigned</span><span style="color: #002200;">&#41;</span>index
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">static</span> <span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span>colorsArray <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> colorsArray <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		colorsArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableArray</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> index <span style="color: #002200;">&amp;</span>gt;<span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>colorsArray count<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #a61390;">unsigned</span> currentNum <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
		<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> currentNum <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>colorsArray count<span style="color: #002200;">&#93;</span>; currentNum <span style="color: #002200;">&amp;</span>lt;<span style="color: #002200;">=</span> index; currentNum<span style="color: #002200;">++</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#123;</span>
			<span style="color: #002200;">&#91;</span>colorsArray addObject<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>self randomColor<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#125;</span>
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>colorsArray objectAtIndex<span style="color: #002200;">:</span>index<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>The <code>generateDrawingInformation:</code> method is quite complicated. It keeps hold of its own pointer to the segmentValuesArray, and uses this to generate the paths array. Here I&#8217;m also using <code>#define</code> to set a value for the padding around the pie-chart.

<div class="wp_syntax"><div 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>generateDrawingInformation
<span style="color: #002200;">&#123;</span>
	<span style="color: #11740a; font-style: italic;">// Keep a pointer to the segmentValuesArray</span>
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>cachedSegmentValuesArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self segmentValuesArray<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Get rid of any existing Paths Array</span>
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> _segmentPathsArray <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #002200;">&#91;</span>_segmentPathsArray release<span style="color: #002200;">&#93;</span>;
		_segmentPathsArray <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #11740a; font-style: italic;">// If there aren't any values to display, we can exit now</span>
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>cachedSegmentValuesArray count<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&amp;</span>lt; <span style="color: #2400d9;">1</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #a61390;">return</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Get the sum of the amounts and exit if it is zero</span>
	<span style="color: #a61390;">float</span> sumOfAmounts <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
	<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> <span style="color: #400080;">NSNumber</span> <span style="color: #002200;">*</span>eachAmountToSum <span style="color: #a61390;">in</span> cachedSegmentValuesArray <span style="color: #002200;">&#41;</span>
		sumOfAmounts <span style="color: #002200;">+=</span> <span style="color: #002200;">&#91;</span>eachAmountToSum floatValue<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> sumOfAmounts <span style="color: #002200;">==</span> <span style="color: #2400d9;">0</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #a61390;">return</span>;
&nbsp;
	_segmentPathsArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableArray</span> alloc<span style="color: #002200;">&#93;</span> initWithCapacity<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>cachedSegmentValuesArray count<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #6e371a;">#define PADDINGAROUNDGRAPH 20.0</span>
&nbsp;
	<span style="color: #a61390;">NSRect</span> viewBounds <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self bounds<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">NSRect</span> graphRect <span style="color: #002200;">=</span> NSInsetRect<span style="color: #002200;">&#40;</span>viewBounds, PADDINGAROUNDGRAPH, PADDINGAROUNDGRAPH<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Make the graphRect square and centred</span>
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> graphRect.size.width <span style="color: #002200;">&amp;</span>gt; graphRect.size.height <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #a61390;">double</span> sizeDifference <span style="color: #002200;">=</span> graphRect.size.width <span style="color: #002200;">-</span> graphRect.size.height;
		graphRect.size.width <span style="color: #002200;">=</span> graphRect.size.height;
		graphRect.origin.x <span style="color: #002200;">+=</span> <span style="color: #002200;">&#40;</span>sizeDifference <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> graphRect.size.height <span style="color: #002200;">&amp;</span>gt; graphRect.size.width <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #a61390;">double</span> sizeDifference <span style="color: #002200;">=</span> graphRect.size.height <span style="color: #002200;">-</span> graphRect.size.width;
		graphRect.size.height <span style="color: #002200;">=</span> graphRect.size.width;
		graphRect.origin.y <span style="color: #002200;">+=</span> <span style="color: #002200;">&#40;</span>sizeDifference <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Calculate how big a 'unit' is</span>
	<span style="color: #a61390;">float</span> unitSize <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span><span style="color: #2400d9;">360.0</span> <span style="color: #002200;">/</span> sumOfAmounts<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> unitSize <span style="color: #002200;">&amp;</span>gt; <span style="color: #2400d9;">360</span> <span style="color: #002200;">&#41;</span>
		unitSize <span style="color: #002200;">=</span> <span style="color: #2400d9;">360</span>;
&nbsp;
	<span style="color: #a61390;">float</span> radius <span style="color: #002200;">=</span> graphRect.size.width <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span>;
&nbsp;
	<span style="color: #a61390;">NSPoint</span> midPoint <span style="color: #002200;">=</span> NSMakePoint<span style="color: #002200;">&#40;</span> NSMidX<span style="color: #002200;">&#40;</span>graphRect<span style="color: #002200;">&#41;</span>, NSMidY<span style="color: #002200;">&#40;</span>graphRect<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// cycle through the segmentValues and create the bezier paths</span>
	<span style="color: #a61390;">float</span> currentDegree <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
	<span style="color: #a61390;">unsigned</span> currentIndex;
	<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> currentIndex <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; currentIndex <span style="color: #002200;">&amp;</span>lt; <span style="color: #002200;">&#91;</span>cachedSegmentValuesArray count<span style="color: #002200;">&#93;</span>; currentIndex<span style="color: #002200;">++</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #400080;">NSNumber</span> <span style="color: #002200;">*</span>eachValue <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>cachedSegmentValuesArray objectAtIndex<span style="color: #002200;">:</span>currentIndex<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #a61390;">float</span> startDegree <span style="color: #002200;">=</span> currentDegree;
		currentDegree <span style="color: #002200;">+=</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>eachValue floatValue<span style="color: #002200;">&#93;</span> <span style="color: #002200;">*</span> unitSize<span style="color: #002200;">&#41;</span>;
		<span style="color: #a61390;">float</span> endDegree <span style="color: #002200;">=</span> currentDegree;
&nbsp;
		<span style="color: #400080;">NSBezierPath</span> <span style="color: #002200;">*</span>eachSegmentPath <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSBezierPath</span> bezierPath<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>eachSegmentPath moveToPoint<span style="color: #002200;">:</span>midPoint<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #002200;">&#91;</span>eachSegmentPath appendBezierPathWithArcWithCenter<span style="color: #002200;">:</span>midPoint radius<span style="color: #002200;">:</span>radius startAngle<span style="color: #002200;">:</span>startDegree endAngle<span style="color: #002200;">:</span>endDegree<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>eachSegmentPath closePath<span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">// close path also handles the lines from the midPoint to the start and end of the arc</span>
		<span style="color: #002200;">&#91;</span>eachSegmentPath setLineWidth<span style="color: #002200;">:</span><span style="color: #2400d9;">2.0</span><span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #002200;">&#91;</span>_segmentPathsArray addObject<span style="color: #002200;">:</span>eachSegmentPath<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>The paths array should be recalculated whenever the bound arrays change, so modify the two setter methods:

<div class="wp_syntax"><div 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>setSegmentNamesArray<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>newArray
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>self willChangeValueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;segmentNamesArray&quot;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>_segmentNamesArray release<span style="color: #002200;">&#93;</span>;
	_segmentNamesArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>newArray copy<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>self didChangeValueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;segmentNamesArray&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>self generateDrawingInformation<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>self setNeedsDisplayInRect<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>self visibleRect<span style="color: #002200;">&#93;</span><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>setSegmentValuesArray<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>newArray
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>self willChangeValueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;segmentValuesArray&quot;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>_segmentValuesArray release<span style="color: #002200;">&#93;</span>;
	_segmentValuesArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>newArray copy<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>self didChangeValueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;segmentValuesArray&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>self generateDrawingInformation<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>self setNeedsDisplayInRect<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>self visibleRect<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>Finally, we can now replace the <code>drawRect:</code> method so that it displays the paths:

<div class="wp_syntax"><div 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>drawRect<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">NSRect</span><span style="color: #002200;">&#41;</span>rect
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSColor</span> whiteColor<span style="color: #002200;">&#93;</span> set<span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">// white background</span>
	NSRectFill<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>self bounds<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>pathsArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self segmentPathsArray<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">unsigned</span> count;
	<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> count <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; count <span style="color: #002200;">&amp;</span>lt; <span style="color: #002200;">&#91;</span>pathsArray count<span style="color: #002200;">&#93;</span>; count<span style="color: #002200;">++</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #400080;">NSBezierPath</span> <span style="color: #002200;">*</span>eachPath <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>pathsArray objectAtIndex<span style="color: #002200;">:</span>count<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// fill the path with the drawing color for this index</span>
		<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self colorForIndex<span style="color: #002200;">:</span>count<span style="color: #002200;">&#93;</span> set<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>eachPath fill<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// draw a black border around it</span>
		<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSColor</span> blackColor<span style="color: #002200;">&#93;</span> set<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>eachPath stroke<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
</ul>
<p>When you build and run the project, you&#8217;ll find that segments added to the table view with non-zero amounts will show up as a basic, colored pie-chart in the view:<br />
<img class="aligncenter size-full wp-image-276" title="Basic pie chart display in the custom NSView" src="http://www.timisted.net/blog/wp-content/uploads/2008/11/basic-pie-chart-display.gif" alt="" width="500" height="281" /></p>
<h3>Displaying the Text</h3>
<p>So far we haven&#8217;t done anything at all with the segment names; we still need to display these on our pie-chart. We&#8217;ll maintain an array of dictionaries containing information to display the text. We&#8217;ll modify the <code>generateDrawingInformation:</code> method so that it creates this array of dictionaries, and change the <code>drawRect:</code> method to display the text on the pie-chart.</p>
<ul>
<li>Open the MyPieChartView.h header file and add in an array for the text information, along with an accessor:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> MyPieChartView <span style="color: #002200;">:</span> <span style="color: #400080;">NSView</span>
<span style="color: #002200;">&#123;</span>
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>_segmentNamesArray;
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>_segmentValuesArray;
&nbsp;
	<span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span>_segmentPathsArray;
	<span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span>_segmentTextsArray;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>segmentNamesArray;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setSegmentNamesArray<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>newArray;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>segmentValuesArray;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setSegmentValuesArray<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>newArray;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>segmentPathsArray;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>segmentTextsArray;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>generateDrawingInformation;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSColor</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>randomColor;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSColor</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>colorForIndex<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">unsigned</span><span style="color: #002200;">&#41;</span>index;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

</li>
<li>Add the accessor method to MyPieChartView.m and change <code>dealloc:</code> to release it:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>segmentTextsArray
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> _segmentTextsArray;
<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>_segmentNamesArray release<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>_segmentValuesArray release<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> _segmentPathsArray <span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#91;</span>_segmentPathsArray release<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> _segmentTextsArray <span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#91;</span>_segmentTextsArray release<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>super dealloc<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>Now modify the <code>generateDrawingInformation:</code> method so that it stores necessary information in this texts array. We draw each segment arc using two &#8216;half&#8217; arcs rather than one in order to find out the midpoint from which we decide where to put the text. We also check to see in which &#8216;quarter&#8217; of the pie-chart view that midpoint falls and offset it by a <code>#defined</code> TEXTPADDING value accordingly, also making sure that the resultant point doesn&#8217;t fall outside of the view&#8217;s bounds:

<div class="wp_syntax"><div 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>generateDrawingInformation
<span style="color: #002200;">&#123;</span>
	<span style="color: #11740a; font-style: italic;">// Keep pointers to the segmentValuesArray and segmentNamesArray</span>
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>cachedSegmentValuesArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self segmentValuesArray<span style="color: #002200;">&#93;</span>;
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>cachedSegmentNamesArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self segmentNamesArray<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Get rid of any existing Paths Array</span>
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> _segmentPathsArray <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #002200;">&#91;</span>_segmentPathsArray release<span style="color: #002200;">&#93;</span>;
		_segmentPathsArray <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Get rid of any existing Texts Array</span>
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> _segmentTextsArray <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #002200;">&#91;</span>_segmentTextsArray release<span style="color: #002200;">&#93;</span>;
		_segmentTextsArray <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #11740a; font-style: italic;">// If there aren't any values to display, we can exit now</span>
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>cachedSegmentValuesArray count<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&amp;</span>lt; <span style="color: #2400d9;">1</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #a61390;">return</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Get the sum of the amounts and exit if it is zero</span>
	<span style="color: #a61390;">float</span> sumOfAmounts <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
	<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> <span style="color: #400080;">NSNumber</span> <span style="color: #002200;">*</span>eachAmountToSum <span style="color: #a61390;">in</span> cachedSegmentValuesArray <span style="color: #002200;">&#41;</span>
		sumOfAmounts <span style="color: #002200;">+=</span> <span style="color: #002200;">&#91;</span>eachAmountToSum floatValue<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> sumOfAmounts <span style="color: #002200;">==</span> <span style="color: #2400d9;">0</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #a61390;">return</span>;
&nbsp;
	_segmentPathsArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableArray</span> alloc<span style="color: #002200;">&#93;</span> initWithCapacity<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>cachedSegmentValuesArray count<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
	_segmentTextsArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableArray</span> alloc<span style="color: #002200;">&#93;</span> initWithCapacity<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>cachedSegmentValuesArray count<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #6e371a;">#define PADDINGAROUNDGRAPH 20.0</span>
<span style="color: #6e371a;">#define TEXTPADDING 5.0</span>
&nbsp;
	<span style="color: #a61390;">NSRect</span> viewBounds <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self bounds<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">NSRect</span> graphRect <span style="color: #002200;">=</span> NSInsetRect<span style="color: #002200;">&#40;</span>viewBounds, PADDINGAROUNDGRAPH, PADDINGAROUNDGRAPH<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Make the graphRect square and centred</span>
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> graphRect.size.width <span style="color: #002200;">&amp;</span>gt; graphRect.size.height <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #a61390;">double</span> sizeDifference <span style="color: #002200;">=</span> graphRect.size.width <span style="color: #002200;">-</span> graphRect.size.height;
		graphRect.size.width <span style="color: #002200;">=</span> graphRect.size.height;
		graphRect.origin.x <span style="color: #002200;">+=</span> <span style="color: #002200;">&#40;</span>sizeDifference <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> graphRect.size.height <span style="color: #002200;">&amp;</span>gt; graphRect.size.width <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #a61390;">double</span> sizeDifference <span style="color: #002200;">=</span> graphRect.size.height <span style="color: #002200;">-</span> graphRect.size.width;
		graphRect.size.height <span style="color: #002200;">=</span> graphRect.size.width;
		graphRect.origin.y <span style="color: #002200;">+=</span> <span style="color: #002200;">&#40;</span>sizeDifference <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #11740a; font-style: italic;">// get NSRects for the different quarters of the pie-chart</span>
	<span style="color: #a61390;">NSRect</span> topLeftRect, topRightRect;
	NSDivideRect<span style="color: #002200;">&#40;</span>viewBounds, <span style="color: #002200;">&amp;</span>amp;topLeftRect, <span style="color: #002200;">&amp;</span>amp;topRightRect, <span style="color: #002200;">&#40;</span>viewBounds.size.width <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span>, NSMinXEdge <span style="color: #002200;">&#41;</span>;
	<span style="color: #a61390;">NSRect</span> bottomLeftRect, bottomRightRect;
	NSDivideRect<span style="color: #002200;">&#40;</span>topLeftRect, <span style="color: #002200;">&amp;</span>amp;topLeftRect, <span style="color: #002200;">&amp;</span>amp;bottomLeftRect, <span style="color: #002200;">&#40;</span>viewBounds.size.height <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span>, NSMinYEdge <span style="color: #002200;">&#41;</span>;
	NSDivideRect<span style="color: #002200;">&#40;</span>topRightRect, <span style="color: #002200;">&amp;</span>amp;topRightRect, <span style="color: #002200;">&amp;</span>amp;bottomRightRect, <span style="color: #002200;">&#40;</span>viewBounds.size.height <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span>, NSMinYEdge <span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Calculate how big a 'unit' is</span>
	<span style="color: #a61390;">float</span> unitSize <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span><span style="color: #2400d9;">360.0</span> <span style="color: #002200;">/</span> sumOfAmounts<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> unitSize <span style="color: #002200;">&amp;</span>gt; <span style="color: #2400d9;">360</span> <span style="color: #002200;">&#41;</span>
		unitSize <span style="color: #002200;">=</span> <span style="color: #2400d9;">360</span>;
&nbsp;
	<span style="color: #a61390;">float</span> radius <span style="color: #002200;">=</span> graphRect.size.width <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span>;
&nbsp;
	<span style="color: #a61390;">NSPoint</span> midPoint <span style="color: #002200;">=</span> NSMakePoint<span style="color: #002200;">&#40;</span> NSMidX<span style="color: #002200;">&#40;</span>graphRect<span style="color: #002200;">&#41;</span>, NSMidY<span style="color: #002200;">&#40;</span>graphRect<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Set the text attributes to be used for each textual display</span>
	<span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>textAttributes <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithObjectsAndKeys<span style="color: #002200;">:</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSColor</span> whiteColor<span style="color: #002200;">&#93;</span>, NSBackgroundColorAttributeName, <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSColor</span> blackColor<span style="color: #002200;">&#93;</span>, NSForegroundColorAttributeName, <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSFont</span> systemFontOfSize<span style="color: #002200;">:</span><span style="color: #2400d9;">12</span><span style="color: #002200;">&#93;</span>, NSFontAttributeName, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// cycle through the segmentValues and create the bezier paths</span>
	<span style="color: #11740a; font-style: italic;">// Also add the text details (note we expect the texts' indexes to tie up with the values' indexes)</span>
	<span style="color: #a61390;">float</span> currentDegree <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
	<span style="color: #a61390;">unsigned</span> currentIndex;
	<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> currentIndex <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; currentIndex <span style="color: #002200;">&amp;</span>lt; <span style="color: #002200;">&#91;</span>cachedSegmentValuesArray count<span style="color: #002200;">&#93;</span>; currentIndex<span style="color: #002200;">++</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #400080;">NSNumber</span> <span style="color: #002200;">*</span>eachValue <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>cachedSegmentValuesArray objectAtIndex<span style="color: #002200;">:</span>currentIndex<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #a61390;">float</span> startDegree <span style="color: #002200;">=</span> currentDegree;
		currentDegree <span style="color: #002200;">+=</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>eachValue floatValue<span style="color: #002200;">&#93;</span> <span style="color: #002200;">*</span> unitSize<span style="color: #002200;">&#41;</span>;
		<span style="color: #a61390;">float</span> endDegree <span style="color: #002200;">=</span> currentDegree;
		<span style="color: #a61390;">float</span> midDegree <span style="color: #002200;">=</span> startDegree <span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>endDegree <span style="color: #002200;">-</span> startDegree<span style="color: #002200;">&#41;</span> <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #400080;">NSBezierPath</span> <span style="color: #002200;">*</span>eachSegmentPath <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSBezierPath</span> bezierPath<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>eachSegmentPath moveToPoint<span style="color: #002200;">:</span>midPoint<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #002200;">&#91;</span>eachSegmentPath appendBezierPathWithArcWithCenter<span style="color: #002200;">:</span>midPoint radius<span style="color: #002200;">:</span>radius startAngle<span style="color: #002200;">:</span>startDegree endAngle<span style="color: #002200;">:</span>midDegree clockwise<span style="color: #002200;">:</span><span style="color: #a61390;">NO</span><span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #a61390;">NSPoint</span> textPoint <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>eachSegmentPath currentPoint<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #002200;">&#91;</span>eachSegmentPath appendBezierPathWithArcWithCenter<span style="color: #002200;">:</span>midPoint radius<span style="color: #002200;">:</span>radius startAngle<span style="color: #002200;">:</span>midDegree endAngle<span style="color: #002200;">:</span>endDegree clockwise<span style="color: #002200;">:</span><span style="color: #a61390;">NO</span><span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #002200;">&#91;</span>eachSegmentPath closePath<span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">// close path also handles the lines from the midPoint to the start and end of the arc</span>
		<span style="color: #002200;">&#91;</span>eachSegmentPath setLineWidth<span style="color: #002200;">:</span><span style="color: #2400d9;">2.0</span><span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #002200;">&#91;</span>_segmentPathsArray addObject<span style="color: #002200;">:</span>eachSegmentPath<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// Get the text to be displayed, if it exists, and see how big it is</span>
		<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>eachText <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;&quot;</span>;
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>cachedSegmentNamesArray count<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&amp;</span>gt; currentIndex <span style="color: #002200;">&#41;</span>
			eachText <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>cachedSegmentNamesArray objectAtIndex<span style="color: #002200;">:</span>currentIndex<span style="color: #002200;">&#93;</span>;
&nbsp;
		NSSize textSize <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>eachText sizeWithAttributes<span style="color: #002200;">:</span>textAttributes<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// Offset it by TEXTPADDING in direction suitable for whichever quarter of the view it is in</span>
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> NSPointInRect<span style="color: #002200;">&#40;</span>textPoint, topLeftRect<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#123;</span>
			textPoint.y <span style="color: #002200;">-=</span> <span style="color: #002200;">&#40;</span>textSize.height <span style="color: #002200;">+</span> TEXTPADDING<span style="color: #002200;">&#41;</span>;
			textPoint.x <span style="color: #002200;">-=</span> <span style="color: #002200;">&#40;</span>textSize.width <span style="color: #002200;">+</span> TEXTPADDING<span style="color: #002200;">&#41;</span>;
		<span style="color: #002200;">&#125;</span>
		<span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> NSPointInRect<span style="color: #002200;">&#40;</span>textPoint, topRightRect<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#123;</span>
			textPoint.y <span style="color: #002200;">-=</span> <span style="color: #002200;">&#40;</span>textSize.height <span style="color: #002200;">+</span> TEXTPADDING<span style="color: #002200;">&#41;</span>;
			textPoint.x <span style="color: #002200;">+=</span> TEXTPADDING;
		<span style="color: #002200;">&#125;</span>
		<span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> NSPointInRect<span style="color: #002200;">&#40;</span>textPoint, bottomLeftRect<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#123;</span>
			textPoint.y <span style="color: #002200;">+=</span> TEXTPADDING;
			textPoint.x <span style="color: #002200;">-=</span> <span style="color: #002200;">&#40;</span>textSize.width <span style="color: #002200;">+</span> TEXTPADDING<span style="color: #002200;">&#41;</span>;
		<span style="color: #002200;">&#125;</span>
		<span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> NSPointInRect<span style="color: #002200;">&#40;</span>textPoint, bottomRightRect<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#123;</span>
			textPoint.y <span style="color: #002200;">+=</span> TEXTPADDING;
			textPoint.x <span style="color: #002200;">+=</span> TEXTPADDING;
		<span style="color: #002200;">&#125;</span>
&nbsp;
		<span style="color: #11740a; font-style: italic;">// Make sure the point isn't outside the view's bounds</span>
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> textPoint.x <span style="color: #002200;">&amp;</span>lt; viewBounds.origin.x <span style="color: #002200;">&#41;</span>
			textPoint.x <span style="color: #002200;">=</span> viewBounds.origin.x;
&nbsp;
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#40;</span>textPoint.x <span style="color: #002200;">+</span> textSize.width<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&amp;</span>gt; <span style="color: #002200;">&#40;</span>viewBounds.origin.x <span style="color: #002200;">+</span> viewBounds.size.width<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>
			textPoint.x <span style="color: #002200;">=</span> viewBounds.origin.x <span style="color: #002200;">+</span> viewBounds.size.width <span style="color: #002200;">-</span> textSize.width;
&nbsp;
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> textPoint.y <span style="color: #002200;">&amp;</span>lt; viewBounds.origin.y <span style="color: #002200;">&#41;</span>
			textPoint.y <span style="color: #002200;">=</span> viewBounds.origin.y;
&nbsp;
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#40;</span>textPoint.y <span style="color: #002200;">+</span> textSize.height<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&amp;</span>gt; <span style="color: #002200;">&#40;</span>viewBounds.origin.y <span style="color: #002200;">+</span> viewBounds.size.height<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>
			textPoint.y <span style="color: #002200;">=</span> viewBounds.origin.y <span style="color: #002200;">+</span> viewBounds.size.height <span style="color: #002200;">-</span> textSize.height;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// Finally add the details as a dictionary to our segmentTextsArray.</span>
		<span style="color: #11740a; font-style: italic;">// We include here the textAttributes lest we decide later to e.g. color the texts the same color as the segment fill</span>
		<span style="color: #002200;">&#91;</span>_segmentTextsArray addObject<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithObjectsAndKeys<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithFloat<span style="color: #002200;">:</span>textPoint.x<span style="color: #002200;">&#93;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;textPointX&quot;</span>, <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithFloat<span style="color: #002200;">:</span>textPoint.y<span style="color: #002200;">&#93;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;textPointY&quot;</span>, eachText, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;text&quot;</span>, textAttributes, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;textAttributes&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>Modify the <code>drawRect:</code> method to display the text:

<div class="wp_syntax"><div 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>drawRect<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">NSRect</span><span style="color: #002200;">&#41;</span>rect
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSColor</span> whiteColor<span style="color: #002200;">&#93;</span> set<span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">// white background</span>
	NSRectFill<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>self bounds<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>pathsArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self segmentPathsArray<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">unsigned</span> count;
	<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> count <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; count <span style="color: #002200;">&amp;</span>lt; <span style="color: #002200;">&#91;</span>pathsArray count<span style="color: #002200;">&#93;</span>; count<span style="color: #002200;">++</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #400080;">NSBezierPath</span> <span style="color: #002200;">*</span>eachPath <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>pathsArray objectAtIndex<span style="color: #002200;">:</span>count<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// fill the path with the drawing color for this index</span>
		<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self colorForIndex<span style="color: #002200;">:</span>count<span style="color: #002200;">&#93;</span> set<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>eachPath fill<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// draw a black border around it</span>
		<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSColor</span> blackColor<span style="color: #002200;">&#93;</span> set<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>eachPath stroke<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>textsArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self segmentTextsArray<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> count <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; count <span style="color: #002200;">&amp;</span>lt; <span style="color: #002200;">&#91;</span>textsArray count<span style="color: #002200;">&#93;</span>; count<span style="color: #002200;">++</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>eachTextDictionary <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>textsArray objectAtIndex<span style="color: #002200;">:</span>count<span style="color: #002200;">&#93;</span>;
		<span style="color: #a61390;">NSPoint</span> textPoint <span style="color: #002200;">=</span> NSMakePoint<span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>eachTextDictionary valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;textPointX&quot;</span><span style="color: #002200;">&#93;</span> floatValue<span style="color: #002200;">&#93;</span>, <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>eachTextDictionary valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;textPointY&quot;</span><span style="color: #002200;">&#93;</span> floatValue<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>textAttributes <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>eachTextDictionary valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;textAttributes&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>text <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>eachTextDictionary valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;text&quot;</span><span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>text drawAtPoint<span style="color: #002200;">:</span>textPoint withAttributes<span style="color: #002200;">:</span>textAttributes<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
</ul>
<p>The view should now display text on the chart:<br />
<img class="aligncenter size-full wp-image-278" title="The Basic Pie Chart now also displaying text" src="http://www.timisted.net/blog/wp-content/uploads/2008/11/basic-pie-chart-with-text.gif" alt="" width="500" height="291" /></p>
<h3>Indicating the Selected Segment(s)</h3>
<p>In order to display which segment is selected, we need to bind to the array controller&#8217;s &#8217;selectionIndexes&#8217;; the view needs to keep its own local copy of these indexes, and expose binding for them:</p>
<ul>
<li>Add an NSIndexSet for the selectionIndexes to the MyPieChartView.h header file, along with a getter and setter:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> MyPieChartView <span style="color: #002200;">:</span> <span style="color: #400080;">NSView</span>
<span style="color: #002200;">&#123;</span>
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>_segmentNamesArray;
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>_segmentValuesArray;
&nbsp;
	<span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span>_segmentPathsArray;
	<span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span>_segmentTextsArray;
&nbsp;
	<span style="color: #400080;">NSIndexSet</span> <span style="color: #002200;">*</span>_selectionIndexes;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>segmentNamesArray;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setSegmentNamesArray<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>newArray;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>segmentValuesArray;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setSegmentValuesArray<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>newArray;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>segmentPathsArray;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>segmentTextsArray;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>generateDrawingInformation;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSColor</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>randomColor;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSColor</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>colorForIndex<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">unsigned</span><span style="color: #002200;">&#41;</span>index;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSIndexSet</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>selectionIndexes;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setSelectionIndexes<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSIndexSet</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>newIndexes;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

</li>
<li>Implement these accessor methods and expose the binding:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSIndexSet</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>selectionIndexes
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>_selectionIndexes retain<span style="color: #002200;">&#93;</span> autorelease<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>setSelectionIndexes<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSIndexSet</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>newIndexes
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>_selectionIndexes <span style="color: #002200;">!=</span> newIndexes<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&amp;</span>amp;<span style="color: #002200;">&amp;</span>amp; <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span><span style="color: #002200;">&#91;</span>_selectionIndexes isEqualToIndexSet<span style="color: #002200;">:</span>newIndexes<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #002200;">&#91;</span>self willChangeValueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;selectionIndexes&quot;</span><span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>_selectionIndexes release<span style="color: #002200;">&#93;</span>;
		_selectionIndexes <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>newIndexes copy<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>self didChangeValueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;selectionIndexes&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #002200;">&#91;</span>self generateDrawingInformation<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>self setNeedsDisplayInRect<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>self visibleRect<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</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>initialize
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>self exposeBinding<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;segmentNamesArray&quot;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>self exposeBinding<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;segmentValuesArray&quot;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>self exposeBinding<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;selectionIndexes&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>In MyDocument.m, bind the selectionIndexes to the array controller:

<div class="wp_syntax"><div 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>windowControllerDidLoadNib<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSWindowController</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>windowController
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>super windowControllerDidLoadNib<span style="color: #002200;">:</span>windowController<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>pieChartView bind<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;segmentNamesArray&quot;</span> toObject<span style="color: #002200;">:</span>segmentsArrayController withKeyPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;arrangedObjects.name&quot;</span> options<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>pieChartView bind<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;segmentValuesArray&quot;</span> toObject<span style="color: #002200;">:</span>segmentsArrayController withKeyPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;arrangedObjects.amount&quot;</span> options<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>pieChartView bind<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;selectionIndexes&quot;</span> toObject<span style="color: #002200;">:</span>segmentsArrayController withKeyPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;selectionIndexes&quot;</span> options<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>We also need to modify the <code>generateDrawingInformation:</code> method to offset the selected segment (and its text) by a <code>#defined</code> amount:

<div class="wp_syntax"><div 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>generateDrawingInformation
<span style="color: #002200;">&#123;</span>
	<span style="color: #11740a; font-style: italic;">// Keep pointers to the segmentValuesArray and segmentNamesArray</span>
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>cachedSegmentValuesArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self segmentValuesArray<span style="color: #002200;">&#93;</span>;
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>cachedSegmentNamesArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self segmentNamesArray<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Get rid of any existing Paths Array</span>
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> _segmentPathsArray <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #002200;">&#91;</span>_segmentPathsArray release<span style="color: #002200;">&#93;</span>;
		_segmentPathsArray <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Get rid of any existing Texts Array</span>
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> _segmentTextsArray <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #002200;">&#91;</span>_segmentTextsArray release<span style="color: #002200;">&#93;</span>;
		_segmentTextsArray <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #11740a; font-style: italic;">// If there aren't any values to display, we can exit now</span>
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>cachedSegmentValuesArray count<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&amp;</span>lt; <span style="color: #2400d9;">1</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #a61390;">return</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Get the sum of the amounts and exit if it is zero</span>
	<span style="color: #a61390;">float</span> sumOfAmounts <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
	<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> <span style="color: #400080;">NSNumber</span> <span style="color: #002200;">*</span>eachAmountToSum <span style="color: #a61390;">in</span> cachedSegmentValuesArray <span style="color: #002200;">&#41;</span>
		sumOfAmounts <span style="color: #002200;">+=</span> <span style="color: #002200;">&#91;</span>eachAmountToSum floatValue<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> sumOfAmounts <span style="color: #002200;">==</span> <span style="color: #2400d9;">0</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #a61390;">return</span>;
&nbsp;
	_segmentPathsArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableArray</span> alloc<span style="color: #002200;">&#93;</span> initWithCapacity<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>cachedSegmentValuesArray count<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
	_segmentTextsArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableArray</span> alloc<span style="color: #002200;">&#93;</span> initWithCapacity<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>cachedSegmentValuesArray count<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #400080;">NSIndexSet</span> <span style="color: #002200;">*</span>selectionIndexes <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self selectionIndexes<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">BOOL</span> shouldOffsetSelectedSegment <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>selectionIndexes count<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&amp;</span>gt; <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span> ? <span style="color: #a61390;">YES</span> <span style="color: #002200;">:</span> <span style="color: #a61390;">NO</span>;
&nbsp;
<span style="color: #6e371a;">#define PADDINGAROUNDGRAPH 20.0</span>
<span style="color: #6e371a;">#define TEXTPADDING 5.0</span>
<span style="color: #6e371a;">#define SELECTEDSEGMENTOFFSET 5.0</span>
&nbsp;
	<span style="color: #a61390;">NSRect</span> viewBounds <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self bounds<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">NSRect</span> graphRect <span style="color: #002200;">=</span> NSInsetRect<span style="color: #002200;">&#40;</span>viewBounds, PADDINGAROUNDGRAPH, PADDINGAROUNDGRAPH<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Make the graphRect square and centred</span>
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> graphRect.size.width <span style="color: #002200;">&amp;</span>gt; graphRect.size.height <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #a61390;">double</span> sizeDifference <span style="color: #002200;">=</span> graphRect.size.width <span style="color: #002200;">-</span> graphRect.size.height;
		graphRect.size.width <span style="color: #002200;">=</span> graphRect.size.height;
		graphRect.origin.x <span style="color: #002200;">+=</span> <span style="color: #002200;">&#40;</span>sizeDifference <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> graphRect.size.height <span style="color: #002200;">&amp;</span>gt; graphRect.size.width <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #a61390;">double</span> sizeDifference <span style="color: #002200;">=</span> graphRect.size.height <span style="color: #002200;">-</span> graphRect.size.width;
		graphRect.size.height <span style="color: #002200;">=</span> graphRect.size.width;
		graphRect.origin.y <span style="color: #002200;">+=</span> <span style="color: #002200;">&#40;</span>sizeDifference <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #11740a; font-style: italic;">// get NSRects for the different quarters of the pie-chart</span>
	<span style="color: #a61390;">NSRect</span> topLeftRect, topRightRect;
	NSDivideRect<span style="color: #002200;">&#40;</span>viewBounds, <span style="color: #002200;">&amp;</span>amp;topLeftRect, <span style="color: #002200;">&amp;</span>amp;topRightRect, <span style="color: #002200;">&#40;</span>viewBounds.size.width <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span>, NSMinXEdge <span style="color: #002200;">&#41;</span>;
	<span style="color: #a61390;">NSRect</span> bottomLeftRect, bottomRightRect;
	NSDivideRect<span style="color: #002200;">&#40;</span>topLeftRect, <span style="color: #002200;">&amp;</span>amp;topLeftRect, <span style="color: #002200;">&amp;</span>amp;bottomLeftRect, <span style="color: #002200;">&#40;</span>viewBounds.size.height <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span>, NSMinYEdge <span style="color: #002200;">&#41;</span>;
	NSDivideRect<span style="color: #002200;">&#40;</span>topRightRect, <span style="color: #002200;">&amp;</span>amp;topRightRect, <span style="color: #002200;">&amp;</span>amp;bottomRightRect, <span style="color: #002200;">&#40;</span>viewBounds.size.height <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span>, NSMinYEdge <span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Calculate how big a 'unit' is</span>
	<span style="color: #a61390;">float</span> unitSize <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span><span style="color: #2400d9;">360.0</span> <span style="color: #002200;">/</span> sumOfAmounts<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> unitSize <span style="color: #002200;">&amp;</span>gt; <span style="color: #2400d9;">360</span> <span style="color: #002200;">&#41;</span>
		unitSize <span style="color: #002200;">=</span> <span style="color: #2400d9;">360</span>;
&nbsp;
	<span style="color: #a61390;">float</span> radius <span style="color: #002200;">=</span> graphRect.size.width <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span>;
&nbsp;
	<span style="color: #a61390;">NSPoint</span> midPoint <span style="color: #002200;">=</span> NSMakePoint<span style="color: #002200;">&#40;</span> NSMidX<span style="color: #002200;">&#40;</span>graphRect<span style="color: #002200;">&#41;</span>, NSMidY<span style="color: #002200;">&#40;</span>graphRect<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Set the text attributes to be used for each textual display</span>
	<span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>textAttributes <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithObjectsAndKeys<span style="color: #002200;">:</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSColor</span> whiteColor<span style="color: #002200;">&#93;</span>, NSBackgroundColorAttributeName, <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSColor</span> blackColor<span style="color: #002200;">&#93;</span>, NSForegroundColorAttributeName, <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSFont</span> systemFontOfSize<span style="color: #002200;">:</span><span style="color: #2400d9;">12</span><span style="color: #002200;">&#93;</span>, NSFontAttributeName, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// cycle through the segmentValues and create the bezier paths</span>
	<span style="color: #11740a; font-style: italic;">// Also add the text details (note we expect the texts' indexes to tie up with the values' indexes)</span>
	<span style="color: #a61390;">float</span> currentDegree <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
	<span style="color: #a61390;">unsigned</span> currentIndex;
	<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> currentIndex <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; currentIndex <span style="color: #002200;">&amp;</span>lt; <span style="color: #002200;">&#91;</span>cachedSegmentValuesArray count<span style="color: #002200;">&#93;</span>; currentIndex<span style="color: #002200;">++</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #400080;">NSNumber</span> <span style="color: #002200;">*</span>eachValue <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>cachedSegmentValuesArray objectAtIndex<span style="color: #002200;">:</span>currentIndex<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #a61390;">float</span> startDegree <span style="color: #002200;">=</span> currentDegree;
		currentDegree <span style="color: #002200;">+=</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>eachValue floatValue<span style="color: #002200;">&#93;</span> <span style="color: #002200;">*</span> unitSize<span style="color: #002200;">&#41;</span>;
		<span style="color: #a61390;">float</span> endDegree <span style="color: #002200;">=</span> currentDegree;
		<span style="color: #a61390;">float</span> midDegree <span style="color: #002200;">=</span> startDegree <span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>endDegree <span style="color: #002200;">-</span> startDegree<span style="color: #002200;">&#41;</span> <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #400080;">NSBezierPath</span> <span style="color: #002200;">*</span>eachSegmentPath <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSBezierPath</span> bezierPath<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>eachSegmentPath moveToPoint<span style="color: #002200;">:</span>midPoint<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #002200;">&#91;</span>eachSegmentPath appendBezierPathWithArcWithCenter<span style="color: #002200;">:</span>midPoint radius<span style="color: #002200;">:</span>radius startAngle<span style="color: #002200;">:</span>startDegree endAngle<span style="color: #002200;">:</span>midDegree clockwise<span style="color: #002200;">:</span><span style="color: #a61390;">NO</span><span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #a61390;">NSPoint</span> textPoint <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>eachSegmentPath currentPoint<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #002200;">&#91;</span>eachSegmentPath appendBezierPathWithArcWithCenter<span style="color: #002200;">:</span>midPoint radius<span style="color: #002200;">:</span>radius startAngle<span style="color: #002200;">:</span>midDegree endAngle<span style="color: #002200;">:</span>endDegree clockwise<span style="color: #002200;">:</span><span style="color: #a61390;">NO</span><span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #002200;">&#91;</span>eachSegmentPath closePath<span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">// close path also handles the lines from the midPoint to the start and end of the arc</span>
		<span style="color: #002200;">&#91;</span>eachSegmentPath setLineWidth<span style="color: #002200;">:</span><span style="color: #2400d9;">2.0</span><span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// Check to see whether we should offset this segment if it's currently selected in the array controller</span>
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> shouldOffsetSelectedSegment <span style="color: #002200;">&amp;</span>amp;<span style="color: #002200;">&amp;</span>amp; <span style="color: #002200;">&#91;</span>selectionIndexes containsIndex<span style="color: #002200;">:</span>currentIndex<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#123;</span>
			<span style="color: #a61390;">float</span> differenceRatio <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>SELECTEDSEGMENTOFFSET <span style="color: #002200;">/</span> radius<span style="color: #002200;">&#41;</span> <span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span>SELECTEDSEGMENTOFFSET <span style="color: #002200;">/</span> <span style="color: #002200;">&#40;</span>endDegree <span style="color: #002200;">-</span> startDegree<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
			<span style="color: #a61390;">float</span> diffY <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>textPoint.y <span style="color: #002200;">-</span> midPoint.y<span style="color: #002200;">&#41;</span> <span style="color: #002200;">*</span> differenceRatio;
			<span style="color: #a61390;">float</span> diffX <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>textPoint.x <span style="color: #002200;">-</span> midPoint.x<span style="color: #002200;">&#41;</span> <span style="color: #002200;">*</span> differenceRatio;
&nbsp;
			<span style="color: #400080;">NSAffineTransform</span> <span style="color: #002200;">*</span>transform <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSAffineTransform</span> transform<span style="color: #002200;">&#93;</span>;
&nbsp;
			<span style="color: #002200;">&#91;</span>transform translateXBy<span style="color: #002200;">:</span>diffX yBy<span style="color: #002200;">:</span>diffY<span style="color: #002200;">&#93;</span>;
			<span style="color: #002200;">&#91;</span>eachSegmentPath transformUsingAffineTransform<span style="color: #002200;">:</span> transform<span style="color: #002200;">&#93;</span>;
&nbsp;
			textPoint <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>transform transformPoint<span style="color: #002200;">:</span>textPoint<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#125;</span>
&nbsp;
		<span style="color: #002200;">&#91;</span>_segmentPathsArray addObject<span style="color: #002200;">:</span>eachSegmentPath<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// Get the text to be displayed, if it exists, and see how big it is</span>
		<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>eachText <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;&quot;</span>;
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>cachedSegmentNamesArray count<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&amp;</span>gt; currentIndex <span style="color: #002200;">&#41;</span>
			eachText <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>cachedSegmentNamesArray objectAtIndex<span style="color: #002200;">:</span>currentIndex<span style="color: #002200;">&#93;</span>;
&nbsp;
		NSSize textSize <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>eachText sizeWithAttributes<span style="color: #002200;">:</span>textAttributes<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// Offset it by TEXTPADDING in direction suitable for whichever quarter of the view it is in</span>
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> NSPointInRect<span style="color: #002200;">&#40;</span>textPoint, topLeftRect<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#123;</span>
			textPoint.y <span style="color: #002200;">-=</span> <span style="color: #002200;">&#40;</span>textSize.height <span style="color: #002200;">+</span> TEXTPADDING<span style="color: #002200;">&#41;</span>;
			textPoint.x <span style="color: #002200;">-=</span> <span style="color: #002200;">&#40;</span>textSize.width <span style="color: #002200;">+</span> TEXTPADDING<span style="color: #002200;">&#41;</span>;
		<span style="color: #002200;">&#125;</span>
		<span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> NSPointInRect<span style="color: #002200;">&#40;</span>textPoint, topRightRect<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#123;</span>
			textPoint.y <span style="color: #002200;">-=</span> <span style="color: #002200;">&#40;</span>textSize.height <span style="color: #002200;">+</span> TEXTPADDING<span style="color: #002200;">&#41;</span>;
			textPoint.x <span style="color: #002200;">+=</span> TEXTPADDING;
		<span style="color: #002200;">&#125;</span>
		<span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> NSPointInRect<span style="color: #002200;">&#40;</span>textPoint, bottomLeftRect<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#123;</span>
			textPoint.y <span style="color: #002200;">+=</span> TEXTPADDING;
			textPoint.x <span style="color: #002200;">-=</span> <span style="color: #002200;">&#40;</span>textSize.width <span style="color: #002200;">+</span> TEXTPADDING<span style="color: #002200;">&#41;</span>;
		<span style="color: #002200;">&#125;</span>
		<span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> NSPointInRect<span style="color: #002200;">&#40;</span>textPoint, bottomRightRect<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#123;</span>
			textPoint.y <span style="color: #002200;">+=</span> TEXTPADDING;
			textPoint.x <span style="color: #002200;">+=</span> TEXTPADDING;
		<span style="color: #002200;">&#125;</span>
&nbsp;
		<span style="color: #11740a; font-style: italic;">// Make sure the point isn't outside the view's bounds</span>
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> textPoint.x <span style="color: #002200;">&amp;</span>lt; viewBounds.origin.x <span style="color: #002200;">&#41;</span>
			textPoint.x <span style="color: #002200;">=</span> viewBounds.origin.x;
&nbsp;
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#40;</span>textPoint.x <span style="color: #002200;">+</span> textSize.width<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&amp;</span>gt; <span style="color: #002200;">&#40;</span>viewBounds.origin.x <span style="color: #002200;">+</span> viewBounds.size.width<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>
			textPoint.x <span style="color: #002200;">=</span> viewBounds.origin.x <span style="color: #002200;">+</span> viewBounds.size.width <span style="color: #002200;">-</span> textSize.width;
&nbsp;
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> textPoint.y <span style="color: #002200;">&amp;</span>lt; viewBounds.origin.y <span style="color: #002200;">&#41;</span>
			textPoint.y <span style="color: #002200;">=</span> viewBounds.origin.y;
&nbsp;
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#40;</span>textPoint.y <span style="color: #002200;">+</span> textSize.height<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&amp;</span>gt; <span style="color: #002200;">&#40;</span>viewBounds.origin.y <span style="color: #002200;">+</span> viewBounds.size.height<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>
			textPoint.y <span style="color: #002200;">=</span> viewBounds.origin.y <span style="color: #002200;">+</span> viewBounds.size.height <span style="color: #002200;">-</span> textSize.height;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// Finally add the details as a dictionary to our segmentTextsArray.</span>
		<span style="color: #11740a; font-style: italic;">// We include here the textAttributes lest we decide later to e.g. color the texts the same color as the segment fill</span>
		<span style="color: #002200;">&#91;</span>_segmentTextsArray addObject<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithObjectsAndKeys<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithFloat<span style="color: #002200;">:</span>textPoint.x<span style="color: #002200;">&#93;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;textPointX&quot;</span>, <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithFloat<span style="color: #002200;">:</span>textPoint.y<span style="color: #002200;">&#93;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;textPointY&quot;</span>, eachText, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;text&quot;</span>, textAttributes, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;textAttributes&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>We&#8217;ll also modify the <code>drawRect:</code> method so that it fills selected segments with blue rather than the usual colour:

<div class="wp_syntax"><div 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>drawRect<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">NSRect</span><span style="color: #002200;">&#41;</span>rect
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSColor</span> whiteColor<span style="color: #002200;">&#93;</span> set<span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">// white background</span>
	NSRectFill<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>self bounds<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>pathsArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self segmentPathsArray<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">unsigned</span> count;
	<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> count <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; count <span style="color: #002200;">&amp;</span>lt; <span style="color: #002200;">&#91;</span>pathsArray count<span style="color: #002200;">&#93;</span>; count<span style="color: #002200;">++</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #400080;">NSBezierPath</span> <span style="color: #002200;">*</span>eachPath <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>pathsArray objectAtIndex<span style="color: #002200;">:</span>count<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// fill the path with the drawing color for this index unless it's selected</span>
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self selectionIndexes<span style="color: #002200;">&#93;</span> containsIndex<span style="color: #002200;">:</span>count<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>
			<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSColor</span> blueColor<span style="color: #002200;">&#93;</span> set<span style="color: #002200;">&#93;</span>;
		<span style="color: #a61390;">else</span>
			<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self colorForIndex<span style="color: #002200;">:</span>count<span style="color: #002200;">&#93;</span> set<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #002200;">&#91;</span>eachPath fill<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// draw a black border around it</span>
		<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSColor</span> blackColor<span style="color: #002200;">&#93;</span> set<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>eachPath stroke<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>textsArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self segmentTextsArray<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> count <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; count <span style="color: #002200;">&amp;</span>lt; <span style="color: #002200;">&#91;</span>textsArray count<span style="color: #002200;">&#93;</span>; count<span style="color: #002200;">++</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>eachTextDictionary <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>textsArray objectAtIndex<span style="color: #002200;">:</span>count<span style="color: #002200;">&#93;</span>;
		<span style="color: #a61390;">NSPoint</span> textPoint <span style="color: #002200;">=</span> NSMakePoint<span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>eachTextDictionary valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;textPointX&quot;</span><span style="color: #002200;">&#93;</span> floatValue<span style="color: #002200;">&#93;</span>, <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>eachTextDictionary valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;textPointY&quot;</span><span style="color: #002200;">&#93;</span> floatValue<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>textAttributes <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>eachTextDictionary valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;textAttributes&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>text <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>eachTextDictionary valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;text&quot;</span><span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>text drawAtPoint<span style="color: #002200;">:</span>textPoint withAttributes<span style="color: #002200;">:</span>textAttributes<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
</ul>
<p>Now the view should display the selected segment as desired, updating when the user clicks on a different line in the table view:<br />
<img class="aligncenter size-full wp-image-280" title="Pie Chart highlighting selected segment" src="http://www.timisted.net/blog/wp-content/uploads/2008/11/pie-chart-with-selection-in.gif" alt="" width="500" height="291" /></p>
<p>If you wish, go into Interface Builder and change the selection type of the table view to &#8216;Multiple&#8217;. You should find that you can shift-click multiple items in the table view and they will show as desired in the chart view.</p>
<h3>Resizing Issues</h3>
<p>Since we cache the array of segment paths, only updating it when the arrays of data are changed, you might have noticed that the view doesn&#8217;t behave very well when we resize it. Assuming you set the view to resize with its window, whilst the white background will enlarge, the actual paths won&#8217;t change. This is easy to rectify:</p>
<ul>
<li>Simply check in the <code>drawRect:</code> method whether we are in the middle of a &#8216;liveResize&#8217; and, if so, update the drawing information arrays:

<div class="wp_syntax"><div 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>drawRect<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">NSRect</span><span style="color: #002200;">&#41;</span>rect
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSColor</span> whiteColor<span style="color: #002200;">&#93;</span> set<span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">// white background</span>
	NSRectFill<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>self bounds<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>self inLiveResize<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #002200;">&#91;</span>self generateDrawingInformation<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>pathsArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self segmentPathsArray<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">unsigned</span> count;
	<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> count <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; count <span style="color: #002200;">&amp;</span>lt; <span style="color: #002200;">&#91;</span>pathsArray count<span style="color: #002200;">&#93;</span>; count<span style="color: #002200;">++</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #400080;">NSBezierPath</span> <span style="color: #002200;">*</span>eachPath <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>pathsArray objectAtIndex<span style="color: #002200;">:</span>count<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// fill the path with the drawing color for this index unless it's selected</span>
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self selectionIndexes<span style="color: #002200;">&#93;</span> containsIndex<span style="color: #002200;">:</span>count<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>
			<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSColor</span> blueColor<span style="color: #002200;">&#93;</span> set<span style="color: #002200;">&#93;</span>;
		<span style="color: #a61390;">else</span>
			<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self colorForIndex<span style="color: #002200;">:</span>count<span style="color: #002200;">&#93;</span> set<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #002200;">&#91;</span>eachPath fill<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// draw a black border around it</span>
		<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSColor</span> blackColor<span style="color: #002200;">&#93;</span> set<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>eachPath stroke<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>textsArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self segmentTextsArray<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> count <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; count <span style="color: #002200;">&amp;</span>lt; <span style="color: #002200;">&#91;</span>textsArray count<span style="color: #002200;">&#93;</span>; count<span style="color: #002200;">++</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>eachTextDictionary <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>textsArray objectAtIndex<span style="color: #002200;">:</span>count<span style="color: #002200;">&#93;</span>;
		<span style="color: #a61390;">NSPoint</span> textPoint <span style="color: #002200;">=</span> NSMakePoint<span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>eachTextDictionary valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;textPointX&quot;</span><span style="color: #002200;">&#93;</span> floatValue<span style="color: #002200;">&#93;</span>, <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>eachTextDictionary valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;textPointY&quot;</span><span style="color: #002200;">&#93;</span> floatValue<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>textAttributes <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>eachTextDictionary valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;textAttributes&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>text <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>eachTextDictionary valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;text&quot;</span><span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>text drawAtPoint<span style="color: #002200;">:</span>textPoint withAttributes<span style="color: #002200;">:</span>textAttributes<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
</ul>
<p>The view will now &#8216;live resize&#8217; itself as would be expected.</p>
<h3>Changing the Selection Indexes when the user clicks a Segment</h3>
<p>We have already set up the view to display a segment &#8217;selected&#8217; in the table view, but it would be nice if the user could also click on a segment to select it. Given that we store an array of segment paths inside the view, it should be pretty easy to &#8216;hit-test&#8217; each path to determine which segment was clicked.</p>
<ul>
<li>To receive clicks in our view, we need to override the <code>acceptsFirstResponder:</code> method for our view to return YES so add the following to MyPieChartView.m:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>acceptsFirstResponder
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> <span style="color: #a61390;">YES</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>Next we&#8217;ll implement a method to determine which segment was clicked so add the method declaration to MyPieChartView.h:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>objectIndexForPoint<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">NSPoint</span><span style="color: #002200;">&#41;</span>thePoint;</pre></div></div>

</li>
<li>And implement it in MyPieChartView.m:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>objectIndexForPoint<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">NSPoint</span><span style="color: #002200;">&#41;</span>thePoint
<span style="color: #002200;">&#123;</span>
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>cachedPathsArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self segmentPathsArray<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">int</span> count;
	<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> count <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; count <span style="color: #002200;">&amp;</span>lt; <span style="color: #002200;">&#91;</span>cachedPathsArray count<span style="color: #002200;">&#93;</span>; count<span style="color: #002200;">++</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #400080;">NSBezierPath</span> <span style="color: #002200;">*</span>eachPath <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>cachedPathsArray objectAtIndex<span style="color: #002200;">:</span>count<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>eachPath containsPoint<span style="color: #002200;">:</span>thePoint <span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#123;</span>
			<span style="color: #a61390;">return</span> count;
		<span style="color: #002200;">&#125;</span>
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #11740a; font-style: italic;">// if control reaches here, no segment contained the point so return -1</span>
	<span style="color: #a61390;">return</span> <span style="color: #002200;">-</span><span style="color: #2400d9;">1</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>We also need to implement a <code>mouseUp:</code> method that will be called when the user has clicked on a segment. Expected behavior varies depending on whether the user is holding down a modifier key at the time the segment was clicked; holding down the command key should either add or remove the clicked segment to the current selection; holding down the shift key and clicking should select a range of segments between the existing selection and the clicked segment:

<div class="wp_syntax"><div 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>mouseUp<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSEvent</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>theEvent
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">int</span> index <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self objectIndexForPoint<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>self convertPoint<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>theEvent locationInWindow<span style="color: #002200;">&#93;</span> fromView<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #400080;">NSMutableIndexSet</span> <span style="color: #002200;">*</span>newSelectionIndexes <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self selectionIndexes<span style="color: #002200;">&#93;</span> mutableCopy<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>theEvent modifierFlags<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&amp;</span>amp; NSCommandKeyMask <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #11740a; font-style: italic;">// Add or remove the clicked segment</span>
		<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>newSelectionIndexes containsIndex<span style="color: #002200;">:</span>index<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#123;</span>
			<span style="color: #002200;">&#91;</span>newSelectionIndexes removeIndex<span style="color: #002200;">:</span>index<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#125;</span>
		<span style="color: #a61390;">else</span>
		<span style="color: #002200;">&#123;</span>
			<span style="color: #002200;">&#91;</span>newSelectionIndexes addIndex<span style="color: #002200;">:</span>index<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#125;</span>
	<span style="color: #002200;">&#125;</span>
	<span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>theEvent modifierFlags<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&amp;</span>amp; NSShiftKeyMask <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #11740a; font-style: italic;">// Add range to selection</span>
		<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>newSelectionIndexes count<span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#123;</span>
			<span style="color: #002200;">&#91;</span>newSelectionIndexes addIndex<span style="color: #002200;">:</span>index<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#125;</span>
		<span style="color: #a61390;">else</span>
		<span style="color: #002200;">&#123;</span>
			<span style="color: #a61390;">unsigned</span> <span style="color: #a61390;">int</span> origin <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>index <span style="color: #002200;">&amp;</span>lt; <span style="color: #002200;">&#91;</span>newSelectionIndexes lastIndex<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> ? index <span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>newSelectionIndexes lastIndex<span style="color: #002200;">&#93;</span>;
			<span style="color: #a61390;">unsigned</span> <span style="color: #a61390;">int</span> length <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>index <span style="color: #002200;">&amp;</span>lt; <span style="color: #002200;">&#91;</span>newSelectionIndexes lastIndex<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> ? <span style="color: #002200;">&#91;</span>newSelectionIndexes lastIndex<span style="color: #002200;">&#93;</span> <span style="color: #002200;">-</span> index <span style="color: #002200;">:</span> index <span style="color: #002200;">-</span> <span style="color: #002200;">&#91;</span>newSelectionIndexes lastIndex<span style="color: #002200;">&#93;</span>;
&nbsp;
			length<span style="color: #002200;">++</span>;
			<span style="color: #002200;">&#91;</span>newSelectionIndexes addIndexesInRange<span style="color: #002200;">:</span>NSMakeRange<span style="color: #002200;">&#40;</span>origin, length<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#125;</span>
	<span style="color: #002200;">&#125;</span>
	<span style="color: #a61390;">else</span> <span style="color: #11740a; font-style: italic;">// the user just clicked without modifier keys so simply select the segment</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #002200;">&#91;</span>newSelectionIndexes removeAllIndexes<span style="color: #002200;">&#93;</span>;
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> index <span style="color: #002200;">&amp;</span>gt;<span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span> <span style="color: #002200;">&#41;</span>
			<span style="color: #002200;">&#91;</span>newSelectionIndexes addIndex<span style="color: #002200;">:</span>index<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #002200;">&#91;</span>self setSelectionIndexes<span style="color: #002200;">:</span>newSelectionIndexes<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>newSelectionIndexes release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>Finally we need to bind the selectionIndexes of our view to the selectionIndexes of the array controller so that it updates the table view as well. Add this last binding in MyDocument.m:

<div class="wp_syntax"><div 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>windowControllerDidLoadNib<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSWindowController</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>windowController
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>super windowControllerDidLoadNib<span style="color: #002200;">:</span>windowController<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>pieChartView bind<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;segmentNamesArray&quot;</span> toObject<span style="color: #002200;">:</span>segmentsArrayController withKeyPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;arrangedObjects.name&quot;</span> options<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>pieChartView bind<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;segmentValuesArray&quot;</span> toObject<span style="color: #002200;">:</span>segmentsArrayController withKeyPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;arrangedObjects.amount&quot;</span> options<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>pieChartView bind<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;selectionIndexes&quot;</span> toObject<span style="color: #002200;">:</span>segmentsArrayController withKeyPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;selectionIndexes&quot;</span> options<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>segmentsArrayController bind<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;selectionIndexes&quot;</span> toObject<span style="color: #002200;">:</span>pieChartView withKeyPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;selectionIndexes&quot;</span> options<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
</ul>
<p>When the user clicks on a segment in our chart view, that segment should become selected and also change the selection in the table view; modifier keys should also affect the way that segments become selected.</p>
<h3>One Last Binding</h3>
<p>Just in case you haven&#8217;t had enough bindings yet, we&#8217;ll add one final feature to the view &#8211; the ability to rotate the chart, perhaps so the user can better fit the related text or place one segment in the middle etc.</p>
<ul>
<li>We&#8217;ll start by adding a &#8216;rotationAmount&#8217; attribute to our view, along with the necessary accessors in MyPieChartView.h:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> MyPieChartView <span style="color: #002200;">:</span> <span style="color: #400080;">NSView</span>
<span style="color: #002200;">&#123;</span>
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>_segmentNamesArray;
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>_segmentValuesArray;
&nbsp;
	<span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span>_segmentPathsArray;
	<span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span>_segmentTextsArray;
&nbsp;
	<span style="color: #400080;">NSIndexSet</span> <span style="color: #002200;">*</span>_selectionIndexes;
&nbsp;
	<span style="color: #a61390;">float</span> _rotationAmount;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>segmentNamesArray;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setSegmentNamesArray<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>newArray;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>segmentValuesArray;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setSegmentValuesArray<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>newArray;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>segmentPathsArray;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>segmentTextsArray;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>generateDrawingInformation;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSColor</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>randomColor;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSColor</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>colorForIndex<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">unsigned</span><span style="color: #002200;">&#41;</span>index;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSIndexSet</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>selectionIndexes;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setSelectionIndexes<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSIndexSet</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>newIndexes;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>objectIndexForPoint<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">NSPoint</span><span style="color: #002200;">&#41;</span>thePoint;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setRotationAmount<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSNumber</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>value;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSNumber</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>rotationAmount;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

</li>
<li>Expose the binding for the rotationAmount attribute and add the accessor methods to MyPieChartView.m:

<div class="wp_syntax"><div 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>initialize
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>self exposeBinding<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;segmentNamesArray&quot;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>self exposeBinding<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;segmentValuesArray&quot;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>self exposeBinding<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;selectionIndexes&quot;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>self exposeBinding<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;rotationAmount&quot;</span><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>setRotationAmount<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSNumber</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>value
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>value floatValue<span style="color: #002200;">&#93;</span> <span style="color: #002200;">!=</span> _rotationAmount <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #002200;">&#91;</span>self willChangeValueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;rotationAmount&quot;</span><span style="color: #002200;">&#93;</span>;
		_rotationAmount <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>value floatValue<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>self didChangeValueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;rotationAmount&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #002200;">&#91;</span>self generateDrawingInformation<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>self setNeedsDisplayInRect<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>self visibleRect<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSNumber</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>rotationAmount
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithFloat<span style="color: #002200;">:</span>_rotationAmount<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>To make use of the rotation amount, we simply need to modify our drawing code to set the &#8216;currentDegree&#8217; to the rotation amount so find and change this in <code>generateDrawingInformation:</code>:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">	<span style="color: #11740a; font-style: italic;">// cycle through the segmentValues and create the bezier paths</span>
	<span style="color: #11740a; font-style: italic;">// Also add the text details (note we expect the texts' indexes to tie up with the values' indexes)</span>
	<span style="color: #a61390;">float</span> currentDegree <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self rotationAmount<span style="color: #002200;">&#93;</span> floatValue<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">unsigned</span> currentIndex;</pre></div></div>

</li>
<li>Next we need to have a control to rotate the graph so open up MyDocument.xib in InterfaceBuilder and add a horizontal slider control; set its minimum and maximum values to 0.0 and 360.0, and its current value to 0.0; tick the &#8216;Continuous&#8217; box in the inspector. My interface looks like this:<br />
<img class="aligncenter size-full wp-image-284" title="The new slider control in Interface Builder" src="http://www.timisted.net/blog/wp-content/uploads/2008/11/the-new-slider-control.gif" alt="" width="500" height="316" /></li>
<li>Back in Xcode, add an IBOutlet for the new slider to MyDocument.h:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> MyDocument <span style="color: #002200;">:</span> <span style="color: #400080;">NSPersistentDocument</span>
<span style="color: #002200;">&#123;</span>
	IBOutlet <span style="color: #400080;">NSArrayController</span> <span style="color: #002200;">*</span>segmentsArrayController;
	IBOutlet <span style="color: #400080;">NSView</span> <span style="color: #002200;">*</span>pieChartView;
	IBOutlet <span style="color: #400080;">NSSlider</span> <span style="color: #002200;">*</span>sliderRotationControl;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

</li>
<li>Connect the outlet to the control in Interface Builder.</li>
<li>In MyDocument.m, bind the rotation control&#8217;s value to the rotationAmount on the chart view:

<div class="wp_syntax"><div 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>windowControllerDidLoadNib<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSWindowController</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>windowController
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>super windowControllerDidLoadNib<span style="color: #002200;">:</span>windowController<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>pieChartView bind<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;segmentNamesArray&quot;</span> toObject<span style="color: #002200;">:</span>segmentsArrayController withKeyPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;arrangedObjects.name&quot;</span> options<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>pieChartView bind<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;segmentValuesArray&quot;</span> toObject<span style="color: #002200;">:</span>segmentsArrayController withKeyPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;arrangedObjects.amount&quot;</span> options<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>pieChartView bind<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;selectionIndexes&quot;</span> toObject<span style="color: #002200;">:</span>segmentsArrayController withKeyPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;selectionIndexes&quot;</span> options<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>segmentsArrayController bind<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;selectionIndexes&quot;</span> toObject<span style="color: #002200;">:</span>pieChartView withKeyPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;selectionIndexes&quot;</span> options<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>sliderRotationControl bind<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;value&quot;</span> toObject<span style="color: #002200;">:</span>pieChartView withKeyPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;rotationAmount&quot;</span> options<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
</ul>
<h3>That&#8217;s It!</h3>
<p>At this point you should have a pretty functional, bindable pie-chart view. For such a view it would make sense to create an Interface Builder palette so that the bindings can be setup visually in Interface Builder rather having to do it programmatically; this may well be a topic for a future post!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.timisted.net/blog/archive/a-bindable-custom-nsview-subclass/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>On Cocoa Bindings – &#8220;Let Us Break Their Bonds Asunder&#8221;</title>
		<link>http://www.timisted.net/blog/archive/on-cocoa-bindings/</link>
		<comments>http://www.timisted.net/blog/archive/on-cocoa-bindings/#comments</comments>
		<pubDate>Wed, 27 Aug 2008 17:38:38 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Bindings]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Apple]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Framework]]></category>
		<category><![CDATA[Interface Builder]]></category>
		<category><![CDATA[Xcode]]></category>

		<guid isPermaLink="false">http://www.timisted.net/blog/?p=200</guid>
		<description><![CDATA[Chances are, if you&#8217;ve developed any kind of Cocoa application, you&#8217;ve probably made use of Cocoa Bindings in one way or another, particularly when it comes to Core Data. The technology is &#8216;key&#8217; to so many of the underlying frameworks but it is a technology that tends to be often-used but frequently not fully understood.
If [...]]]></description>
			<content:encoded><![CDATA[<p>Chances are, if you&#8217;ve developed any kind of Cocoa application, you&#8217;ve probably made use of Cocoa Bindings in one way or another, particularly when it comes to Core Data. The technology is &#8216;key&#8217; to so many of the underlying frameworks but it is a technology that tends to be often-used but frequently not fully understood.</p>
<p>If you are someone who happily binds Array Controllers and View objects together using Interface Builder but have always wondered discretely exactly how the magic behind the scenes happens, or if you just don&#8217;t quite comprehend the difference between &#8216;KVC&#8217; and &#8216;KVO&#8217; (or perhaps can never quite remember what they stand for&#8230;), this article might help you out. In it, I&#8217;ll attempt to demonstrate a &#8216;faux-bindings&#8217; methodology that connects a value from one object with a value from another object, but without using the actual Apple Cocoa Bindings technology. The bare-bones of how the &#8216;faux binding&#8217; works will be there for all to see; while the internal workings of Cocoa Bindings are a little different, this will hopefully at least show you what, how and why certain things are required for the real Cocoa Bindings to work successfully.</p>
<p><span id="more-200"></span></p>
<h3>Basic Principles</h3>
<p>The simple premise of our &#8216;faux-bindings&#8217; technology is that we link two objects together such that when a certain value changes in the first, the other changes its linked value to match. One way to accomplish this would be to have the objects (A and B) maintain links to each other such that when object A&#8217;s value changes, A can call a method on object B to update B&#8217;s value. This is all very well for two simple objects working in one direction but if you have multiple objects all bound together, each with a link to all the others, things can quickly become complicated, leak memory all over the place, and be difficult to maintain.</p>
<h4>Notifications</h4>
<p>Notifications are another wonderful, often underused, technology that allow communication between objects without those objects knowing anything about each other. Using a Notification Center, objects (e.g. objects B and C) can request notifications when specified types of messages are posted by other objects (e.g. object A). So, when object A decides to post a message, the notification center looks at the list of objects who want to receive such messages, and goes and tells those objects B and C that object A posted a notification. If object A is later released from memory but B and C are still listening for notifications, it doesn&#8217;t matter &#8212; they maintain no direct link to object A so they simply go on waiting without receiving anything. Similarly, if object C is released before object A posts a notification, again it doesn&#8217;t matter &#8212; A has no knowledge or worry about who receives the notifications as they go through a third party, a notification center.</p>
<p>To post a notification, an object can simply do this:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">	<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNotificationCenter</span> defaultCenter<span style="color: #002200;">&#93;</span> postNotificationName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;helloIChanged&quot;</span> object<span style="color: #002200;">:</span>self<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>This tells the default notification center (helpfully already created ready for you to use so you don&#8217;t need to worry about it) to distribute the &#8216;helloIChanged&#8217; notification to anyone who listens for it.</p>
<p>To specify that you want to listen for such a notification, you can do this:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">	<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><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>receivedNotification<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span>
	                                             name<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;helloIChanged&quot;</span>
	                                           object<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>This code tells the notification center that you want to receive all &#8216;helloIChanged&#8217; notifications, and when such a notification is received, your <code>receivedNotification:</code> method should be called. The <code>object</code> parameter of this method allows you to specify that you only want to receive notifications from a single object instance rather than from any object. If you wished, you could say &#8220;name:nil&#8221; but instead limit your interest to a specific object so that <em>any</em> notification posted by that object (e.g. @&#8221;helloIChanged&#8221; or @&#8221;helloSomethingElseHappened&#8221;) would cause the notification centre to call your <code>receivedNotification:</code> method.</p>
<h3>The Sample Project</h3>
<p>We&#8217;re going to create a very simple sample project for this article. If you wish to download some sample code for our Faux Binding, you can do so from here: <a href='http://www.timisted.net/blog/wp-content/uploads/2008/08/fauxbindings.zip'>fauxbindings.zip</a>.</p>
<ul>
<li>In Xcode, create a new &#8216;Cocoa Application&#8217; project called &#8216;FauxBindings&#8217;.</li>
<li>Right-click on the &#8216;Classes&#8217; group and Add -> New File&#8230; to add a basic Objective-C class file called &#8220;BindController.m&#8221;; make sure the &#8216;Also create &#8220;BindController.h&#8221;&#8216; box is ticked, and click Finish. If you don&#8217;t have a two-button mouse, read &#8216;right-click&#8217; as &#8216;CTRL-click&#8217; throughout this article.</li>
<li>We are going to need an <code>IBOutlet</code> for an <code>NSTextField</code> and a <code>NSString</code> object. We&#8217;re also going to need an <code>IBAction</code> method to display the string using <code>NSLog()</code>. So, make your BindController.h file look like this:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &lt;cocoa/Cocoa.h&gt;</span>
&nbsp;
&nbsp;
<span style="color: #a61390;">@interface</span> BindController <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span> <span style="color: #002200;">&#123;</span>
&nbsp;
	IBOutlet <span style="color: #400080;">NSTextField</span> <span style="color: #002200;">*</span>theTextField;
&nbsp;
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>theString;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span>showTheStringInLog<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>sender;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

</li>
<li>Next open MainMenu.nib in Xcode and open up the &#8216;Window&#8217; object. Add an <code>NSTextField</code> and an <code>NSButton</code> called &#8216;Log theString&#8217; so that it looks something like this:<br />
<img src="http://www.timisted.net/blog/wp-content/uploads/2008/08/fauxbindingssimplewindow.gif" alt="The FauxBindings main Window layout in Interface Builder" title="fauxbindingssimplewindow" width="348" height="179" class="size-full wp-image-205" /><br />
Not the most exciting window ever created but functional enough for this demonstration.</li>
<li>From the Interface Builder Library tool drag out an NSObject (that&#8217;s the blue cube thing) into your MainMenu.nib window. Click this object and in the Inspector, use the Identity tab to specify that the object&#8217;s class is BindController. By adding a BindController object in this way to our nib file, an instance of the BindController class will be created when the nib is loaded; there is no other code anywhere in the project that does a [[BindController alloc] init] etc. It&#8217;s all handled for you.</li>
<li>Next right-click on the cube and connect its <code>theTextField</code> outlet to our text field in the window, and connect the <code>showTheStringInLog:</code> method to the &#8220;Log theString&#8221; button:<br />
<img src="http://www.timisted.net/blog/wp-content/uploads/2008/08/fauxbindingsconnections.gif" alt="Connections between the BindController object and the FauxBindings main window" title="fauxbindingsconnections" width="347" height="401" class="size-full wp-image-206" /></li>
<li>Return now to Xcode and open up the BindController.m class file. We need to add the code for the <code>showTheStringInLog:</code> method so write that now:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;BindController.h&quot;</span>
&nbsp;
&nbsp;
<span style="color: #a61390;">@implementation</span> BindController
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span>showTheStringInLog<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>sender
<span style="color: #002200;">&#123;</span>
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Value of theString is: %@&quot;</span>, theString<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

</li>
<li>Make sure the Console window is open (listed under the &#8216;Run&#8217; menu, or CMD-SHIFT-R) and then build and run your application. Press the button. No surprises here, you&#8217;ll see that the &#8220;Value of theString is: (null)&#8221; because we haven&#8217;t set theString to anything yet.</li>
</ul>
<h4>Establishing a Link</h4>
<p>Now we need to setup a <code>FauxBind:</code> method that can be used to link a specified value of one object to a specified value on another. We&#8217;ll use it to link the <code>stringValue</code> on the text field to our BindController&#8217;s <code>theString</code> object. We need to call this method when the application first loads so that the faux bindings are ready for the user input.</p>
<ul>
<li>Go back to Interface Builder and click on the Application object in the nib file. Use the Connections Inspector panel to connect the &#8216;delegate&#8217; outlet to the Bind Controller object. This will allow our object to respond to the &#8216;awakeFromNib&#8217; method that is called when the application has loaded the nib file into memory.</li>
<li>Open BindController.h in Xcode and add two objects to the controller &#8211; we&#8217;ll need an <code>originatingObject</code> and a <code>destinationObject</code>, together with two strings for that hold the names of the values to which we&#8217;ll be binding. Also add a FauxBind: method that will set up our faux binding between those objects. Finally, we&#8217;ll be using notifications so declare a method to be called when a notification is received:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &lt;cocoa/Cocoa.h&gt;</span>
&nbsp;
&nbsp;
<span style="color: #a61390;">@interface</span> BindController <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span> <span style="color: #002200;">&#123;</span>
&nbsp;
	IBOutlet <span style="color: #400080;">NSTextField</span> <span style="color: #002200;">*</span>theTextField;
&nbsp;
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>theString;
&nbsp;
	<span style="color: #a61390;">id</span> originatingObject;
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>originatingValueName;
	<span style="color: #a61390;">id</span> destinationObject;
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>destinationValueName;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span>showTheStringInLog<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>sender;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>FauxBindObject<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>destObject valueName<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>destValueName toObject<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>origObject valueName<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>origValueName;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>receivedNotification<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>notification;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

</li>
<li>Next open BindController.m and add an awakeFromNib: method after the showTheStringInLog: method. This method says we&#8217;re going to bind a value called &#8220;theString&#8221; in our own object, to a value called &#8220;stringValue&#8221; on theTextField:

<div class="wp_syntax"><div 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>awakeFromNib
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>self FauxBindObject<span style="color: #002200;">:</span>self valueName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;theString&quot;</span> toObject<span style="color: #002200;">:</span>theTextField valueName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;stringValue&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>We can also write the first steps of this faux bind method. Initially, let&#8217;s just see what notifications are being posted by theTextField and log them to the console:

<div class="wp_syntax"><div 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>FauxBindObject<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>destObject valueName<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>destValueName toObject<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>origObject valueName<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>origValueName
<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> 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>receivedNotification<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span>
	                                             name<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span>
	                                           object<span style="color: #002200;">:</span>origObject<span style="color: #002200;">&#93;</span>;
&nbsp;
	originatingObject <span style="color: #002200;">=</span> origObject;
	originatingValueName <span style="color: #002200;">=</span> origValueName;
	destinationObject <span style="color: #002200;">=</span> destObject;
	destinationValueName <span style="color: #002200;">=</span> destValueName;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>And, of course, we need to code the <code>receivedNotification:</code> method that is called when a notification is received; here we&#8217;ll simply log the whole notification to the console so that we can examine its contents:

<div class="wp_syntax"><div 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>receivedNotification<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>notification
<span style="color: #002200;">&#123;</span>
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Received a notification: %@&quot;</span>, notification<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>Again, make sure the console is visible, then build and run the application.</li>
</ul>
<p>When you click, type and tab out of the text field, you&#8217;ll see a whole string of messages sent to the console, indicating a whole chain of notifications. You&#8217;ll see an <code>NSControlTextDidBeginEditingNotification</code> at the start, then an <code>NSControlTextDidChangeNotification</code> sent every time the text changed, then (assuming you pressed tab or enter to commit the text field), you&#8217;ll see an <code>NSControlTextDidEndEditingNotification</code>.</p>
<p>We want our <code>theString</code> value to change when the text field value is committed by the user pressing tab or enter so:</p>
<ul>
<li>Change the <code>FauxBindObject:</code> method to listen only for NSControlTextDidEndEditingNotifications:

<div class="wp_syntax"><div 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>FauxBindObject<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>destObject valueName<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>destValueName toObject<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>origObject valueName<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>origValueName
<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> 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>receivedNotification<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span>
	                                             name<span style="color: #002200;">:</span>NSControlTextDidEndEditingNotification
	                                           object<span style="color: #002200;">:</span>origObject<span style="color: #002200;">&#93;</span>;
&nbsp;
	originatingObject <span style="color: #002200;">=</span> origObject;
	originatingValueName <span style="color: #002200;">=</span> origValueName;
	destinationObject <span style="color: #002200;">=</span> destObject;
	destinationValueName <span style="color: #002200;">=</span> destValueName;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>This time when you build and run, you&#8217;ll see that we only receive a notification when the text field commits editing on return or tab.</li>
</ul>
<p>Having found out when the originating object changes, we need to be able to obtain the value of that object before updating the value of the destination object.</p>
<h4>A Note about Selectors</h4>
<p>Feel free to skip this part but it might be helpful to know a little about method calls in Objective-C. An in-depth discussion about the way that Objective-C handles such method calls between objects is outside the scope of this article. But, if you are comfortable with bog-standard C or C++, you will know all about functions. Functions look something like this <code>FunctionName( variable1, variable2)</code>. When you call a <strong>method</strong> in Objective-C with code like this: <code>[self doSomethingWith:anObject]</code>, the compiler is actually translating this method call into a call to the <code>objc_msgSend()</code> <strong>function</strong>. This function takes arguments that specify which &#8216;object&#8217; is receiving the method call, and a &#8217;selector&#8217; which specifies the name of a method that is being called.</p>
<p>A discussion of how selectors work is again outside the scope of this article but hold in your mind that the messaging function finds out which methods you&#8217;re calling (and the calls to those methods) at <strong>runtime</strong>. This means your code can also refer to methods &#8216;indirectly&#8217; by using selectors, perhaps in order to find out information about them. We used a selector earlier in our call to register for notifications, saying that our &#8216;receivedNotification:&#8217; method should be called. There is a method on any NSObject subclass called &#8216;respondsToSelector:&#8217; which makes it possible to find out whether an object responds to a particular selector (aka method). So, let&#8217;s assume for now that the Notification Center will ask our object to make sure it can actually receive the method call we specified. It is then also possible to actually call a method using a selector with the &#8216;performSelector:&#8217; NSObject method. This functionality is all provided to any object that subclasses NSObject.</p>
<p>Thinking about the Notification Center example, it might do the following when it needs to tell our object that a notification was received:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>object respondsToSelector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>receivedNotification<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #11740a; font-style: italic;">// then actually call the selector</span>
		<span style="color: #002200;">&#91;</span>object performSelector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>receivedNotification<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> withObject<span style="color: #002200;">:</span>theNotification<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Lastly, you can use a function <code>NSSelectorFromString()</code> to get hold of a selector from a string specifying its name, so the code above could also be written:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>object respondsToSelector<span style="color: #002200;">:</span>NSSelectorFromString<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;receivedNotification&quot;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #11740a; font-style: italic;">// then actually call the selector</span>
		<span style="color: #002200;">&#91;</span>object performSelector<span style="color: #002200;">:</span>NSSelectorFromString<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;receivedNotification&quot;</span><span style="color: #002200;">&#41;</span> withObject<span style="color: #002200;">:</span>theNotification<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span></pre></div></div>

<p>For more information on messaging and selectors, see: <a href="http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/Articles/chapter_9_section_1.html" target="_blank">Apple Developer Connection: How Messaging Works</a>.</p>
<p>With our short discussion of selectors out of the way, we can now start writing the code to handle accessing and setting our bound values.</p>
<h3>Getters and Setters</h3>
<p>Our <code>receivedNotification:</code> method is being called now whenever our originating object changes. We can modify that method so that it tries to find out the specified value of the object. To get the value, we need to call a suitable method on the originating object that will return to us that current value. The most sensible name for such a method would be a method with the same name as the name of the value &#8212; in other words, if an object has a &#8217;someValue&#8217; internal value, you could call:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">	<span style="color: #a61390;">id</span> theValue <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>object someValue<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>to find out that value.</p>
<p>On this assumption then, our method needs to find out whether our originating object responds to a method with the name of the originating value name and, if so, call that method to acquire the current value.</p>
<ul>
<li>Open BindController.m in Xcode and modify your <code>receivedNotification:</code> method to look like the following:

<div class="wp_syntax"><div 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>receivedNotification<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>notification
<span style="color: #002200;">&#123;</span>
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Received a notification that an object changed&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>notification object<span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> originatingObject <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Originating Value to fetch is: %@&quot;</span>, originatingValueName<span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// see if there is a method to fetch this value name</span>
		<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>getterMethodName <span style="color: #002200;">=</span> originatingValueName;
		<span style="color: #a61390;">id</span> fetchedValue <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
		NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Originating Method to test is: %@&quot;</span>, getterMethodName<span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>originatingObject respondsToSelector<span style="color: #002200;">:</span>NSSelectorFromString<span style="color: #002200;">&#40;</span>getterMethodName<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#123;</span>
			NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Originating Object responds to: %@&quot;</span>, getterMethodName<span style="color: #002200;">&#41;</span>;
			fetchedValue <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>originatingObject performSelector<span style="color: #002200;">:</span>NSSelectorFromString<span style="color: #002200;">&#40;</span>getterMethodName<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
			NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Fetched value is: %@&quot;</span>, fetchedValue<span style="color: #002200;">&#41;</span>;
		<span style="color: #002200;">&#125;</span>
		<span style="color: #a61390;">else</span>
		<span style="color: #002200;">&#123;</span>
			NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Originating Object is not KVC compliant for the value: %@&quot;</span>, originatingValueName<span style="color: #002200;">&#41;</span>;
			<span style="color: #a61390;">return</span>;
		<span style="color: #002200;">&#125;</span>
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
</ul>
<p>If you examine this code, you&#8217;ll see that we are first of all checking to make sure that the notification we are receiving has come from the object in which we are interested. We then look at the string containing the name of the value we want, and ask our originating object whether it responds to a method with the name of that value. Assuming it does, we fetch the value by calling that method.</p>
<p>Build and run the project and type something in the text field before pressing tab or enter. You&#8217;ll see in the console log all the various steps above and if everything goes to plan, you should see something like:</p>
<pre>
2008-08-27 14:33:26.756 FauxBindings[2411:10b] Received a notification that an object changed
2008-08-27 14:33:26.761 FauxBindings[2411:10b] Originating Value to fetch is: stringValue
2008-08-27 14:33:26.785 FauxBindings[2411:10b] Originating Method to test is: stringValue
2008-08-27 14:33:26.805 FauxBindings[2411:10b] Originating Object responds to: stringValue
2008-08-27 14:33:26.823 FauxBindings[2411:10b] Fetched value is: something
</pre>
<h4>KVC Compliance</h4>
<p>You&#8217;ll notice in the above code that if an object doesn&#8217;t respond to our suspected getter method name, we log a message to the console that the object <strong>isn&#8217;t KVC compliant</strong> for that value. To test this, change the call in the <code>awakeFromNib:</code> method to something like this:</p>

<div class="wp_syntax"><div 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>awakeFromNib
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>self FauxBindObject<span style="color: #002200;">:</span>self valueName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;theString&quot;</span> toObject<span style="color: #002200;">:</span>theTextField valueName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;anUnknownValue&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>When you run the application and modify the text field, you&#8217;ll see that the object doesn&#8217;t respond to a method called &#8216;<code>anUnknownValue</code>&#8216; and so we can&#8217;t acquire that value.</p>
<p><strong>KVC</strong> stands for <strong>Key-Value-Coding</strong>; what this means is that an object provides suitably named methods to access its values. In the above example, we ask our <code>theTextField</code> for the value named &#8220;anUnknownValue&#8221;. This &#8216;value name&#8217; is also referred to as a &#8216;key&#8217; so it would be best to say:</p>
<p>&#8220;We ask our <code>theTextField</code> object for the value of its &#8220;anUnknownValue&#8221; key&#8221;</p>
<p>For an object to be KVC-compliant for a particular key, it must provide methods to access the value of the key that can be worked out given the name of that key. This generally means that a &#8216;getter&#8217; method is given the same name as the name of the key (as in &#8220;stringValue&#8221;). There are other possibilities too; it might make sense for a Boolean (true/false) value called &#8220;onWatchList&#8221; to be accessed using an accessor &#8220;isOnWatchList&#8221; and in full-blown Cocoa bindings, there are various options you can use when naming accessor methods. For the purposes of this article, however, we won&#8217;t include checking for those other options in our FauxBind: method.</p>
<p>The other half of KVC-compliance is that an object provide a suitably-named &#8217;setter&#8217; method, i.e. a method to set the value of its key. Although there are again various options, this generally means that you put the word &#8217;set&#8217; at the front of the name of the key and correct the capitalization such that for a value &#8220;stringValue&#8221;, the setter would be &#8220;setStringValue&#8221;.</p>
<p>With this knowledge, we can go about changing the <code>receivedNotifiction:</code> method to try and set the value of the key on the destination object using a KVC setter method.</p>
<ul>
<li>If you changed the &#8220;stringValue&#8221; key above to &#8220;anUnknownValue&#8221; etc then change it back now:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">	<span style="color: #002200;">&#91;</span>self FauxBindObject<span style="color: #002200;">:</span>self valueName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;theString&quot;</span> toObject<span style="color: #002200;">:</span>theTextField valueName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;stringValue&quot;</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

</li>
<li>Modify the <code>receivedNotification:</code> method to the following:

<div class="wp_syntax"><div 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>receivedNotification<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>notification
<span style="color: #002200;">&#123;</span>
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Received a notification that an object changed&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>notification object<span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> originatingObject <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Key to fetch is: %@&quot;</span>, originatingValueName<span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// see if there is a method to fetch this value name</span>
		<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>getterMethodName <span style="color: #002200;">=</span> originatingValueName;
		<span style="color: #a61390;">id</span> fetchedValue <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
		NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Originating Method to test is: %@&quot;</span>, getterMethodName<span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>originatingObject respondsToSelector<span style="color: #002200;">:</span>NSSelectorFromString<span style="color: #002200;">&#40;</span>getterMethodName<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#123;</span>
			NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Originating Object responds to: %@&quot;</span>, getterMethodName<span style="color: #002200;">&#41;</span>;
			fetchedValue <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>originatingObject performSelector<span style="color: #002200;">:</span>NSSelectorFromString<span style="color: #002200;">&#40;</span>getterMethodName<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
			NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Fetched value for key is: %@&quot;</span>, fetchedValue<span style="color: #002200;">&#41;</span>;
		<span style="color: #002200;">&#125;</span>
		<span style="color: #a61390;">else</span>
		<span style="color: #002200;">&#123;</span>
			NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Originating Object is not KVC compliant for the key: %@&quot;</span>, originatingValueName<span style="color: #002200;">&#41;</span>;
			<span style="color: #a61390;">return</span>;
		<span style="color: #002200;">&#125;</span>
&nbsp;
&nbsp;
		NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;<span style="color: #2400d9;">\n</span>&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;Destination's Key to change is: %@&quot;</span>, destinationValueName<span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// see if there is a method to change this value name</span>
		<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>setterMethodNameFirstCharacter <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>destinationValueName substringToIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span>;
		<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>setterMethodName <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;set%@%@:&quot;</span>, <span style="color: #002200;">&#91;</span>setterMethodNameFirstCharacter capitalizedString<span style="color: #002200;">&#93;</span>, <span style="color: #002200;">&#91;</span>destinationValueName substringFromIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
		NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Destination Method to test is: %@&quot;</span>, setterMethodName<span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>destinationObject respondsToSelector<span style="color: #002200;">:</span>NSSelectorFromString<span style="color: #002200;">&#40;</span>setterMethodName<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#123;</span>
			NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Destination Object responds to: %@&quot;</span>, setterMethodName<span style="color: #002200;">&#41;</span>;
&nbsp;
			<span style="color: #002200;">&#91;</span>destinationObject performSelector<span style="color: #002200;">:</span>NSSelectorFromString<span style="color: #002200;">&#40;</span>setterMethodName<span style="color: #002200;">&#41;</span> withObject<span style="color: #002200;">:</span>fetchedValue<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#125;</span>
		<span style="color: #a61390;">else</span>
		<span style="color: #002200;">&#123;</span>
			NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Destination Object is not KVC complicant for the value: %@&quot;</span>, destinationValueName<span style="color: #002200;">&#41;</span>;
		<span style="color: #002200;">&#125;</span>
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
</ul>
<p>The additional code above now uses various <code>NSString</code> methods to get a suitable setter method name string by putting the word &#8220;set&#8221; on the front of the key name, which in turn has had its first letter capitalized.</p>
<p>If you run the application now, you&#8217;ll see the following:</p>
<pre>
2008-08-27 15:09:30.464 FauxBindings[2587:10b] Received a notification that an object changed
2008-08-27 15:09:30.467 FauxBindings[2587:10b] Key to fetch is: stringValue
2008-08-27 15:09:30.482 FauxBindings[2587:10b] Originating Method to test is: stringValue
2008-08-27 15:09:30.494 FauxBindings[2587:10b] Originating Object responds to: stringValue
2008-08-27 15:09:30.498 FauxBindings[2587:10b] Fetched value for key is: something
2008-08-27 15:09:30.499 FauxBindings[2587:10b]
2008-08-27 15:09:30.502 FauxBindings[2587:10b] Destination's Key to change is: theString
2008-08-27 15:09:30.503 FauxBindings[2587:10b] Destination Method to test is: setTheString:
2008-08-27 15:09:30.504 FauxBindings[2587:10b] Destination Object is not KVC complicant for the value: theString
</pre>
<p>The method is trying to set the value for the &#8216;theString&#8217; key but fails because our BindController object isn&#8217;t KVC compliant for the &#8216;theString&#8217; key &#8212; which is because we haven&#8217;t implemented a &#8217;setTheString&#8217; method in our BindController object. Let&#8217;s do that now:</p>
<ul>
<li>In BindController.h, add a method declaration for the &#8217;setTheString&#8217; method:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &lt;cocoa/Cocoa.h&gt;</span>
&nbsp;
&nbsp;
<span style="color: #a61390;">@interface</span> BindController <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span> <span style="color: #002200;">&#123;</span>
&nbsp;
	IBOutlet <span style="color: #400080;">NSTextField</span> <span style="color: #002200;">*</span>theTextField;
&nbsp;
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>theString;
&nbsp;
	<span style="color: #a61390;">id</span> originatingObject;
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>originatingValueName;
	<span style="color: #a61390;">id</span> destinationObject;
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>destinationValueName;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span>showTheStringInLog<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>sender;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>FauxBindObject<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>destObject valueName<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>destValueName toObject<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>origObject valueName<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>origValueName;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>receivedNotification<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>notification;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setTheString<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>value;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

</li>
<li>Implement this method in BindController.m with the following code:

<div class="wp_syntax"><div 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>setTheString<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>value
<span style="color: #002200;">&#123;</span>
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Setting theString to value: %@&quot;</span>, value<span style="color: #002200;">&#41;</span>;
	theString <span style="color: #002200;">=</span> value;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
</ul>
<p>Now when you run the application, press the &#8216;Log theString&#8217; button before doing anything else, then type something in the text box and press enter, then press the &#8216;Log theString&#8217; button again. If all has gone to plan, you&#8217;ll have seen the following in the Console:</p>
<pre>
2008-08-27 15:20:41.207 FauxBindings[2652:10b] Value of theString is: (null)
2008-08-27 15:20:44.022 FauxBindings[2652:10b] Received a notification that an object changed
2008-08-27 15:20:44.023 FauxBindings[2652:10b] Key to fetch is: stringValue
2008-08-27 15:20:44.034 FauxBindings[2652:10b] Originating Method to test is: stringValue
2008-08-27 15:20:44.040 FauxBindings[2652:10b] Originating Object responds to: stringValue
2008-08-27 15:20:44.045 FauxBindings[2652:10b] Fetched value for key is: something
2008-08-27 15:20:44.064 FauxBindings[2652:10b]
2008-08-27 15:20:44.070 FauxBindings[2652:10b] Destination's Key to change is: theString
2008-08-27 15:20:44.074 FauxBindings[2652:10b] Destination Method to test is: setTheString:
2008-08-27 15:20:44.085 FauxBindings[2652:10b] Destination Object responds to: setTheString:
2008-08-27 15:20:44.086 FauxBindings[2652:10b] Setting theString to value: something
2008-08-27 15:20:45.222 FauxBindings[2652:10b] Value of theString is: something
</pre>
<p>Finally, we have a working binding between our text field&#8217;s <code>stringValue</code> key and our BindController object&#8217;s <code>theString</code> key. When the text field posts the notification that its value has changed, the value of our <code>theString</code> key is updated to match the new <code>stringValue</code> value.</p>
<h3>Reversing the Process</h3>
<p>Let&#8217;s see if we can get this working the other way around. In other words, can we get our <code>theTextField</code> object to update its <code>stringValue</code> when our <code>BindController</code> object&#8217;s <code>theString</code> is changed?</p>
<ul>
<li>First of all, change the <code>awakeFromNib:</code> method so that the binding happens the other way around:

<div class="wp_syntax"><div 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>awakeFromNib
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>self FauxBindObject<span style="color: #002200;">:</span>theTextField valueName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;stringValue&quot;</span> toObject<span style="color: #002200;">:</span>self valueName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;theString&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>Next, we&#8217;ll change the user interface so that we can specify a value for theString and see what happens. Open up BindController.h and add another <code>IBOutlet</code> for an <code>NSTextField</code>. Also add a method to be called to set <code>theString</code> to the value of the text field:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &lt;cocoa/Cocoa.h&gt;</span>
&nbsp;
&nbsp;
<span style="color: #a61390;">@interface</span> BindController <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span> <span style="color: #002200;">&#123;</span>
&nbsp;
	IBOutlet <span style="color: #400080;">NSTextField</span> <span style="color: #002200;">*</span>theTextField;
	IBOutlet <span style="color: #400080;">NSTextField</span> <span style="color: #002200;">*</span>secondTextField;
&nbsp;
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>theString;
&nbsp;
	<span style="color: #a61390;">id</span> originatingObject;
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>originatingValueName;
	<span style="color: #a61390;">id</span> destinationObject;
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>destinationValueName;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span>showTheStringInLog<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>sender;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>FauxBindObject<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>destObject valueName<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>destValueName toObject<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>origObject valueName<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>origValueName;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>receivedNotification<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>notification;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setTheString<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>value;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span>modifyTheStringToValueOfTextField<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>sender;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

</li>
<li>Open MainMenu.nib in Interface Builder and add a second text field along with a second button, and connect these to the new outlet and action. My interface looks like this:<br />
<img src="http://www.timisted.net/blog/wp-content/uploads/2008/08/fauxbindingsmainwindow2.gif" alt="The revised window for Faux Bindings in Interface Builder" title="fauxbindingsmainwindow2" width="348" height="273" class="size-full wp-image-214" /></li>
<li>Return to Xcode and open BindController.m, adding in the implementation for the <code>modifyTheStringToValueOfTextField:</code> method:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span>modifyTheStringToValueOfTextField<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>sender
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>self setTheString<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>secondTextField stringValue<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
</ul>
<p>Notice that we set the value of &#8216;theString&#8217; using the KVC setter method and not directly. Why we do this will become clear in a minute&#8230;</p>
<p>If at this point you build and run the application, you&#8217;ll find that nothing happens at all when you enter text in the second field and click the button other than a console log that <code>theString</code> was set to a new value. Why doesn&#8217;t the supposedly-bound text field update to match the new value of <code>theString</code>? Because no notification is ever sent that its value changed.</p>
<ul>
<li>Change the <code>setTheString:</code> method to the following:

<div class="wp_syntax"><div 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>setTheString<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>value
<span style="color: #002200;">&#123;</span>
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Setting theString to value: %@&quot;</span>, value<span style="color: #002200;">&#41;</span>;
	theString <span style="color: #002200;">=</span> value;
	<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>NSControlTextDidEndEditingNotification object<span style="color: #002200;">:</span>self<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
</ul>
<p>Now, when you type something in the second text field (and press enter to commit it), then click the button, our notification method tries to obtain the new value of theString but complains that our BindController object is not KVC compliant for theString:</p>
<pre>
2008-08-27 15:48:19.487 FauxBindings[2869:10b] Setting theString to value: something
2008-08-27 15:48:19.494 FauxBindings[2869:10b] Received a notification that an object changed
2008-08-27 15:48:19.514 FauxBindings[2869:10b] Key to fetch is: theString
2008-08-27 15:48:19.521 FauxBindings[2869:10b] Originating Method to test is: theString
2008-08-27 15:48:19.523 FauxBindings[2869:10b] Originating Object is not KVC compliant for the key: theString
</pre>
<p> Why? Because there is no getter method defined in the BindController object.</p>
<ul>
<li>Open BindController.h and add a declaration for the getter method underneath its setter declaration:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>theString;</pre></div></div>

</li>
<li>Implement this method in BindController.m thus:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>theString
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> theString;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
</ul>
<p>This time when you run the application, you should find that everything functions as expected. Type something in the second field, press enter and then press the button. The first text field updates its content to match.</p>
<p>This is where the <strong>KVO</strong> comes from &#8212; standing for <strong>Key-Value-Observing</strong>. For an object to allow Observation of its Key Values, its setter methods must announce when they are changing a particular value so that notifications can be picked up and changes propagated.</p>
<h4>One more Check</h4>
<p>Just one final way to demonstrate that this functionality works is to modify the awakeFromNib: method to connect the two text fields directly.</p>
<ul>
<li>Modify <code>awakeFromNib:</code> to this:

<div class="wp_syntax"><div 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>awakeFromNib
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>self FauxBindObject<span style="color: #002200;">:</span>secondTextField valueName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;stringValue&quot;</span> toObject<span style="color: #002200;">:</span>theTextField valueName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;stringValue&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
</ul>
<p>This time, if you type something in the first text field and press enter, the second text field will change its value; note that there is no <code>BindController theString</code> value changing here, it&#8217;s only the two text fields. If you want the first text field to update when the second changes too, you would have to initiate that binding separately.</p>
<h3>Starting to Replace Code</h3>
<p>Assuming that an object is KVC-compliant (remember, that&#8217;s when it has suitably named accessors), you can access a named value (key) from that object by using the method <code>valueForKey:</code> and set it using <code>setValue:forKey:</code>. These methods perform all the necessary magic to find out if an object has KVC accessors and, assuming they do, call them to access the values.</p>
<ul>
<li>We can simplify our receivedNotification: method somewhat by changing it to the following:

<div class="wp_syntax"><div 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>receivedNotification<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>notification
<span style="color: #002200;">&#123;</span>
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Received a notification that an object changed&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>notification object<span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> originatingObject <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Key to fetch is: %@&quot;</span>, originatingValueName<span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #a61390;">id</span> fetchedValue <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>originatingObject valueForKey<span style="color: #002200;">:</span>originatingValueName<span style="color: #002200;">&#93;</span>;
&nbsp;
		NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Destination's Key to change is: %@&quot;</span>, destinationValueName<span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #002200;">&#91;</span>destinationObject setValue<span style="color: #002200;">:</span>fetchedValue forKey<span style="color: #002200;">:</span>destinationValueName<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
</ul>
<p>If you run using this code, you&#8217;ll see that everything happens as before, only we&#8217;re using the <code>valueForKey:</code> and <code>setValue:forKey:</code> methods instead of our previous logic.</p>
<h4>Proper KVO</h4>
<p>You&#8217;ll remember that earlier we &#8216;faked&#8217; an <code>NSControlTextDidEndEditing</code> notification to force our demonstration to work when our <code>theString</code> key was modified &#8212; whilst not being problematic in this particular demonstration, it&#8217;s not good practice to post notifications that aren&#8217;t legitimate. It would be better in this case to post a &#8220;theStringValueDidChange&#8221; notification that is more suitably named (and, if you did this, you&#8217;d obviously also need to change the notification registration code to specify the new notification type that the object was hoping to receive).</p>
<p>For information on naming conventions for notifications (and other things), see: <a href="http://developer.apple.com/documentation/Cocoa/Conceptual/CodingGuidelines/Articles/NamingIvarsAndTypes.html" target="_blank">Apple Developer Connection : Naming Instance Variables and Data Types</a>.</p>
<p>In a real bindings scenario, you use two methods <code>willChangeValueForKey:</code> and <code>didChangeValueForKey:</code> to tell the bindings mechanism that suitable notifications should be sent out. Our setTheString: method should really look like this:</p>

<div class="wp_syntax"><div 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>setTheString<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>value
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>self willChangeValueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;theString&quot;</span><span style="color: #002200;">&#93;</span>;
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Setting theString to value: %@&quot;</span>, value<span style="color: #002200;">&#41;</span>;
	theString <span style="color: #002200;">=</span> value;
	<span style="color: #002200;">&#91;</span>self didChangeValueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;theString&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Again, these methods are available to any subclass of NSObject.</p>
<p>When writing Core Data NSManagedObject subclasses, you&#8217;ll find that you also need to use <code>willAccessValueForKey:</code> and <code>didAccessValueForKey:</code> in your <strong>getter</strong> accessor methods &#8212; these inform the managed object that it needs to fetch the relevant data from the store if necessary.</p>
<h3>Real Bindings</h3>
<p>Now that we know what&#8217;s going on behind the scenes, let&#8217;s change our sample application to use real bindings rather than our faux bindings.</p>
<ul>
<li>First off, our BindController object now doesn&#8217;t need to do anything about notifications so we can remove the methods that handle them. We&#8217;ll also take out the second text field and button methods. Change the BindController.h file to the following:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &lt;cocoa/Cocoa.h&gt;</span>
&nbsp;
&nbsp;
<span style="color: #a61390;">@interface</span> BindController <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span> <span style="color: #002200;">&#123;</span>
&nbsp;
	IBOutlet <span style="color: #400080;">NSTextField</span> <span style="color: #002200;">*</span>theTextField;
&nbsp;
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>theString;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span>showTheStringInLog<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>sender;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setTheString<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>value;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>theString;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

</li>
<li>In Interface Builder, remove the second text field and button from the Window.</li>
<li>We also need to remove the unused methods from BindController.m and modify our <code>awakeFromNib:</code> method to use proper Cocoa Bindings. Change the file to the following:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;BindController.h&quot;</span>
&nbsp;
&nbsp;
<span style="color: #a61390;">@implementation</span> BindController
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span>showTheStringInLog<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>sender
<span style="color: #002200;">&#123;</span>
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Value of theString is: %@&quot;</span>, theString<span style="color: #002200;">&#41;</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>awakeFromNib
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>theTextField bind<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;value&quot;</span> toObject<span style="color: #002200;">:</span>self withKeyPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;theString&quot;</span> options<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><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>setTheString<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>value
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>self willChangeValueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;theString&quot;</span><span style="color: #002200;">&#93;</span>;
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Setting theString to value: %@&quot;</span>, value<span style="color: #002200;">&#41;</span>;
	theString <span style="color: #002200;">=</span> value;
	<span style="color: #002200;">&#91;</span>self didChangeValueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;theString&quot;</span><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: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>theString
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> theString;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

</li>
</ul>
<p>In the <code>awakeFromNib:</code> method, we bind the text field&#8217;s value to our <code>theString</code> key. All the magic is handled for us and the application behaves as expected &#8212; typing text in the text field and pressing enter will update <code>theString</code>.</p>
<p>To be able to do this the other way around using Cocoa Bindings (i.e. change the value of the text field when <code>theString</code> changes), you need to make a further modification.</p>
<ul>
<li>Change the BindController.m file to the following:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;BindController.h&quot;</span>
&nbsp;
&nbsp;
<span style="color: #a61390;">@implementation</span> BindController
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>initialize
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>self exposeBinding<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;theString&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span>showTheStringInLog<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>sender
<span style="color: #002200;">&#123;</span>
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Value of theString is: %@&quot;</span>, theString<span style="color: #002200;">&#41;</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>awakeFromNib
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>self bind<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;theString&quot;</span> toObject<span style="color: #002200;">:</span>theTextField withKeyPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;stringValue&quot;</span> options<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><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>setTheString<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>value
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>self willChangeValueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;theString&quot;</span><span style="color: #002200;">&#93;</span>;
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Setting theString to value: %@&quot;</span>, value<span style="color: #002200;">&#41;</span>;
	theString <span style="color: #002200;">=</span> value;
	<span style="color: #002200;">&#91;</span>self didChangeValueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;theString&quot;</span><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: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>theString
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> theString;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

</li>
</ul>
<p>You&#8217;ll notice that there is also now an &#8216;initialize&#8217; <strong>class method</strong>. If you&#8217;re not sure what this means, a class method is one that is called on a class rather than an instance of that class and is designated as such with a &#8216;+&#8217; in front of the method declaration rather than a &#8216;-&#8217;. The <code>+ (void)initialize</code> method can be called like this:</p>

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

<p>whereas <code>- (void)awakeFromNib</code>, for example, must be called on an instance of the object like this:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">	BindController <span style="color: #002200;">*</span>bcInstance <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>BindController alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>bcInstance awakeFromNib<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>(and yes, there&#8217;s a memory leak just in that little code snippet &#8212; &#8220;if you&#8217;re alloc-initing, you should be releasing&#8221; [or "auto-releasing"...])</p>
<p>The <code>+initialize</code> method is called before any instances of our object are used in code so that features of the class are known for the future; here we&#8217;re exposing the fact that our <code>BindController</code> object has a bindable key called &#8216;theString&#8217;.</p>
<p>In the <code>awakeFromNib:</code> method, we then actually bind this bindable key to the <code>stringValue</code> key on our text field.</p>
<p>If you build and run the application with the binding set up this way round, obviously you&#8217;ll find that changing the value of the text field doesn&#8217;t have any effect on our <code>theString</code> key. But, remember earlier in this article that when our <code>theString</code> attribute hadn&#8217;t yet been initialized to any value, the &#8216;Log theString&#8217; button would cause a &#8216;Value of theString is: (null)&#8217; to be displayed. This time, however, if you click the button, you&#8217;ll find that it outputs &#8216;Value of theString is: &#8216; with a blank string (@&#8221;" in object terms) as the value (and indeed, outputs this to the console when you first run the application). You might like to philosophize to yourself about where that comes from&#8230;</p>
<h3>Things to Note</h3>
<p>There are a few further points worth noting from all this:</p>
<ul>
<li>You could bind to the BindController &#8216;theString&#8217; key from another object rather than in the BindController&#8217;s <code>awakeFromNib:</code> method since its binding is &#8216;exposed&#8217;. For example:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">	<span style="color: #400080;">NSTextField</span> <span style="color: #002200;">*</span>someTextField <span style="color: #002200;">=</span> <span style="color: #11740a; font-style: italic;">//get a text field from somewhere</span>
	BindController <span style="color: #002200;">*</span>bcInstance <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>BindController alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>someTextField bind<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;stringValue&quot;</span> toObject<span style="color: #002200;">:</span>bcInstance withKeyPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;theString&quot;</span> options<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

</li>
<li>The name of the attribute that holds a value does not need to be the same as the key for that value. It is common practice to follow a different naming convention for attributes that are in the interface for the class rather than attributes allocated inside methods, one example being to put an underscore character on the front of class attributes. Storage of the value for our theString key could be done like this:

<div class="wp_syntax"><div 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>setTheString<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>value
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>self willChangeValueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;theString&quot;</span><span style="color: #002200;">&#93;</span>;
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Setting theString to value: %@&quot;</span>, value<span style="color: #002200;">&#41;</span>;
	_theString <span style="color: #002200;">=</span> value;
	<span style="color: #002200;">&#91;</span>self didChangeValueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;theString&quot;</span><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: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>theString
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> _theString;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>There are various other features in KVC and KVO that I haven&#8217;t discussed here such as if one key&#8217;s value depends on another key&#8217;s value, you can specify using another class method of the form <code>keyPathsForValuesAffecting...</code> such that if that depended-upon value changes, the Bindings mechanism will automatically notify objects bound to your dependent key of the change. Consider the following example where there is a second string key value exposed to which objects can bind called &#8220;dependentString&#8221;. It simply returns a modified version of theString so if an object is bound to that and theString changes, they need to be told so that they can request the new version of dependentString, all of which is accomplished quite simply, like this:

<div class="wp_syntax"><div 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>setTheString<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>value
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>self willChangeValueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;theString&quot;</span><span style="color: #002200;">&#93;</span>;
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Setting theString to value: %@&quot;</span>, value<span style="color: #002200;">&#41;</span>;
	_theString <span style="color: #002200;">=</span> value;
	<span style="color: #002200;">&#91;</span>self didChangeValueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;theString&quot;</span><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: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>theString
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> _theString;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>dependentString
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self theString<span style="color: #002200;">&#93;</span> stringByAppendingString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot; Plus Something Else&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
<span style="color: #11740a; font-style: italic;">// note dependentString has no setter</span>
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSSet</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>keyPathsForValuesAffectingDependentString
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSSet</span> setWithObject<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;theString&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>There are also various validation methods that, if they exist, can be called automatically to make sure that a value is suitable before allowing it to be set for a particular key. See <a href="http://developer.apple.com/documentation/Cocoa/Conceptual/KeyValueCoding/Concepts/Validation.html" target="_blank">Apple Developer Connection : KVC Key-Value Validation</a> for more details but here is a small example:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>validateTheString<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>valueRef error<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSError</span> <span style="color: #002200;">**</span><span style="color: #002200;">&#41;</span>outError
<span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// Insert custom validation logic here.</span>
    <span style="color: #a61390;">return</span> <span style="color: #a61390;">YES</span>; <span style="color: #11740a; font-style: italic;">// YES if is valid, NO otherwise</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>It&#8217;s worth noting that if an object doesn&#8217;t appear to be KVC-compliant for a particular value, the system will call the &#8216;valueForUndefinedKey:&#8217; method on an object which, on NSObject, raises an exception (and thus halts program execution). You can override this method, if you wish, to prevent exceptions being raised and to provide some sort of &#8216;default&#8217; value if such a thing is meaningful.</li>
<li>There is also a very useful &#8216;dictionaryWithValuesForKeys:&#8217; method which will build an NSDictionary containing the specified keys and their respective values &#8212; useful if you need to access, persist or copy several values and don&#8217;t want to call &#8216;valueForKey:&#8217; etc on each one. The equally useful setter equivalent of this is &#8217;setValuesForKeysWithDictionary:&#8217; which does exactly what it sounds like.</li>
</ul>
<h3>The End</h3>
<p>I hope that this article has been of some use and/or interest if you&#8217;ve made it this far. Obviously I haven&#8217;t talked here about KVC compliance for accessing arrays, sets and scalar values etc but these aren&#8217;t at all complicated, provided the fundamental ideas are understood. It is also worth thinking about whether accessor methods should retain and release their values under traditional memory management.</p>
<p>Finally, since most accessor methods follow standard formats (i.e. broadcast that you&#8217;re changing a value, change the value, broadcast that you&#8217;ve changed the value) it&#8217;s possible to automate the process of accessor creation, most notably using Objective-C 2.0 properties. Using @property and @synthesize are both powerful features of the language for all sorts of reasons although many developers remain unconvinced of their usefulness. Using Objective-C properties offers faster (in terms of processing cycles) access to an object&#8217;s keys than either using traditional accessors, or (even slower) using valueForKey: etc. But, because of the need for terms used to specify how a @property is @synthesized, it is easy to pick the wrong words (e.g. atomic, retain, copy, readwrite etc) and not be able to work out why a particular KVC procedure or Bindings technique is crashing. Use of Objective-C 2.0 code also means your application won&#8217;t run on anything earlier than Mac OS X 10.5 Leopard.</p>
<p>Should you be interested in reading more about views on Objective-C 2.0 properties, take a look at: <a href="http://cocoawithlove.com/2008/08/in-defense-of-objective-c-20-properties.html" target="_blank">Matt Gallagher &#8212; In defense of Objective-C 2.0 Properties</a>.</p>
<p>This post contains links to various other posts that attack properties, and also clarifies their purpose, notably, &#8220;<em>not</em> to provide auto-generated getter and setter methods&#8221; (although this is certainly one <em>side effect</em> of their use!).</p>
<p>As always, all comments are welcomed either as Blog post comments below or by email.</p>
<p>Happy Binding!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.timisted.net/blog/archive/on-cocoa-bindings/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Core Data Migration and Versioning</title>
		<link>http://www.timisted.net/blog/archive/core-data-migration/</link>
		<comments>http://www.timisted.net/blog/archive/core-data-migration/#comments</comments>
		<pubDate>Mon, 28 Jul 2008 12:45:26 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Core Data]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Apple]]></category>
		<category><![CDATA[Bindings]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Framework]]></category>
		<category><![CDATA[Interface Builder]]></category>
		<category><![CDATA[Xcode]]></category>

		<guid isPermaLink="false">http://www.timisted.net/blog/?p=181</guid>
		<description><![CDATA[By default, a Core Data application will refuse to load data created using an older data model. So, if you&#8217;ve been using one version of your model for ages and have loads of data, it&#8217;s a bit of a pain that should you wish to add an entity, attribute or relationship or make other changes [...]]]></description>
			<content:encoded><![CDATA[<p>By default, a Core Data application will refuse to load data created using an older data model. So, if you&#8217;ve been using one version of your model for ages and have loads of data, it&#8217;s a bit of a pain that should you wish to add an entity, attribute or relationship or make other changes to your model, you won&#8217;t be able to open the old data in a new version of your application.</p>
<p>Prior to OS X 10.5 if you wanted to convert data from one model version to another, you had to handle the conversion code entirely yourself. With Leopard, however, Apple built in versioning and migration capabilities to make dealing with data migration a great deal easier.</p>
<p>This article looks at how you can support data migration in your Core Data applications. It shows how to handle changes entirely using Data Model versions and Mapping Models, before looking at customizing the process by using custom migration classes.<br />
<span id="more-181"></span></p>
<h3>Data Migration Concepts</h3>
<p>When making a change to a data model in Xcode, it&#8217;s tempting just to open up the existing model, make that change and save the file. In order to take advantage of versioning, however, you need to have saved the previous version into your project by using a bundle for the managed object models.</p>
<p>A managed object model bundle allows your project to store multiple versions of the same data model, specifying which is the current version. It also allows storage of mapping models; a mapping model translates the old version of the model to the new version by specifying which source entities map to which destination entities, similarly which attributes match and also relationships. It even allows you to specify custom mapping objects that let you run code to fill out information that doesn&#8217;t just map straight across.</p>
<p>If you&#8217;ve enabled mapping in your application and have the right object and mapping models available, Core Data will handle the conversion transparently to the user when opening pre-existing data stores. A version hash is maintained with each entity; upon opening a persistent store, this version hash is checked against that in the current data model version. If it doesn&#8217;t match, previous models are checked until a suitable model is found, along with the mapping to convert the data.</p>
<h3>Enabling Data Migration</h3>
<p>In order to take advantage of the migration capabilities, you need to change the code that configures the persistent store so that it enables automatic migration. In a Core Data non-document-based application, you typically change the method that returns the persistent store coordinator such that it enables versioning when the coordinator is created by specifying <code>NSMigratePersistentStoresAutomaticallyOption</code> in the options for the <code>addPersistentStoreWithType:</code> method. In a document-based application, the <code>NSPersistentDocument</code> class offers a method <code>configurePersistentStoreCoordinatorForURL:</code> that you can override to set the <code>NSMigratePersistentStoresAutomaticallyOption</code>.</p>
<p>There&#8217;s some great sample code in an article written by Marcus Zarra for InformIT available at:<br />
<a href="http://www.informit.com/articles/article.aspx?p=1178181&#038;seqNum=7" target="_blank">http://www.informit.com/articles/article.aspx?p=1178181&#038;seqNum=7</a></p>
<p>Marcus demonstrates enabling migration in both document-based and non-document-based Core Data applications. For our demo application, though, we&#8217;ll use a document-based application so that it&#8217;s easy to make copies of different data versions for the migration demonstration. ***Note that if you use his code for a non-document-based app, there&#8217;s a small error in the <code>persistentStoreCoordinator:</code> method that the <code>optionsDictionary</code> mutable dictionary is created but not passed to the relevant <code>addPersistentStoreWithType:</code> method (<code>nil</code> is passed instead) meaning that migration is never enabled. Be sure to replace <code>options:nil</code> with <code>options:optionsDictionary</code>.***</p>
<h3>The Demo Project</h3>
<p>We&#8217;ll start by writing a simple Core Data application with a standard data model. We&#8217;ll then create a new version of the model and a mapping model to handle migration of our old data. Finally we&#8217;ll change the new version of the model such that it requires some code to migrate the old data; we&#8217;ll write two different versions of a subclass of <code>NSEntityMigrationPolicy</code> demonstrating two different ways to handle the migration.</p>
<h4>The Basic Core Data Application</h4>
<p>The basic application maintains a list of people, storing their full name along with some notes on each person:</p>
<ul>
<li>Begin by creating a Core Data document-based application in Xcode.</li>
<li>Open up the data model and create a &#8216;Person&#8217; entity with a &#8216;fullName&#8217; string attribute and a &#8216;notes&#8217; string attribute.</li>
<li>Open MyDocument.nib in Interface Builder and add an <code>NSArrayController</code> called &#8216;People&#8217;. Set its Mode to &#8216;Entity&#8217; and Entity Name to &#8216;Person&#8217;. Tick the &#8216;Prepares Content&#8217; checkbox. Bind its Managed Object Context to the File&#8217;s Owner &#8216;managedObjectContext&#8217;.</li>
<li>Make the interface look something like this:<br />
<img src="http://www.timisted.net/blog/wp-content/uploads/2008/07/windowlayout.gif" alt="The Interface Builder layout of the document window for the example application" title="windowlayout" width="500" height="308" class="size-full wp-image-184" /><br />
Here, I&#8217;ve used an <code>NSTableView</code> for the list of people with its one column bound to the fullName key, along with buttons to add and remove new people; there is a text field bound to the selected person&#8217;s full name and a text view (rich text disabled) for the notes.</li>
<li>Build and run the application to make sure it works as it should. <strong>Be sure to save a sample data file</strong> containing several people so that we have something to reopen later in the tutorial.</li>
</ul>
<h4>Enabling Migration for the Application</h4>
<p>To enable migration for a document-based application, you need to override the method <code>configurePersistentStoreCoordinatorForURL:</code> so that it enables the migration option:</p>
<ul>
<li>Open MyDocument.m and add the following method which checks to see whether any options are currently set; if not, it creates a mutable <code>newOptions</code> dictionary and enables the <code>NSMigratePersistentStoresAutomaticallyOption</code>. Otherwise it copies the existing dictionary to gain a mutable version before enabling the option and calling the overridden <code>configurePersistentStoreCoordinatorForURL:</code> method, supplying the new dictionary:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>configurePersistentStoreCoordinatorForURL<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSURL</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>url ofType<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>fileType modelConfiguration<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>configuration storeOptions<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>storeOptions error<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSError</span> <span style="color: #002200;">**</span><span style="color: #002200;">&#41;</span>error
<span style="color: #002200;">&#123;</span>
	<span style="color: #400080;">NSMutableDictionary</span> <span style="color: #002200;">*</span>newOptions;
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> storeOptions <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		newOptions <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>storeOptions mutableCopy<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
	<span style="color: #a61390;">else</span>
	<span style="color: #002200;">&#123;</span>
		newOptions <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableDictionary</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #002200;">&#91;</span>newOptions setObject<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithBool<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span> forKey<span style="color: #002200;">:</span>NSMigratePersistentStoresAutomaticallyOption<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">BOOL</span> result <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super configurePersistentStoreCoordinatorForURL<span style="color: #002200;">:</span>url ofType<span style="color: #002200;">:</span>fileType modelConfiguration<span style="color: #002200;">:</span>configuration storeOptions<span style="color: #002200;">:</span>newOptions error<span style="color: #002200;">:</span>error<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>newOptions release<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">return</span> result;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>Build and run the project to make sure everything is still ok. Reopen the data file you created before &#8212; hopefully nothing has changed visibly as all we&#8217;ve done is enable versioning.</li>
</ul>
<h4>Adding a new Data Model version</h4>
<p>We&#8217;ll now add a new version of the data model by using Data Model Bundles:</p>
<ul>
<li>In your Xcode project browser, click once on the MyDocument.xcdatamodel file to select it.</li>
<li>Choose the &#8216;Design -> Data Model -> Add Model Version&#8217; menu command and you&#8217;ll see that the MyDocument.xcdatamodel becomes MyDocument.xcdatamodel<strong>d</strong> with a little triangle to expand its contents.</li>
<li>Expand the bundle and rename the MyDocument 2.xcdatamodel file to &#8216;MyDocument1.0.xcdatamodel&#8217; to distinguish between the two data models. You&#8217;ll also see that MyDocument.xcdatamodel has a little green tick in its icon to tell you that this is the current model verison.</li>
<li>Open up the current model MyDocument.xcdatamodel and add a &#8217;salary&#8217; decimal attribute to the Person entity.</li>
<li>Back in the project browser, right click on the data model bundle and choose &#8216;Add -> New File&#8230;&#8217;. Choose the &#8216;Mapping Model&#8217; template and call the file &#8216;MyDocument1.0.xcmappingmodel&#8217;. You&#8217;ll then be asked to choose Source and Destination models; select the MyDocument1.0.xcdatamodel file and choose &#8216;Set Source Model&#8217;; select the MyDocument.xcdatamodel and choose &#8216;Set Destination Model&#8217;.</li>
<li>When you click Finish, Xcode will generate a Mapping Model to convert the source Person entity to the destination Person entity. It will also try to map attributes for you. If you open the generated mapping model, you&#8217;ll see that it has filled out the &#8216;fullName&#8217; and &#8216;notes&#8217; property mappings. As it can&#8217;t find a previous &#8217;salary&#8217; attribute, the value expression for that property mapping is blank. If you wish, enter &#8216;0&#8242; for the value expression or just leave it blank.</li>
<li>Open the MyDocument.nib file in Interface Builder and change it so there is also a bound Salary text field. Drag a suitable NSNumberFormatter onto the field:<br />
<img src="http://www.timisted.net/blog/wp-content/uploads/2008/07/windowlayout2.gif" alt="Interface Builder layout for the window showing the new salary text field" title="windowlayout2" width="500" height="309" class="size-full wp-image-187" /></li>
<li>Before proceeding further, use the Finder to find the data file that you created earlier and duplicate it so that we have a copy to use later on.</li>
<li>Build and run the application and try to open your original data file. You&#8217;ll find that it opens just fine with the fullName and notes fields containing the data they had before; the salary field will either be blank or set at 0 depending on whether you entered the value expression in the property mapping earlier.<br />
If you run into any problems, make sure the target tickbox in Xcode is set for the current MyDocument.xcdatamodel but not the other. Otherwise you&#8217;ll get a logged problem that your application can&#8217;t merge models with two different entities named &#8216;Person&#8217;.</li>
</ul>
<h4>Customizing the Migration Process</h4>
<p>Now that we&#8217;ve seen how easy it is to migrate data after adding a new attribute, we&#8217;ll look at how to customize this behaviour by splitting the old fullName attribute into a firstName and a lastName attribute using a custom entity migration policy. There are several ways to code this functionality and we&#8217;ll cover two; the first will copy attributes manually from an old entity instance to a new one whilst the second will use all the available information in the mapping model having changed the expression value for the firstName and lastName attributes at runtime.</p>
<h5>Manual copying of data</h5>
<ul>
<li>Firstly, we&#8217;ll change the current MyDocument.xcdatamodel file so that it has two fields for a person&#8217;s name. We won&#8217;t bother for this tutorial creating a new model version &#8212; typically in a release application, you wouldn&#8217;t worry about versions that you create during development, only the ones you release to the user. Rename the fullName attribute &#8216;firstName&#8217; and add a &#8216;lastName&#8217; string attribute to the Person entity.</li>
<li>Open MyDocument.nib in Interface Builder and change the interface so that the table view shows two columns (bound to &#8216;firstName&#8217; and &#8216;lastName&#8217;), change the fullName text field to bind to &#8216;firstName&#8217; and add a second bound to &#8216;lastName&#8217;.</li>
<li>Back in Xcode, delete the existing MyDocument1.0.xcmappingmodel and create a new one in the same way as before. You&#8217;ll see that Xcode has left the firstName and lastName properties blank as it can&#8217;t determine how to make the right mapping.</li>
<li>Create a new Objective-C class in your Xcode project called &#8216;PersonMigrationClass&#8217;.</li>
<li>In the header file for that class change the inheritance so that we subclass <code>NSEntityMigrationPolicy</code>.</li>
<li>Implement the following method which will get called during the migration process. It migrates the Person entity by creating a new managed object in the destination managed object context before manually copying the source entity instance&#8217;s attributes into the destination entity&#8217;s attributes. Note that at the end of the method we call <code>associateSourceInstance: withDestinationInstance:</code> so that the internal magic can happen ready for subsequent migration steps that set up relationships etc:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>createDestinationInstancesForSourceInstance<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSManagedObject</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>sInstance entityMapping<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSEntityMapping</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>mapping manager<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSMigrationManager</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>manager error<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSError</span> <span style="color: #002200;">**</span><span style="color: #002200;">&#41;</span>error
<span style="color: #002200;">&#123;</span>
	<span style="color: #400080;">NSManagedObject</span> <span style="color: #002200;">*</span>newObject <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSEntityDescription</span> insertNewObjectForEntityForName<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>mapping destinationEntityName<span style="color: #002200;">&#93;</span> inManagedObjectContext<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>manager destinationContext<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>nameStrings <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>sInstance valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;fullName&quot;</span><span style="color: #002200;">&#93;</span> componentsSeparatedByString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot; &quot;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>newObject setValue<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>nameStrings objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span> forKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;firstName&quot;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>newObject setValue<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>nameStrings objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span> forKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;lastName&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>notes <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>sInstance valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;notes&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>newObject setValue<span style="color: #002200;">:</span>notes forKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;notes&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>manager associateSourceInstance<span style="color: #002200;">:</span>sInstance withDestinationInstance<span style="color: #002200;">:</span>newObject forEntityMapping<span style="color: #002200;">:</span>mapping<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">return</span> <span style="color: #a61390;">YES</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>Back in the mapping model, click on the PersonToPerson entity mapping and on the right of the window set the &#8216;Custom Policy&#8217; to &#8216;PersonMigrationClass&#8217;. You&#8217;ll see that the type of mapping changes to &#8216;Custom&#8217;.</li>
<li>Again, make a copy of the very first data file that you created so that we can use it later. Get rid of the more recent version.</li>
<li>Build and run the application and open your original data file. Hopefully, you&#8217;ll find that the first and last name fields are correctly filled out.</li>
</ul>
<h5>Another way to copy objects</h5>
<p>It&#8217;s annoying in the previous version that we have to copy across manually the &#8216;notes&#8217; attribute when we&#8217;ve already setup that attribute in the mapping model. Here we&#8217;ll implement a method that maps the entity using the mapping model&#8217;s attribute mapping, changing only the value expression for the firstName and lastName attributes. We&#8217;ll also define a &#8217;sourceVersion&#8217; user info tag so that we can use the same class to handle any future versions of the data model changes:</p>
<ul>
<li>Open up the MyDocument1.0.xcmappingmodel and click on the PersonToPerson entity mapping. In the far right of the window, click the &#8216;User Info&#8217; tab and add a key called &#8217;sourceVersion&#8217; with value &#8216;1.0&#8242;:<br />
<img src="http://www.timisted.net/blog/wp-content/uploads/2008/07/mappingmodel.gif" alt="The Xcode mapping modeler showing the User Info tab" title="mappingmodel" width="500" height="354" class="size-full wp-image-189" /></li>
<li>Change the implementation in our <code>PersonMigrationClass</code> to the following; this code checks the user info &#8217;sourceVersion&#8217; value and, assuming it is &#8216;1.0&#8242; cycles through the provided attributeMappings array and sets only the &#8216;firstName&#8217; and &#8216;lastName&#8217; mappings. The overridden method is then called to handle all other mapping for us:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>createDestinationInstancesForSourceInstance<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSManagedObject</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>sInstance entityMapping<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSEntityMapping</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>mapping manager<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSMigrationManager</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>manager error<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSError</span> <span style="color: #002200;">**</span><span style="color: #002200;">&#41;</span>error
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">float</span> sourceVersion <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>mapping userInfo<span style="color: #002200;">&#93;</span> valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;sourceVersion&quot;</span><span style="color: #002200;">&#93;</span> floatValue<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> sourceVersion <span style="color: #002200;">==</span> <span style="color: #2400d9;">1.0</span> <span style="color: #002200;">&#41;</span> <span style="color: #11740a; font-style: italic;">// need to convert fullName into firstName and lastName strings</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>nameStrings <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>sInstance valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;fullName&quot;</span><span style="color: #002200;">&#93;</span> componentsSeparatedByString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot; &quot;</span><span style="color: #002200;">&#93;</span>;
		<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>firstName <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>nameStrings objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span>;
		<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>lastName <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>nameStrings objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>attributeMappings <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>mapping attributeMappings<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #a61390;">int</span> count;
		<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> count <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; count &lt; <span style="color: #002200;">&#91;</span>attributeMappings count<span style="color: #002200;">&#93;</span>; count <span style="color: #002200;">++</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#123;</span>
			<span style="color: #400080;">NSPropertyMapping</span> <span style="color: #002200;">*</span>currentMapping <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>attributeMappings objectAtIndex<span style="color: #002200;">:</span>count<span style="color: #002200;">&#93;</span>;
&nbsp;
			<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>currentMapping name<span style="color: #002200;">&#93;</span> isEqualToString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;firstName&quot;</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>
				<span style="color: #002200;">&#91;</span>currentMapping setValueExpression<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSExpression</span> expressionForConstantValue<span style="color: #002200;">:</span>firstName<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
			<span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>currentMapping name<span style="color: #002200;">&#93;</span> isEqualToString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;lastName&quot;</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>
				<span style="color: #002200;">&#91;</span>currentMapping setValueExpression<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSExpression</span> expressionForConstantValue<span style="color: #002200;">:</span>lastName<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#125;</span>
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>super createDestinationInstancesForSourceInstance<span style="color: #002200;">:</span>sInstance entityMapping<span style="color: #002200;">:</span>mapping manager<span style="color: #002200;">:</span>manager error<span style="color: #002200;">:</span>error<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>If you build and run the application and then open your original data file, it will again open with the information correctly populated but this time by using the mapping model value expressions for everything other than the two mappings we needed to customize.</li>
</ul>
<h3>Some Considerations</h3>
<p>Notice that whilst both the above customization methods work as desired, they are potentially quite expensive in terms of processing. On a large dataset with many entities all using custom mapping in this way, the conversion could take quite a long time. There are also lots of objects floating around in memory that wouldn&#8217;t get deallocated until the next cycle through the event loop. You might consider customizing the migration process further by splitting it into multiple passes and using memory allocation pools.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.timisted.net/blog/archive/core-data-migration/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Core Data and Sync Services</title>
		<link>http://www.timisted.net/blog/archive/core-data-and-sync-services/</link>
		<comments>http://www.timisted.net/blog/archive/core-data-and-sync-services/#comments</comments>
		<pubDate>Sat, 26 Jul 2008 10:44:51 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Core Data]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Apple]]></category>
		<category><![CDATA[Bindings]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Framework]]></category>
		<category><![CDATA[Interface Builder]]></category>
		<category><![CDATA[Sync Services]]></category>
		<category><![CDATA[Xcode]]></category>

		<guid isPermaLink="false">http://www.timisted.net/blog/?p=123</guid>
		<description><![CDATA[Apple&#8217;s Sync Services framework allows applications and devices to keep in sync with each other&#8217;s data. If you subscribe to the MobileMe service, for example, it is Sync Services that handles synchronization between the server, your computers and any connected devices such as an iPhone. This article discusses adding synchronization capabilities to Core Data applications. [...]]]></description>
			<content:encoded><![CDATA[<p>Apple&#8217;s Sync Services framework allows applications and devices to keep in sync with each other&#8217;s data. If you subscribe to the MobileMe service, for example, it is Sync Services that handles synchronization between the server, your computers and any connected devices such as an iPhone. This article discusses adding synchronization capabilities to Core Data applications. We&#8217;ll start by looking at the information you must provide to allow synchronization to take place and then build two sample applications that will keep in sync with each other.</p>
<p><span id="more-123"></span><img src="http://www.timisted.net/blog/wp-content/uploads/2008/07/twoapplicationsrunning.gif" alt="The two applications running showing the same data displayed in different ways" title="twoapplicationsrunning" width="500" height="486" class="size-full wp-image-173" /><br />
Each window is from one application, and each keeps its own copy of the data; any change made and saved to one will be communicated to the other.</p>
<p>Sample project downloads are available at the end of this article.</p>
<h3>The Synchonizing Parts</h3>
<p>For an application to be able to synchronize via Sync Services, it needs to provide various bits of information. In the first place, there needs to be a registered Sync Schema that defines the structure of the data available to synchronize. Secondly, each application needs to provide and register a Client Description that defines which of the available data in the sync schema the application will use. Then, of course, there&#8217;s the necessary code to handle the synchronization itself.</p>
<h4>Synchronization with Core Data</h4>
<p>The Sync Schema lists the model objects available to be synchronized. Apple&#8217;s Bookmarks schema, for example, lists two entities &#8212; the Bookmark entity and the Folder entity, each with attributes for names, urls and parent relationships etc.</p>
<p>Traditionally, a sync schema is created from scratch with entries for the different entities and their attributes, entries for relationships and inverse relationships between entities, entries to allow different types of synchronization, and a whole lot more. Writing such a schema can be a bit daunting; luckily, creating a schema under Core Data is a great deal easier. Provided you define your data model with the Data Modeling Tool in Xcode, you can have most of your schema property list created for you automatically without the need to hand-code all the entities, attributes and relationships.</p>
<p>The Client Description, on the other hand, won&#8217;t be written for you, but this is a relatively straightforward property list simply telling Sync Services which entities and attributes your application wishes to synchronize.</p>
<p>Writing code to handle the synchronization typically includes methods that handle pushing and pulling data. You must invoke various methods to start the synchronization process, register your sync schema and client description, deal with changes made in your application and inform Sync Services about these changes, and then handle any changes made by other applications. When using Core Data to handle your underlying data, most of this is handled for you in the <code>NSPersistentStoreCoordinator</code>.</p>
<h3>Demo Application 1</h3>
<p>Our first project is a version of the ubiquitous Departments and Employees application, this time using a non-document-based application. We&#8217;ll start by building a standard Cocoa Core Data application using Xcode. Only after the application is working will we then start adding synchronizing functionality &#8212; this way, it&#8217;s obvious how easy it is to add synchronizing capabilities to existing applications.</p>
<h4>The Basic Project</h4>
<ul>
<li>Begin by creating a Cocoa Core Data Application (don&#8217;t use the Document-based template) and call it &#8216;DepartmentsAndEmployeesSyncApp&#8217;.</li>
<li>In the Xcode Data Modeler, create a &#8216;Department&#8217; entity with a &#8216;name&#8217; string attribute. Create a &#8216;Person&#8217; entity with &#8216;firstName&#8217; and &#8216;lastName&#8217; string attributes, and a &#8217;salary&#8217; decimal attribute. In the Department entity create an &#8216;employees&#8217; to-many relationship to the Person entity, and create its inverse &#8216;department&#8217; to-one relationship between Person and Department.</li>
<li>Open MainMenu.nib in Interface Builder and create an <code>NSArrayController</code> called &#8216;Departments&#8217;. Set its Mode to &#8216;Entity&#8217; and Entity Name to &#8216;Department&#8217;. Check the &#8216;Prepares Content&#8217; box. Bind its Managed Object Context to the App Delegate object&#8217;s &#8216;managedObjectContext&#8217;.</li>
<li>Create a second <code>NSArrayController</code> called &#8216;People&#8217; for the Person entity that also Prepares Content. Bind its Managed Object Context to the App Delegate&#8217;s managedObjectContext and its Content Set to the Department controller&#8217;s &#8217;selection&#8217; &#8216;employees&#8217;.</li>
<li>For the user interface, add to the nib file&#8217;s Window layout a one-column <code>NSTableView</code> that displays the departments, binding its column Value to the Departments array controller &#8216;arrangedObjects&#8217; &#8216;name&#8217; key.</li>
<li>Add a second NSTableView, this time with three columns, bound to the People array controller &#8216;firstName&#8217;, &#8216;lastName&#8217; and &#8217;salary&#8217; keys respectively. Drag an NSNumberFormatter onto the salary column.</li>
<li>Finally add &#8216;+&#8217; and &#8216;-&#8217; buttons for both table views that connect to the relevant <code>add:</code> and <code>remove:</code> array controller actions.</li>
<li>Build and run your application to make sure everything works as it should. Mine looks like this:<br />
<img src="http://www.timisted.net/blog/wp-content/uploads/2008/07/firstapprunning.gif" alt="The first application in use" title="firstapprunning" width="500" height="303" class="size-full wp-image-133" /></li>
</ul>
<p>Nothing particularly impressive here &#8212; just the construction of the basic project ready for synchronization implementation. Obviously, we&#8217;re using a standard Core Data application rather than a document-based app so that we have only a single known set of data when we come to start synching. Now we&#8217;ll move on to define the synch schema.</p>
<h4>The Sync Schema</h4>
<ul>
<li>In Xcode, create another new project. Use the &#8216;Sync Schema&#8217; template, which for me is listed under &#8216;Standard Apple Plug-ins&#8217;. Call this project &#8216;DemoEmploymentData&#8217;.</li>
<li>The resultant project contains various files, the most important of which is the &#8216;Schema.plist&#8217; file. Open this file and you&#8217;ll find a demo schema already in there. We can remove most of this as our Entities information will be generated for us from our data model in the DepartmentsAndEmployeesSyncApp project.</li>
<li>Delete from the opening Entities key down to the closing of the array for that key. You should be left with a DataClasses key and a Name key.</li>
<li>Under the DataClasses key we define names for the collections of data we are going to be sharing. Since we&#8217;re only sharing one type of information (employment data) we will define a single data class. Replace the existing &#8216;com.mycompany.myschema.dataclass&#8217; string with &#8216;com.mycompany.DemoEmploymentData&#8217;. Sync Services uses reverse DNS syntax to keep track of its information. Note that this key has to be unique across all schemas.</li>
<li>There is another key under DataClasses called &#8216;ImagePath&#8217;. This might be called &#8216;Image&#8217; in your schema; if it is, rename it to &#8216;ImagePath&#8217; to avoid errors later. This path is used to display an icon representing the data class; this might be used, for example, if it&#8217;s displayed in the MobileMe System Preferences tab. For this demonstration, we&#8217;ll leave it as it is with the standard &#8216;SyncDataClass.tiff&#8217; image.</li>
<li>Change the string for the &#8216;Name&#8217; key to be &#8216;com.mycompany.DemoEmploymentDataSchema&#8217;.</li>
<li>To make use of the automatic generation functionality, define a new key called &#8216;ManagedObjectModels&#8217; with an array containing a string pointing to our data model. This is a relative path; assuming you&#8217;re going to follow this tutorial through, enter it as:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">&lt;key&gt;ManagedObjectModels&lt;<span style="color: #002200;">/</span>key&gt;
&lt;array&gt;
	&lt;string&gt;..<span style="color: #002200;">/</span>..<span style="color: #002200;">/</span>..<span style="color: #002200;">/</span>DepartmentsAndEmployeesSyncApp_DataModel.mom&lt;<span style="color: #002200;">/</span>string&gt;
&lt;<span style="color: #002200;">/</span>array&gt;</pre></div></div>

</li>
<li>If you wish, open the Schema.strings file and enter localization information for the schema keys.</li>
</ul>
<p>The complete schema.plist file should look something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">&lt;?xml version<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;1.0&quot;</span> encoding<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;ASCII&quot;</span>?&gt;
&lt;<span style="color: #002200;">!</span>DOCTYPE plist PUBLIC <span style="color: #bf1d1a;">&quot;-//Apple Computer//DTD PLIST 1.0//EN&quot;</span> <span style="color: #bf1d1a;">&quot;http://www.apple.com/DTDs/PropertyList-1.0.dtd&quot;</span>&gt;
&nbsp;
&lt;<span style="color: #002200;">!--</span> Schema.plist <span style="color: #002200;">--</span>&gt;
&lt;<span style="color: #002200;">!--</span> EmployeeData <span style="color: #002200;">--</span>&gt;
&lt;<span style="color: #002200;">!--</span> Created by Tim Isted on <span style="color: #2400d9;">25</span><span style="color: #002200;">/</span>07<span style="color: #002200;">/</span>2008. <span style="color: #002200;">--</span>&gt;
&lt;<span style="color: #002200;">!--</span> Copyright __MyCompanyName__ 2008. All rights reserved. <span style="color: #002200;">--</span>&gt;
&nbsp;
&lt;plist version<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;1.0&quot;</span>&gt;
&lt;dict&gt;
	&lt;key&gt;DataClasses&lt;<span style="color: #002200;">/</span>key&gt;
	&lt;array&gt;
		&lt;dict&gt;
			&lt;key&gt;Name&lt;<span style="color: #002200;">/</span>key&gt;
			&lt;string&gt;com.mycompany.DemoEmploymentData&lt;<span style="color: #002200;">/</span>string&gt;
			&lt;key&gt;ImagePath&lt;<span style="color: #002200;">/</span>key&gt;
			&lt;string&gt;SyncDataClass.tiff&lt;<span style="color: #002200;">/</span>string&gt;
		&lt;<span style="color: #002200;">/</span>dict&gt;
	&lt;<span style="color: #002200;">/</span>array&gt;
&nbsp;
	&lt;key&gt;ManagedObjectModels&lt;<span style="color: #002200;">/</span>key&gt;
	&lt;array&gt;
		&lt;string&gt;..<span style="color: #002200;">/</span>..<span style="color: #002200;">/</span>..<span style="color: #002200;">/</span>DepartmentsAndEmployeesSyncApp_DataModel.mom&lt;<span style="color: #002200;">/</span>string&gt;
	&lt;<span style="color: #002200;">/</span>array&gt;
&nbsp;
	&lt;key&gt;Name&lt;<span style="color: #002200;">/</span>key&gt;
	&lt;string&gt;com.mycompany.DemoEmploymentDataSchema&lt;<span style="color: #002200;">/</span>string&gt;
&lt;<span style="color: #002200;">/</span>dict&gt;
&lt;<span style="color: #002200;">/</span>plist&gt;</pre></div></div>

<p>Now we need to include the built schema in our original app project.</p>
<ul>
<li>Build the DemoEmploymentData project.</li>
<li>Return to the DepartmentsAndEmployeeSyncApp. Right-click on the &#8216;Resources&#8217; group and choose &#8216;Add -> Existing Files&#8230;&#8217;.</li>
<li>Find the DemoEmploymentData project directory and inside that find the &#8216;build&#8217; directory. Inside there you&#8217;ll find a &#8216;release&#8217; directory containing a directory called &#8216;DemoEmploymentData.syncschema&#8217;. Select this directory and Add it to the project. In the dialog box that appears after this, check the &#8216;Create Folder References for any added folders&#8217; radio button so that the directory structure is maintained. Once the syncschema is added, your Xcode project&#8217;s Groups &#038; Files outline view should look like this:
<p><img src="http://www.timisted.net/blog/wp-content/uploads/2008/07/xcodeprojectlist.gif" alt="The Xcode Project Listing showing the syncschema listed" title="xcodeprojectlist" width="305" height="344" class="size-full wp-image-139" /></li>
</ul>
<h4>Changes to the Data Model</h4>
<p>Now that we&#8217;ve defined our data schema by telling it about our data model, we need to make a few changes to the data model so that it will populate the schema properly.</p>
<ul>
<li>Open up the data model in Xcode. Click on the Department entity and in the far top right of the window, click the little &#8217;sync&#8217; icon to setup the synchronization settings for the entity. Enter the Data Class as the one we setup earlier in the syncschema &#8216;com.mycompany.DemoEmploymentData&#8217;. Make sure the &#8216;Synchronize&#8230;&#8217; box is ticked as shown:<br />
<img src="http://www.timisted.net/blog/wp-content/uploads/2008/07/entitysynchronization.gif" alt="The entity synchronization tab in the Xcode data modeler" title="entitysynchronization" width="247" height="132" class="size-full wp-image-141" /></li>
<li>Click the &#8216;name&#8217; attribute and check the &#8216;Identity property&#8217; box. This specifies that the name attribute will be unique and used to track different departments.</li>
<li>For the person entity, make sure the data class is again &#8216;com.mycompany.DemoEmploymentData&#8217; and that it should be synchronized. Specify that the &#8216;firstName&#8217; and &#8216;lastName&#8217; attributes are Identity properties.</li>
</ul>
<h4>The Client Description</h4>
<p>Our final piece of synchronization information is the client description that defines which data in the syncschema we will be synchronizing.</p>
<ul>
<li>Right-click on the &#8216;Resources&#8217; group in the project listing and choose &#8216;Add -> New File&#8230;&#8217;. Choose the &#8216;Sync Client Description&#8217; under &#8216;Sync Services&#8217; and call the file &#8216;clientDescription.plist&#8217;.</li>
<li>Under the Entities key, change the first dictionary key to &#8216;com.mycompany.DemoEmploymentData.Department&#8217;.</li>
<li>The first entry in the following array is a string that must be left as &#8216;com.apple.syncservices.RecordEntityName&#8217;. The subsequent strings define the chosen attributes for the specific entity; change the &#8216;first name&#8217; string to be just &#8216;name&#8217;. Add another string for the &#8216;employees&#8217; relationship.</li>
<li>Add a second entity key called &#8216;com.mycompany.DemoEmploymentData.Person&#8217; and again add the &#8216;com.apple.syncservices.RecordEntityName&#8217; string. Also add strings for the &#8216;firstName&#8217;, &#8216;lastName&#8217;, &#8217;salary&#8217; and &#8216;department&#8217; attributes.</li>
</ul>
<p>Your finished client description should look something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">&lt;?xml version<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;1.0&quot;</span> encoding<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;UTF-8&quot;</span>?&gt;
&lt;<span style="color: #002200;">!</span>DOCTYPE plist PUBLIC <span style="color: #bf1d1a;">&quot;-//Apple Computer//DTD PLIST 1.0//EN&quot;</span> <span style="color: #bf1d1a;">&quot;http://www.apple.com/DTDs/PropertyList-1.0.dtd&quot;</span>&gt;
&nbsp;
&lt;<span style="color: #002200;">!--</span> clientDescription.plist <span style="color: #002200;">--</span>&gt;
&lt;<span style="color: #002200;">!--</span> DepartmentsAndEmployeesSyncApp <span style="color: #002200;">--</span>&gt;
&lt;<span style="color: #002200;">!--</span> Created by Tim Isted on <span style="color: #2400d9;">25</span><span style="color: #002200;">/</span>07<span style="color: #002200;">/</span>2008. <span style="color: #002200;">--</span>&gt;
&lt;<span style="color: #002200;">!--</span> Copyright <span style="color: #2400d9;">2008</span> __MyCompanyName__. All rights reserved. <span style="color: #002200;">--</span>&gt;
&nbsp;
&lt;plist version<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;1.0&quot;</span>&gt;
&lt;dict&gt;
	&lt;key&gt;DisplayName&lt;<span style="color: #002200;">/</span>key&gt;
	&lt;string&gt;DepartmentsAndEmployeesSyncApp&lt;<span style="color: #002200;">/</span>string&gt;
&nbsp;
	&lt;key&gt;Entities&lt;<span style="color: #002200;">/</span>key&gt;
	&lt;dict&gt;
		&lt;key&gt;com.mycompany.DemoEmploymentData.Department&lt;<span style="color: #002200;">/</span>key&gt;
		&lt;array&gt;
			&lt;string&gt;com.apple.syncservices.RecordEntityName&lt;<span style="color: #002200;">/</span>string&gt;
			&lt;string&gt;name&lt;<span style="color: #002200;">/</span>string&gt;
			&lt;string&gt;employees&lt;<span style="color: #002200;">/</span>string&gt;
		&lt;<span style="color: #002200;">/</span>array&gt;
&nbsp;
		&lt;key&gt;com.mycompany.DemoEmploymentData.Person&lt;<span style="color: #002200;">/</span>key&gt;
		&lt;array&gt;
			&lt;string&gt;com.apple.syncservices.RecordEntityName&lt;<span style="color: #002200;">/</span>string&gt;
			&lt;string&gt;firstName&lt;<span style="color: #002200;">/</span>string&gt;
			&lt;string&gt;lastName&lt;<span style="color: #002200;">/</span>string&gt;
			&lt;string&gt;salary&lt;<span style="color: #002200;">/</span>string&gt;
			&lt;string&gt;department&lt;<span style="color: #002200;">/</span>string&gt;
		&lt;<span style="color: #002200;">/</span>array&gt;
	&lt;<span style="color: #002200;">/</span>dict&gt;
&nbsp;
	&lt;key&gt;Type&lt;<span style="color: #002200;">/</span>key&gt;
	&lt;string&gt;app&lt;<span style="color: #002200;">/</span>string&gt;
&lt;<span style="color: #002200;">/</span>dict&gt;
&lt;<span style="color: #002200;">/</span>plist&gt;</pre></div></div>

<h4>Making our application synchronize</h4>
<p>In order to get our application to synchronize its data, we need to make a few modifications to the way the application sets up its persistent store. We also need to change the save action so that whenever data is persisted, it is also synchronized.</p>
<ul>
<li>Firstly, we need to add the relevant framework to the project. Expand the Frameworks group in your project browser and right-click on the Linked Frameworks group. Choose &#8216;Add -> Existing Frameworks&#8230;&#8217; and select the &#8216;SyncServices.framework&#8217;.</li>
<li>Next we&#8217;ll modify the application delegate so that it handles both synchronization and the necessary callbacks. Open &#8216;DepartmentsAndEmployeesSyncApp_AppDelegate.h&#8217;. After the #import for Cocoa.h add a line to import the SyncServices header:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &lt;syncServices/SyncServices.h&gt;</span></pre></div></div>

</li>
<li>In order to be able to handle the synchronization callbacks, an object must conform to the <code>NSPersistentStoreCoordinatorSyncing</code> protocol. So, change the object interface to be:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> DepartmentsAndEmployeesSyncApp_AppDelegate <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span> &lt;nspersistentStoreCoordinatorSyncing&gt;</pre></div></div>

</li>
<li>We&#8217;re going to be writing a method that will initiate a sync so add a declaration for it:

<div class="wp_syntax"><div 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>syncAction<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>sender;</pre></div></div>

</li>
</ul>
<p>The Core Data framework handles synchronization through the <code>NSPersistentStoreCoordinator</code>. To support fast synchronization operations, you need to specify a &#8216;fastsyncstore&#8217; file for the coordinator to maintain.</p>
<ul>
<li>Open the DepartmentsAndEmployeesSyncApp_AppDelegate.m files and find the <code>persistentStoreCoordinator:</code> method. We need to change this so that it keeps track of the created <code>NSPersistentStore</code>, and then adds the .fastsyncstore file to it. Rewrite the method so it looks like this:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSPersistentStoreCoordinator</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> persistentStoreCoordinator
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>persistentStoreCoordinator <span style="color: #002200;">!=</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		<span style="color: #a61390;">return</span> persistentStoreCoordinator;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #400080;">NSFileManager</span> <span style="color: #002200;">*</span>fileManager;
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>applicationSupportFolder <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
	<span style="color: #400080;">NSURL</span> <span style="color: #002200;">*</span>url;
	<span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span>error;
&nbsp;
	fileManager <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSFileManager</span> defaultManager<span style="color: #002200;">&#93;</span>;
	applicationSupportFolder <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self applicationSupportFolder<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> <span style="color: #002200;">!</span><span style="color: #002200;">&#91;</span>fileManager fileExistsAtPath<span style="color: #002200;">:</span>applicationSupportFolder isDirectory<span style="color: #002200;">:</span><span style="color: #a61390;">NULL</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		<span style="color: #002200;">&#91;</span>fileManager createDirectoryAtPath<span style="color: #002200;">:</span>applicationSupportFolder attributes<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	url <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURL</span> fileURLWithPath<span style="color: #002200;">:</span> <span style="color: #002200;">&#91;</span>applicationSupportFolder stringByAppendingPathComponent<span style="color: #002200;">:</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;DepartmentsAndEmployeesSyncApp.xml&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
	persistentStoreCoordinator <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSPersistentStoreCoordinator</span> alloc<span style="color: #002200;">&#93;</span> initWithManagedObjectModel<span style="color: #002200;">:</span> <span style="color: #002200;">&#91;</span>self managedObjectModel<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #400080;">NSPersistentStore</span> <span style="color: #002200;">*</span>store <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>persistentStoreCoordinator addPersistentStoreWithType<span style="color: #002200;">:</span>NSXMLStoreType configuration<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> URL<span style="color: #002200;">:</span>url options<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> error<span style="color: #002200;">:&amp;</span>error<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> store <span style="color: #002200;">!=</span> <span style="color: #a61390;">nil</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		<span style="color: #400080;">NSURL</span> <span style="color: #002200;">*</span>fastSyncFileURL <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURL</span> fileURLWithPath<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>applicationSupportFolder stringByAppendingPathComponent<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;DepartmentsAndEmployeesSyncApp.fastsyncstore&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>persistentStoreCoordinator setStoresFastSyncDetailsAtURL<span style="color: #002200;">:</span>fastSyncFileURL forPersistentStore<span style="color: #002200;">:</span>store<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
	<span style="color: #a61390;">else</span> <span style="color: #002200;">&#123;</span>
		<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSApplication</span> sharedApplication<span style="color: #002200;">&#93;</span> presentError<span style="color: #002200;">:</span>error<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #a61390;">return</span> persistentStoreCoordinator;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>We need to write a method that will return an <code>ISyncClient</code> object ready for use in synchronization. Sync Services keeps track of different sync clients by using the unique application bundle identifier. Add the following method at the end of the file. It asks whether such a client exists for our application; if not it creates one, registers the schema and client description before specifying which client types it should synchronize with, displaying an error to the user if something goes wrong:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>ISyncClient <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>syncClient
<span style="color: #002200;">&#123;</span>
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>clientIdentifier <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSBundle</span> mainBundle<span style="color: #002200;">&#93;</span> bundleIdentifier<span style="color: #002200;">&#93;</span>;
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>reason <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;unknown error&quot;</span>;
	ISyncClient <span style="color: #002200;">*</span>client;
&nbsp;
	<span style="color: #a61390;">@try</span> <span style="color: #002200;">&#123;</span>
		client <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>ISyncManager sharedManager<span style="color: #002200;">&#93;</span> clientWithIdentifier<span style="color: #002200;">:</span>clientIdentifier<span style="color: #002200;">&#93;</span>;
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> client <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#123;</span>
			<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>ISyncManager sharedManager<span style="color: #002200;">&#93;</span> registerSchemaWithBundlePath<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSBundle</span> mainBundle<span style="color: #002200;">&#93;</span> pathForResource<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;DemoEmploymentData&quot;</span> ofType<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;syncschema&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
			<span style="color: #002200;">&#123;</span>
				reason <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;error registering the DemoEmploymentData sync schema&quot;</span>;
			<span style="color: #002200;">&#125;</span>
			<span style="color: #a61390;">else</span>
			<span style="color: #002200;">&#123;</span>
				client <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>ISyncManager sharedManager<span style="color: #002200;">&#93;</span> registerClientWithIdentifier<span style="color: #002200;">:</span>clientIdentifier descriptionFilePath<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSBundle</span> mainBundle<span style="color: #002200;">&#93;</span> pathForResource<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;clientDescription&quot;</span> ofType<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;plist&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
				<span style="color: #002200;">&#91;</span>client setShouldSynchronize<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span> withClientsOfType<span style="color: #002200;">:</span>ISyncClientTypeApplication<span style="color: #002200;">&#93;</span>;
				<span style="color: #002200;">&#91;</span>client setShouldSynchronize<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span> withClientsOfType<span style="color: #002200;">:</span>ISyncClientTypeDevice<span style="color: #002200;">&#93;</span>;
				<span style="color: #002200;">&#91;</span>client setShouldSynchronize<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span> withClientsOfType<span style="color: #002200;">:</span>ISyncClientTypeServer<span style="color: #002200;">&#93;</span>;
				<span style="color: #002200;">&#91;</span>client setShouldSynchronize<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span> withClientsOfType<span style="color: #002200;">:</span>ISyncClientTypePeer<span style="color: #002200;">&#93;</span>;
			<span style="color: #002200;">&#125;</span>
		<span style="color: #002200;">&#125;</span>
	<span style="color: #002200;">&#125;</span>
	<span style="color: #a61390;">@catch</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span> exception<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		client <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
		reason <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>exception reason<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> client <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		NSRunAlertPanel<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;You can not sync your Employee Data.&quot;</span>, <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Failed to register the sync client: %@&quot;</span>, reason<span style="color: #002200;">&#93;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;OK&quot;</span>, <span style="color: #a61390;">nil</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #a61390;">return</span> client;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>Next, implement the <code>syncAction:</code> method to start off the synchronization process by calling the <code>syncWithClient:</code> method on the application&#8217;s <code>persistentStoreCoordinator</code>:

<div class="wp_syntax"><div 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>syncAction<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>sender
<span style="color: #002200;">&#123;</span>
	<span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span>error <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
	ISyncClient <span style="color: #002200;">*</span>client <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self syncClient<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> client <span style="color: #002200;">!=</span> <span style="color: #a61390;">nil</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self managedObjectContext<span style="color: #002200;">&#93;</span> persistentStoreCoordinator<span style="color: #002200;">&#93;</span> syncWithClient<span style="color: #002200;">:</span>client inBackground<span style="color: #002200;">:</span><span style="color: #a61390;">NO</span> handler<span style="color: #002200;">:</span>self error<span style="color: #002200;">:&amp;</span>error<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> error <span style="color: #002200;">!=</span> <span style="color: #a61390;">nil</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSApplication</span> sharedApplication<span style="color: #002200;">&#93;</span> presentError<span style="color: #002200;">:</span>error<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>Find the <code>saveAction:</code> method and change it to call our <code>syncAction:</code> method once the <code>managedObjectContext</code> has saved:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span> saveAction<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>sender <span style="color: #002200;">&#123;</span>
&nbsp;
	<span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span>error <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self managedObjectContext<span style="color: #002200;">&#93;</span> save<span style="color: #002200;">:&amp;</span>error<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #002200;">&#91;</span>self syncAction<span style="color: #002200;">:</span>sender<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
	<span style="color: #a61390;">else</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSApplication</span> sharedApplication<span style="color: #002200;">&#93;</span> presentError<span style="color: #002200;">:</span>error<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>If a synchronization involving our data is triggered by another application, we need to know so that we can update our own application. So, firstly add an <code>applicationDidFinishLaunching:</code> method to set the <code>syncAlertHandler</code> to a selector for a second method that calls <code>saveAction:</code>

<div class="wp_syntax"><div 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><span style="color: #400080;">NSNotification</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>notification
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self syncClient<span style="color: #002200;">&#93;</span> setSyncAlertHandler<span style="color: #002200;">:</span>self selector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>client<span style="color: #002200;">:</span>mightWantToSyncEntityNames<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>self syncAction<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><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>client<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>ISyncClient <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>client mightWantToSyncEntityNames<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>entityNames
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>self saveAction<span style="color: #002200;">:</span>self<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>Add an <code>applicationWillTerminate:</code> method that calls <code>saveAction:</code> so that our data is automatically saved and synchronized when the user exits the application:

<div class="wp_syntax"><div 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>applicationWillTerminate<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>notification
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>self saveAction<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>Next, add methods that specify which <code>managedObjectContext</code>s should be monitored when synchronizing our persistent store. If a synchronization is triggered outside of the application by another application synching with data in our schema, the managed object contexts listed in the second method will be reloaded to reflect any changes that were made:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>managedObjectContextsToMonitorWhenSyncingPersistentStoreCoordinator<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSPersistentStoreCoordinator</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>coordinator
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSArray</span> arrayWithObject<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>self managedObjectContext<span style="color: #002200;">&#93;</span><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: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>managedObjectContextsToReloadAfterSyncingPersistentStoreCoordinator<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSPersistentStoreCoordinator</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>coordinator
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSArray</span> arrayWithObject<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>self managedObjectContext<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>Finally, add methods that get called when an object is pushed or changed during synchronization. These methods allow information contained within the object or change to be modified to suit the way data is stored in our model. For this application, we can simply return the provided object as it stands:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>persistentStoreCoordinator<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSPersistentStoreCoordinator</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>coordinator willPushRecord<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>record forManagedObject<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSManagedObject</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>managedObject inSyncSession<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>ISyncSession <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>session
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> record;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>ISyncChange <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>persistentStoreCoordinator<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSPersistentStoreCoordinator</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>coordinator willApplyChange<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>ISyncChange <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>change toManagedObject<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSManagedObject</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>managedObject inSyncSession<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>ISyncSession <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>session
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> change;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
</ul>
<p>Assuming all has gone to plan, when you build and run the project, you won&#8217;t notice very much difference, except that if you previously inputted some data into your application, you&#8217;ll probably experience a small delay before it is displayed while the synchronization is setup.</p>
<p>Apple provides a useful development tool called &#8216;Syncrospector&#8217; that can be used to inspect sync schemas and clients. If you find and launch the tool you should be able to see your new sync schema listed, together with its specified entities. You can also view the sync client details for the DepartmentsAndEmployeesSyncApp application, hopefully displaying successful synchronization of the two entities:<br />
<img src="http://www.timisted.net/blog/wp-content/uploads/2008/07/synchrospector.gif" alt="The Synchrospector application inspecting our sync client details" title="synchrospector" width="453" height="496" class="size-full wp-image-154" /></p>
<h3>Demo Application 2</h3>
<p>Our second project is a simplified employee viewing application that holds the same data as our first but displays it in a slightly different way. We&#8217;ll define the client description and setup synching in a similar way to our first project such that the two applications will keep in sync with each other.</p>
<ul>
<li>Create a new Core Data Application in Xcode and call it &#8216;EmployeesListSyncApp&#8217;.</li>
<li>Open the data model and add the same entities as before &#8212; Department and Person &#8212; with the same attributes and relationships.</li>
<li>Open MainWindow.nib in Interface Builder and add a single <code>NSArrayController</code> called &#8216;People&#8217;. Set it to Prepare Content from the &#8216;Person&#8217; entity and bind its Managed Object Context to the application delegate&#8217;s &#8216;managedObjectContext&#8217;.</li>
<li>Open the Window and add an NSTableView. Give it four columns bound to the &#8216;firstName&#8217;, &#8216;lastName&#8217;, &#8217;salary&#8217; and &#8216;department.name&#8217; keys respectively from the People controller. Drag an <code>NSNumberFormatter</code> onto the Salary column and make the Department column uneditable.</li>
</ul>
<p>Next we&#8217;ll define the client description for this application which is essentially the same as for our first application &#8212; namely that it synchronizes the &#8216;Department&#8217; and &#8216;Person&#8217; entities along with their various attributes and relationships.</p>
<ul>
<li>Add a &#8216;Sync Client Description&#8217; called &#8216;clientDescription&#8217; to the Resources group of our project.</li>
<li>Change the Entities keys to access our DemoEmploymentData entities and attributes.</li>
</ul>
<p>The finished clientDescription.plist file should look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">&lt;?xml version<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;1.0&quot;</span> encoding<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;UTF-8&quot;</span>?&gt;
&lt;<span style="color: #002200;">!</span>DOCTYPE plist PUBLIC <span style="color: #bf1d1a;">&quot;-//Apple Computer//DTD PLIST 1.0//EN&quot;</span> <span style="color: #bf1d1a;">&quot;http://www.apple.com/DTDs/PropertyList-1.0.dtd&quot;</span>&gt;
&nbsp;
&lt;<span style="color: #002200;">!--</span> clientDescription.plist <span style="color: #002200;">--</span>&gt;
&lt;<span style="color: #002200;">!--</span> EmployeesListSyncApp <span style="color: #002200;">--</span>&gt;
&lt;<span style="color: #002200;">!--</span> Created by Tim Isted on <span style="color: #2400d9;">25</span><span style="color: #002200;">/</span>07<span style="color: #002200;">/</span>2008. <span style="color: #002200;">--</span>&gt;
&lt;<span style="color: #002200;">!--</span> Copyright <span style="color: #2400d9;">2008</span> __MyCompanyName__. All rights reserved. <span style="color: #002200;">--</span>&gt;
&nbsp;
&lt;plist version<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;1.0&quot;</span>&gt;
&lt;dict&gt;
	&lt;key&gt;DisplayName&lt;<span style="color: #002200;">/</span>key&gt;
	&lt;string&gt;EmployeesListSyncApp&lt;<span style="color: #002200;">/</span>string&gt;
&nbsp;
	&lt;key&gt;Entities&lt;<span style="color: #002200;">/</span>key&gt;
	&lt;dict&gt;
		&lt;key&gt;com.mycompany.DemoEmploymentData.Department&lt;<span style="color: #002200;">/</span>key&gt;
		&lt;array&gt;
			&lt;string&gt;com.apple.syncservices.RecordEntityName&lt;<span style="color: #002200;">/</span>string&gt;
			&lt;string&gt;name&lt;<span style="color: #002200;">/</span>string&gt;
			&lt;string&gt;employees&lt;<span style="color: #002200;">/</span>string&gt;
		&lt;<span style="color: #002200;">/</span>array&gt;
&nbsp;
		&lt;key&gt;com.mycompany.DemoEmploymentData.Person&lt;<span style="color: #002200;">/</span>key&gt;
		&lt;array&gt;
			&lt;string&gt;com.apple.syncservices.RecordEntityName&lt;<span style="color: #002200;">/</span>string&gt;
			&lt;string&gt;firstName&lt;<span style="color: #002200;">/</span>string&gt;
			&lt;string&gt;lastName&lt;<span style="color: #002200;">/</span>string&gt;
			&lt;string&gt;salary&lt;<span style="color: #002200;">/</span>string&gt;
			&lt;string&gt;department&lt;<span style="color: #002200;">/</span>string&gt;
		&lt;<span style="color: #002200;">/</span>array&gt;
	&lt;<span style="color: #002200;">/</span>dict&gt;
&nbsp;
	&lt;key&gt;Type&lt;<span style="color: #002200;">/</span>key&gt;
	&lt;string&gt;app&lt;<span style="color: #002200;">/</span>string&gt;
&lt;<span style="color: #002200;">/</span>dict&gt;
&lt;<span style="color: #002200;">/</span>plist&gt;</pre></div></div>

<p>Now open up the data model again and setup the synchronization for the entities and their attributes as we did in the first application.</p>
<ul>
<li>The Data Class for both entities is &#8216;com.mycompany.DemoEmploymentData&#8217;.</li>
<li>Make the Department &#8216;name&#8217; attribute and the Person &#8216;firstName&#8217; and &#8216;lastName&#8217; attributes into Identity properties.</li>
</ul>
<p>Finally we can setup the application to synchronize data in a similar way to the first project:</p>
<ul>
<li>Add the SyncServices framework to your project Linked Frameworks group.</li>
<li><code>#import</code> the SyncServices/SyncServices.h header file into your application delegate header and change the interface to adopt the <code>NSPersistentStoreCoordinatorSyncing</code> protocol.</li>
<li>Since we&#8217;ll be synchronizing this application in the same way as the first, add the same <code>- (void)syncAction:(id)sender;</code> declaration in the header.</li>
<li>Change the implementation for the <code>persistentStoreCoordinator:</code> so it behaves as before:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSPersistentStoreCoordinator</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> persistentStoreCoordinator
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>persistentStoreCoordinator <span style="color: #002200;">!=</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		<span style="color: #a61390;">return</span> persistentStoreCoordinator;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #400080;">NSFileManager</span> <span style="color: #002200;">*</span>fileManager;
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>applicationSupportFolder <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
	<span style="color: #400080;">NSURL</span> <span style="color: #002200;">*</span>url;
	<span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span>error;
&nbsp;
	fileManager <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSFileManager</span> defaultManager<span style="color: #002200;">&#93;</span>;
	applicationSupportFolder <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self applicationSupportFolder<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> <span style="color: #002200;">!</span><span style="color: #002200;">&#91;</span>fileManager fileExistsAtPath<span style="color: #002200;">:</span>applicationSupportFolder isDirectory<span style="color: #002200;">:</span><span style="color: #a61390;">NULL</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		<span style="color: #002200;">&#91;</span>fileManager createDirectoryAtPath<span style="color: #002200;">:</span>applicationSupportFolder attributes<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	url <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURL</span> fileURLWithPath<span style="color: #002200;">:</span> <span style="color: #002200;">&#91;</span>applicationSupportFolder stringByAppendingPathComponent<span style="color: #002200;">:</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;EmployeesListSyncApp.xml&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
	persistentStoreCoordinator <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSPersistentStoreCoordinator</span> alloc<span style="color: #002200;">&#93;</span> initWithManagedObjectModel<span style="color: #002200;">:</span> <span style="color: #002200;">&#91;</span>self managedObjectModel<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #400080;">NSPersistentStore</span> <span style="color: #002200;">*</span>store <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>persistentStoreCoordinator addPersistentStoreWithType<span style="color: #002200;">:</span>NSXMLStoreType configuration<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> URL<span style="color: #002200;">:</span>url options<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> error<span style="color: #002200;">:&amp;</span>error<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> store <span style="color: #002200;">!=</span> <span style="color: #a61390;">nil</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		<span style="color: #400080;">NSURL</span> <span style="color: #002200;">*</span>fastSyncFileURL <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURL</span> fileURLWithPath<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>applicationSupportFolder stringByAppendingPathComponent<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;EmployeesListSyncApp.fastsyncstore&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>persistentStoreCoordinator setStoresFastSyncDetailsAtURL<span style="color: #002200;">:</span>fastSyncFileURL forPersistentStore<span style="color: #002200;">:</span>store<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
	<span style="color: #a61390;">else</span> <span style="color: #002200;">&#123;</span>
		<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSApplication</span> sharedApplication<span style="color: #002200;">&#93;</span> presentError<span style="color: #002200;">:</span>error<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #a61390;">return</span> persistentStoreCoordinator;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>Add the <code>syncClient:</code> method but this time we only need to register the client description, not the main sync schema:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>ISyncClient <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>syncClient
<span style="color: #002200;">&#123;</span>
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>clientIdentifier <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSBundle</span> mainBundle<span style="color: #002200;">&#93;</span> bundleIdentifier<span style="color: #002200;">&#93;</span>;
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>reason <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;unknown error&quot;</span>;
	ISyncClient <span style="color: #002200;">*</span>client;
&nbsp;
	<span style="color: #a61390;">@try</span> <span style="color: #002200;">&#123;</span>
		client <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>ISyncManager sharedManager<span style="color: #002200;">&#93;</span> clientWithIdentifier<span style="color: #002200;">:</span>clientIdentifier<span style="color: #002200;">&#93;</span>;
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> client <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#123;</span>
			client <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>ISyncManager sharedManager<span style="color: #002200;">&#93;</span> registerClientWithIdentifier<span style="color: #002200;">:</span>clientIdentifier descriptionFilePath<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSBundle</span> mainBundle<span style="color: #002200;">&#93;</span> pathForResource<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;clientDescription&quot;</span> ofType<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;plist&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
			<span style="color: #002200;">&#91;</span>client setShouldSynchronize<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span> withClientsOfType<span style="color: #002200;">:</span>ISyncClientTypeApplication<span style="color: #002200;">&#93;</span>;
			<span style="color: #002200;">&#91;</span>client setShouldSynchronize<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span> withClientsOfType<span style="color: #002200;">:</span>ISyncClientTypeDevice<span style="color: #002200;">&#93;</span>;
			<span style="color: #002200;">&#91;</span>client setShouldSynchronize<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span> withClientsOfType<span style="color: #002200;">:</span>ISyncClientTypeServer<span style="color: #002200;">&#93;</span>;
			<span style="color: #002200;">&#91;</span>client setShouldSynchronize<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span> withClientsOfType<span style="color: #002200;">:</span>ISyncClientTypePeer<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#125;</span>
	<span style="color: #002200;">&#125;</span>
	<span style="color: #a61390;">@catch</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span> exception<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		client <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
		reason <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>exception reason<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> client <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		NSRunAlertPanel<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;You can not sync your Employee Data.&quot;</span>, <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Failed to register the sync client: %@&quot;</span>, reason<span style="color: #002200;">&#93;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;OK&quot;</span>, <span style="color: #a61390;">nil</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #a61390;">return</span> client;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>Implement the <code>syncAction:</code> method as before:

<div class="wp_syntax"><div 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>syncAction<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>sender
<span style="color: #002200;">&#123;</span>
	<span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span>error <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
	ISyncClient <span style="color: #002200;">*</span>client <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self syncClient<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> client <span style="color: #002200;">!=</span> <span style="color: #a61390;">nil</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self managedObjectContext<span style="color: #002200;">&#93;</span> persistentStoreCoordinator<span style="color: #002200;">&#93;</span> syncWithClient<span style="color: #002200;">:</span>client inBackground<span style="color: #002200;">:</span><span style="color: #a61390;">NO</span> handler<span style="color: #002200;">:</span>self error<span style="color: #002200;">:&amp;</span>error<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> error <span style="color: #002200;">!=</span> <span style="color: #a61390;">nil</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSApplication</span> sharedApplication<span style="color: #002200;">&#93;</span> presentError<span style="color: #002200;">:</span>error<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>Change the <code>saveAction:</code> method to call the <code>syncAction:</code> method:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span> saveAction<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>sender <span style="color: #002200;">&#123;</span>
&nbsp;
	<span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span>error <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self managedObjectContext<span style="color: #002200;">&#93;</span> save<span style="color: #002200;">:&amp;</span>error<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #002200;">&#91;</span>self syncAction<span style="color: #002200;">:</span>sender<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
	<span style="color: #a61390;">else</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSApplication</span> sharedApplication<span style="color: #002200;">&#93;</span> presentError<span style="color: #002200;">:</span>error<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>Finally add the other callback methods:

<div class="wp_syntax"><div 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><span style="color: #400080;">NSNotification</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>notification
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self syncClient<span style="color: #002200;">&#93;</span> setSyncAlertHandler<span style="color: #002200;">:</span>self selector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>client<span style="color: #002200;">:</span>mightWantToSyncEntityNames<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>self syncAction<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><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>client<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>ISyncClient <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>client mightWantToSyncEntityNames<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>entityNames
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>self saveAction<span style="color: #002200;">:</span>self<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>applicationWillTerminate<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>notification
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>self saveAction<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><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: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>managedObjectContextsToMonitorWhenSyncingPersistentStoreCoordinator<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSPersistentStoreCoordinator</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>coordinator
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSArray</span> arrayWithObject<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>self managedObjectContext<span style="color: #002200;">&#93;</span><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: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>managedObjectContextsToReloadAfterSyncingPersistentStoreCoordinator<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSPersistentStoreCoordinator</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>coordinator
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSArray</span> arrayWithObject<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>self managedObjectContext<span style="color: #002200;">&#93;</span><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: #400080;">NSDictionary</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>persistentStoreCoordinator<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSPersistentStoreCoordinator</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>coordinator willPushRecord<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>record forManagedObject<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSManagedObject</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>managedObject inSyncSession<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>ISyncSession <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>session
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> record;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>ISyncChange <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>persistentStoreCoordinator<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSPersistentStoreCoordinator</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>coordinator willApplyChange<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>ISyncChange <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>change toManagedObject<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSManagedObject</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>managedObject inSyncSession<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>ISyncSession <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>session
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> change;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
</ul>
<p>If you build and run this second application, you&#8217;ll find that any data you might have input into the first application is automatically displayed by this one. Notice how the Department relationship is pulled across and correctly displayed.</p>
<p>Try changing the name or salary of one of the people in either application. Once you save or exit the program, you&#8217;ll find that the other application automatically updates the display (and its underlying data) accordingly. Create a new person in the first application; once you save, the second application will then display that new person too.</p>
<h3>Sample Project Downloads</h3>
<p>The sample projects are available in one file (136kb) here: <a href='http://www.timisted.net/blog/wp-content/uploads/2008/07/coredatasyncserv.zip'>coredatasyncserv.zip</a>.</p>
<p>Once you have downloaded the file, decompress it and you&#8217;ll find three projects inside. The first two need to be built in a specific order as one requires the build release of the other:</p>
<ol>
<li>Open and build the DemoEmploymentData project.</li>
<li>Open the DepartmentsAndEmployeesSyncApp project and check that the references to the DemoEmploymentData.syncschema are valid (ie they&#8217;re not displayed in red text). If they are not valid, delete the references and follow the instructions earlier in the article about adding the syncschema into your project. Once the references are valid, build and run the first application.</li>
<li>Finally, open the EmployeesListSyncApp project and build/run it. There are no references to worry about inside this one!</li>
</ol>
<p>If you run into problems, please let me know.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.timisted.net/blog/archive/core-data-and-sync-services/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Custom Table Cells, Bindings and Core Data</title>
		<link>http://www.timisted.net/blog/archive/custom-cells-and-core-data/</link>
		<comments>http://www.timisted.net/blog/archive/custom-cells-and-core-data/#comments</comments>
		<pubDate>Wed, 09 Jul 2008 11:56:05 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Core Data]]></category>
		<category><![CDATA[Apple]]></category>
		<category><![CDATA[Bindings]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Framework]]></category>
		<category><![CDATA[Interface Builder]]></category>
		<category><![CDATA[Xcode]]></category>

		<guid isPermaLink="false">http://www.timisted.net/blog/?p=15</guid>
		<description><![CDATA[In this post we&#8217;ll walk through one way to use a custom cell to display multiple managed object values in a single table view cell using bindings. The obvious question here is how to bind the Value of the table column in Interface Builder to more than one Model Key Path from Core Data. You [...]]]></description>
			<content:encoded><![CDATA[<p>In this post we&#8217;ll walk through one way to use a custom cell to display multiple managed object values in a single table view cell using bindings. The obvious question here is how to bind the Value of the table column in Interface Builder to more than one Model Key Path from Core Data. You can&#8217;t, for example, type in a string of attributes like &#8216;firstName, lastName&#8217; or anything like that to specify that multiple values be taken.</p>
<p><span id="more-15"></span>Again, this article takes the previous CoreDataMultiWin application (in its third incarnation with multiple windows, multiple managed object contexts and table view double clicks) as its basis. This time, the interface for the application should end up looking like this:</p>
<p><img src="http://www.timisted.net/blog/wp-content/uploads/2008/07/mainwindowsinglecellapp.gif" alt="The application running showing a single custom cell with multiple Core Data values" title="mainwindowsinglecellapp" width="340" height="337" class="aligncenter size-full wp-image-16" /></p>
<p>- Edit &#8211; The example project for this post is available to download from here: <a href='http://www.timisted.net/blog/wp-content/uploads/2008/07/coredatamultiwin4.zip'>coredatamultiwin4.zip</a>.</p>
<h3>Store some additional data to display</h3>
<p>Before we go into the code to show the cell and its values, let&#8217;s make a change to the data model so that we have a little more information to display.</p>
<p>Open MyDocument.xcdatamodel in Xcode and add a third string attribute to the Person entity called &#8217;salary&#8217;. Note that in a real-world app, you&#8217;d most likely want to use a number attribute rather than a string here and perform all sorts of validation in the interface to check input; since we&#8217;re using this attribute only for this demonstration, however, we&#8217;ll leave it as a string so that the user can type in anything they want.</p>
<p>Now that we&#8217;ve sorted out the model, we need to change the interface accordingly.</p>
<p>Open the PersonWindow.nib Window in Interface Builder and add a labelled text field for the salary so it looks like this:</p>
<p><img src="http://www.timisted.net/blog/wp-content/uploads/2008/07/newemployeewindow.gif" alt="The layout for the revised Person Window" title="newemployeewindow" width="277" height="131" class="aligncenter size-full wp-image-17" /></p>
<p>Bind the Value of the new text field to the Person &#8217;selection&#8217; &#8217;salary&#8217; model key path.</p>
<p>Next open AddEmployeeWindow.nib in Interface Builder and make the window look like this:</p>
<p><img src="http://www.timisted.net/blog/wp-content/uploads/2008/07/newaddemployeewindow.gif" alt="The layout for the revised Add Employee window" title="newaddemployeewindow" width="287" height="180" class="aligncenter size-full wp-image-18" /></p>
<p>Again, bind the Value of the new text field to the New Employee &#8217;selection&#8217; &#8217;salary&#8217;.</p>
<p>We need to make a change to the <code>AddEmployeeController</code> code in order that the salary value is copied across contexts so open up the AddEmployeeController.m file. Assuming you made the change to use a dictionary of keys for copying the new employee object as suggested in the article on multiple managed object contexts, simply change the line that makes the <code>employeeKeys</code> array to the following:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>employeeKeys <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSArray</span> alloc<span style="color: #002200;">&#93;</span> initWithObjects<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;firstName&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;lastName&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;department&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;salary&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>It&#8217;s as simple as adding in the additional key to this array to make our new salary attribute work.</p>
<p>Build and run the application to check that the salary attribute is copied across from the Add Employee window to the main window (you&#8217;ll need to open a display window for an employee to check the value).</p>
<h3>A custom table view cell</h3>
<p>Now that we&#8217;ve got some very slightly more interesting data stored, let&#8217;s create a custom table view cell to display it.</p>
<p>Create a new Cocoa class called <code>EmployeeTableCell</code> and change EmployeeTableCell.h so that our object inherits from <code>NSCell</code> rather than <code>NSObject</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> EmployeeTableCell <span style="color: #002200;">:</span> <span style="color: #400080;">NSCell</span> <span style="color: #002200;">&#123;</span>
&nbsp;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>Now open EmployeeTableCell.m and implement a method <code>drawWithFrame: inView:</code> that will be called to display the custom cell:</p>

<div class="wp_syntax"><div 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>drawWithFrame<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">NSRect</span><span style="color: #002200;">&#41;</span>cellFrame inView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSView</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>controlView
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">NSPoint</span> textPoint;
&nbsp;
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>helloString <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Hello!&quot;</span>;
&nbsp;
	textPoint.x <span style="color: #002200;">=</span> cellFrame.origin.x <span style="color: #002200;">+</span> <span style="color: #2400d9;">1</span>;
	textPoint.y <span style="color: #002200;">=</span> cellFrame.origin.y;
&nbsp;
	<span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>textAttributes <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithObjectsAndKeys<span style="color: #002200;">:</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSColor</span> blackColor<span style="color: #002200;">&#93;</span>, NSForegroundColorAttributeName, <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSFont</span> systemFontOfSize<span style="color: #002200;">:</span><span style="color: #2400d9;">13</span><span style="color: #002200;">&#93;</span>, NSFontAttributeName, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>helloString drawAtPoint<span style="color: #002200;">:</span>textPoint withAttributes<span style="color: #002200;">:</span>textAttributes<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>This method just displays the text &#8220;Hello!&#8221; inside the cell so that we can test our custom table cell is working.</p>
<p>Now we need to tell our table view to use the custom cell so open up MyDocument.h and add an <code>IBOutlet</code> for an <code>NSTableColumn</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">IBOutlet <span style="color: #400080;">NSTableColumn</span> <span style="color: #002200;">*</span>peopleTableColumn;</pre></div></div>

<p>Open MyDocument.nib and connect this Interface Builder outlet to the first column in the people table view.</p>
<p>Back in Xcode, open MyDocument.m and <code>#import</code> the EmployeeTableCell.h header file. Find the <code>windowControllerDidLoadNib:</code> method and add the following after the call to <code>super</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">EmployeeTableCell <span style="color: #002200;">*</span>infoCell <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>EmployeeTableCell alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>peopleTableColumn setDataCell<span style="color: #002200;">:</span>infoCell<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>Here we allocate a single instance of our new <code>EmployeeTableCell</code> class and tell the table view column to use this cell to display its data.</p>
<p>If you build and run the application you&#8217;ll find that instead of displaying the <code>firstName</code> in the column, our Hello! message is now displayed.</p>
<p>Return now to the EmployeeTableCell.m file and change the <code>drawWithFrame: inView:</code> method to the following:</p>

<div class="wp_syntax"><div 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>drawWithFrame<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">NSRect</span><span style="color: #002200;">&#41;</span>cellFrame inView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSView</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>controlView
<span style="color: #002200;">&#123;</span>
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>firstNameString <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self objectValue<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">NSPoint</span> textPoint;
&nbsp;
	textPoint.x <span style="color: #002200;">=</span> cellFrame.origin.x <span style="color: #002200;">+</span> <span style="color: #2400d9;">1</span>;
	textPoint.y <span style="color: #002200;">=</span> cellFrame.origin.y;
&nbsp;
	<span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>textAttributes <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithObjectsAndKeys<span style="color: #002200;">:</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSColor</span> blackColor<span style="color: #002200;">&#93;</span>, NSForegroundColorAttributeName, <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSFont</span> systemFontOfSize<span style="color: #002200;">:</span><span style="color: #2400d9;">13</span><span style="color: #002200;">&#93;</span>, NSFontAttributeName, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>firstNameString drawAtPoint<span style="color: #002200;">:</span>textPoint withAttributes<span style="color: #002200;">:</span>textAttributes<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>This code pulls the <code>firstName</code> value from the object represented by the cell and then displays it instead of the Hello! string.</p>
<p>Again, build and run the application and you will see the first and last names displayed in the columns as they were originally but this time the first name is displayed for us by our custom cell.</p>
<h3>Getting multiple values to a cell</h3>
<p>Since we find out the string to display by getting the cell&#8217;s <code>objectValue</code>, in order to receive multiple values we obviously need to have the <code>objectValue</code> somehow return an array, dictionary or some such structure of information rather than just the one string. For this example, we&#8217;re going to generate a dictionary of <code>Person</code> information which we then pass to the cell.</p>
<p>At this point we need to make a custom <code>NSManagedObject</code> subclass for our Person entity so that we can generate the necessary dictionary of information on demand when the cell requires it.</p>
<p>Open the data model again and add to the Person entity an Undefined, Transient attribute called &#8216;personDictionary&#8217;. Set the class of the Person entity to <code>PersonObject</code> rather than <code>NSManagedObject</code>.</p>
<p>With the Person entity selected in the data model, make a new file in Xcode and selected the &#8216;Managed Object Class&#8217; template in the &#8216;Design&#8217; category. Click &#8216;Next&#8217; to get to the &#8216;Managed Object Class Generation&#8217; screen and make sure the Person entity is ticked along with the Generate accessors checkbox at the bottom of the screen. Deselect the &#8216;Generate Obj-C 2.0 Properties&#8217;:</p>
<p><img src="http://www.timisted.net/blog/wp-content/uploads/2008/07/generateobjectclassfiles.gif" alt="The Managed Object Class Generation Assistant in Xcode" title="generateobjectclassfiles" width="387" height="322" class="aligncenter size-full wp-image-19" /></p>
<p>Click &#8216;Finish&#8217; and you&#8217;ll be greeted with an automatically-generated PersonObject.h file with declarations for the setters and getters.</p>
<p>Remove the setter for the <code>personDictionary</code> attribute and replace the <code>UNKNOWN_TYPE</code> with an <code>NSDictionary</code> pointer. Also add a class method <code>employeeKeys</code> so that your <code>PersonObject</code> declarations look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> PersonObject <span style="color: #002200;">:</span>  <span style="color: #400080;">NSManagedObject</span>
<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: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>employeeKeys;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>personDictionary;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>salary;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setSalary<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>value;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>firstName;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setFirstName<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>value;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>lastName;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setLastName<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>value;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSManagedObject</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>department;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setDepartment<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSManagedObject</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>value;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>Now open up PersonObject.m and again remove the <code>setPersonDictionary:</code> setter from the implementations.</p>
<p>We&#8217;ll implement the <code>employeeKeys</code> method to return the keys of the employee data (like the array we used in previous articles when copying managed objects from one context to another).</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>employeeKeys
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">static</span> <span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>employeeKeys <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> employeeKeys <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span> <span style="color: #002200;">&#41;</span>
		employeeKeys <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSArray</span> alloc<span style="color: #002200;">&#93;</span> initWithObjects<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;firstName&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;lastName&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;department&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;salary&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">return</span> employeeKeys;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>This class method maintains a static array of keys that can be used by other methods to build a dictionary.</p>
<p>Next change the getter method&#8217;s <code>UNKNOWN_TYPE</code> again and implement it like this:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>personDictionary
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>self dictionaryWithValuesForKeys<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self class<span style="color: #002200;">&#93;</span> employeeKeys<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>This very simple method simply returns a dictionary filled with the required keys and values.</p>
<p>Back in Interface Builder, change the binding for the table column to the &#8216;personDictionary&#8217; key instead of the &#8216;firstName&#8217; key.</p>
<p>Now that we&#8217;ve changed the object provided to the table column, we need to make changes to our custom cell to collect the data properly. Open EmployeeTableCell.m and change the method to the following:</p>

<div class="wp_syntax"><div 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>drawWithFrame<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">NSRect</span><span style="color: #002200;">&#41;</span>cellFrame inView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSView</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>controlView
<span style="color: #002200;">&#123;</span>
	<span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>cellValues <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self objectValue<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">NSPoint</span> textPoint;
&nbsp;
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>firstNameString <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>cellValues valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;firstName&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	textPoint.x <span style="color: #002200;">=</span> cellFrame.origin.x <span style="color: #002200;">+</span> <span style="color: #2400d9;">1</span>;
	textPoint.y <span style="color: #002200;">=</span> cellFrame.origin.y;
&nbsp;
	<span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>textAttributes <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithObjectsAndKeys<span style="color: #002200;">:</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSColor</span> blackColor<span style="color: #002200;">&#93;</span>, NSForegroundColorAttributeName, <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSFont</span> systemFontOfSize<span style="color: #002200;">:</span><span style="color: #2400d9;">13</span><span style="color: #002200;">&#93;</span>, NSFontAttributeName, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>firstNameString drawAtPoint<span style="color: #002200;">:</span>textPoint withAttributes<span style="color: #002200;">:</span>textAttributes<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Now the <code>firstName</code> attribute is pulled from the dictionary and displayed as before.</p>
<p>Build and run the application to make sure all works as expected.</p>
<p>Now that we have the potential to display multiple values in the cell, return to Interface Builder and change the MyDocument.nib main Window so that there is only our custom cell table column in the table view, resized to fill the table. Change the title of the column to &#8220;Employees&#8221;. Check the &#8216;Alternating Rows&#8217; checkbox for the table view and set its row height to 50:</p>
<p><img src="http://www.timisted.net/blog/wp-content/uploads/2008/07/newmainwindow.gif" alt="The revised layout of the main window with our single custom cell column" title="newmainwindow" width="340" height="337" class="aligncenter size-full wp-image-20" /></p>
<p>Back in Xcode change our custom cell <code>drawWithFrame: inView:</code> method to the following:</p>

<div class="wp_syntax"><div 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>drawWithFrame<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">NSRect</span><span style="color: #002200;">&#41;</span>cellFrame inView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSView</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>controlView
<span style="color: #002200;">&#123;</span>
	<span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>cellValues <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self objectValue<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">NSPoint</span> textPoint;
&nbsp;
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>nameString <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>cellValues valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;firstName&quot;</span><span style="color: #002200;">&#93;</span>;
	nameString <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>nameString stringByAppendingFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot; %@&quot;</span>, <span style="color: #002200;">&#91;</span>cellValues valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;lastName&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>salaryString <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>cellValues valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;salary&quot;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>departmentString <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Department: %@&quot;</span>, <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>cellValues valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;department&quot;</span><span style="color: #002200;">&#93;</span> valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;name&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	textPoint.x <span style="color: #002200;">=</span> cellFrame.origin.x <span style="color: #002200;">+</span> <span style="color: #2400d9;">1</span>;
	textPoint.y <span style="color: #002200;">=</span> cellFrame.origin.y;
&nbsp;
	<span style="color: #400080;">NSMutableDictionary</span> <span style="color: #002200;">*</span>textAttributes <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableDictionary</span> dictionaryWithObjectsAndKeys<span style="color: #002200;">:</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSColor</span> blackColor<span style="color: #002200;">&#93;</span>, NSForegroundColorAttributeName, <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSFont</span> systemFontOfSize<span style="color: #002200;">:</span><span style="color: #2400d9;">13</span><span style="color: #002200;">&#93;</span>, NSFontAttributeName, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>nameString drawAtPoint<span style="color: #002200;">:</span>textPoint withAttributes<span style="color: #002200;">:</span>textAttributes<span style="color: #002200;">&#93;</span>;
&nbsp;
	textPoint.y <span style="color: #002200;">=</span> cellFrame.origin.y <span style="color: #002200;">+</span> <span style="color: #2400d9;">18</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>textAttributes setValue<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSFont</span> systemFontOfSize<span style="color: #002200;">:</span><span style="color: #2400d9;">15</span><span style="color: #002200;">&#93;</span> forKey<span style="color: #002200;">:</span>NSFontAttributeName<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>salaryString drawAtPoint<span style="color: #002200;">:</span>textPoint withAttributes<span style="color: #002200;">:</span>textAttributes<span style="color: #002200;">&#93;</span>;
&nbsp;
	textPoint.y <span style="color: #002200;">=</span> cellFrame.origin.y <span style="color: #002200;">+</span> <span style="color: #2400d9;">36</span>;
	<span style="color: #002200;">&#91;</span>textAttributes setValue<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSFont</span> systemFontOfSize<span style="color: #002200;">:</span><span style="color: #2400d9;">11</span><span style="color: #002200;">&#93;</span> forKey<span style="color: #002200;">:</span>NSFontAttributeName<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>textAttributes setValue<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSColor</span> grayColor<span style="color: #002200;">&#93;</span> forKey<span style="color: #002200;">:</span>NSForegroundColorAttributeName<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>departmentString drawAtPoint<span style="color: #002200;">:</span>textPoint withAttributes<span style="color: #002200;">:</span>textAttributes<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>This code builds and displays a <code>nameString</code> using the <code>firstName</code> and <code>lastName</code> keys from the dictionary. It also displays the <code>salary</code> and shows that we are able to display the <code>department</code> &#8220;name&#8221; from a managed object value for the key &#8220;department&#8221;.</p>
<p>Build and run the application to make sure the display works. You will, however, notice that if you open an employee window and change the salary, the main window doesn&#8217;t automatically update the employee cell. This is because the binding mechanism does not know what values affect the values in the <code>personDictionary</code>.</p>
<p>To fix this problem, add the following method to PersonObject.m:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSSet</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>keyPathsForValuesAffectingPersonDictionary
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSSet</span> setWithObjects<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;firstName&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;lastName&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;department&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;salary&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>The Key-Value Coding mechanism allows for a class method that asks which values affect a dependent key such as our <code>personDictionary</code>; the name of the method must follow the format &#8216;keyPathsForValuesAffecting&#8230;&#8217; with the attribute name starting with an initial capital letter.</p>
<p>If you now build and run the application, whenever a relevant attribute is changed in a Person Window, anything bound to the <code>personDictionary</code> key is told that the values have changed and here the cell in the main document window automatically updates to display the correct information.</p>
<h3>Error Checking</h3>
<p>None of the code here checks for potential errors. You&#8217;ll discover, for example, that creating a new employee and leaving the salary field blank will crash the application. You could fix this by making it a required field, or by using validation, or just by having any code that expects a salary value check that it hasn&#8217;t received a nil string. This goes for all the attributes in this example!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.timisted.net/blog/archive/custom-cells-and-core-data/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
