<?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/objective-c/</link>
    <description>Recent content on Tewha.net</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-us</language>
    <lastBuildDate>Thu, 12 Mar 2015 05:56:07 +0000</lastBuildDate>
    <atom:link href="https://tewha.net/tags/objective-c/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Info.plist for command line tools</title>
      <link>https://tewha.net/2015/03/info-plist-for-command-line-tools/</link>
      <pubDate>Thu, 12 Mar 2015 05:56:07 +0000</pubDate>
      <guid>https://tewha.net/2015/03/info-plist-for-command-line-tools/</guid>
      <description>&lt;p&gt;In the past if you wanted to include data in your command line app, you could either include it in a separate file or do some linker trickery to embed it. Personally, I never really got into the linker trickery (though it&amp;rsquo;s probably the better solution of the two).&lt;/p&gt;
&lt;p&gt;For a while, though, Xcode has supported a build option &lt;strong&gt;Create Info.plist Section In Binary&lt;/strong&gt;, also known as &lt;code&gt;CREATE_INFOPLIST_SECTION_IN_BINARY&lt;/code&gt;. If on, this will embed the target&amp;rsquo;s Info.plist into the executable as a data segment.&lt;/p&gt;
&lt;p&gt;I couldn&amp;rsquo;t find any documentation on how to get it out, though. After a lot of searching, I came up with &lt;code&gt;getsectbyname&lt;/code&gt;. This is defined in &lt;strong&gt;mach-o/getsect.h&lt;/strong&gt; like so:&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p>In the past if you wanted to include data in your command line app, you could either include it in a separate file or do some linker trickery to embed it. Personally, I never really got into the linker trickery (though it&rsquo;s probably the better solution of the two).</p>
<p>For a while, though, Xcode has supported a build option <strong>Create Info.plist Section In Binary</strong>, also known as <code>CREATE_INFOPLIST_SECTION_IN_BINARY</code>. If on, this will embed the target&rsquo;s Info.plist into the executable as a data segment.</p>
<p>I couldn&rsquo;t find any documentation on how to get it out, though. After a lot of searching, I came up with <code>getsectbyname</code>. This is defined in <strong>mach-o/getsect.h</strong> like so:</p>






<pre tabindex="0"><code>extern const struct section_64 *getsectbyname(
    const char *segname,
    const char *sectname);</code></pre>
<p>(There&rsquo;s a 32-bit version as well, but I was only interested in 64-bit.)</p>
<p>One of the fields in <code>section_64</code> was the address of the data in memory, another its size. Easy to wrap with a <code>NSData</code>, right?</p>
<p>Not so fast. This worked in debug <em>and</em> release builds when run in Xcode, but didn&rsquo;t work when run on Terminal or Instruments. There, accessing the raw bytes caused a segmentation fault.</p>
<p><a href="https://twitter.com/gparker/status/575527435539771392">A tweet from Greg Parker</a> put me on track.</p>
<p>The difference turned out to be <a href="https://en.wikipedia.org/wiki/Address_space_layout_randomization">Address space layout randomization (ASLR)</a>. You can read all about it on Wikipedia, but the gist of it is that in order to make it harder to exploit a vulnerability many operating systems have implemented a system where executables are randomly located in the address space. This makes it harder for injected code to access application or system code. The results returned by <code>getsectbyname</code> don&rsquo;t take ASLR into account.</p>
<p>Instead of using <code>getsectbyname</code>, I had to use <code>getsectiondata</code>. <code>getsectiondata</code> would return a pointer to the exact data I needed. It took me quite a while to find anything relevant online.</p>
<p>It&rsquo;s declared like this:</p>






<pre tabindex="0"><code>extern uint8_t *getsectiondata(
    const struct mach_header *mhp,
    const char *segname,
    const char *sectname,
    unsigned long *size);</code></pre>
<p>But what&rsquo;s that fist thing, the <code>mach_header</code>?</p>
<p>It took me a long time to find anything relevant, but I eventually found out that I needed to use <code>_mh_execute_header</code> from <strong>mach-o/ldsyms.h</strong>.</p>
<p>The resulting code looks like this, and works:</p>






