<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Tewha.net</title>
    <link>https://tewha.net/tags/automatic-reference-counting/</link>
    <description>Recent content on Tewha.net</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-us</language>
    <lastBuildDate>Thu, 27 Sep 2012 14:00:00 +0000</lastBuildDate>
    <atom:link href="https://tewha.net/tags/automatic-reference-counting/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Make your library enforce ARC</title>
      <link>https://tewha.net/2012/09/make-your-library-enforce-arc/</link>
      <pubDate>Thu, 27 Sep 2012 14:00:00 +0000</pubDate>
      <guid>https://tewha.net/2012/09/make-your-library-enforce-arc/</guid>
      <description>&lt;p&gt;If you have an open source library that requires Automatic Reference Counting (ARC), you may have issues with your users trying to build it without ARC turned on. Luckily, it’s pretty simple to &lt;em&gt;enforce&lt;/em&gt; ARC.&lt;/p&gt;
&lt;p&gt;I’m going to discuss how to do so, and why it’s a good idea.&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p>If you have an open source library that requires Automatic Reference Counting (ARC), you may have issues with your users trying to build it without ARC turned on. Luckily, it’s pretty simple to <em>enforce</em> ARC.</p>
<p>I’m going to discuss how to do so, and why it’s a good idea.</p>
<p>Here’s an example, which I submitted to AFNetworking:</p>
<pre><code>#if !__has_feature(objc_arc)
#error AFNetworking must be built with ARC.
// You can turn on ARC for only AFNetworking files by adding -fobjc-arc to the build phase for each of its files.
#endif
</code></pre>
<p>When the user builds the unit, the error will be highlighted. They’ll see the comment just below, explaining how to resolve the problem.</p>
<p>Adding it to every .m file may seem overkill, but there’s two good reasons for this:</p>
<ol>
<li>No matter which file gets compiled first an error will top the list instead of warnings. It’s sufficient to add it only to a central file, but you’ll probably still get support requests that way. You’re better off adding it everywhere; this is all about reducing your work.</li>
<li>ARC can be decided on a per-file basis. If you include it in only one file, the user may add -fobjc-arc to just that one file!</li>
</ol>
<p>But do not add this to your headers. Even if your code requires ARC, there’s no reason to require that the user’s code also use ARC. Apple did the right thing here.</p>]]></content:encoded>
    </item>
    <item>
      <title>Automatic Reference Counting</title>
      <link>https://tewha.net/2012/05/automatic-reference-counting/</link>
      <pubDate>Thu, 31 May 2012 14:00:00 +0000</pubDate>
      <guid>https://tewha.net/2012/05/automatic-reference-counting/</guid>
      <description>&lt;p&gt;If you’re an application developer: Yes, you should use Automatic Reference Counting (ARC).&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p>If you’re an application developer: Yes, you should use Automatic Reference Counting (ARC).</p>
<p>Odds are, the compiler understands Objective-C and Cocoa better than you. I don’t mean this as an insult; it certainly understands it better than me. I think you could safely say it understands the rules better than all but maybe a dozen people worldwide. And it knows tricks to use them to a degree that you and I can’t repeat, even if we understood as well as it does.</p>
<p>The rest is just details, but they’re important:</p>
<ol>
<li>You will write a lot less boring code. Code so boring it’s easy to make mistakes.</li>
<li>As a blended compile time and run time process, it has access to tricks that you don’t.</li>
<li>It will a better a job of writing memory management code than you can, even if you write the theoretical perfect memory management code.</li>
<li>It will reduce “high tide” memory usage (somewhat) without any effort on your part.</li>
</ol>
<p>If you are starting a new application, stop thinking about it and just use it.</p>
<p>If you have an existing application, you need to think about it. You’re balancing the ease of future development vs. a need to re-test the app. The ARC conversion isn’t awful, but it isn’t painless or foolproof either. You will need to make sure you have no circular references, and you will need to make sure that objects don’t disappear sooner than you think.</p>
<p>If you have an existing application that targets iOS before iOS 5, zeroing weak references are not supported. You can find people who will tell you that zeroing weak references aren’t a big deal. They’re wrong. While it’s true you can use assign to prevent circular references, weak references are what turns ARC from a useful way to write less code to a fantastic way to write stable applications. You should seriously consider requiring iOS 5.</p>
<p>If you have an existing application that targets iOS before iOS 4, you can’t use it at all. What are you thinking, supporting a version of iOS that old?</p>
<p>See also:</p>
<ul>
<li><a href="https://developer.apple.com/library/ios/#releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html#//apple_ref/doc/uid/TP40011226">Transitioning to ARC</a>: Apple’s ARC overview. A great overview, but possibly less than you need to know.</li>
<li><a href="http://clang.llvm.org/docs/AutomaticReferenceCounting.html">Automatic Reference Counting</a>: The living and detailed ARC specification and rationales. Certainly much more information than you require.</li>
</ul>]]></content:encoded>
    </item>
  </channel>
</rss>
