<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Extensions on Andrew Bancroft</title>
    <link>https://www.andrewcbancroft.com/tags/extensions/</link>
    <description>Recent content about iOS development with Swift in Extensions  from Andrew Bancroft.</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-us</language>
    <lastBuildDate>Wed, 22 Apr 2015 17:52:36 +0000</lastBuildDate>
    
        <atom:link href="https://www.andrewcbancroft.com/tags/extensions/index.xml" rel="self" type="application/rss+xml" />
    
    
    <item>
      <title>3 Nuances of Swift Extensions</title>
      <link>https://www.andrewcbancroft.com/2015/04/22/3-nuances-of-swift-extensions/</link>
      <pubDate>Wed, 22 Apr 2015 17:52:36 +0000</pubDate>
      
      <guid>https://www.andrewcbancroft.com/2015/04/22/3-nuances-of-swift-extensions/</guid>
      <description>&lt;p&gt;How often do we take an initial cursory look at some documentation, shake our heads and say, &amp;ldquo;Ok, sure! Got it!”, and then some time later get to the actual usage of that perceived understanding only to find out, &amp;ldquo;Woah – this is behaving differently than I expected! I wonder if the documentation says anything about this?!”&lt;/p&gt;
&lt;p&gt;A few discussions I&amp;rsquo;ve had recently have prompted me to question what I thought I knew about Swift extensions. I have read documentation about extensions and I &lt;em&gt;thought&lt;/em&gt; I understood them pretty thoroughly. However, these conversations, along with some experimentation done on my own revealed a few nuances that I didn&amp;rsquo;t pick up on before.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Update:&lt;/strong&gt; Almost immediately after publishing this article, the Swift community chimed in and helped me figure out my fundamental hiccup which prompted the aforementioned experimentation in the first place. I&amp;rsquo;ve written a follow-up article called &lt;a href=&#34;http://www.andrewcbancroft.com/2015/04/24/clarifying-swift-access-control-hint-swift-isnt-c-sharp/&#34; title=&#34;Clarifying Swift Access Control (Hint:  Swift Isn’t C#)&#34;&gt;&amp;ldquo;Clarifying Swift Access Control”&lt;/a&gt;, describing that misunderstanding. I recommend giving that one a read to avoid making the same mistake I did!&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;3-nuances&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;three-nuances-of-extensions&#34;&gt;Three nuances of extensions&lt;/h3&gt;
&lt;p&gt;In particular, the following three nuances challenged what I thought I knew about Swift extensions:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The visibility Swift extensions have into the Type they&amp;rsquo;re extending. Can they see things marked &lt;code&gt;private&lt;/code&gt;, for example?&lt;/li&gt;
&lt;li&gt;How that visibility is affected by where the extension is defined. If I have the source for a Type that I&amp;rsquo;m writing an extension for, does defining it within that same source file vs defining it in a separate file affect what it can &amp;ldquo;see”?&lt;/li&gt;
&lt;li&gt;The default access modifiers of the extension&amp;rsquo;s &amp;ldquo;members” and how specifying them or &lt;em&gt;not&lt;/em&gt; specifying them affect what an extension exposes as public API for the Type it&amp;rsquo;s extending.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Before I begin, suppose that I have a public struct called &lt;code&gt;Person&lt;/code&gt;. It has some private properties, &lt;code&gt;name&lt;/code&gt;, &lt;code&gt;gender&lt;/code&gt;, and &lt;code&gt;age&lt;/code&gt;. An enum encapsulates the idea of &lt;code&gt;Gender&lt;/code&gt;. The struct looks something like this:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-swift&#34; data-lang=&#34;swift&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 1&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;struct&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;Person&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 2&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;private&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;name&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;String&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 3&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;private&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;gender&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Gender&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 4&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;private&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;age&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;Int&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 5&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 6&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;init&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;name&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;String&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;gender&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Gender&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;age&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;Int&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 7&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;kc&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;name&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;name&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 8&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;kc&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;gender&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;gender&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 9&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;kc&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;age&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;age&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;10&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;11&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;12&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;howOldAreYou&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;String&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;13&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;formattedAge&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;14&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;15&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;16&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// private func, simply to show extension visibility traits in the analysis below...&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;17&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;private&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;formattedAge&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;String&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;18&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;switch&lt;/span&gt; &lt;span class=&#34;kc&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;gender&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;19&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;case&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;Male&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;20&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;            &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;I&amp;#39;m &lt;/span&gt;&lt;span class=&#34;si&#34;&gt;\(&lt;/span&gt;&lt;span class=&#34;kc&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;age&lt;/span&gt;&lt;span class=&#34;si&#34;&gt;)&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;.&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;21&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;case&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;Female&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;22&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;            &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;Not telling.&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;23&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;24&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;25&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;26&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;enum&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;Gender&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;27&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;case&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Male&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;28&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;case&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Female&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;29&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;30&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Now, suppose that I wanted to extend &lt;code&gt;Person&lt;/code&gt; and inspect the three nuances about the extension&amp;rsquo;s capabilities and behaviors that I introduced at the beginning of this article…&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;extension-visibility&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;extensions-and-visibility-into-extended-type&#34;&gt;Extensions and visibility into extended Type&lt;/h3&gt;
&lt;p&gt;When I introduced that first nuance about visibility into the extended Type, I asked the question, &amp;ldquo;Can they see things marked &lt;code&gt;private&lt;/code&gt;?” The answer surprised me at first: &lt;em&gt;Yes&lt;/em&gt;…they &lt;em&gt;can&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;However, here&amp;rsquo;s where the second nuance comes in: It absolutely does matter &lt;em&gt;where&lt;/em&gt; the extension is defined.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;defined-same-file&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;defined-within-same-file&#34;&gt;Defined within same file&lt;/h4&gt;
&lt;p&gt;Extensions defined within the same file as the Type they&amp;rsquo;re extending have access to &lt;code&gt;private&lt;/code&gt; members of that Type.&lt;/p&gt;
&lt;p&gt;For example, defining an extension to &lt;code&gt;Person&lt;/code&gt; &lt;em&gt;within&lt;/em&gt; Person.swift allows the extension to access &lt;code&gt;private&lt;/code&gt; properties and functions! Who knew?!&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-swift&#34; data-lang=&#34;swift&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;extension&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;Person&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;2&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;getAge&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;Int&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;3&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;age&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;// compiles, even though age is --private--&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;4&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;5&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;6&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;getFormattedAge&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;String&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;7&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;formattedAge&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;// compiles, even though formattedAge is --private--&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;8&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;9&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&amp;ldquo;What?? Why?”, I thought to myself…&lt;/p&gt;
&lt;p&gt;My reasoning as to why extensions defined within the same file behave this way is because when it comes down to it, I could have just written the extension&amp;rsquo;s implementation as part of the Type itself and it would have had the same effect.&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;m &lt;em&gt;in the source file&lt;/em&gt; of the Type I&amp;rsquo;m &amp;ldquo;extending”. So whether I write the additional functionality as an extension for the Type, or just define what would have been in the extension &lt;em&gt;inside the Type, itself&lt;/em&gt;, the net effect is the same.&lt;/p&gt;
&lt;p&gt;Therefore, the compiler essentially says, &amp;ldquo;I see this extension being defined, but there&amp;rsquo;s really no point. It&amp;rsquo;s in the same file that the Type is defined in… so the developer &lt;em&gt;could have&lt;/em&gt; just written all this code within the Type itself… so I&amp;rsquo;ll let him/her refer to &lt;code&gt;private&lt;/code&gt; code blocks.”&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Update:&lt;/strong&gt; My reasoning above reveals that I truly didn&amp;rsquo;t have an understanding of Swift access control. I recommend giving my followup article titled &lt;a href=&#34;http://www.andrewcbancroft.com/2015/04/24/clarifying-swift-access-control-hint-swift-isnt-c-sharp/&#34; title=&#34;Clarifying Swift Access Control (Hint:  Swift Isn’t C#)&#34;&gt;&amp;ldquo;Clarifying Swift Access Control”&lt;/a&gt; a read for more details!&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;defined-separate-file&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;defined-in-a-separate-file&#34;&gt;Defined in a separate file&lt;/h4&gt;
&lt;p&gt;Moving the extension definition into a separate file, however, causes the extension to lose that visibility into the Type it&amp;rsquo;s extending.&lt;/p&gt;
&lt;p&gt;Following the inverse of my previous reasoning about &lt;code&gt;private&lt;/code&gt; visibility when the extension is defined within the same file, this behavior actually makes sense to me.&lt;/p&gt;
&lt;p&gt;Most of the time, you&amp;rsquo;d be writing an extension for Types that you &lt;em&gt;don&amp;rsquo;t&lt;/em&gt; have the source to. In that scenario, extensions would have the same visibility that any client of the Type&amp;rsquo;s exposed API would have, namely, the things marked &lt;code&gt;public&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/04/PersonExtensions_swift.png&#34;&gt;&lt;img src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/04/PersonExtensions_swift.png&#34; alt=&#34;PersonExtensions.swift&#34; width=&#34;506&#34; height=&#34;159&#34; class=&#34;alignnone size-full wp-image-11731&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/04/PersonExtensions_swift.png 506w, https://www.andrewcbancroft.com/wp-content/uploads/2015/04/PersonExtensions_swift-300x94.png 300w&#34; sizes=&#34;(max-width: 506px) 100vw, 506px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;default-acces-control&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;default-extension-access-control&#34;&gt;Default extension access control&lt;/h3&gt;
&lt;p&gt;The final nuance also yielded some semi-surprising results for me. &lt;a href=&#34;https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/AccessControl.html#//apple_ref/doc/uid/TP40014097-CH41-ID25&#34;&gt;Apple&amp;rsquo;s documentation&lt;/a&gt; says it, but until I experimented and saw it in action, I didn&amp;rsquo;t catch the nuance around the default access control modifiers applied to extensions.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;no-explicit-access-control&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;default-access-when-no-explicit-access-modifiers-specified&#34;&gt;Default access when no explicit access modifiers specified&lt;/h4&gt;
&lt;p&gt;In short, when you declare an extension but specify no explicit access modifiers (ie, you just use the default), the extension&amp;rsquo;s default access level depends on the access level of the Type it&amp;rsquo;s extending.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If the Type is &lt;code&gt;public&lt;/code&gt; or &lt;code&gt;internal&lt;/code&gt;, the extension&amp;rsquo;s implementation &amp;ldquo;members” will be &lt;code&gt;internal&lt;/code&gt; by default. The &amp;ldquo;surprise” for me I think is that extensions for &lt;code&gt;public&lt;/code&gt; Types have &lt;code&gt;internal&lt;/code&gt; members by default, unless you specify otherwise.&lt;/li&gt;
&lt;li&gt;If the Type is &lt;code&gt;private&lt;/code&gt;, the extension&amp;rsquo;s implementation &amp;ldquo;members” will be &lt;code&gt;private&lt;/code&gt; by default.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Here&amp;rsquo;s what the extension looks like if we analyze it from the perspective of using no explicitly declared access modifiers (note that to gain access to private properties and functions, I&amp;rsquo;m declaring the extension within Person.swift):&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-swift&#34; data-lang=&#34;swift&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 1&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;struct&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;Person&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 2&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// ...&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 3&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 4&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// ...&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 5&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 6&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 7&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;extension&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;Person&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 8&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;getAge&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;Int&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 9&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;age&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;10&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;11&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;12&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;getFormattedAge&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;String&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;13&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;formattedAge&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;14&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;15&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Using the default access modifiers as shown in the code snippet above exposes access to the extension&amp;rsquo;s new API to instances within the same module. However, it does &lt;em&gt;not&lt;/em&gt; expose additional public API for the Type it&amp;rsquo;s extending to a client of that Type that&amp;rsquo;s in another &lt;em&gt;module&lt;/em&gt; (for example, the unit test target, which is another Swift module).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Same Module&lt;/strong&gt;&lt;br&gt;
&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/04/SameModule.png&#34;&gt;&lt;img src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/04/SameModule.png&#34; alt=&#34;Extended API within the same module&#34; width=&#34;505&#34; height=&#34;264&#34; class=&#34;alignnone size-full wp-image-11733&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/04/SameModule.png 505w, https://www.andrewcbancroft.com/wp-content/uploads/2015/04/SameModule-300x157.png 300w&#34; sizes=&#34;(max-width: 505px) 100vw, 505px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Different Module (test target)&lt;/strong&gt;&lt;br&gt;
&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/04/DifferentModule.png&#34;&gt;&lt;img src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/04/DifferentModule.png&#34; alt=&#34;Extended API in a different module&#34; width=&#34;507&#34; height=&#34;171&#34; class=&#34;alignnone size-full wp-image-11734&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/04/DifferentModule.png 507w, https://www.andrewcbancroft.com/wp-content/uploads/2015/04/DifferentModule-300x101.png 300w&#34; sizes=&#34;(max-width: 507px) 100vw, 507px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;For some reason, I had it in my head that if a Type that I&amp;rsquo;m extending is &lt;code&gt;public&lt;/code&gt;, the extension&amp;rsquo;s members would default to &lt;code&gt;public&lt;/code&gt;. I don&amp;rsquo;t know &lt;em&gt;why&lt;/em&gt; I thought that, but thankfully my experimentation cleared up!&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;default-declaration-public-implementation&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;default-access-when-using-default-extension-declaration-but-specify-public-for-implementation&#34;&gt;Default access when using default extension declaration, but specify public for implementation&lt;/h4&gt;
&lt;p&gt;Adding &lt;code&gt;public&lt;/code&gt; access control modifiers to the extension implementation&amp;rsquo;s members makes those members visible everywhere (that is, both within the same module, and within the test target).&lt;/p&gt;
&lt;p&gt;The location of the extension&amp;rsquo;s declaration, be it within the same source file as the Type it&amp;rsquo;s extending, or in a separate source file, does not matter in terms of what the extension exposes when adding &lt;code&gt;public&lt;/code&gt; members… But only extensions declared within the same source file as the Type it&amp;rsquo;s extending can see &lt;code&gt;private&lt;/code&gt; members of that Type, as we discovered previously.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Extensions declared in same &lt;em&gt;and&lt;/em&gt; separate source files&lt;/strong&gt;&lt;br&gt;
&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/04/public_extension_members.png&#34;&gt;&lt;img src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/04/public_extension_members-1024x645.png&#34; alt=&#34;Public extension members defined within the same source file, and in separate source files...&#34; width=&#34;1024&#34; height=&#34;645&#34; class=&#34;alignnone size-large wp-image-11752&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/04/public_extension_members-1024x645.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2015/04/public_extension_members-300x189.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2015/04/public_extension_members.png 1061w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Public extension members visible in different module (test target)&lt;/strong&gt;&lt;br&gt;
&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/04/public_member_visibility.png&#34;&gt;&lt;img src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/04/public_member_visibility.png&#34; alt=&#34;Regardless of where the extension is declared, all public members are visible to  other modules (such as the test target).&#34; width=&#34;533&#34; height=&#34;353&#34; class=&#34;alignnone size-full wp-image-11753&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/04/public_member_visibility.png 533w, https://www.andrewcbancroft.com/wp-content/uploads/2015/04/public_member_visibility-300x199.png 300w&#34; sizes=&#34;(max-width: 533px) 100vw, 533px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;But notice that on the line where I&amp;rsquo;ve written &lt;code&gt;extension Person { ... }&lt;/code&gt; that I haven&amp;rsquo;t specified an access control modifier for the extension, itself. I&amp;rsquo;ve only added &lt;code&gt;public&lt;/code&gt; to its &lt;em&gt;members&lt;/em&gt;. Even still, the new functions are visible to the test target which is a different module.&lt;/p&gt;
&lt;p&gt;In other words, there&amp;rsquo;s no need to write &lt;code&gt;public extension Person { ... }&lt;/code&gt;. Since &lt;code&gt;Person&lt;/code&gt; is &lt;code&gt;public&lt;/code&gt;, the extension just uses the Type&amp;rsquo;s access level for its own declaration.&lt;/p&gt;
&lt;h3 id=&#34;wrapping-up&#34;&gt;Wrapping up&lt;/h3&gt;
&lt;p&gt;The three nuances about Swift extensions that were analyzed here were &amp;ldquo;surprising” enough to me to warrant some experimentation. My hope is that the analysis that was done will help clear up these subtleties for others who are struggling with understanding how Swift extensions behave!&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;related&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;div class=&#34;resources&#34;&gt;
  &lt;div class=&#34;resources-header&#34;&gt;
    You might also enjoy&amp;#8230;
  &lt;/div&gt;
  &lt;ul class=&#34;resources-content&#34;&gt;
    &lt;li&gt;
      &lt;i class=&#34;fa fa-angle-right&#34;&gt;&lt;/i&gt; &lt;a href=&#34;http://www.andrewcbancroft.com/2014/11/03/the-5-ws-of-swift-extensions/&#34; title=&#34;The 5 W’s of Swift Extensions&#34;&gt;The 5 W’s of Swift Extensions&lt;/a&gt;
    &lt;/li&gt;
    &lt;li&gt;
      &lt;i class=&#34;fa fa-angle-right&#34;&gt;&lt;/i&gt; &lt;a href=&#34;http://www.andrewcbancroft.com/2014/12/02/send-a-type-to-obedience-school-using-swift-extensions-for-additional-protocol-conformance/&#34; title=&#34;Send a Type to Obedience School – Using Swift Extensions for Additional Protocol Conformance&#34;&gt;Send a Type to Obedience School – Using Swift Extensions for Additional Protocol Conformance&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;
