<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title><![CDATA[Chris Greenwood]]></title>
  <link href="http://cmgreenwood.com/atom.xml" rel="self"/>
  <link href="http://cmgreenwood.com/"/>
  <updated>2013-11-12T08:16:27-08:00</updated>
  <id>http://cmgreenwood.com/</id>
  <author>
    <name><![CDATA[Chris Greenwood]]></name>
    
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  
  <entry>
    <title type="html"><![CDATA[Overriding isEquals in Objective-C]]></title>
    <link href="http://cmgreenwood.com/blog/2013/11/12/overriding-isequals-in-objectivec/"/>
    <updated>2013-11-12T07:44:00-08:00</updated>
    <id>http://cmgreenwood.com/blog/2013/11/12/overriding-isequals-in-objectivec</id>
    <content type="html"><![CDATA[<p>I am currently working on a small personal project, an iPhone app written in Objective-C.  I needed a simple data structure to hold a 2-element tuple of integers.  I originally implemented this as a C-struct:</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='objc'><span class='line'><span class="k">struct</span> <span class="n">Index</span> <span class="p">{</span>
</span><span class='line'>    <span class="n">NSInteger</span> <span class="n">row</span><span class="p">;</span>
</span><span class='line'>    <span class="n">NSInteger</span> <span class="n">column</span><span class="p">;</span>
</span><span class='line'><span class="p">};</span>
</span><span class='line'><span class="k">typedef</span> <span class="k">struct</span> <span class="n">Index</span> <span class="n">Index</span><span class="p">;</span>
</span></code></pre></td></tr></table></div></figure>


<p>This worked fine, but I often found myself doing a multiline declare-then-set-members pattern every time I wanted to use the data structure.  To simplify, I added a small C function to generate structs, similar to <code>CGRectMake</code>:</p>

<!-- more -->




<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='objc'><span class='line'><span class="n">Index</span> <span class="nf">IndexMake</span><span class="p">(</span><span class="n">NSInteger</span> <span class="n">r</span><span class="p">,</span> <span class="n">NSInteger</span> <span class="n">c</span><span class="p">)</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>    <span class="n">Index</span> <span class="n">index</span><span class="p">;</span>
</span><span class='line'>    <span class="n">index</span><span class="p">.</span><span class="n">row</span> <span class="o">=</span> <span class="n">r</span><span class="p">;</span>
</span><span class='line'>    <span class="n">index</span><span class="p">.</span><span class="n">column</span> <span class="o">=</span> <span class="n">c</span><span class="p">;</span>
</span><span class='line'>    <span class="k">return</span> <span class="n">index</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>This worked great until I needed to put the data structure into a collection such as an NSMutableSet.  Because the contained objects in such a collection need to be Objective-C pointers, you need to encapsulate the struct.  According to <a href="http://stackoverflow.com/questions/4516991/whats-the-best-way-to-put-a-c-struct-in-an-nsarray">this SO post</a>, the correct way to do so is this:</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='objc'><span class='line'><span class="n">Index</span> <span class="n">index</span> <span class="o">=</span> <span class="n">IndexMake</span><span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="n">j</span><span class="p">);</span>
</span><span class='line'><span class="p">[</span><span class="n">selectedIndices</span> <span class="nl">addObject:</span><span class="p">[</span><span class="n">NSValue</span> <span class="nl">valueWithBytes:</span><span class="o">&amp;</span><span class="n">index</span> <span class="nl">objCType:</span><span class="k">@encode</span><span class="p">(</span><span class="n">Index</span><span class="p">)]]</span>
</span></code></pre></td></tr></table></div></figure>


<p>So this is now starting to get a bit ugly.  Once I realized that I would be using this data structure all throughout my app, I decided it would be way easier to just make it a proper Objective-C class.</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='objc'><span class='line'><span class="k">@interface</span> <span class="nc">GridIndex</span> : <span class="nc">NSObject</span>
</span><span class='line'><span class="k">@property</span> <span class="p">(</span><span class="n">nonatomic</span><span class="p">)</span> <span class="n">NSInteger</span> <span class="n">row</span><span class="p">;</span>
</span><span class='line'><span class="k">@property</span> <span class="p">(</span><span class="n">nonatomic</span><span class="p">)</span> <span class="n">NSInteger</span> <span class="n">column</span><span class="p">;</span>
</span><span class='line'><span class="k">@end</span>
</span></code></pre></td></tr></table></div></figure>


<p>With a static initializer, this really cleaned up the dependent code.  Of course, this also promptly broke the entire program.  Why?  Because I hadn&rsquo;t overridden the default <code>isEqual:</code> method in my custom class.  My first attempt at doing so did not work as expected:</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='objc'><span class='line'><span class="k">-</span><span class="p">(</span><span class="kt">BOOL</span><span class="p">)</span><span class="nf">isEqual:</span><span class="p">(</span><span class="kt">id</span><span class="p">)</span><span class="nv">object</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>    <span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="p">[(</span><span class="n">NSObject</span><span class="o">*</span><span class="p">)</span><span class="n">object</span> <span class="nl">isKindOfClass:</span><span class="p">[</span><span class="n">GridIndex</span> <span class="n">class</span><span class="p">]])</span>
</span><span class='line'>        <span class="k">return</span> <span class="n">NO</span><span class="p">;</span>
</span><span class='line'>    <span class="n">GridIndex</span> <span class="o">*</span><span class="n">other</span> <span class="o">=</span> <span class="p">(</span><span class="n">GridIndex</span><span class="o">*</span><span class="p">)</span><span class="n">object</span><span class="p">;</span>
</span><span class='line'>    <span class="k">return</span> <span class="n">self</span><span class="p">.</span><span class="n">row</span> <span class="o">==</span> <span class="n">other</span><span class="p">.</span><span class="n">row</span> <span class="o">&amp;&amp;</span> <span class="n">self</span><span class="p">.</span><span class="n">column</span> <span class="o">==</span> <span class="n">other</span><span class="p">.</span><span class="n">column</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>After adding a bunch of debugging I realized that the problem was when checking if an NSSet contained a GridIndex with a specific row and column.  More debugging showed that my <code>isEqual:</code> method was not called deterministically by the set containment method.  Sometimes my overridden function was called and worked as expected, sometimes it was totally bypassed and the result was always false.</p>

