<?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>Red Sweater Blog &#187; Programming</title>
	<atom:link href="http://www.red-sweater.com/blog/category/articles/programming/feed" rel="self" type="application/rss+xml" />
	<link>http://www.red-sweater.com/blog</link>
	<description>Mac &#38; Technology Writings by Daniel Jalkut</description>
	<lastBuildDate>Tue, 17 Jan 2012 22:03:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.1</generator>
		<item>
		<title>Learn To Code</title>
		<link>http://www.red-sweater.com/blog/2298/learn-to-code</link>
		<comments>http://www.red-sweater.com/blog/2298/learn-to-code#comments</comments>
		<pubDate>Mon, 02 Jan 2012 04:20:35 +0000</pubDate>
		<dc:creator>Daniel Jalkut</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.red-sweater.com/blog/?p=2298</guid>
		<description><![CDATA[If learning to program is even a minor goal for you, Code Year (via Brent Simmons) might be just the encouragement you need. They promise to email you on a weekly basis with coding lessons to help you achieve your goal. I&#8217;m one of those computer programmers who downplays the difficulty of the profession, because &#8220;if I [...]]]></description>
			<content:encoded><![CDATA[<p>If learning to program is even a minor goal for you, <a href="http://codeyear.com/">Code Year</a> (via <a href="http://inessential.com/2012/01/01/code_year">Brent Simmons</a>) might be just the encouragement you need. They promise to email you on a weekly basis with coding lessons to help you achieve your goal.</p>
<p>I&#8217;m one of those computer programmers who downplays the difficulty of the profession, because &#8220;if I can do it, anybody can do it!&#8221; On the other hand, I have faced challenges that made me question whether I&#8217;m vaguely qualified for the job. What it boils down to is that programming is both incredibly simple and impossibly hard, like so many important things in life.</p>
<p>There was a time when nobody knew how to write literary prose. The geniuses who invented it shared their special tool with a few friends, and they relished in their private, elite communications. Eventually monks, politicians, and academics joined the club. Now, we judge a society&#8217;s overall level of intellectual advancement by the literacy rate: the percentage of people who have learned to read and write.</p>
<p>Literacy isn&#8217;t about becoming a Hemingway or a Chabon. It&#8217;s about learning the basic tools to get a job done. I think programming — coding — is much the same. You don&#8217;t have to be the world&#8217;s best programmer to develop a means of expressing yourself, of solving a problem, of making something happen. If you&#8217;re lucky, you&#8217;ll be a  genius, but you start out with the basics.</p>
<p>Long ago, it would have been ridiculous to assume a whole society could be judged by its ability to read and write prose. It feels ridiculous now, to assume that we might use computer programming as a similar benchmark. Yet it may happen.</p>
<p>Did you always mean to learn another language, but never did? By all means, learn Spanish, French, or Chinese. But learn to code, too.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.red-sweater.com/blog/2298/learn-to-code/feed</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Objective-C Is The Language</title>
		<link>http://www.red-sweater.com/blog/2256/objective-c-is-the-language</link>
		<comments>http://www.red-sweater.com/blog/2256/objective-c-is-the-language#comments</comments>
		<pubDate>Mon, 07 Nov 2011 06:31:52 +0000</pubDate>
		<dc:creator>Daniel Jalkut</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.red-sweater.com/blog/?p=2256</guid>
		<description><![CDATA[My good friend Brent Simmons invokes a historical email from Linus Torvalds, about his disdain for C++ C++ is a horrible language. It&#8217;s made more horrible by the fact that a lot of substandard programmers use it, to the point where it&#8217;s much much easier to generate total and utter crap with it. Brent affirms [...]]]></description>
			<content:encoded><![CDATA[<p>My good friend Brent Simmons invokes a historical email from Linus Torvalds, about his disdain for C++</p>
<blockquote>
<p>C++ is a horrible language. It&#8217;s made more horrible by the fact that a lot of substandard programmers use it, to the point where it&#8217;s much much easier to generate total and utter crap with it.</p>
</blockquote>
<p>Brent <a href="http://inessential.com/2011/11/06/linus_on_c_">affirms his support</a> while paying homage to plain-old C:</p>
<blockquote>
<p>But I will admit to an enduring love of C. I still think of C not as C but as <em>the language</em>.</p>
</blockquote>
<p>I loved C. Emphasis on the past-tense. As object-oriented programming concepts became popular, those of us who were programming in C or similar procedural languages had to find new, object-oriented languages to fulfill our needs. At the time, I chose C++. Or I should say, I had C++ forced upon me. Because C++ popularized the notion of object-oriented programming, it was the only choice presented to many programmers.</p>
<p>Because I ended up at Apple, the time I spent with C++ was thankfully short. They were fortuitously behind the times at the dawn of my career, and wound up taking a different path while my career was ascending.</p>
<p>Objective-C was Apple&#8217;s response to object-oriented programming, and continues to be the lingua-franca for programmers on Macs, iPhones and iPads. I loved C. I love C. But it always fell short for me. It lacked something. Objective-C fixed that. I hope I never have to program in C++ again. But tellingly, I also hope I never have to program in C again.</p>
<p>There are lots of great features from Python, Ruby, or JavaScript, that I&#8217;d love to see incorporated into Objective-C. By no means is it perfect. But for its elegance, and for the fact that it fulfills many of the requirements of object-oriented programming, while maintaining the familiar simplicity of C, it presently earns the title of <em>the language</em> for me.</p>
<p>[<strong>Update Nov 7</strong>: I've had a lot of reaction to my claim above that Objective-C was "Apple's response to object-oriented programming." This makes it sound like Apple invented the language, and they didn't. But they have done more to popularize and promote it than anybody else. I stand by the meaning of it being "what they bring to the table," when it comes to object-oriented programming].</p>
]]></content:encoded>
			<wfw:commentRss>http://www.red-sweater.com/blog/2256/objective-c-is-the-language/feed</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>Sandbox Corners</title>
		<link>http://www.red-sweater.com/blog/2170/sandbox-corners</link>
		<comments>http://www.red-sweater.com/blog/2170/sandbox-corners#comments</comments>
		<pubDate>Fri, 09 Sep 2011 18:48:00 +0000</pubDate>
		<dc:creator>Daniel Jalkut</dc:creator>
				<category><![CDATA[Apple]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.red-sweater.com/blog/?p=2170</guid>
		<description><![CDATA[Apple&#8217;s sandboxing technologies make it possible to control at a very fine-grain system level exactly which system resources an application should be allowed to access. It offers control over reading and writing  files, opening  network resources, and much more. I&#8217;m really excited about sandboxing and also really terrified. Apple has given us, thus far, a [...]]]></description>
			<content:encoded><![CDATA[<p>Apple&#8217;s sandboxing technologies make it possible to control at a very fine-grain system level exactly which system resources an application should be allowed to access. It offers control over reading and writing  files, opening  network resources, and much more.</p>
<p>I&#8217;m really excited about sandboxing and also really terrified. Apple has given us, thus far, a limiting set of entitlements that don&#8217;t quite cover everything that reasonable apps want to do, or even everything that Apple itself has approved as acceptable behavior in the Mac App Store. Yet Apple has made it clear that it wants to see all apps adopt sandboxing, and the writing is on the wall that in particular, participants in the Mac App Store should be prepared for the day when non-sandboxed apps may not be approved for sale in the store.</p>
<p>For us developers looking into sandboxing our own apps, it can be tough to wrap one&#8217;s head around exactly what privileges need to be requested. One way to go about it is to sandbox your application with the strictest of controls (basically disallow everything disallowable), and see what breaks. Then you could add back whatever entitlements are necessary to get things working again.</p>
<p>On the other hand, it would be handier to have the system simply tell us what kinds of behaviors our app is engaging in, and what the corresponding entitlements would be to allow it to work even while sandboxed. Thanks to a tracing mechanism in the sandbox, this is in fact possible. Furthermore, you can use a command-line tool to apply arbitrary sandbox profiles to an application without having to modify the application itself.</p>
<p>I defined a handy shortcut in zsh for running an arbitrary app with the &#8220;trace&#8221; mechanism enabled, to show exactly what the app is accessing, simplify the output, and open it in my default text editor:</p>
<pre style="font-size: 1.0em;">function sbx()
{
        echo '(version 1)\n(trace "/tmp/traceout.sb")' &gt; /tmp/tracein.sb
        sandbox-exec -f /tmp/tracein.sb $1
        sandbox-simplify /tmp/traceout.sb &gt; /tmp/tracesimple.sb
        open -t /tmp/tracesimple.sb
}</pre>
<p>After you&#8217;ve defined this in your .zshrc (other shells, you are on your own!), you can do something like:</p>
<pre style="font-size: 1.0em;"><span>sbx /Applications/FastScripts</span>.app/Contents/MacOS/FastScripts</pre>
<p>Then you use whatever features in your app you are concerned about, and quit the app. A text file will open with exquisite details about all the privileged actions your app was permitted to do, which would otherwise be forbidden by the sandbox.  Great, just copy that list of permissions into your sandbox entitlements plist, and we&#8217;re done. Right? Not quite.</p>
<p>The rules generated by the trace are very precise and may not be sufficient to cover your app&#8217;s behavior in practice. For example, if I open FastScripts, my scripting utility, and run a single AppleScript that controls the terminal, the resulting permissions trace reveals a sandbox rule that would allow that behavior to happen:</p>
<pre style="font-size: 1.0em;">(allow appleevent-send
       (appleevent-destination "com.apple.terminal"))
</pre>
<p>That&#8217;s well and good for a utility that only ever needs to send events to the Terminal, but of course FastScripts is a general purpose scripting application that needs to send events &#8220;wherever the heck the user wants to send them.&#8221; Currently, Apple doesn&#8217;t offer a sandbox entitlement for this broad behavior, so it is not possible to sandbox FastScripts.</p>
<p>I think that Apple would have a lot more developer enthusiasm for this feature if it wasn&#8217;t so clear to many of us that our apps will be forced to lose features in order to adopt sandboxing. And while users may be happy about the prospects of improved security with the sandbox, I think there will be less excitement about the diminished functionality of apps whose features don&#8217;t fit nicely into the sandbox confines.</p>
<p>Developers and power-users can use the sandbox command-line tools now to get a good sense for what will or will not work down the road if sandboxing, with the current set of entitlements, is enforced by Apple for a large number of 3rd party applications. There is some documentation for these tools in e.g. &#8220;man sandbox-exec&#8221;, but the documentation is pretty minimal. If you want to read more, check out this <a href="http://reverse.put.as/wp-content/uploads/2011/09/Apple-Sandbox-Guide-v0.1.pdf">useful document</a>, which aims to give a better understanding of the sandbox, entitlement profiles, and how to use the command-line tools.</p>
<p> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.red-sweater.com/blog/2170/sandbox-corners/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>The Power Of Plist</title>
		<link>http://www.red-sweater.com/blog/2083/the-power-of-plist</link>
		<comments>http://www.red-sweater.com/blog/2083/the-power-of-plist#comments</comments>
		<pubDate>Wed, 03 Aug 2011 19:12:50 +0000</pubDate>
		<dc:creator>Daniel Jalkut</dc:creator>
				<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Darwin]]></category>
		<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Hacking]]></category>
		<category><![CDATA[Xcode]]></category>

		<guid isPermaLink="false">http://www.red-sweater.com/blog/?p=2083</guid>
		<description><![CDATA[Most Mac and iOS developers know that when you build an application, you advertise a number of details about the application in the &#8220;Info.plist&#8221; file, located inside the application bundle. You can examine any application on your Mac and see what kind of information the developer has conveyed about it: Navigate to a .app file [...]]]></description>
			<content:encoded><![CDATA[<p>Most Mac and iOS developers know that when you build an application, you advertise a number of details about the application in the &#8220;Info.plist&#8221; file, located inside the application bundle. You can examine any application on your Mac and see what kind of information the developer has conveyed about it:</p>
<ol>
<li>Navigate to a .app file in the Finder, e.g. /Applications/Mail.app</li>
<li>Control-click the app icon and select &#8220;Show Package Contents&#8221;</li>
<li>Navigate to Contents/Info.plist</li>
<li>Open with a plist editor or your favorite text editor.</li>
</ol>
<p>These files advertise a number of interesting attributes including the application&#8217;s official version and copyright strings. They also include important clues to the system about what kinds of files the application knows how to read and write, and what its minimum system requirements are.</p>
<h3>Minimum System Requirements</h3>
<p>Declaring the right Info.plist values not only allows a developer to control which operating systems a product should run on, but which of the application&#8217;s architectures should be considered for a particular operating system. For example, <a href="http://www.red-sweater.com/blackink/">Black Ink</a> uses a number of keyed values describing its requirements:</p>
<pre>	&lt;key&gt;LSArchitecturePriority&lt;/key&gt;
	&lt;array&gt;
		&lt;string&gt;x86_64&lt;/string&gt;
		&lt;string&gt;i386&lt;/string&gt;
		&lt;string&gt;ppc&lt;/string&gt;
	&lt;/array&gt;
	&lt;key&gt;LSMinimumSystemVersion&lt;/key&gt;
	&lt;string&gt;10.4.0&lt;/string&gt;
	&lt;key&gt;LSMinimumSystemVersionByArchitecture&lt;/key&gt;
	&lt;dict&gt;
		&lt;key&gt;i386&lt;/key&gt;
		&lt;string&gt;10.4.0&lt;/string&gt;
		&lt;key&gt;x86_64&lt;/key&gt;
		&lt;string&gt;10.6.0&lt;/string&gt;
		&lt;key&gt;ppc&lt;/key&gt;
		&lt;string&gt;10.4.0&lt;/string&gt;
	&lt;/dict&gt;
</pre>
<p>Notice the fidelity of some of these options. Black Ink is an application that is compiled with binary code for three separate architectures embedded into it: 64-bit Intel, 32-bit Intel, and 32-bit PowerPC. In order to give the user the best possible experience after they double-click the application icon, the system consults the Info.plist values to determine which binary is most appropriate to load on the current system.</p>
<p>The options for Black Ink start by indicating that all things being equal, it prefers to run as 64-bit Intel, but will settle for 32-bit Intel or PowerPC in a pinch. Next, it conveys that it should not be run on any operating system earlier than Mac OS X 10.4. Finally, it adds an additional restriction by architecture, stating that in the case of 64-bit Intel, the system should consider Mac OS X 10.6 the minimum required OS.</p>
<p>Why so picky? For example, Mac OS X 10.5 will run on computers that are new enough to support 64-bit Intel code, but the 10.5 operating environment itself doesn&#8217;t contain enough compatible libraries to make it very useful to a full-featured Cocoa application. So we need to convince the 10.5 system <em>to ignore our 64-bit Intel code.</em> And this is how it&#8217;s done.</p>
<h3>Embedding Info.plist</h3>
<p>This is all well and good for applications, but what about other code that may be run on a variety of hardware and operating systems? In particular, command-line tools do not come in the same bundled-folder format that applications do. Fortunately, Apple thought of this when they implemented support in their developer tools for embedding the contents of an Info.plist directly into the binary itself. You just have to pass the Info.plist path to Apple&#8217;s linker tool when it&#8217;s constructing your binary file. (Thanks to <a href="http://www.noodlesoft.com/">Paul Kim</a> for reminding me of this functionality). What this means in practice is adding arguments like this to your Xcode target&#8217;s &#8220;Other Linker Flags&#8221; build setting:</p>
<pre>-sectcreate __TEXT __info_plist $(INFOPLIST_FILE)
</pre>
<p>This instructs the linker to crete a new text segment named &#8220;__info_plist&#8221; in the resulting binary, and to fill it with the contents of the referenced Info.plist file. To make sure the INFOPLIST_FILE expands correctly, just make sure you set it in the separate build setting for identifying the Info.plist (the one you&#8217;d normally use in an app, and that is just ignored by default for a command-line tool target).</p>
<p>I brushed up on all this information because I wanted to help ensure that some recent work I did to create a <a href="https://github.com/danielpunkass/peg-multimarkdown">standalone MultiMarkdown tool</a> would be backward compatible with older systems. This required <a href="https://github.com/danielpunkass/peg-multimarkdown/commit/ca523bac1fb03b9cfdcf540805e677086c063704">adding an embedded plist</a> much as I&#8217;m describing here.</p>
<p>(<strong>Note:</strong> I have gotten some feedback to the effect that the __info_plist segment&#8217;s minimum system version requirements may not actually be consulted properly on Mac OS X 10.5. This would be a bummer, and in fact sort of reject the whole point of embedding the Info.plist in this case. I&#8217;m going to see if I can confirm whether it doesn&#8217;t in fact work.)</p>
<p>You could technically embed all kinds of stuff in the binary, with different section names. But the contract in this case is that when Mac OS X is determining information about an executable, it will consult <strong>the bundle&#8217;s Info.plist file</strong>, if it happens to be an executable in a bundle, or <em>the binary&#8217;s __info_plist segment</em>, if it happens to have one.</p>
<p>Another common reason for needing to embed Info.plist information into a standalone binary is if you are using Apple&#8217;s code signing mechanism to sign an individual binary tool. Code signing uses information from the Info.plist of a product, and in the case of standalone binaries, the only product-cohesive place to put this is in the binary itself.</p>
<h3>Examining Binary Info.plists</h3>
<p>After you&#8217;ve performed the magic above, you&#8217;ll probably be keen to inspect your command-line tool&#8217;s Info.plist to see if it worked. To do this, you can use the otool command from the Finder. If you run otool with the &#8220;-l&#8221; option to show all load commands, it will include the load command that identifies the __info_plist segment. Use grep to narrow it down to the part you&#8217;re interested in:</p>
<pre>otool -l ./yourTool | grep info_plist -B1 -A10
</pre>
<p>Any results at will confirm that there is Info.plist information in the binary, but how does it look? The &#8220;-s&#8221; option to otool lets you examine the contents of a specific segment in a binary, but it dumps it out as hex code. Fortunately, another system-bundled tool called &#8220;xxd&#8221; is capable of easily converting between hex-dump and text formats. As it turns out, some of Apple&#8217;s bundled developer tools (at least on my Mac) contain these embedded __info_plist sections, so if you don&#8217;t have a command line tool of your own to try this on, you can peek at what the &#8220;atos&#8221; tool has to say for itself:</p>
<pre>otool -s __TEXT __info_plist /usr/bin/atos | xxd -r
</pre>
<h3>In Summary</h3>
<p>Info.plist values are important for conveying information about your product, from the crudest, most obvious stuff like version number, to the most nuanced details such as which architectures are supported. Knowing how to specify an application or tool&#8217;s requirements as precisely as possible will ensure that the product performs optimally on whatever supported system your user happens to be using. I&#8217;ve barely scratched the surface here with a few common deployment keys that control execution behavior on Mac OS X. Be sure to skim Apple&#8217;s <a href="http://developer.apple.com/library/mac/#documentation/General/Reference/InfoPlistKeyReference/Introduction/Introduction.html">Information Property List Key Reference</a> to see if there are other useful bits of information you can share for your application <em>or for your command-line tool.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.red-sweater.com/blog/2083/the-power-of-plist/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Windowless Skyscraper</title>
		<link>http://www.red-sweater.com/blog/2065/windowless-skyscraper</link>
		<comments>http://www.red-sweater.com/blog/2065/windowless-skyscraper#comments</comments>
		<pubDate>Wed, 03 Aug 2011 15:11:09 +0000</pubDate>
		<dc:creator>Daniel Jalkut</dc:creator>
				<category><![CDATA[Business]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Motivation]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://www.red-sweater.com/blog/?p=2065</guid>
		<description><![CDATA[One of the thrills of software development is how much power is placed into the hands, literally, of a single engineer. Software takes work, and lots of it. But thanks to frameworks of reusable code, individuals are consistently able to &#8220;outbuild&#8221; the work of our predecessors, while exerting the same or less effort. Consider the work [...]]]></description>
			<content:encoded><![CDATA[<p>One of the thrills of software development is how much power is placed into the hands, literally, of a single engineer. Software takes work, and lots of it. But thanks to frameworks of reusable code, individuals are consistently able to &#8220;outbuild&#8221; the work of our predecessors, while exerting the same or less effort.</p>
<p>Consider the work that went into the first word processors and web browsers, whose work can now be mimicked by a Mac developer who knows how to embed <a href="http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSTextView_Class/Reference/Reference.html">NSTextView</a> or or <a href="http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/WebKit/Classes/WebView_Class/Reference/Reference.html">WebView</a>. Imagine the time that early game developers invested in rudimentary sprite drawing, animation, and collision, which is now achieved easily in iOS games with open source packages such as <a href="http://www.cocos2d-iphone.org/about">cocos2d</a>.</p>
<p>Still, building truly great software continues to elude most developers. It&#8217;s easy to assume that a large amount of time invested in a project, combined with an impressive outcome, is the key to a successful product. But it&#8217;s not so simple.</p>
<p>The <a href="http://en.wikipedia.org/wiki/Ninety-ninety_rule">ninety-ninety rule</a> is often cited when describing the challenge of finessing a product after most of the hard work has seemingly been done:</p>
<blockquote>
<p>&#8220;The first 90 percent of the code accounts for the first 90 percent of the development time. The remaining 10 percent of the code accounts for the other 90 percent of the development time.&#8221; &#8212; Tom Cargill</p>
</blockquote>
<p>I agree with this perspective, and I cite it often when discussing software with others. The rule as stated is in terms of &#8220;code&#8221; but is also applicable to all the visual design, user experience, marketing, and positioning of a product. I think the rule is also recursive: whatever is missing can always be split up such that an &#8220;easy&#8221; 90% of the job can be done, leaving an elusive 10% of finesse work.</p>
<p>Many developers mistakenly assume that after they&#8217;ve put a ton of work into a project, and it achieves some impressive feat, that their work is done. When you focus on the enormity of work completed thus far, it&#8217;s tempting to pat yourself on the back and call it a day.</p>
<p>But customers don&#8217;t care about the hard work that went into the 90%. They only notice the flaws or omissions in the 10%. Imagine if Michelangelo&#8217;s statue of David was perfect in every detail, except that where David&#8217;s face should be, a clump of ragged, unfinished marble lay crudely in its place. Instead of marveling at the exquisite details of David&#8217;s feet, torso, and arms, viewers would reject it as unfinished and clumsy.</p>
<p>An important takeaway for software developers is that the missing 10%, or the missing one-tenth of 10%, may be something that will take a great deal of work to get right, but it may be something you simply overlooked the importance of.</p>
<p>You might build an impressive skyscraper projecting 40 stories into the sky. The architecture, interior design, plumbing, and electrical work may all be superior to that of your peers. But if some blindness in your product vision prevents you from adding windows, the product will never sell. It doesn&#8217;t matter how much time or effort went into this masterpiece: nobody will live in a windowless skyscraper.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.red-sweater.com/blog/2065/windowless-skyscraper/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Safari Keychain Woes</title>
		<link>http://www.red-sweater.com/blog/2039/safari-keychain-woes</link>
		<comments>http://www.red-sweater.com/blog/2039/safari-keychain-woes#comments</comments>
		<pubDate>Fri, 29 Jul 2011 20:57:24 +0000</pubDate>
		<dc:creator>Daniel Jalkut</dc:creator>
				<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Lion]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://www.red-sweater.com/blog/?p=2039</guid>
		<description><![CDATA[I&#8217;ve been running OS X Lion for a long time and generally enjoying it. But at some point along the line, Safari 5.1 stopped correctly inserting my keychain passwords into forms for me. For the longest time, I assumed it was simple mistakes: I had neglected to ever approve &#8220;remember this password,&#8221; or I had [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been running OS X Lion for a long time and generally enjoying it. But at some point along the line, Safari 5.1 stopped correctly inserting my keychain passwords into forms for me.</p>
<p>For the longest time, I assumed it was simple mistakes: I had neglected to ever approve &#8220;remember this password,&#8221; or I had somehow invalidated the password in my keychain. But recently I have come to realize that Safari is no longer auto-filling passwords for me on any site. Ever. This sucks. Usually when something like this happens, the first thing I do is &#8220;Repair Keychain&#8221; from the Keychain Access application. But in this case, the keychain is reportedly not damaged. No help.</p>
<p>To research this problem, I started the way I usually do: Googling it. While there are <a href="http://www.google.com/search?client=safari&amp;rls=en&amp;q=safari+keychain+lion&amp;ie=UTF-8&amp;oe=UTF-8">many promising search results</a>, most of them lead to &#8220;solutions&#8221; that are not applicable to my scenario. Most commonly it seems the user has somehow ended up with the appropriate &#8220;User names and passwords&#8221; checkbox being turned off in Safari&#8217;s AutoFill preferences.</p>
<p>So how does a programmer like myself approach a problem like this? In recent years, Apple&#8217;s Instruments application has become an incredible asset for tracking down weird behavior not only in my own apps, but in other apps and in the system itself. I opened it up and started a new &#8220;Time Profiler&#8221; instrumentation session, targeting Safari. I know that Safari <em>used to</em> autofill my password whenever I typed in an account name that it recognized, so I start Instruments &#8220;sampling&#8221; Safari while I type my PayPal account name into the PayPal login page, and stop it after I&#8217;m done.</p>
<p>I want to figure out where Safari is failing to get my password, so I take a wild leap and assume the corresponding function call in Safari contains the word &#8220;password.&#8221; Typing this into the Instruments search box yields promising results:</p>
<p><img title="Instruments.png" src="http://www.red-sweater.com/blog/wp-content/downloads/2011/07/Instruments.png" border="0" alt="Instruments" width="448" height="103" /></p>
<p>Now I know that Safari is at least <em>making an effort</em> to fetch the corresponding password for me. I see that it&#8217;s calling -[NSURLCredential password], which in turn calls a lower-level Keychain Services function, SecItemCopyMatching(). But why isn&#8217;t it filling in my password?</p>
<p>At this point I switch to gdb, where i can attach to the running instance of Safari and dig a little deeper into the issue.</p>
<pre>% gdb
(gdb) attach Safari
(gdb) b -[NSURLCredential password]
Breakpoint 1 at 0x7fff8ddff4af
(gdb) c
Continuing.
</pre>
<p>I know from Instruments that Safari will call this method when it tries to autofill my password, so I go back to Safari and enter my PayPal ID again. Sure enough, Safari &#8220;freezes&#8221; indicating that gdb has interrupted it where my breakpoint was set. Now, I realize this isn&#8217;t for the faint of heart, but you can use gdb to effectively debug an application, even if you don&#8217;t have the source code. You just have to know a bit about how Apple&#8217;s compiler translates source code into corresponding assembly language instructions, and where the <a href="http://www.clarkcox.com/blog/2009/02/04/inspecting-obj-c-parameters-in-gdb/">arguments are placed</a> when calling methods.</p>
<pre>Breakpoint 1, 0x00007fff8ddff4af in
-[NSURLCredential password] ()
(gdb) d
Delete all breakpoints? (y or n) y
(gdb) po $rdi
&lt;NSURLCredential: 0x7ffd34d62920&gt;: paypal@jalkut.com
(gdb) po [$rdi password]
2: x/i $pc  0x7fff9192760e &lt;gdb_class_getClass+4&gt;:
	mov    0x20(%rdi),%rax
Can't print the description of a NIL object.
(gdb)
(gdb) p (int) [$rdi hasPassword]
$11 = 1
</pre>
<p>What I&#8217;ve done here is first delete the breakpoint, so I don&#8217;t get interrupted while poking around from gdb itself. Then, I display the NSURLCredential object&#8217;s description, confirming it corresponds to the account I&#8217;m trying to log into. Finally, I call the password method on this NSURLCredential object myself, so I can see quickly and easily what the response will be. Gdb&#8217;s objection to trying to print a NIL object confirms that <em>the password is not being returned.</em> I am further able to confirm with NSURLCredential&#8217;s &#8220;hasPassword&#8221; method that the keychain <em>does have a password for this account</em>, but it isn&#8217;t being returned.</p>
<p>Here we have a situation where apparently Safari is trying to get my password, but the system is vexing it. Let&#8217;s confirm by setting a breakpoint a little deeper, at the Keychain Services level that we also noticed in Instruments.</p>
<pre>(gdb) b SecItemCopyMatching
Breakpoint 3 at 0x7fff85919582
(gdb) c
Continuing.

Breakpoint 3, 0x00007fff85919582 in SecItemCopyMatching ()
(gdb) po $rdi
{
    acct = "paypal@jalkut.com";
    atyp = form;
    cdat = "2007-06-02 16:17:01 +0000";
    class = inet;
    desc = "Web form password";
    icmt = default;
    labl = "www.paypal.com (paypal@jalkut.com)";
    "m_Limit" = "m_LimitOne";
    mdat = "2011-07-29 15:30:15 +0000";
    port = 0;
    ptcl = htps;
    "r_Data" = 1;
    srvr = "www.paypal.com";
}
</pre>
<p>So far, so good. It looks like NSURLCredential is properly passing a set of criteria to the keychain, describing the kind of password item it&#8217;s looking for. I take a peek at the second argument to SecItemCopyMatching, which is a pointer to where the result will be stored, and then I ask gdb to continue executing until the function is complete.</p>
<pre>(gdb) x/x $rsi
0x7fff62eb3590:	0x0000000000000000
(gdb) fin
Run till exit from #0  0x00007fff85919582 in SecItemCopyMatching ()
0x00007fff86417c29 in URLCredentialInternetPassword::copyPassword ()
</pre>
<p>When gdb returns control to me, I know the function has finished calling, so I examine the result area in memory.</p>
<pre>(gdb) x/x 0x7fff62eb3590
0x7fff62eb3590:	0x0000000000000000
</pre>
<p>Indeed, it&#8217;s NULL! What is going on, here? I decided to dust off my <a href="http://www.red-sweater.com/blog/170/usable-keychain-scripting">Usable Keychain Scripting</a> tool, which makes it easy to use AppleScript to search and inspect the keychain. Is the inability to access the password a system-wide thing, or just something that is vexing Safari?</p>
<pre>tell application "Usable Keychain Scripting"
	tell current keychain
		password of keychain item "www.paypal.com (paypal@jalkut.com)"
	end tell
end tell
</pre>
<p>The script works perfectly, first causing the system to ask permission to authorize &#8220;Usable Keychain Scripting&#8221; to access the keychain item, and then returning the expected password string. This got me thinking, is it possible I&#8217;ve somehow got Keychain Accessing refuse access only to Safari?</p>
<p>When I look at the &#8220;Access Controls&#8221; for the affected keychain item, it lists Safari as one of the permitted apps. What the hell, I think. I&#8217;ll remove it from the list, and then re-add it. To try to &#8220;dislodge&#8221; something. Confoundingly, everytime I try to add it, it just silently fails to appear in the list. I am able to add any other app without an issue.</p>
<p><img title="AuthorizedApps.png" src="http://www.red-sweater.com/blog/wp-content/downloads/2011/07/AuthorizedApps.png" border="0" alt="AuthorizedApps" width="454" height="330" /></p>
<p>As a quick check, I change the setting to &#8220;Allow all applications to access this item,&#8221; and save the changes. When I return to Safari and type in the login name, it instantly fills in the password for me.</p>
<p>My system has a functional keychain but Safari seems unable to be authorized to access it, either on a one-time basis or as a &#8220;always allow&#8221; permission. I finally got the clever idea to look at /var/log/secure.log, which frankly, I should have done about 5 hours earlier. What&#8217;s this?</p>
<pre>com.apple.SecurityServer[33]: suppressing keychain prompt
for invalidly signed client /Applications/Safari.app(2938)
</pre>
<p>Aha! So that would explain it. Funny, I don&#8217;t <em>remember</em> tweaking anything inside the Safari application bundle, but this is certainly a very real example of how a broken code signature on Mac OS X can cause extremely subtle, hard to track-down &#8220;bugs.&#8221; I re-signed the applicaton on my own:</p>
<pre>codesign -f -s - /Applications/Safari.app
</pre>
<p>When I reopened the app, and went to PayPal&#8217;s site, voila! It prompted me for permission, I approved, and now everything is back to normal.</p>
<p>I examined the Safari.app bundle contents to try to remember what I might have tweaked. Then my memory was jogged. I had opened and edited Safari&#8217;s Info.plist file to play around with some settings. Don&#8217;t ask, it&#8217;s an even longer story than this.</p>
<p>I take full responsibility for getting myself into this mess. I&#8217;ll certainly be more careful in the future to consider the implications of breaking code signatures on apps. However, I do think that the failure behavior could have been more informative. That NSLog I found in the console about refusing to prompt the user because of a broken code signature, could have been presented as a dialog that would instantly inform the user something is fishy. If my copy of Safari <em>actually had been compromised</em> by a nefarious individual, I wouldn&#8217;t have thought twice about continuing to enter my passwords and trying my damnedest to get Keychain to share my passwords with the compromised application.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.red-sweater.com/blog/2039/safari-keychain-woes/feed</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Usable Keychain Scripting For Lion</title>
		<link>http://www.red-sweater.com/blog/2035/usable-keychain-scripting-for-lion</link>
		<comments>http://www.red-sweater.com/blog/2035/usable-keychain-scripting-for-lion#comments</comments>
		<pubDate>Fri, 29 Jul 2011 19:15:43 +0000</pubDate>
		<dc:creator>Daniel Jalkut</dc:creator>
				<category><![CDATA[AppleScript]]></category>
		<category><![CDATA[Hacking]]></category>

		<guid isPermaLink="false">http://www.red-sweater.com/blog/?p=2035</guid>
		<description><![CDATA[I&#8217;m tracking down a mysterious behavior of Safari in Lion, where it seems to fail to enter my password for logins that I&#8217;ve saved to the keychain. In the process of looking into this, I noticed that &#8220;Keychain Scripting&#8221; has mysteriously disappeared from Lion. As far as I can tell you must copy Apple&#8217;s scripting addition [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m tracking down a mysterious behavior of Safari in Lion, where it seems to fail to enter my password for logins that I&#8217;ve saved to the keychain. In the process of looking into this, I noticed that &#8220;Keychain Scripting&#8221; has mysteriously disappeared from Lion. As far as I can tell you must copy Apple&#8217;s scripting addition from Snow Leopard in order to keep using it.</p>
<p>On the other hand, I wrote an alternative years ago, called <a href="http://www.red-sweater.com/blog/170/usable-keychain-scripting">Usable Keychain Scripting</a>. Its main advantage over Apple&#8217;s implementation is that it is (or at least, was) enormously faster. Today I updated the app to be 64-bit compatible and to fix a pernicious bug in which the password value returned for a keychain item would sometimes have garbage appended to the end of it.</p>
<p><a href="http://red-sweater.com/blog/downloads/UsableKeychainScripting.dmg">Download Usable Keychain Scripting 1.0b4</a></p>
<p>This is not a supported product, and your success with it may vary. But it has been very handy in the past for me, and hopefully it will come in handy for you if you need to script the keychain.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.red-sweater.com/blog/2035/usable-keychain-scripting-for-lion/feed</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Restore Safari&#8217;s Downloads Keyboard Shortcut</title>
		<link>http://www.red-sweater.com/blog/1949/restore-safaris-downloads-keyboard-shortcut</link>
		<comments>http://www.red-sweater.com/blog/1949/restore-safaris-downloads-keyboard-shortcut#comments</comments>
		<pubDate>Fri, 22 Jul 2011 18:51:35 +0000</pubDate>
		<dc:creator>Daniel Jalkut</dc:creator>
				<category><![CDATA[AppleScript]]></category>
		<category><![CDATA[Lion]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://www.red-sweater.com/blog/?p=1949</guid>
		<description><![CDATA[I&#8217;m pretty excited about most of the enhancements in OS X Lion, and in Safari 5.1, which was released along with it. But one of the most annoying changes in the version of Safari that ships with Lion is the removal of any keyboard shortcut for showing and hiding the active downloads list. Downloads used [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m pretty excited about most of the enhancements in OS X Lion, and in Safari 5.1, which was released along with it. But one of the most annoying changes in the version of Safari that ships with Lion is the removal of any keyboard shortcut for showing and hiding the active downloads list.</p>
<p>Downloads used to be shown in a completely separate window, which could be toggled using the keyboard shortcut Cmd-Opt-L. In Lion, they appear in a popover panel attached to the toolbar of whatever browser window you happen to be using. Unfortunately, there is no keyboard shortcut to toggle the appearance of this popover.</p>
<p>Using <a href="http://www.red-sweater.com/fastscripts/">FastScripts</a> and a simple UI Scripting script, I was able to restore this functionality, so that Safari on Lion toggles the panel using the familiar Cmd-Opt-L shortcut.</p>
<p><a href="http://www.red-sweater.com/AppleScript/ToggleDownloadsPopover.zip">Download the &#8220;Toggle Downloads Popover&#8221; script</a></p>
<p>Download the script, and copy it to:</p>
<p>[Home] -&gt; Library -&gt; Scripts -&gt; Applications -&gt; Safari</p>
<p>Here it will show up in FastScripts (or Apple&#8217;s script menu) only when Safari is the front-most app. You can also assign it a keyboard shortcut, like Cmd-Opt-L, that will only be active when Safari is active.</p>
<p><strong>Important: </strong>If your Mac is not configured to run with English as the primary language, the script will not work without a minor adjustment. You will need to open up the script and change the text string &#8220;Downloads&#8221; to the language-specific description for the downloads panel in your language. For example, to make it work with Safari running in Spanish, you would change the string to &#8220;Descargas&#8221;.</p>
<p>I find it very useful to be able to popup the panel when I am checking on the status of a long download, or when I want to check quickly whether I already downloaded something I had intended to. Hope this script works well for you as well!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.red-sweater.com/blog/1949/restore-safaris-downloads-keyboard-shortcut/feed</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
		<item>
		<title>Bit Hacking</title>
		<link>http://www.red-sweater.com/blog/1947/bit-hacking</link>
		<comments>http://www.red-sweater.com/blog/1947/bit-hacking#comments</comments>
		<pubDate>Thu, 21 Jul 2011 14:28:37 +0000</pubDate>
		<dc:creator>Daniel Jalkut</dc:creator>
				<category><![CDATA[Apple]]></category>
		<category><![CDATA[Hacking]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.red-sweater.com/blog/?p=1947</guid>
		<description><![CDATA[Lion is the first operating system to require, and to fully take advantage of, 64-bit addressing modes in the Intel chips that power Apple&#8217;s Macintosh computers. One of the side-effects of this is that every object identifier in Mac OS X&#8217;s Cocoa programming framework (typically an address in memory), is now twice as long as [...]]]></description>
			<content:encoded><![CDATA[<p>Lion is the first operating system to require, and to fully take advantage of, 64-bit addressing modes in the Intel chips that power Apple&#8217;s Macintosh computers. One of the side-effects of this is that every object identifier in Mac OS X&#8217;s Cocoa programming framework (typically an address in memory), is now twice as long as it was in a 32-bit environment.</p>
<p>Apple has apparently taken advantage of the 64-bit runtime in Lion by optimizing the Objective C runtime itself to use some of these extra bits for, shall we say, clever purposes. <a href="http://objectivistc.tumblr.com/">Bavarious describes an optimization</a> through which Apple is able to replace previously full-fledged opaque objects such as NSNumber with an object-placeholder that exists entirely as the 64-bit &#8220;object address&#8221; itself. This means that, for a wide range of &#8220;simple&#8221; objects, no additional memory allocation is required, and no retain/release memory management is required for the &#8220;object.&#8221;</p>
<p>The trick relies on a implementation detail of the system, that allocated blocks of memory will always be aligned at 16-byte offsets into the address space. This leaves a bunch of numbers that can be represented in 64-bits, that cannot reasonably be assigned to any other object. To understand this practically, imagine that your neighborhood&#8217;s postal addresses are all assigned at offsets of 10: 30, 40, 50, etc. A clever postal service could institute an addressing system that uses an &#8220;invalid&#8221; address such as &#8220;31,&#8221; to perhaps mean &#8220;deliver to 30 with expedited afternoon delivery.&#8221;</p>
<p>Cleverness like this with encoding extra information in memory addresses is a time-honored tradition. I recall the days of 24-bit addressing on classic Mac OS, where Apple, and many 3rd party developers, observed that the high 8 bits of a typical memory address could be tweaked and used to store additional information, because the system would never reference those bits when resolving a particular address.</p>
<p>In those days, using those extra bits turned out to be a pretty significant headache when 32-bit addressing ultimately came along, and lots of code had this &#8220;crufty&#8221; treatment of addresses to clean up. Perhaps it is a memory of situations like this that caused Jon &#8220;Wolf&#8221; Rentzsch to <a href="http://www.delicious.com/url/a01d14b2e7785507e55d15edf3a131d6">comment in his bookmarking</a> of the above-referenced blog post:</p>
<blockquote>
<p>&#8220;Every tagged pointer has its lowest bit set, hence tagged pointers are odd integers&#8221; <strong>Strikes me as a really bad idea. </strong>[Emphasis Mine]</p>
</blockquote>
<p>But the difference now, in this scenario, is the &#8220;cute hacking&#8221; is all being done by a central power, with and in terms of opaque objects that only Apple has the authority to change. I think this is a really clever hack that will undoubtedly lead to some serious performance gains in Lion and beyond. It&#8217;s hard to imagine specific outcomes that will make Apple regret adopting this strategy. In the worst case scenario, an addressing system of future Macs will not leave any &#8220;spare&#8221; bits to be exploited, so the runtime will simply revert to its previous behavior.</p>
<p> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.red-sweater.com/blog/1947/bit-hacking/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Core Data Model Merging</title>
		<link>http://www.red-sweater.com/blog/1862/core-data-model-merging</link>
		<comments>http://www.red-sweater.com/blog/1862/core-data-model-merging#comments</comments>
		<pubDate>Wed, 04 May 2011 16:27:44 +0000</pubDate>
		<dc:creator>Daniel Jalkut</dc:creator>
				<category><![CDATA[Apple]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.red-sweater.com/blog/?p=1862</guid>
		<description><![CDATA[I haven&#8217;t used Apple&#8217;s Core Data framework all that much, but I&#8217;m trying to dabble more in it with newer projects where I don&#8217;t rely as much on legacy data storage, or am willing to take the hit of migrating from those legacy persistence models. As a developer, the obvious upside to using Core Data [...]]]></description>
			<content:encoded><![CDATA[<p>I haven&#8217;t used Apple&#8217;s <a href="http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/cdProgrammingGuide.html">Core Data</a> framework all that much, but I&#8217;m trying to dabble more in it with newer projects where I don&#8217;t rely as much on legacy data storage, or am willing to take the hit of migrating from those legacy persistence models.</p>
<p>As a developer, the obvious upside to using Core Data is Apple&#8217;s powerful framework manages most of the nitty-gritty details of managing the &#8230; well, the data your app uses both in memory and on disk. But giving up precise control over the storage and mangement of this data also becomes maddening when you run into problems: you&#8217;re not directly responsible for the management, so you&#8217;re less likely to know exactly what is going wrong.</p>
<p>I ran into a problem recently having to do with <em>versioning</em> of core data model schemas, and how good a job the frameworks do or do not do of automatically updating existing data that a customer may have on disk. In recent years Apple has made it increasingly easy to make <em>minor changes</em> to your model schema and having the frameworks handle the migration easily. Apple calls this trivial technique <a href="http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/CoreDataVersioning/Articles/vmLightweight.html">lightweight migration</a>, and when it works well, it works very well. To modify your existing data model, you just add a new &#8220;version&#8221; of your xcdatamodel file, and add a few options flags when creating your persistent store:</p>
<pre>
NSDictionary *options =
   [NSDictionary dictionaryWithObjectsAndKeys:
   [NSNumber numberWithBool:YES],
     NSMigratePersistentStoresAutomaticallyOption,
   [NSNumber numberWithBool:YES],
     NSInferMappingModelAutomaticallyOption, nil];

[myPSC addPersistentStoreWithType:NSSQLiteStoreType
   configuration:nil URL:storeUrl
   options:options error:&amp;error];
</pre>
<p>This actually works quite well for simple addition or removal of entity attributes, but you might be forgiven for assuming its <em>completely freaking broken</em> if you run into a problem I did, having to do with a stale &#8220;.mom&#8221; file being left in the built application&#8217;s bundle in the iOS simulator and on iOS devices.</p>
<p>Essentially, if you&#8217;ve been building your project up to now with a single, unversioned xcdatamodel file, and you convert it to a versioned xcdatamodel file, it will start producing a new &#8220;.momd&#8221; file in your application bundle, and stop producing the older &#8220;.mom&#8221;. But because of a bug in the way Xcode installs the application binary in the simulator and devices, it will leave existing files from previous builds where they were in the bundle.</p>
<p>If, like many folks, you use the convenient [NSManagedObjectModel mergedModelFromBundles:nil] method to initialize your managed object model, this will, to use a technical term, bite you in the ass. The problem is Core Data looks at the .mom and the .momd files as equally viable, and attempts to merge them into some cohesive whole. When it discovers they both redundantly describe the same entities, it blows a gasket with a runtime error like &#8220;Can&#8217;t merge models with two different entities named &#8216;MyEntity&#8217;&#8221;</p>
<p>I did some googling around and discovered that the simplest solution is to simply delete the iOS app from the device or simulator, ensuring that the next time you install, you get a pristine, clean copy of the app without the troubling &#8220;.mom&#8221; file. But there&#8217;s a huge problem with that: in deleting the app you also delete its associated data files, and thus are left in a position where you aren&#8217;t actually testing the ability of Core Data to effectively migrate from your previously versioned data.</p>
<p>On the simulator, it&#8217;s no problem to dive in and trash the problematic, stale file. You can find the application binary somewhere in:</p>
<p>[Home] -&gt; Library -&gt; Application Support -&gt; iTunes Simulator</p>
<p>Once you&#8217;ve deleted the &#8220;.mom&#8221; file you can test with your new model and everything hopefully works great.</p>
<p>On the device, it&#8217;s not as easy (I think) to get in there and do pinpoint editing of the application contents, but I still wanted to test my changes against the real data on my device. I was also starting to get paranoid about the possibility of an App Store update at some point also causing a stale &#8220;.mom&#8221; file to be left in an app bundle, and wreaking havoc. So the 100% safe solution to make sure the one-true &#8220;.momd&#8221; file is consulted when building the Core Data object model, is to point directly at it:</p>
<pre>
NSString *momdPath = [[NSBundle mainBundle]
	pathForResource:@"MyModel" ofType:@"momd"];

NSURL *momdURL = [NSURL fileURLWithPath:momdPath];

managedObjectModel = [[NSManagedObjectModel alloc]
	initWithContentsOfURL:momdURL];
</pre>
<p>Hopefully seeding this solution into my blog will make it available for folks searching on similar failures in their Core Data projects.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.red-sweater.com/blog/1862/core-data-model-merging/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