<pre tabindex="0"><code>#include &amp;lt;mach-o/getsect.h&amp;gt;
#include &amp;lt;mach-o/ldsyms.h&amp;gt;

- (instancetype)init {
    self = [super init];
    if (!self) return nil;
    
    NSError *e;
    unsigned long size;
    void *ptr = getsectiondata(&amp;_mh_execute_header, &#34;__TEXT&#34;, &#34;__info_plist&#34;, &amp;size);
    NSData *plistData = [NSData dataWithBytesNoCopy:ptr length:size freeWhenDone:NO];
   _infoPlistContents = [NSPropertyListSerialization propertyListWithData:plistData options:NSPropertyListImmutable format:NULL error:&amp;e];

   return self;
}</code></pre>
<p>This only works in the application; if you need to do this from a dynamic library, try <a href="http://stackoverflow.com/questions/19848567/how-to-get-the-build-uuid-in-runtime-and-the-image-base-address/22674561#22674561">this approach from Cédric Luthi</a>.</p>
<p>Full credit to those two, but I wanted to post something about this so that future searches might find it. Thanks, guys!</p>]]></content:encoded>
    </item>
    <item>
      <title>C99 initializers</title>
      <link>https://tewha.net/2013/02/c99-initializers/</link>
      <pubDate>Fri, 08 Feb 2013 16:00:00 +0000</pubDate>
      <guid>https://tewha.net/2013/02/c99-initializers/</guid>
      <description>&lt;p&gt;You probably know &lt;code&gt;CGRectMake&lt;/code&gt;, but did you know it’s not the only way to make rectangles? It’s not even the best way, really.&lt;/p&gt;