&lt;p&gt;&lt;a name=&#34;share&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Send a Type to Obedience School – Using Swift Extensions for Additional Protocol Conformance</title>
      <link>https://www.andrewcbancroft.com/2014/12/02/send-a-type-to-obedience-school-using-swift-extensions-for-additional-protocol-conformance/</link>
      <pubDate>Tue, 02 Dec 2014 19:15:33 +0000</pubDate>
      
      <guid>https://www.andrewcbancroft.com/2014/12/02/send-a-type-to-obedience-school-using-swift-extensions-for-additional-protocol-conformance/</guid>
      <description>&lt;p&gt;Did you know that you can make &lt;em&gt;any&lt;/em&gt; Type (here meaning Class, Enumeration, or Struct), even already-existing ones that you don&amp;rsquo;t have the source code for, adopt a protocol that it doesn&amp;rsquo;t normally conform to out of the box?&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;You can&lt;/strong&gt; – &lt;em&gt;even if you don&amp;rsquo;t have the original source code for that Type&lt;/em&gt;! It&amp;rsquo;s a powerful and intriguing proposition. Let&amp;rsquo;s explore how this is possible.&lt;/p&gt;
&lt;h3 id=&#34;how-you-ask&#34;&gt;How, you ask?&lt;/h3&gt;
&lt;p&gt;Well, as the title of this article suggests, Swift extensions are the way to do it. To demonstrate this capability, consider the following scenario:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;You&amp;rsquo;re using a library that lets you create instances of &lt;code&gt;Bird&lt;/code&gt; , and you don&amp;rsquo;t have access to the source code for the library.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Bird &lt;/code&gt; has a property called &lt;code&gt;species &lt;/code&gt; and a property called &lt;code&gt;commonName&lt;/code&gt; .&lt;/li&gt;
&lt;li&gt;You&amp;rsquo;d like &lt;code&gt;Bird &lt;/code&gt; to conform to the &lt;a href=&#34;https://developer.apple.com/library/ios/documentation/General/Reference/SwiftStandardLibraryReference/Printable.html&#34; title=&#34;Swift Standard Library Reference - Printable Protocol&#34;&gt;Printable protocol, defined in the Swift standard library&lt;/a&gt;, so that you can call &lt;code&gt;println(_:)&lt;/code&gt; on &lt;code&gt;Bird &lt;/code&gt; instances, and have it log something useful to the console.&lt;/li&gt;
&lt;li&gt;When &lt;code&gt;println(_:)&lt;/code&gt; is passed a &lt;code&gt;Bird &lt;/code&gt; instance, you&amp;rsquo;d like it to print out something like &amp;ldquo;[species] (ie, [commonName]).”, and have [species] and [commonName] be replaced by the &lt;code&gt;Bird&lt;/code&gt;‘s real values.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Remember that you don&amp;rsquo;t have access to the original source code of &lt;code&gt;Bird&lt;/code&gt; . Without Swift extensions, there would be no way for you to tell the compiler that you&amp;rsquo;d like &lt;code&gt;Bird &lt;/code&gt; to adopt and conform to the &lt;code&gt;Printable &lt;/code&gt; protocol.&lt;/p&gt;
&lt;p&gt;Thankfully, extensions &lt;em&gt;do&lt;/em&gt; exist, and we can teach a &lt;code&gt;Bird &lt;/code&gt; new tricks, enabling its conformance to &lt;code&gt;Printable&lt;/code&gt; .&lt;/p&gt;
&lt;h3 id=&#34;protocol-conformance-extension&#34;&gt;Protocol Conformance Extension&lt;/h3&gt;
&lt;p&gt;As stated in the &lt;a href=&#34;https://developer.apple.com/library/ios/documentation/General/Reference/SwiftStandardLibraryReference/Printable.html&#34; title=&#34;Swift Standard Library Reference - Printable Protocol&#34;&gt;Printable protocol documentation&lt;/a&gt;, a Type adopting the &lt;code&gt;Printable &lt;/code&gt; protocol must implement a single, read-only property named &lt;code&gt;description&lt;/code&gt; .&lt;/p&gt;
&lt;p&gt;The extension, then would be implemented as follows:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-swift&#34; data-lang=&#34;swift&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;// Explicitly specify protocol adoption&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;2&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;extension&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;Bird&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;Printable&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;3&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;4&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Implement the required property to make Bird conform to the protocol&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;5&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;description&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;String&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;6&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;&lt;/span&gt;&lt;span class=&#34;si&#34;&gt;\(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;species&lt;/span&gt;&lt;span class=&#34;si&#34;&gt;)&lt;/span&gt;&lt;span class=&#34;s&#34;&gt; (ie, &lt;/span&gt;&lt;span class=&#34;si&#34;&gt;\(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;commonName&lt;/span&gt;&lt;span class=&#34;si&#34;&gt;)&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;)&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;7&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;8&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The magic line in the code above is highlighted. This tells the compiler that &lt;code&gt;Bird&lt;/code&gt; will be extended to adopt the &lt;code&gt;Printable&lt;/code&gt; protocol.&lt;/p&gt;
&lt;p&gt;Of course, the remaining requirement then, is to implement the specification of the protocol so that &lt;code&gt;Bird&lt;/code&gt; conforms to it, which is what the body of the extension contains.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Note that simply extending &lt;code&gt;Bird&lt;/code&gt; to have a read-only &lt;code&gt;description&lt;/code&gt; property will *not&lt;/em&gt; suffice for making the Type adopt the protocol. There is no &amp;ldquo;implicit” protocol adoption in Swift, so you must specify in the extension declaration that you intend for the Type to adopt the protocol.&lt;/p&gt;
&lt;h3 id=&#34;conclusion&#34;&gt;Conclusion&lt;/h3&gt;
&lt;p&gt;With this little example, you&amp;rsquo;ve seen how using an extension can enable a Class, Enumeration, or Struct to adopt and conform to a protocol that it normally wouldn&amp;rsquo;t out of the box. It&amp;rsquo;s especially neat that you can do this for &lt;em&gt;any&lt;/em&gt; such Type, whether you have control over its original source code or not.&lt;/p&gt;
</description>
    </item>
    
  </channel>
</rss>