<p>After some more research, <a href="http://nshipster.com/equality/">this post</a> gave me the answer.  As it turns out, to override <code>isEquals:</code> in Objective-C you are actually expected to do three things.</p>

<blockquote><ul>
<li>Implement a new <code>isEqualTo__ClassName__:</code> method, which performs the meaningful value comparison.</li>
<li>Override <code>isEqual:</code> to make class and object identity checks, falling back on the aforementioned value comparison method.</li>
<li>Override <code>hash</code>.</li>
</ul>
</blockquote>

<p>Doing so fixed the problem.  Good thing someone had already written on this issue because I did not realize all of this was strictly necessary when overriding a simple <code>isEquals:</code> method.</p>

<p>Here&rsquo;s my final implementation:</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
</pre></td><td class='code'><pre><code class='objc'><span class='line'><span class="k">-</span><span class="p">(</span><span class="kt">BOOL</span><span class="p">)</span><span class="nf">isEqualToGridIndex:</span><span class="p">(</span><span class="n">GridIndex</span><span class="o">*</span><span class="p">)</span><span class="nv">other</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>    <span class="k">return</span> <span class="n">self</span><span class="p">.</span><span class="n">row</span> <span class="o">==</span> <span class="n">other</span><span class="p">.</span><span class="n">row</span> <span class="o">&amp;&amp;</span> <span class="n">self</span><span class="p">.</span><span class="n">column</span> <span class="o">==</span> <span class="n">other</span><span class="p">.</span><span class="n">column</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="k">-</span><span class="p">(</span><span class="kt">BOOL</span><span class="p">)</span><span class="nf">isEqual:</span><span class="p">(</span><span class="kt">id</span><span class="p">)</span><span class="nv">object</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>    <span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="p">[(</span><span class="n">NSObject</span><span class="o">*</span><span class="p">)</span><span class="n">object</span> <span class="nl">isKindOfClass:</span><span class="p">[</span><span class="n">GridIndex</span> <span class="n">class</span><span class="p">]])</span>
</span><span class='line'>        <span class="k">return</span> <span class="n">NO</span><span class="p">;</span>
</span><span class='line'>    <span class="k">return</span> <span class="p">[</span><span class="n">self</span> <span class="nl">isEqualToGridIndex:</span><span class="p">(</span><span class="n">GridIndex</span><span class="o">*</span><span class="p">)</span><span class="n">object</span><span class="p">];</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="k">-</span><span class="p">(</span><span class="n">NSUInteger</span><span class="p">)</span><span class="nf">hash</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>    <span class="k">return</span> <span class="n">self</span><span class="p">.</span><span class="n">row</span> <span class="o">+</span> <span class="n">self</span><span class="p">.</span><span class="n">column</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Aliasing when creating EPS files in Matlab]]></title>
    <link href="http://cmgreenwood.com/blog/2013/09/27/aliasing-in-eps-files/"/>
    <updated>2013-09-27T23:20:00-07:00</updated>
    <id>http://cmgreenwood.com/blog/2013/09/27/aliasing-in-eps-files</id>
    <content type="html"><![CDATA[<p>I have been working in LaTeX for a paper I&rsquo;m writing, and have found that if you want to include Matlab graphs in a LaTeX document, EPS files are by far the best way to go.  EPS files are converted to PDF graphics and then included in the resulting PDF, which looks way better than plugging a JPEG or a PNG file in directly.</p>

<p>However, I came across a weird issue when trying to save one of my graphs.  I used the <code>confusionmat</code> function in Matlab to create a confusion matrix from a set of ground truth and observation data pairs.  I then wrote a simple script to create a nice graphical representation of the confusion matrix (adapted from <a href="http://wrongpc.blogspot.com/2010/08/draw-confusion-matrix-in-matlab.html">here</a>).</p>

<!-- more -->


<p>The only problem was that when I tried to export as a EPS file, I got the following weird aliasing effect.</p>

<p><img style="width:50%;" src="http://cmgreenwood.com/images/confusionmat_alias.png"></img></p>

<p>I looked at a bunch of different options for fixing the issue: <a href="http://www.latex-community.org/forum/viewtopic.php?f=45&amp;p=53569">different LaTeX compiler settings</a>, <a href="http://stackoverflow.com/questions/6614207/how-to-export-non-blurry-eps-images">applying filters in Matlab</a>, <a href="http://pages.cs.wisc.edu/~ghost/">using GhostView as the viewer for the EPS file</a>.  None of it seemed to help.  I think at the end of the day, it didn&rsquo;t have to do with the EPS file itself, but the software/driver my Mac was using to view the EPS file and subsequently convert it to a PDF graphic.</p>

<p>The solution that ended up working for me?  A surprisingly easy one.  I finally found that if I scaled the matrix itself up by a large factor before graphing into a Matlab figure, the aliasing effect was eliminated.  I had to change some of the logic that printed confusion matrix numbers themselves at the right position, but it was a surprisingly simple fix to a strange problem.  Full script below.</p>

<pre><code>function [pcts] = confusion_plot(cmat, order)

SCALE = 100;

% turn into percentages
totals = sum(cmat, 2);
pcts = zeros(size(cmat));
for i = 1:size(cmat, 1)
    for j = 1:size(cmat, 2)
        point = cmat(i,j);
        total = totals(i);
        pcts(i,j) = point / total;
    end
end

% flip so that the darks are high percents
pcts_flipped = 1 - pcts;

pcts_scaled = scale_matrix(pcts_flipped, SCALE);

imagesc(pcts_scaled);
colormap(gray);

set(gca,'FontSize',20);
set(gca,'XTick',SCALE/2+1/2:SCALE:SCALE*length(order)-SCALE/2);
set(gca,'YTick',SCALE/2+1/2:SCALE:SCALE*length(order)-SCALE/2);
set(gca,'XTickLabel',order);
set(gca,'YTickLabel',order);
set(gca, 'Ticklength', [0 0])
set(gca,'XAxisLocation','top');
ylabel('Ground Truth Object Count');
xlabel('Observed Object Count');

% show the text percentage values
for i = 1:size(pcts, 1)
    for j = 1:size(pcts, 2)

        pct = pcts(i,j);

        color = [0 0 0];
        if pct &gt; 0.6
            color = [1 1 1];
        end

        text(j*SCALE-SCALE/2+1/2,i*SCALE-SCALE/2+1/2,num2str(cmat(i,j), 4), ...
            'FontSize', 20, ...
            'FontWeight', 'bold', ...
            'HorizontalAlignment', 'center', ...
            'Color', color);
    end
end

end

function [scaled] = scale_matrix(mat, scale)

scaled = zeros(scale * size(mat));

for i = 1:size(scaled, 1)
    for j = 1:size(scaled, 2)
        i_prime = ceil(i / scale);
        j_prime = ceil(j / scale);
        scaled(i, j) = mat(i_prime, j_prime);
    end
end

end
</code></pre>

<p>Here&rsquo;s the resulting EPS file, with the aliasing pretty much eliminated.</p>

<p><img style="width:50%;" src="http://cmgreenwood.com/images/confusionmat_nonalias.png"></img></p>

<p>I&rsquo;m sure its not the most elegant way to solve the issue, but if you&rsquo;re looking for a simple way to minimize the odd aliasing effect caused by exporting Matlab figures to EPS files, give this approach a shot.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[iOS: Smooth Adjustment of UI Following Rotation]]></title>
    <link href="http://cmgreenwood.com/blog/2013/05/29/ios-smooth-adjustment-of-ui-following-rotation/"/>
    <updated>2013-05-29T13:44:00-07:00</updated>
    <id>http://cmgreenwood.com/blog/2013/05/29/ios-smooth-adjustment-of-ui-following-rotation</id>
    <content type="html"><![CDATA[<p>Today I came across a small issue when trying to adjust a UIView based on rotation of the device.  The UINavigationController has two main delegate methods that pertain to rotation of the device: <code>willRotateToInterfaceOrientation:duration:</code> and <code>didRotateFromInterfaceOrientation:</code>.  They are pretty self-explanatory.  The former is called when the view is about to rotate, and provides the destination rotation.  The latter is called when the view rotation animation is over.</p>

<p>The problem I have is that I want to wait until the rotation is over so that I can query the UIViewController&rsquo;s view for its size and then update subviews accordingly.  But if we wait until the <code>didRotateFromInterfaceOrientation:</code> method is called, the entire rotation animation is over and then all my views update &ndash; it&rsquo;s pretty jerky.  But if I perform my view manipulation in the <code>willRotate...</code> method, the view controller&rsquo;s view has not been assigned its new view container yet, so I don&rsquo;t know what the new size of the view is.</p>

<!-- more -->


<p>I originally thought there might be a function that allows me to ask the view controller what its size will be for a particular rotation, but I couldn&rsquo;t find this anywhere in the docs.  As it turns out, there is a third delegate method that does exactly what I want.  According to <a href="http://stackoverflow.com/a/4432980/1560633">this Stack Overflow response</a>, when the <code>willAnimateRotationToInterfaceOrientation:duration:</code> method is called, the view controller&rsquo;s view has already been assigned its destination view container and the animation is about to start.  If I perform my view manipulation then, I know what the view size is about to be, and any changes I make occur at the same time as the actual rotation of the view.  It is a small and easy modification, but makes a big difference.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Radio buttons with Bootstrap and Knockout]]></title>
    <link href="http://cmgreenwood.com/blog/2013/05/28/radio-buttons-with-bootstrap-and-knockout/"/>
    <updated>2013-05-28T15:16:00-07:00</updated>
    <id>http://cmgreenwood.com/blog/2013/05/28/radio-buttons-with-bootstrap-and-knockout</id>
    <content type="html"><![CDATA[<p>I&rsquo;m currently building a web app using <a href="http://knockoutjs.com/">Knockout.js</a> as a frontend framework, with Bootstrap as my CSS framework.  I wanted to create a button group in which only a single button at a time could be selected (a set of <em>radio buttons</em>).  I then wanted to detect when those buttons change value, and which value is subsequently selected.</p>

<p>There are a variety of ways to go about this.  You could solve this using jQuery events, Bootstrap-specific events, or within the Knockout framework.  I decided to do it using a Knockout <code>click</code> binding so that once the event does fire, I&rsquo;m right where I need to be to update my ViewModel, and possibly my backend.</p>

<!-- more -->


<p>Here&rsquo;s what the HTML looks like:</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='html'><span class='line'><span class="nt">&lt;div</span> <span class="na">id=</span><span class="s">&quot;radio&quot;</span> <span class="na">class=</span><span class="s">&quot;btn-group&quot;</span> <span class="na">data-toggle=</span><span class="s">&quot;buttons-radio&quot;</span><span class="nt">&gt;</span>
</span><span class='line'>  <span class="nt">&lt;button</span> <span class="na">type=</span><span class="s">&quot;button&quot;</span> <span class="na">class=</span><span class="s">&quot;btn active&quot;</span> <span class="na">data-bind=</span><span class="s">&quot;click: $root.updateReadPreferences&quot;</span> <span class="na">value=</span><span class="s">&quot;all&quot;</span><span class="nt">&gt;</span>All<span class="nt">&lt;/button&gt;</span>
</span><span class='line'>  <span class="nt">&lt;button</span> <span class="na">type=</span><span class="s">&quot;button&quot;</span> <span class="na">class=</span><span class="s">&quot;btn&quot;</span> <span class="na">data-bind=</span><span class="s">&quot;click: $root.updateReadPreferences&quot;</span> <span class="na">value=</span><span class="s">&quot;unread&quot;</span><span class="nt">&gt;</span>Unread<span class="nt">&lt;/button&gt;</span>
</span><span class='line'><span class="nt">&lt;/div&gt;</span>
</span></code></pre></td></tr></table></div></figure>


<p>And then the JS in my ViewModel:</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="kd">function</span> <span class="nx">FeedsViewModel</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>  <span class="p">...</span>
</span><span class='line'>
</span><span class='line'>  <span class="nx">self</span><span class="p">.</span><span class="nx">readPreferences</span> <span class="o">=</span> <span class="s1">&#39;all&#39;</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>  <span class="nx">self</span><span class="p">.</span><span class="nx">updateReadPreferences</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">koElem</span><span class="p">,</span> <span class="nx">event</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>      <span class="kd">var</span> <span class="nx">value</span> <span class="o">=</span> <span class="nx">$</span><span class="p">(</span><span class="nx">event</span><span class="p">.</span><span class="nx">currentTarget</span><span class="p">).</span><span class="nx">attr</span><span class="p">(</span><span class="s1">&#39;value&#39;</span><span class="p">);</span>
</span><span class='line'>      <span class="nx">self</span><span class="p">.</span><span class="nx">readPreferences</span> <span class="o">=</span> <span class="nx">value</span><span class="p">;</span>
</span><span class='line'>      <span class="nx">alert</span><span class="p">(</span><span class="s1">&#39;Unread or all? : &#39;</span> <span class="o">+</span> <span class="nx">value</span><span class="p">);</span>
</span><span class='line'>  <span class="p">};</span>
</span><span class='line'>
</span><span class='line'>  <span class="p">...</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>A Knockout event takes the Knockout view object that is in scope for the DOM element as its first parameter &ndash; in our case it is the FeedsViewModel.  The second parameter, if provided, is the HTML DOM element that gave rise to the event trigger.  We simply have to use jQuery to ask for the &ldquo;value&rdquo; attribute for the DOM element, and then update our ViewModel accordingly.  One downside of this approach is that it is fairly verbose &ndash; we need a <code>value</code> attribute and a <code>click</code> event declared for each button in the button group.  Another way to do it might be to add the <code>click</code> event to the button group div instead of each button, but then you have to iterate through the children of the button group checking for the button that has the CSS class <code>active</code>.  This creates a race condition between the Knockout event being triggered and Bootstrap updating the CSS.  Since button groups will generally only contain a handful of elements, I found it easiest to just declare the <code>click</code> events at the button level.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[UITableViewCell from Nib]]></title>
    <link href="http://cmgreenwood.com/blog/2013/05/23/test-post/"/>
    <updated>2013-05-23T14:38:00-07:00</updated>
    <id>http://cmgreenwood.com/blog/2013/05/23/test-post</id>
    <content type="html"><![CDATA[<p>As far as I can tell, this is the best and most correct way to create a custom UITableViewCell that has a backing Objective-C class (so custom subviews) and is layed out using a Nib.  If anyone knows of a better way to do it, let me know.  There&rsquo;s a bunch of conflicting information on the web.</p>

<!-- more -->


<ol>
<li>Create your Nib</li>
<li>Remove the default UIView from XIB, replace it with a UITableViewCell</li>
<li><strong>Update:</strong> Remember to set a reuse identifier in the NIB properties.  It cannot be set programmatically.</li>
<li>Create your UITableViewCell subclass in Objective-C</li>
<li>Set the class of the cell in IB to be your custom class</li>
<li>Manually write your UIOutlet properties for the custom view properties you want to access from the cell.</li>
<li>Add sub-views in IB that correspond to those outlets</li>
<li>Select the cell and open the right menu, right tab (Connections inspector).  There you should see the custom outlets you just coded up in the Outlets list</li>
<li>Select the handle to the right of each outlet and drag it over to the subview in IB that corresponds to that outlet</li>
<li>Finally, in your <code>tableView:cellForRowAtIndexPath:</code> method, instantiate the cell</li>
</ol>


<p>The cell needs to be instantiated using the Nib to display your custom layout:</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
</pre></td><td class='code'><pre><code class='objc'><span class='line'><span class="p">...</span>
</span><span class='line'><span class="n">NSString</span> <span class="o">*</span><span class="n">ident</span> <span class="o">=</span> <span class="s">@&quot;customCell&quot;</span><span class="p">;</span>
</span><span class='line'><span class="n">CustomTableViewCell</span> <span class="o">*</span><span class="n">cell</span> <span class="o">=</span> <span class="p">[</span><span class="n">tableView</span> <span class="nl">dequeueReusableCellWithIdentifier:</span><span class="n">ident</span><span class="p">];</span>
</span><span class='line'>
</span><span class='line'><span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="n">cell</span><span class="p">)</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>  <span class="n">NSArray</span> <span class="o">*</span><span class="n">tlobjs</span> <span class="o">=</span> <span class="p">[[</span><span class="n">NSBundle</span> <span class="n">mainBundle</span><span class="p">]</span> <span class="nl">loadNibNamed:</span><span class="s">@&quot;CustomTableViewCell&quot;</span> <span class="nl">owner:</span><span class="nb">nil</span> <span class="nl">options:</span><span class="nb">nil</span><span class="p">];</span>
</span><span class='line'>  <span class="n">cell</span> <span class="o">=</span> <span class="p">(</span><span class="n">CustomTableViewCell</span><span class="o">*</span><span class="p">)[</span><span class="n">tlobjs</span> <span class="nl">objectAtIndex:</span><span class="mi">0</span><span class="p">];</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'><span class="p">...</span>
</span></code></pre></td></tr></table></div></figure>



]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[RestKit POST with nested data]]></title>
    <link href="http://cmgreenwood.com/blog/2013/05/23/restkit-post/"/>
    <updated>2013-05-23T00:00:00-07:00</updated>
    <id>http://cmgreenwood.com/blog/2013/05/23/restkit-post</id>
    <content type="html"><![CDATA[<p>I am using <a href="http://www.restkit.org">RestKit</a> to map objects in my iPhone application to RESTful requests to my <a href="http://flask.pocoo.org/">Flask</a> server.  I have to use RKRequestMapping to specify the format of the data a POST request expects.  This works fine when the data is flat, but I had trouble when there was an array of nested mappings that needed to be accounted for.  For example, here is the object that will be passed as data to the POST request:</p>

<!-- more -->




<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='objc'><span class='line'><span class="k">@interface</span> <span class="nc">TestArgs</span> : <span class="nc">NSObject</span>
</span><span class='line'>
</span><span class='line'><span class="k">@property</span> <span class="p">(</span><span class="n">copy</span><span class="p">,</span> <span class="n">nonatomic</span><span class="p">)</span> <span class="n">NSString</span> <span class="o">*</span><span class="n">name</span><span class="p">;</span>
</span><span class='line'><span class="k">@property</span> <span class="p">(</span><span class="n">strong</span><span class="p">,</span> <span class="n">nonatomic</span><span class="p">)</span> <span class="n">NSArray</span> <span class="o">*</span><span class="n">users</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="n">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>The users array is an array of dictionaries, each containing the keys {&lsquo;id&rsquo;, &lsquo;type&rsquo;}.  What I expected to see on the Flask end in the <code>request.form</code> variable was:</p>

<div>
  <pre><code class='python'>{'name': 'theName', 'users': [{'id':1, 'type':'user'},{'id':2,'type':'admin'}]}</code></pre>
</div>


<p>What I actually got was:</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="p">{</span><span class="s">&#39;name&#39;</span><span class="p">:</span> <span class="s">&#39;theName&#39;</span><span class="p">,</span> <span class="s">&#39;users[][&quot;id&quot;]&#39;</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span> <span class="s">&#39;users[][&quot;id&quot;]&#39;</span><span class="p">:</span> <span class="mi">2</span><span class="p">,</span> <span class="s">&#39;users[][&quot;type&quot;]&#39;</span><span class="p">:</span> <span class="s">&#39;user&#39;</span><span class="p">,</span> <span class="s">&#39;users[][&quot;type&quot;]&#39;</span><span class="p">:</span> <span class="s">&#39;admin&#39;</span><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>Not very helpful.  The reason this happens is that form data is generally encoded in URL encoding, not JSON encoding.  URL encoding doesn&rsquo;t handle array values very well, so I needed to use JSON encoding to capture the nested information I wanted to send.  Took me a while, but as it turns out I had to change two things in my code to make this work.  First, on the client side, I had to change the serialization type that RestKit uses to serialize POST data:</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='objc'><span class='line'><span class="n">RKObjectManager</span> <span class="o">*</span><span class="n">objectManager</span> <span class="o">=</span> <span class="p">...</span>
</span><span class='line'><span class="p">[</span><span class="n">objectManager</span> <span class="nl">setRequestSerializationMIMEType:</span><span class="n">RKMIMETypeJSON</span><span class="p">];</span>
</span></code></pre></td></tr></table></div></figure>


<p>Second, on the server side, I had to access the JSON data that was submitted with the POST differently:</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="n">post_data</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">json</span>
</span><span class='line'><span class="n">user_array</span> <span class="o">=</span> <span class="n">post_data</span><span class="p">[</span><span class="s">&#39;users&#39;</span><span class="p">]</span>
</span></code></pre></td></tr></table></div></figure>

]]></content>
  </entry>
  
</feed>