&lt;p&gt;There’s also &lt;a href=&#34;http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8l.doc%2Flanguage%2Fref%2Fstrin.htm&#34;&gt;C99 initializer syntax&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The main advantage to the C99 syntax is that it gives you some very Objective-C like syntax, where the fields are close to the values rather than implied by positioning. (That’s not to say this is intentionally similar, or that it’s the only advantage. But it is nice.) It also provides type checking, and since fields are named it catches drifts in meaning that you otherwise wouldn’t catch.&lt;/p&gt;
&lt;p&gt;It’s sometimes slightly &lt;em&gt;more&lt;/em&gt; typing, but I use it everywhere now.&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p>You probably know <code>CGRectMake</code>, but did you know it’s not the only way to make rectangles? It’s not even the best way, really.</p>
<p>There’s also <a href="http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8l.doc%2Flanguage%2Fref%2Fstrin.htm">C99 initializer syntax</a>.</p>
<p>The main advantage to the C99 syntax is that it gives you some very Objective-C like syntax, where the fields are close to the values rather than implied by positioning. (That’s not to say this is intentionally similar, or that it’s the only advantage. But it is nice.) It also provides type checking, and since fields are named it catches drifts in meaning that you otherwise wouldn’t catch.</p>
<p>It’s sometimes slightly <em>more</em> typing, but I use it everywhere now.</p>
<h2 id="the_syntax">The syntax</h2>
<p>Consider the <code>CGRectMake</code> way to make a rectangle:</p>
<pre><code>CGRect a = CGRectMake(a+c/2, b+d/2, c, d);
</code></pre>
<p>In order to understand this, you need to understand the order of the parameters. You also need to be able to catch the commas easily with your eyes. In this case, that’s pretty easy, but if the expressions were more complicated you’d probably be storing them in a temporary variable first.</p>
<p>The C99 way:</p>
<pre><code>CGRect a = (CGRect){
    .origin.x = a+c/2,
    .origin.y = b+d/2,
    .size.width = c,
    .size.height = d
};
</code></pre>
<p>It’s longer, but it’s more explicit. It’s also very easy to follow what is assigned to what, no matter how long the expression are. It’s also more like an Objective-C method. After all, if <code>CGRect</code> was a class, it would probably look like this:</p>
<pre><code>CGRect a = [[CGRect alloc] initWithOriginX:x originY:y width:w height:h];
</code></pre>
<p>Note that any field you don’t explicitly initialize is initialized to zero. This code, for instance, will create a rectangle with an <code>origin</code> of 0,0.</p>
<pre><code>CGRect smallRect = (CGRect){
    .size = mySize
}
</code></pre>
<p>The C99 syntax is a nice compromise; it’s more compact than class construction, but still names field values.</p>
<h2 id="catching_changes">Catching changes</h2>
<p>Naming fields might seem like extra work, but it’s also extra safety. If the names or meanings of the parameters change, you’re going to get compiler errors. The compiler will help you find these, now! And if you write new code based on the old rules, compiler errors will point out what you’ve done.</p>
<p>This isn’t much of a concern for <code>CGRect</code>; it is how it is. Apple is probably never going to redefine it. But many years ago, QuickDraw used edges for its <code>Rect</code> structure instead of points. If you were still trying to break that habit, the compiler would complain every time you did this:</p>
<pre><code>CGRect a = (CGRect){
    .left = 0,
    .right = 100,
    .top = 0,
    .bottom = 100
};
</code></pre>
<p>In your own code, this may be an active concern to you. You may have <code>structs</code> (or use C libraries that have <code>structs</code>) that will change layout in the future.</p>
<h2 id="type_checking">Type checking</h2>
<p>That’s only the start of C99 initializer coolness, though.</p>
<p>You can also do things like this:</p>
<pre><code>CGRect a = (CGRect){
    .origin = myOrigin,
    .size = computedSize
};
</code></pre>
<p>Here, you’re building a rectangle using a <code>CGPoint</code> and <code>CGSize</code>. The compiler understands that <code>.origin</code> expects a <code>CGPoint</code>, and <code>.size</code> expects a <code>CGSize</code>. You’ve provided that. All’s gravy.</p>
<p>The equivalent code would be <code>CGRectMake(myOrigin.x, myOrigin.y, size.width, size.height)</code>. By using <code>CGRectMake</code> you’re no longer expressing the same kind of meaning to the compiler. It can’t stop you from assigning part of the size to the origin. It also won’t stop you from assigning the width to the height. It doesn’t even give you a good clue about which is the X and Y; if you’ve used APIs that provide vertical coordinates first, you’ll get it wrong. If you’re used to APIs that provide two points or something else (like QuickDraw’s edges) instead of a point and size you’ll also get a compiler error.</p>
<p>You can assign part from a structure and part from floats as well:</p>
<pre><code>CGRect a = (CGRect){
    .origin = myOrigin,
    .size.width = c,
    .size.height = d
};
</code></pre>
<h2 id="drawbacks">Drawbacks</h2>
<p>As I said, this is more typing than <code>CGRectMake</code>. What I didn’t say is that it’s even more typing than you expect, because as of Xcode 4.6, you still can’t autocomplete the field names. You really do need to type all that extra code.</p>
<p>I think this is a good pay off for being able to read the code so easily later, but you may disagree.</p>
<h2 id="conclusion">Conclusion</h2>
<p>The <code>CGRectMake</code> function predates C99. I have no evidence to this effect, but I think if C99 had come first <code>CGRectMake</code> probably wouldn’t exist at all; it’s the sort of crusty function you write when your language has no direct way to perform the initialization. But now it does.</p>
<p>This doesn’t only apply to <code>CGRect</code>. Any of the simple <code>struct</code> types can be initialized this way, such as <code>CGPoint</code>, <code>CGSize</code> and <code>NSRange</code>.</p>
<p>Avantages:</p>
<ul>
<li><strong>Explicit</strong>: You’ve named the parameters.</li>
<li><strong>Type safe</strong>: You’ve provided enough meaning to the compiler that it can <em>help you not screw up</em>.</li>
<li><strong>Easy to read</strong>: Especially with longer expressions, you’ve eliminated needing to scan for commas when you read the code later.</li>
</ul>
<p>Disadvantages:</p>
<ul>
<li><strong>More typing</strong>: You read code a lot more than you type it, so I think this is a great tradeoff.</li>
</ul>
<p>Averaged over time, anything you can screw up you eventually will. Using defensive techniques makes sense. Try it for a while; I think you’ll like it!</p>
<p>Special thanks to:</p>
<ul>
<li>
<p><a href="https://twitter.com/boredzo">Peter Hosey</a> for reminding me how QuickDraw worked and inspiring “Catching changes.”</p>
</li>
<li>
<p><a href="https://twitter.com/luka_bernardi">Luke Bernardi</a> for reminding me that Xcode’s autocompletion can’t suggest field names.</p>
</li>
<li>
<p><a href="https://twitter.com/bjhomer">BJ Homer</a> for tipping me that unnamed fields are initialized to 0.</p>
</li>
</ul>]]></content:encoded>
    </item>
    <item>
      <title>Why you should use instancetype instead of id</title>
      <link>https://tewha.net/2013/02/why-you-should-use-instancetype-instead-of-id/</link>
      <pubDate>Wed, 06 Feb 2013 16:00:00 +0000</pubDate>
      <guid>https://tewha.net/2013/02/why-you-should-use-instancetype-instead-of-id/</guid>
      <description>&lt;p&gt;In my previous entry, I discussed &lt;a href=&#34;https://tewha.net/2013/01/when-is-id-promoted-to-instancetype/&#34;&gt;when &lt;code&gt;id&lt;/code&gt; will be promoted to &lt;code&gt;instancetype&lt;/code&gt;&lt;/a&gt;. But now that I’ve explained this, I’d like to explain why you should &lt;em&gt;understand&lt;/em&gt; this but not &lt;em&gt;rely&lt;/em&gt; on it. Instead, you should use &lt;code&gt;instancetype&lt;/code&gt; directly.&lt;/p&gt;
