<?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>Whatever happened to Benjamin Ragheb? &#187; code</title>
	<atom:link href="http://www.benzado.com/blog/tags/code/feed" rel="self" type="application/rss+xml" />
	<link>http://www.benzado.com/blog</link>
	<description>I apologize that this blog is using the default Wordpress template.</description>
	<lastBuildDate>Thu, 06 May 2010 16:33:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Make Xcode nag you about unfinished TODOs</title>
		<link>http://www.benzado.com/blog/post/329/make-xcode-nag-you-about-unfinished-todos</link>
		<comments>http://www.benzado.com/blog/post/329/make-xcode-nag-you-about-unfinished-todos#comments</comments>
		<pubDate>Fri, 08 Jan 2010 08:51:15 +0000</pubDate>
		<dc:creator>Benjamin</dc:creator>
				<category><![CDATA[Nerdery]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[xcode]]></category>

		<guid isPermaLink="false">http://www.benzado.com/blog/?p=329</guid>
		<description><![CDATA[Add a simple Run Script Build Phase to your project and you'll never forget another TODO again.]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re like me, you often make promises to yourself in the form of TODO comments in your code. For example:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// TODO: make sure file exists before opening!</span>
fooBar <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>FooBar alloc<span style="color: #002200;">&#93;</span> initWithFile<span style="color: #002200;">:</span>path<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>This is a reasonable thing to do, because sometimes you just want to get something working right now and aren&#8217;t in the mood to write all the required error checking code. But, you also know that you cannot trust your <a href="http://theinfosphere.org/Transcript:I,_Roommate#time-04-13">soft human brain</a> to remember to add the check later, so you write a comment to remind yourself to do it.</p>
<p>Xcode recognizes the TODO: keyword in your comments and helpfully <a href="http://developer.apple.com/mac/library/documentation/DeveloperTools/Conceptual/XcodeWorkspace/100-The_Text_Editor/text_editor.html#//apple_ref/doc/uid/TP40002679-SW10">adds items to the function popup menu</a> so that you can quickly navigate to them. In addition to <code>TODO:</code>, Xcode will also recognize <code>FIXME:</code> (when you know the code is broken), <code>???:</code> (when you don&#8217;t know what it does), and <code>!!!:</code> (when you wish you didn&#8217;t know).</p>
<p>That&#8217;s helpful when you&#8217;re editing a file, but what about a TODO tucked away in some dark corner of your source code that you haven&#8217;t visited in a while? You&#8217;re likely to forget about it, and how can you keep a promise you forgot that you made?</p>
<p>The answer, of course, is to have somebody nag you. Fortunately, there&#8217;s a way to have Xcode fill that role. All you have to do is add a simple Run Script Build Phase which turns them into Build Warnings.</p>
<p>Select <b>Project &gt; New Build Phase &gt; New Run Script Build Phase</b> from the menu bar. Then, copy and paste this into the script window:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">KEYWORDS</span>=<span style="color: #ff0000;">&quot;TODO:|FIXME:|\?\?\?:|\!\!\!:&quot;</span>
<span style="color: #c20cb9; font-weight: bold;">find</span> <span style="color: #800000;">${SRCROOT}</span> \<span style="color: #7a0874; font-weight: bold;">&#40;</span> <span style="color: #660033;">-name</span> <span style="color: #ff0000;">&quot;*.h&quot;</span> <span style="color: #660033;">-or</span> <span style="color: #660033;">-name</span> <span style="color: #ff0000;">&quot;*.m&quot;</span> \<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #660033;">-print0</span> <span style="color: #000000; font-weight: bold;">|</span> \
    <span style="color: #c20cb9; font-weight: bold;">xargs</span> <span style="color: #660033;">-0</span> <span style="color: #c20cb9; font-weight: bold;">egrep</span> <span style="color: #660033;">--with-filename</span> <span style="color: #660033;">--line-number</span> <span style="color: #660033;">--only-matching</span> <span style="color: #ff0000;">&quot;(<span style="color: #007800;">$KEYWORDS</span>).*<span style="color: #000099; font-weight: bold;">\$</span>&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> \
    <span style="color: #c20cb9; font-weight: bold;">perl</span> <span style="color: #660033;">-p</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;s/(<span style="color: #007800;">$KEYWORDS</span>)/ warning: <span style="color: #000099; font-weight: bold;">\$</span>1/&quot;</span></pre></td></tr></table></div>

<p>What does it mean?</p>
<p>Line 1 defines the keywords we want to search for. If you want to exclude a keyword or include a different one, edit this line.</p>
<p>Line 2 uses the <a href="http://developer.apple.com/mac/library/DOCUMENTATION/Darwin/Reference/ManPages/man1/find.1.html">find</a> command to generate a list of all files in your project directory (SRCROOT) having an .h or .m extension. If you want to search more files, you will need to edit this line.</p>
<p>Line 3 uses <a href="http://developer.apple.com/mac/library/DOCUMENTATION/Darwin/Reference/ManPages/man1/xargs.1.html">xargs</a> to pass those file names along to <a href="http://developer.apple.com/mac/library/DOCUMENTATION/Darwin/Reference/ManPages/man1/egrep.1.html">egrep</a>, which searches inside the files for lines containing one of the keywords. If any are found, it outputs the file name, line number, and the matching part of the line.</p>
<p>Line 4 uses Perl to format the lines as warnings.</p>
<p>The output of the script will look like this:</p>
<p><code>/Users/benzado/Projects/FooBart/Baz.m:42: warning: TODO: make sure file exists before opening!</code></p>
<p>Xcode will recognize lines in this format and treat them as first class build warnings. You can see them in the Build Results panel and, just like a warning from the compiler, a double click will open an editor window and take you directly to the offending line.</p>
<p><i>An Exercise For The Enterprising Reader: modify the script so that no warnings or errors are reported during Debug builds, but TODOs are flagged as errors in Release builds.</i></p>
]]></content:encoded>
			<wfw:commentRss>http://www.benzado.com/blog/post/329/make-xcode-nag-you-about-unfinished-todos/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Draw your own Disclosure Indicator</title>
		<link>http://www.benzado.com/blog/post/325/draw-your-own-disclosure-indicator</link>
		<comments>http://www.benzado.com/blog/post/325/draw-your-own-disclosure-indicator#comments</comments>
		<pubDate>Mon, 04 Jan 2010 03:21:17 +0000</pubDate>
		<dc:creator>Benjamin</dc:creator>
				<category><![CDATA[Nerdery]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[iPhone]]></category>

		<guid isPermaLink="false">http://www.benzado.com/blog/?p=325</guid>
		<description><![CDATA[In which I share code for a function to draw an iPhone disclosure indicator.]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m writing Cocoa Touch code to draw a button which, when pressed, pushes a new view controller onto the stack. If I was working with a UITableView, I&#8217;d simply set the cell&#8217;s accessory to be a <a href="http://developer.apple.com/iphone/library/documentation/UserExperience/Conceptual/MobileHIG/ContentViews/ContentViews.html#//apple_ref/doc/uid/TP40006556-CH12-SW13">disclosure indicator</a> (the little gray arrowhead) and call it a day.</p>
<p>But I&#8217;m not working with table cells, so even though the standard disclosure indicator is <em>perfect</em> for this situation, if I want one I&#8217;ll have to draw it myself.</p>
<p>In this situation I will usually take a screenshot of the real iPhone control, add the image file to my project, and then feel kind of guilty about it. I began to do this, but I realized that the disclosure indicator is really only two gray lines. Two lines! How hard can it be to just draw it in code?</p>
<p>It turns out that it&#8217;s not too hard, if you&#8217;re willing to spend some time experimenting with different numbers and seeing what looks right. Fortunately for you, I&#8217;ve saved you the trouble by posting the answer here:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Draws a disclosure indicator such that the tip of the arrow is at (x,y)</span>
<span style="color: #a61390;">void</span> BRDrawDisclosureIndicator<span style="color: #002200;">&#40;</span>CGContextRef ctxt, CGFloat x, CGFloat y<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">static</span> <span style="color: #a61390;">const</span> CGFloat R <span style="color: #002200;">=</span> <span style="color: #2400d9;">4.5</span>; <span style="color: #11740a; font-style: italic;">// &quot;radius&quot; of the arrow head</span>
    <span style="color: #a61390;">static</span> <span style="color: #a61390;">const</span> CGFloat W <span style="color: #002200;">=</span> <span style="color: #2400d9;">3</span>; <span style="color: #11740a; font-style: italic;">// line width</span>
    CGContextSaveGState<span style="color: #002200;">&#40;</span>ctxt<span style="color: #002200;">&#41;</span>;
    CGContextMoveToPoint<span style="color: #002200;">&#40;</span>ctxt, x<span style="color: #002200;">-</span>R, y<span style="color: #002200;">-</span>R<span style="color: #002200;">&#41;</span>;
    CGContextAddLineToPoint<span style="color: #002200;">&#40;</span>ctxt, x, y<span style="color: #002200;">&#41;</span>;
    CGContextAddLineToPoint<span style="color: #002200;">&#40;</span>ctxt, x<span style="color: #002200;">-</span>R, y<span style="color: #002200;">+</span>R<span style="color: #002200;">&#41;</span>;
    CGContextSetLineCap<span style="color: #002200;">&#40;</span>ctxt, kCGLineCapSquare<span style="color: #002200;">&#41;</span>;
    CGContextSetLineJoin<span style="color: #002200;">&#40;</span>ctxt, kCGLineJoinMiter<span style="color: #002200;">&#41;</span>;
    CGContextSetLineWidth<span style="color: #002200;">&#40;</span>ctxt, W<span style="color: #002200;">&#41;</span>;
    CGContextStrokePath<span style="color: #002200;">&#40;</span>ctxt<span style="color: #002200;">&#41;</span>;
    CGContextRestoreGState<span style="color: #002200;">&#40;</span>ctxt<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Before calling the function, you should set the stroke color to 50% gray if you&#8217;re drawing on a white background or white if the control is highlighted and you&#8217;re drawing on a blue background. Or, <a href="http://stackoverflow.com/questions/1852672/best-way-to-change-the-color-view-of-disclosure-indicator-accessory-view-in-a-tab">you can use whatever color you like</a>.</p>
<p>To be honest, I only eyeballed the result, so it <em>might</em> not be a pixel-perfect reproduction of the real thing. However, I think my eyeballs are at least as good as most users&#8217; eyeballs, so I will confidently declare this code Good Enough.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.benzado.com/blog/post/325/draw-your-own-disclosure-indicator/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