&lt;p&gt;Let me start with this bold statement, then I’ll back up and explain it: Use &lt;code&gt;instancetype&lt;/code&gt; whenever it’s appropriate, which is whenever a class returns an instance of that same class.&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p>In my previous entry, I discussed <a href="/2013/01/when-is-id-promoted-to-instancetype/">when <code>id</code> will be promoted to <code>instancetype</code></a>. But now that I’ve explained this, I’d like to explain why you should <em>understand</em> this but not <em>rely</em> on it. Instead, you should use <code>instancetype</code> directly.</p>
<p>Let me start with this bold statement, then I’ll back up and explain it: Use <code>instancetype</code> whenever it’s appropriate, which is whenever a class returns an instance of that same class.</p>
<p>First, some definitions:</p>
<pre><code>@interface Foo:NSObject
- (id)initWithBar:(NSInteger)bar; // initializer
+ (id)fooWithBar:(NSInteger)bar;  // convenience constructor
@end
</code></pre>
<p>For a convenience constructor, you should <strong>always</strong> use <code>instancetype</code>. The compiler does not automatically convert <code>id</code> to <code>instancetype</code>.</p>
<p>For initializer, it’s more complicated. When you type this:</p>
<pre><code>- (id)initWithBar:(NSInteger)bar
</code></pre>
<p>…the compiler will pretend you typed this instead:</p>
<pre><code>- (instancetype)initWithBar:(NSInteger)bar
</code></pre>
<p>This was necessary for ARC. This is why people will tell you it isn’t necessary to use <code>instancetype</code>, though I contend you should. The rest of this answer deals with this.</p>
<p>There’s three advantages:</p>
<ol>
<li><strong>Explicit.</strong> Your code is doing what it says, rather than something else.</li>
<li><strong>Pattern.</strong> You’re building good habits for times it does matter, which do exist.</li>
<li><strong>Consistency.</strong> You’ve established some consistency to your code, which makes it more readable.</li>
</ol>
<h2 id="explicit">Explicit</h2>
<p>It’s true that there’s no <em>technical</em> benefit to returning <code>instancetype</code> from an <code>init</code>. But this is because the compiler automatically converts the <code>id</code> to <code>instancetype</code>. You are relying on this quirk; while you’re writing that the <code>init</code> returns an <code>id</code>, the compiler is interpreting it as if it returns an <code>instancetype</code>.</p>
<p>These are <em>equivalent</em> to the compiler:</p>
<pre><code>- (id)initWithBar:(NSInteger)bar;
- (instancetype)initWithBar:(NSInteger)bar;
</code></pre>
<p>These are not equivalent to your eyes. At best, you will learn to ignore the difference and skim over it. <strong>This is not something you should learn to ignore.</strong></p>
<h2 id="pattern">Pattern</h2>
<p>While there’s no difference with <code>init</code> and other methods, there <strong>is</strong> a different as soon as you define a convenience constructor.</p>
<p>These two are not equivalent:</p>
<pre><code>+ (id)fooWithBar:(NSInteger)bar;
+ (instancetype)fooWithBar:(NSInteger)bar;
</code></pre>
<p>You want the second form. If you are used to typing <code>instancetype</code> as the return type of a constructor, you’ll get it right every time.</p>
<h2 id="consistency">Consistency</h2>
<p>Finally, imagine if you put it all together: you want an <code>init</code> function and also a convenience constructor.</p>
<p>If you use <code>id</code> for <code>init</code>, you end up with code like this:</p>
<pre><code>- (id)initWithBar:(NSInteger)bar;
+ (instancetype)fooWithBar:(NSInteger)bar;
</code></pre>
<p>But if you use <code>instancetype</code>, you get this:</p>
<pre><code>- (instancetype)initWithBar:(NSInteger)bar;
+ (instancetype)fooWithBar:(NSInteger)bar;
</code></pre>
<p>It’s more consistent and more readable. They return the same thing, and now that’s obvious.</p>
<h2 id="conclusion">Conclusion</h2>
<p>Unless you’re intentionally writing code for old compilers, you should use <code>instancetype</code> when appropriate.</p>
<p>You should hesitate before writing a message that returns <code>id</code>. Ask yourself: Is this returning an instance of this class? If so, it’s an <code>instancetype</code>.</p>
<p>There are certainly cases where you need to return <code>id</code>; namely, if you’re returning a different class. But you’ll probably use <code>instancetype</code> much more frequently than <code>id</code>.</p>]]></content:encoded>
    </item>
    <item>
      <title>When is id promoted to instancetype?</title>
      <link>https://tewha.net/2013/01/when-is-id-promoted-to-instancetype/</link>
      <pubDate>Mon, 21 Jan 2013 16:00:00 +0000</pubDate>
      <guid>https://tewha.net/2013/01/when-is-id-promoted-to-instancetype/</guid>
      <description>&lt;p&gt;&lt;code&gt;instancetype&lt;/code&gt; is a special type that can be returned from an Objective-C method. It specifies that the return is an object of the same type as the receiving class. In some cases, the compiler promotes an &lt;code&gt;id&lt;/code&gt; return to an instancetype: For instance, despite the definition of &lt;code&gt;[[NSString alloc] init]&lt;/code&gt;, the compiler knows that it returns an &lt;code&gt;NSString&lt;/code&gt;.&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p><code>instancetype</code> is a special type that can be returned from an Objective-C method. It specifies that the return is an object of the same type as the receiving class. In some cases, the compiler promotes an <code>id</code> return to an instancetype: For instance, despite the definition of <code>[[NSString alloc] init]</code>, the compiler knows that it returns an <code>NSString</code>.</p>
<p>When?</p>
<p>This is straight from <a href="http://clang.llvm.org/docs/LanguageExtensions.html#related-result-types">Clang Language Extensions</a>:</p>
<ul>
<li>the first word is “alloc” or “new”, and the method is a class method, or</li>
<li>the first word is “autorelease”, “init”, “retain”, or “self”, and the method is an instance method.</li>
</ul>
<p>Left unsaid is whether it’s better to actually specify <code>instancetype</code> for these rather than rely on automatic promotion of <code>id</code>. Most code I’ve seen relies on the automatic promotion. I’m not sure what combination this is of momentum, readability, compatibility and convention. I’ve kept to using <code>id</code> in these cases, but that’s mostly due to momentum.</p>
<p>However, you should certainly use <code>instancetype</code> if you want to specify that an instance of the receiving class is returned in cases where autopromotion doesn’t occur, such as a convenience factory.</p>
<p>For example, in this case:</p>
<pre><code>MyObject *bar = [MyObject objectWithParameter: foo];
</code></pre>
<p>…you would want <code>MyObject</code> to specify that <code>objectWithParameter</code> returns an <code>instancetype</code> rather than an <code>id</code>.</p>
<p>See also:</p>
<ul>
<li>Mattt Thompson: <a href="http://nshipster.com/instancetype/">instancetype</a>.</li>
</ul>]]></content:encoded>
    </item>
    <item>
      <title>A primer on @property and dot syntax</title>
      <link>https://tewha.net/2013/01/a-primer-on-property-and-dot-syntax/</link>
      <pubDate>Fri, 18 Jan 2013 16:00:00 +0000</pubDate>
      <guid>https://tewha.net/2013/01/a-primer-on-property-and-dot-syntax/</guid>
      <description>&lt;p&gt;Properties were new in Objective-C 2.0, introduced in 2006. While pretty uncontroversial, along with them came dot syntax. Dot syntax is much more controversial.&lt;/p&gt;
&lt;p&gt;In this article, I’ll discuss the advantages of &lt;code&gt;@property&lt;/code&gt; which make it worth using, and discuss “dot syntax.”&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p>Properties were new in Objective-C 2.0, introduced in 2006. While pretty uncontroversial, along with them came dot syntax. Dot syntax is much more controversial.</p>
<p>In this article, I’ll discuss the advantages of <code>@property</code> which make it worth using, and discuss “dot syntax.”</p>
<h2 id="properties">Properties</h2>
<p>Properties are awesome. A <code>@property</code> declaration formalizes some things that previously had to be explained in comments or by convention.</p>
<p>Without properties, you’d specify a getter and setter like this:</p>
<pre><code>@interface Bar : NSObject
- (void)setFoo: (NSObject *)foo;
- (NSObject *)foo;
@end
</code></pre>
<p>With properties, you can specify it like this:</p>
<pre><code>@interface Bar : NSObject
@property (readwrite, atomic, strong) NSObject *foo;
@end
</code></pre>
<p>Whereas a set accessor defines only the owning object and the new value, a <code>@property</code> declaration specifies the memory management model for the new value. Is the setter going to copy it, own it, or just assign it? With ARC, these become even more useful: You can copy it, specify that it’s kept via a strong reference, specify that it’s a weak, zeroing reference, or specify that it’s a weak reference that could become a dangling pointer.</p>
<p>Further, you can specify whether the property is atomic: If two threads try to access a property at the same time, is the behaviour fully defined?</p>
<p>Even more useful, if your getter and setter are trivial Objective-C can synthesize them for you. (In older compilers, you needed to specify this with <code>@synthesize</code>; in the latest version, the compiler will do this for you.)</p>
<p>Note: If you write your own getter/setter, you’re responsible for making sure it complies with the memory model and atomicity you’ve specified in the <code>@property</code>. The <code>@property</code> is a promise to other compilation units, and your object is not held to that promise by the compiler.</p>
<h2 id="what_is_dot_syntax">What is dot syntax?</h2>
<p>Dot syntax is syntactical sugar for accessing properties. Whereas the <code>@property</code> statement defines the property, dot syntax is an option for <strong>calling</strong> it.</p>
<p>Excluding dot syntax these are equivalent:</p>
<pre><code>_view1.frame = _view2.frame;
[_view1 setFrame: [_view2 frame]];
</code></pre>
<p>There are some unexpected gotchas involving dot syntax. This, for instance, does not compile:</p>
<pre><code>_view1.frame.size.width = 10;
</code></pre>
<p>The reason is it’s equivalent to this:</p>
<pre><code>[_view1 frame].size.width = 10;
</code></pre>
<p>You can’t change a field in a rvalue like this. Instead, you need to do this:</p>
<pre><code>CGFrame frame = _view1.frame;
frame.size.width = 10;
_view1.frame = frame;
</code></pre>
<p>This shows an important distinction: dot syntax may <em>look</em> like struct access, but it isn’t the same thing at all.</p>
<h2 id="using_dot_syntax">Using dot syntax</h2>
<p>In some cases the compiler will even let you get away with dot syntax for methods that aren’t properties. But you really shouldn’t; Objective-C isn’t Visual Basic (where this is supported) and this runs counter to the language. In the past, I’ve seen Apple engineers argue that doing so is taking advantage of a compiler limitation that will probably eventually be fixed. Or maybe it’s a compiler limitation that they’ll never get around to fixing. In either case, it’s taking advantage of a compiler limitation.</p>
<p>There’s a huge grey area here. Some will argue setting a property shouldn’t have side effects, shouldn’t perform an action, or many things between.</p>
<p>Clearly, setting something without side effects is a property. I think most Objective-C developers will agree with this, though some argue that calling code at all makes dot syntax inappropriate. The argument is that it looks like struct access, so it should behave <strong>exactly</strong> like struct access. I don’t agree with this, but some very wise people do.</p>
<p>By UIKit convention, visual side effects (such as changing a frame) is acceptable. Some of these are true properties, complete with a <code>@property</code> definition. Some lack the <code>@property</code> definition, as <code>@proeprty</code> predates much of UIKit but make sense as properties anyway. (I picked UIKit here because it’s more modern than AppKit, but the same applies there.)</p>
<p>However, a method that takes no parameters but does not return a value is clearly <strong>not</strong> a property. Obviously, a method that takes parameters that do not conform to a getter/setter signature does not represent part of a property.</p>
<p>This, while legal under some configurations, is never appropriate:</p>
<pre><code>obj.release;
</code></pre>
<p>Dot syntax should be limited to things that are semantically properties only. Whether you further limit that to things that are only defined with <code>@property</code> is up to you. I do for my own objects, but I prefer to believe that Apple would fix UIKit’s missing <code>@property</code> definitions before enforcing this at a compiler level. If I’m wrong, it’ll be easy enough to remove dot syntax from the affected code. But I think it’s more likely that Apple won’t enforce this at the compiler level.</p>
<p>There are even greyer areas to this. What constitutes a property? For instance, is <code>NSArray</code>’s count a property? I would argue it isn’t; it’s an attribute, a side-effect based on the other properties of the <code>NSArray</code>. But Apple template code uses the count of an array as if it was a property, and the result is not horrible. You could certainly argue it’s wrong, but I view it as just over the line.</p>
<h2 id="conclusion">Conclusion</h2>
<p>Embrace <code>@property</code>. The additional information it supplies regarding memory management and thread safety is great. Not only that, but it’s less code than the old way of declaring your getter and setter.</p>
<p>Use dot syntax where appropriate. You need to decide where the line is for yourself, as there are no hard rules about this. Everyone has an opinion over it, and the only thing you can be sure of is that there’s little consensus, except that some things are always inappropriate.</p>]]></content:encoded>
    </item>
    <item>
      <title>Trimming a level of indent with @synchronized and @autoreleasepool</title>
      <link>https://tewha.net/2012/10/trimming-a-level-of-indent-with-synchronized-and-autoreleasepool/</link>
      <pubDate>Tue, 30 Oct 2012 14:00:00 +0000</pubDate>
      <guid>https://tewha.net/2012/10/trimming-a-level-of-indent-with-synchronized-and-autoreleasepool/</guid>
      <description>&lt;p&gt;I haven’t seen this discussed anywhere, but you can eliminate a level of indentation when using &lt;code&gt;@synchronized&lt;/code&gt; and &lt;code&gt;@autoreleasepool&lt;/code&gt; blocks in a loop or conditionally. This falls naturally as a result of the way the C language works, and how these blocks work, but it took me a while to realize it.&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p>I haven’t seen this discussed anywhere, but you can eliminate a level of indentation when using <code>@synchronized</code> and <code>@autoreleasepool</code> blocks in a loop or conditionally. This falls naturally as a result of the way the C language works, and how these blocks work, but it took me a while to realize it.</p>
<p>You may have code that looks like this:</p>
<pre><code>if (_foo) {
    @synchronized(self) {
        // stuff here
    }
}
</code></pre>
<p>This can also be expressed more simply:</p>
<pre><code>if (_foo) @synchronized(self) {
    // stuff here
}
</code></pre>
<p>Note that you can’t use this if your <code>if</code> has an <code>else</code> statement.</p>
<p>This is also true of <code>@autoreleasepool</code>, and may be even more handy there:</p>
<pre><code>for (NSDictionary *item in items) {
    @autoreleasepool {
        // lots of stuff here
    }
}
</code></pre>
<p>Can become:</p>
<pre><code>for (NSDictionary *item in items) @autoreleasepool {
    // lots of stuff here
}
</code></pre>
<p>Now, always making C statements compound in a condition or loop is a pretty common convention. And if you stick by this convention in other places (as I do), that’s fine. But the reasoning behind it doesn’t really apply with <code>@autoreleasepool</code> and <code>@synchronized</code>: you can just consider them part of the opening bracket.</p>
<p>Unfortunately, you can’t do this at the top level of message handlers. Wouldn’t that be nice?</p>]]></content:encoded>
    </item>
    <item>
      <title>Objective-C Feature Availability Index</title>
      <link>https://tewha.net/2012/09/objective-c-feature-availability-index/</link>
      <pubDate>Tue, 18 Sep 2012 14:00:00 +0000</pubDate>
      <guid>https://tewha.net/2012/09/objective-c-feature-availability-index/</guid>
      <description>&lt;p&gt;Which modern Objective-C feature can you use where? Check the &lt;a href=&#34;http://developer.apple.com/library/ios/releasenotes/ObjectiveC/ObjCAvailabilityIndex/_index.html&#34;&gt;Objective-C Feature Availability Index&lt;/a&gt; (via &lt;a href=&#34;https://twitter.com/0xced/statuses/248017993903267841&#34;&gt;0xced&lt;/a&gt;).&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p>Which modern Objective-C feature can you use where? Check the <a href="http://developer.apple.com/library/ios/releasenotes/ObjectiveC/ObjCAvailabilityIndex/_index.html">Objective-C Feature Availability Index</a> (via <a href="https://twitter.com/0xced/statuses/248017993903267841">0xced</a>).</p>
]]></content:encoded>
    </item>
    <item>
      <title>Learning a new programming language</title>
      <link>https://tewha.net/2009/10/learning-a-new-programming-language/</link>
      <pubDate>Sat, 24 Oct 2009 14:00:00 +0000</pubDate>
      <guid>https://tewha.net/2009/10/learning-a-new-programming-language/</guid>
      <description>&lt;p&gt;It took me a while to learn Objective-C.&lt;/p&gt;
&lt;p&gt;I started at the most basic level, wondering at the language. What are these brackets? What’s with the @ signs? What’s the difference between a - and a +? These aren’t hard things to learn, but understanding the reasoning behind them helps. And then there’s a point where it suddenly makes sense.&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p>It took me a while to learn Objective-C.</p>
<p>I started at the most basic level, wondering at the language. What are these brackets? What’s with the @ signs? What’s the difference between a - and a +? These aren’t hard things to learn, but understanding the reasoning behind them helps. And then there’s a point where it suddenly makes sense.</p>
<p>But the framework was confusing. How do I do this? Though I was less confused, this one isn’t solved directly. I became competent. And I started to ask the best question: “Why?”</p>
<p>The patterns were still confusing. Why does this work this way? What’s the purpose of this? Why is this done, but not this other thing?</p>
<p>And then there was a point where the patterns became obvious. More, the pattern in the patterns became obvious to me. And now, I look to find more examples of patterns, and patterns of patterns, to better build my knowledge.</p>
<p>It took me a while to get here, and it’s the same for every language. I’m feeling pretty confident about Objective-C now.</p>
<p>Back on the first day, I was confused and lost.</p>]]></content:encoded>
    </item>
  </channel>
</rss>
