<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>iOS / Mac on Andrew Bancroft</title>
    <link>https://www.andrewcbancroft.com/categories/ios-/-mac/</link>
    <description>Recent content about iOS development with Swift in iOS / Mac  from Andrew Bancroft.</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-us</language>
    
        <atom:link href="https://www.andrewcbancroft.com/categories/ios-/-mac/index.xml" rel="self" type="application/rss+xml" />
    
    
    <item>
      <title>Every Developer is an API Designer</title>
      <link>https://www.andrewcbancroft.com/2017/04/25/every-developer-api-designer/</link>
      <pubDate>Tue, 25 Apr 2017 13:35:04 +0000</pubDate>
      
      <guid>https://www.andrewcbancroft.com/2017/04/25/every-developer-api-designer/</guid>
      <description>&lt;p&gt;Every function; every class; every struct and enum and protocol is an API.&lt;/p&gt;
&lt;h1 id=&#34;you-8212-are-an-api-designer&#34;&gt;YOU — are an API designer&lt;/h1&gt;
&lt;p&gt;As developers, we move into and out of the role of &amp;ldquo;API Designer” &lt;em&gt;constantly&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;Have you ever thought about that? &lt;em&gt;You&lt;/em&gt; are an API designer! You create Application Programming Interfaces &lt;em&gt;all the time&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;I believe that everything we create has design built in, whether we&amp;rsquo;ve thought much about it or not.&lt;/p&gt;
&lt;p&gt;I don&amp;rsquo;t know why that thought changed my mindset so much, but it did. Maybe it&amp;rsquo;s because I always associated APIs with massive frameworks, standard libraries, and 3rd party code modules. It&amp;rsquo;s as if I thought there was a special class of developer that did that work, and all my job entailed was putting the jigsaw puzzle together.&lt;/p&gt;
&lt;p&gt;But when you and I design and create Types of any kind, be they data structures or functions, we ourselves, as &amp;ldquo;regular developers”, are in the business of creating APIs. We&amp;rsquo;re building ways for ourselves and others to &amp;ldquo;connect” to and interact with our code… to interface with it and &lt;em&gt;use&lt;/em&gt; it in order to perform work.&lt;/p&gt;
&lt;p&gt;You may be building these things for your team to use, or you may be building them for the future [you] to use. The fact is, we slip in and out of consuming &lt;em&gt;and creating&lt;/em&gt; APIs all the time.&lt;/p&gt;
&lt;h1 id=&#34;being-mindful-of-shifting-roles&#34;&gt;Being mindful of shifting roles&lt;/h1&gt;
&lt;p&gt;It&amp;rsquo;s been helpful for me to recognize when I shift in and out of each role, because creating an API applies a different aspect of the programming mind than consuming an API does.&lt;/p&gt;
&lt;p&gt;When you &lt;em&gt;create&lt;/em&gt; an API, you&amp;rsquo;ve got to be super conscious of your decisions. Types matter. Names matter. Inputs, outputs, dependencies and the like all matter when you&amp;rsquo;re creating an API. &lt;strong&gt;Why?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Because other people (or you!) will have to deal with what you create in a direct way. Sure, everyone might be concerned about the implementation and whether or not it has bugs or not. But they&amp;rsquo;ll be &lt;em&gt;most&lt;/em&gt; concerned about how to use the thing… the API… to get work done.&lt;/p&gt;
&lt;p&gt;When we drop out of the mode of designing and creating APIs into API &lt;em&gt;consumer&lt;/em&gt; mode, we experience the ramifications of our API designs directly.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&amp;ldquo;Why is this property named &lt;em&gt;that&lt;/em&gt;??”&lt;/li&gt;
&lt;li&gt;&amp;ldquo;Does this function really need &lt;em&gt;all these parameters&lt;/em&gt;?”&lt;/li&gt;
&lt;li&gt;&amp;ldquo;This function returns &lt;em&gt;what&lt;/em&gt;?”&lt;/li&gt;
&lt;li&gt;&amp;ldquo;I wish this concept was represented as a ___ instead of a ___…”&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;When you&amp;rsquo;re cognizant of what you&amp;rsquo;re doing with respect to creating and consuming APIs, it&amp;rsquo;s a huge help to your team and to [future you].&lt;/p&gt;
&lt;h1 id=&#34;apis-and-team-mates&#34;&gt;APIs and team mates&lt;/h1&gt;
&lt;p&gt;I was working with a teammate, and we were having a discussion about the Type of a property in a class we were building together.&lt;/p&gt;
&lt;p&gt;The original API was a property of Type &lt;code&gt;String&lt;/code&gt;, and the name of the property was &lt;code&gt;FieldNames&lt;/code&gt;. I thought to myself, &amp;ldquo;Huh… FieldName&lt;strong&gt;s&lt;/strong&gt;… plural… Why is this Typed as a &lt;code&gt;String&lt;/code&gt;? Either the name should be singular, or the Type should be switched out.”&lt;/p&gt;
&lt;p&gt;We ended up switching that out to a Type that conveyed the idea of being able to store multiple things in that property.&lt;/p&gt;
&lt;p&gt;The point? Something as small as the letter &lt;code&gt;s&lt;/code&gt; and the Type of a property made a head-scratching difference in my understanding of the API. I didn&amp;rsquo;t know how to use it until I asked. And that&amp;rsquo;s a tiny bit dangerous if the person who built it isn&amp;rsquo;t there to ask, anymore, right?&lt;/p&gt;
&lt;h1 id=&#34;1-takeaway&#34;&gt;#1 Takeaway&lt;/h1&gt;
&lt;p&gt;The number one takeway I want you to glean is this: Whether you realize it or not, if you&amp;rsquo;re in the software development business, you&amp;rsquo;re in the API design business. For me that meant becoming super conscious of the kind of code I&amp;rsquo;m writing, depending on which role I&amp;rsquo;m in as the keystrokes flow. Maybe this realization will impact you as well!&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Solving React Native’s “No bundle url present” Error</title>
      <link>https://www.andrewcbancroft.com/2017/04/22/solving-react-natives-no-bundle-url-present-error/</link>
      <pubDate>Sat, 22 Apr 2017 17:49:11 +0000</pubDate>
      
      <guid>https://www.andrewcbancroft.com/2017/04/22/solving-react-natives-no-bundle-url-present-error/</guid>
      <description>&lt;p&gt;I hit the &amp;ldquo;No bundle url present.” roadblock today and had to dig a bit to find the solution. I&amp;rsquo;m going to describe how I encountered the problem, and provide the solution that&amp;rsquo;s worked more than once for me, ever since I found the suggestion in the React Native GitHub repository.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;problem&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;the-problem&#34;&gt;The Problem&lt;/h1&gt;
&lt;ul&gt;
&lt;li&gt;You&amp;rsquo;ve got a React Native app.&lt;/li&gt;
&lt;li&gt;In the terminal, you run &lt;code&gt;react-native run-ios&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In the simulator, you hit a roadblock with the following error in a bright red screen:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;No bundle url present. Make sure you&amp;rsquo;re running a packager server or have included a .jsbundle file in your application bundle.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2017/04/Glass.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2017/04/Glass.png&#34; alt=&#34;No bundle URL present&#34; width=&#34;370&#34; height=&#34;666&#34; class=&#34;alignnone size-full wp-image-13258&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2017/04/Glass.png 370w, https://www.andrewcbancroft.com/wp-content/uploads/2017/04/Glass-167x300.png 167w&#34; sizes=&#34;(max-width: 370px) 100vw, 370px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;solution&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;the-solution&#34;&gt;The Solution&lt;/h1&gt;
&lt;p&gt;Buried &lt;a href=&#34;https://github.com/facebook/react-native/issues/12754&#34;&gt;in the comments&lt;/a&gt; of one of the React Native repo&amp;rsquo;s issues on GitHub, I found a solution that worked for me…&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Open a terminal window&lt;/li&gt;
&lt;li&gt;&lt;code&gt;cd&lt;/code&gt; into &lt;code&gt;YOUR_PROJECT/ios&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Remove the build folder with &lt;code&gt;rm -r build&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Run &lt;code&gt;react-native run-ios&lt;/code&gt; again&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Alternatively, you could open Finder, navigate to &lt;code&gt;YOUR_PROJECT/ios&lt;/code&gt; and delete the &lt;code&gt;build&lt;/code&gt; folder. Then run &lt;code&gt;react-native run-ios&lt;/code&gt; again.&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;m not sure what causes this (that bothers me), but but at least I&amp;rsquo;ve found something to get me going again.&lt;/p&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>Disabling Xcode’s OS-Level Debug Logging</title>
      <link>https://www.andrewcbancroft.com/2016/10/28/disabling-xcode-os-level-debug-logging/</link>
      <pubDate>Fri, 28 Oct 2016 05:06:19 +0000</pubDate>
      
      <guid>https://www.andrewcbancroft.com/2016/10/28/disabling-xcode-os-level-debug-logging/</guid>
      <description>&lt;p&gt;Full credit to &lt;a href=&#34;https://twitter.com/rustyshelf&#34;&gt;Russell Ivanovic&lt;/a&gt; for his &lt;a href=&#34;https://twitter.com/rustyshelf/status/775505191160328194&#34;&gt;tweet&lt;/a&gt; for figuring out how to disable Xcode 8&amp;rsquo;s extremely chatty debug console logging. I just wanted this here for my own reference, but perhaps it&amp;rsquo;s helpful to you as well.&lt;/p&gt;
&lt;p&gt;If you&amp;rsquo;re tired of sifting through operating system-level logging to the debug console while you&amp;rsquo;re debugging your app, it&amp;rsquo;s actually fairly easy to silence.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;edit-scheme&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Edit the scheme for your app:&lt;br&gt;
&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/10/DataHelper_swift-2.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/10/DataHelper_swift-2.png&#34; alt=&#34;Edit Scheme&#34; width=&#34;529&#34; height=&#34;182&#34; class=&#34;alignnone size-full wp-image-13064&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/10/DataHelper_swift-2.png 529w, https://www.andrewcbancroft.com/wp-content/uploads/2016/10/DataHelper_swift-2-300x103.png 300w&#34; sizes=&#34;(max-width: 529px) 100vw, 529px&#34; /&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;a name=&#34;new-environment-variable&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;br&gt;
2. In the ‘Run&amp;rsquo; section, click on ‘Arguments&amp;rsquo; tab, and add a new Environment Variable. Use &lt;code&gt;OS_ACTIVITY_MODE&lt;/code&gt; for the name, and &lt;code&gt;disable&lt;/code&gt; for the value:&lt;br&gt;
&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/10/DataHelper_swift-4.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/10/DataHelper_swift-4.png&#34; alt=&#34;New Environment Variable&#34; width=&#34;895&#34; height=&#34;506&#34; class=&#34;alignnone size-full wp-image-13068&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/10/DataHelper_swift-4.png 895w, https://www.andrewcbancroft.com/wp-content/uploads/2016/10/DataHelper_swift-4-300x170.png 300w&#34; sizes=&#34;(max-width: 895px) 100vw, 895px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;That&amp;rsquo;s it! Closing the Scheme editor and re-running your app will produce a beautifully silent console window, leaving you free to see what you wanted to see with &lt;code&gt;print()&lt;/code&gt;, or other app-specific info (such as runtime exceptions) without having to sift through all that OS-level logging.&lt;/p&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>Deploying Parse Dashboard</title>
      <link>https://www.andrewcbancroft.com/2016/04/17/deploying-parse-dashboard/</link>
      <pubDate>Sun, 17 Apr 2016 21:56:45 +0000</pubDate>
      
      <guid>https://www.andrewcbancroft.com/2016/04/17/deploying-parse-dashboard/</guid>
      <description>&lt;p&gt;Below are steps that I&amp;rsquo;ve taken, myself, to deploy the [Parse Dashboard][1] to a cloud host, such as Azure, or Heroku.&lt;/p&gt;
&lt;p&gt;Before you worry with a cloud host, you need a copy of the parse-dashboard to prepare for deployment…&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;clone-git-repo&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;1--clone-parse-dashboard-git-repository&#34;&gt;1 – Clone parse-dashboard git repository&lt;/h1&gt;
&lt;p&gt;First, you&amp;rsquo;ll want to clone (or fork, then clone) the [parse-dashboard git repository from GitHub][1].&lt;/p&gt;
&lt;p&gt;I typically make folders in my ~/Developer directory for projects that I&amp;rsquo;m working on. You can make the directory in Finder, then open Terminal and &lt;code&gt;cd&lt;/code&gt; into it. Or you can just run &lt;code&gt;mkdir&lt;/code&gt; in Terminal and then &lt;code&gt;cd&lt;/code&gt; into it.&lt;/p&gt;
&lt;p&gt;Once the directory for your parse dashboard is created, run the following command.&lt;br&gt;
&lt;code&gt;git clone https://github.com/ParsePlatform/parse-dashboard.git&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;run-npm-install&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;2--run-npm-install&#34;&gt;2 – Run npm install&lt;/h1&gt;
&lt;p&gt;Once the repository has been cloned to your local machine, change directory (&lt;code&gt;cd&lt;/code&gt;) into the ‘parse-dashboard&amp;rsquo; directory. You can run &lt;code&gt;ls&lt;/code&gt; at the Terminal to list out the contents of the directory you just cloned the dashboard repository into and you should see a new directory named ‘parse-dashboard&amp;rsquo;. That&amp;rsquo;s the one you need to be inside next.&lt;/p&gt;
&lt;p&gt;So, in the ‘parse-dashboard&amp;rsquo; folder, run &lt;code&gt;npm install&lt;/code&gt; at the Terminal. It could take a minute or three for the installation of all the Node packages to complete.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;edit-config&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;3--edit-parse-dashboard-configjson&#34;&gt;3 – Edit parse-dashboard-config.json&lt;/h1&gt;
&lt;p&gt;After the npm package installation in Step 3 is complete, you need to &lt;code&gt;cd&lt;/code&gt; into the ‘Parse-Dashboard&amp;rsquo; directory to find the parse-dashboard-config.json file. Note the capitalization of ‘Parse-Dashboard&amp;rsquo;. Earlier you &lt;code&gt;cd&lt;/code&gt;‘d into the ‘parse-dashboard&amp;rsquo; (lower-case) directory. Well, there&amp;rsquo;s &lt;em&gt;another&lt;/em&gt; directory inside named exactly the same, only the ‘P&amp;rsquo; and the ‘D&amp;rsquo; are capitalized.&lt;/p&gt;
&lt;p&gt;You can run &lt;code&gt;ls&lt;/code&gt; inside ‘Parse-Dashboard&amp;rsquo; and should see parse-dashboard-config.json in there. If you don&amp;rsquo;t, you&amp;rsquo;re probably not in the right directory. At this point, you could actually be in Finder to locate the file if you&amp;rsquo;re more comfortable there. Just run &lt;code&gt;open .&lt;/code&gt;, and you&amp;rsquo;ll be in a Finder window at the directory you&amp;rsquo;re in inside the Terminal. Then you can just double-click the configuration file to open it up in a text editor.&lt;/p&gt;
&lt;p&gt;Once you have it open, you need to have a few pieces of information about your self-hosted Parse server handy:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Your Parse Server&amp;rsquo;s URL&lt;/li&gt;
&lt;li&gt;Your App ID that&amp;rsquo;s configured on your Parse Server&lt;/li&gt;
&lt;li&gt;Your Master Key that&amp;rsquo;s configured on your Parse Server&lt;/li&gt;
&lt;li&gt;Your App&amp;rsquo;s Name (can be an arbitrary value)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Adjust your Parse Dashboard&amp;rsquo;s configuration file as follows with the values you&amp;rsquo;ve gathered:&lt;/p&gt;
&lt;pre class=&#34;lang:js decode:true &#34; title=&#34;parse-dashboard-config.json&#34; &gt;{
  &#34;apps&#34;: [
    {
        &#34;serverURL&#34;: &#34;http://url-to-your-parse-server/parse&#34;, // If your Parse Server&#39;s endpoint is not at /parse, you need to replace /parse with the correct endpoint
        &#34;appId&#34;: &#34;your-app-Id&#34;,
        &#34;masterKey&#34;: &#34;your-master-key&#34;,
        &#34;appName&#34;: &#34;your-app-name&#34;
    }
  ],
  &#34;users&#34;: [
    {
      &#34;user&#34;:&#34;username1&#34;, // Used to log in to your Parse Dashboard
      &#34;pass&#34;:&#34;password1&#34;
    }
  ]
}
```

&lt;a name=&#34;modify-git-ignore&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;

# 4 – Modify local .gitignore

Your local .gitignore file ignores some needed directories that, while helpful to ignore in the actual Open Source parse-dashboard repo on GitHub, are _not_ as helpful to ignore when you need to deploy the dashboard to a cloud host.

Edit the .gitignore file using a text editor and remove these three lines:

&lt;pre class=&#34;lang:vim decode:true &#34; title=&#34;.gitignore&#34; &gt;bundles/
Parse-Dashboard/public/bundles/
Parse-Dashboard/parse-dashboard-config.json
```

&lt;a name=&#34;stage-commit-changes&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;

# 5 – Stage and commit all changes

Stage all of the changes you&#39;ve made to the parse-dashboard-config.json and the .gitignore files, and add the files and directories that were previously ignored by git by running `git add -A`

Then commit the staged changes:  
`git commit -m &#34;Prepared app for deployment&#34;`

&lt;a name=&#34;setup-private-repo&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;

# 6 – Set up private git repository

Since you&#39;re storing values that are intended to be kept secret (such as your app&#39;s Master Key), you need to set up a private git repository that can be used for two purposes: 1) To keep your configuration under source control and 2) to be able to use as a deployment source for your cloud host provider.

The idea is that you can wire up your cloud app in Azure/Heroku/AWS/wherever to refresh itself every time you commit code to the master branch of this private repository. It&#39;s really handy for making updates.

[Bitbucket][2] offers its members unlimited private repositories, so that&#39;s where I&#39;ve chosen to host mine.

&lt;a name=&#34;add-remote&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;

# 7 – Add new remote to your local parse-dashboard git repository

With the new private repository set up, you need to add it as a remote to your local parse-dashboard repo.

In the Terminal, make sure you&#39;re in the directory of your local parse-dashboard git repository and run:

&lt;pre class=&#34;lang:sh decode:true &#34; title=&#34;Add new remote&#34; &gt;git remote add privateorigin https://url-to-your-private-parse-dashboard-repo
```

&lt;a name=&#34;push-changes&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;

# 8 – Push to your private git repository

With all the changes to your configuration committed and the new remote repository added, you can push those changes up to that repo:

In the Terminal, make sure you&#39;re in the directory of your local parse-dashboard git repository and run:

&lt;pre class=&#34;lang:sh decode:true &#34; title=&#34;Push changes&#34; &gt;git push -u privateorigin --all # pushes up the repo and its refs for the first time
git push privateorigin --tags # pushes up any tags
```

&lt;a name=&#34;create-cloud-app&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;

# 9 – Create app on the cloud host

Now you need to create a new app in your hosting provider of choice. I typically go with [Azure][3], because I&#39;m most familiar with it. I also like that they offer a completely free option, though if you want to really run this stuff for a larger-scale &#34;production&amp;#8221; or &#34;enterprise&amp;#8221; app, it&#39;s going to cost no matter where you choose to host it.

The cloud host may want to know what flavor of app you want to create. In the case of parse-dashboard, it&#39;s a Node.JS app.

&lt;a name=&#34;configure-git-deployment&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;

# 10 – Configure cloud host for git deployment

Once you&#39;ve created a new Node app on your cloud host of choice, you need to configure it for git deployment.

This step will be different for each cloud host, but the general idea is that you should be able to configure your private git repository that you set up in Step 5, to be the source from which the cloud host will go out, grab the code, and perform the necessary steps to get the Parse Dashboard bootstrapped for access.

In Azure, you simply click on your app, and configure git deployment in the Deployment Source settings:

[&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/04/AzureGitDeployment1-1024x737.png&#34; alt=&#34;Azure Git Deployment Settings&#34; width=&#34;1024&#34; height=&#34;737&#34; class=&#34;alignnone size-large wp-image-12806&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/04/AzureGitDeployment1-1024x737.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2016/04/AzureGitDeployment1-300x216.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2016/04/AzureGitDeployment1.png 1215w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;][4]

You might check out [Heroku&#39;s startup guide][5] for help on deploying to their service.

Note that instead of doing the git deployment option, you could always FTP your whole parse-dashboard directory (the one that you cloned and ran `npm install` inside), if your cloud host supports this deployment option.

&lt;a name=&#34;configure-ssl&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;

# 11 – Enable SSL (or run as insecure app)

If you access your app after you&#39;ve deployed it to your chosen cloud host, you&#39;ll likely run into an error page stating that &#34;Parse Dashboard can only be remotely accessed via HTTPS&amp;#8221;.

Configuring SSL is beyond the scope of this walk-through, but that would be the next step if you chose to do it for your production app. Note that on Azure, your App Service Plan needs to be at &#34;Basic&amp;#8221; or above. Heroku charges a monthly fee to set up an SSL endpoint as well.

If you choose not to afford it or if you just want to test things out in the deployed environment to see if it&#39;s working or not, you can set up an environment variable in your app&#39;s settings on the cloud host.

In Azure, you need to choose your app, then go to &#34;Application Settings&amp;#8221; in the Azure Portal. Then add a new key-value pair under App settings: PARSE\_DASHBOARD\_ALLOW\_INSECURE\_HTTP is the key, and 1 (the number 1) is the value:

[&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/04/AzureAppSettings1-1024x639.png&#34; alt=&#34;Azure App Settings for PARSE_DASHBOARD_ALLOW_INSECURE_HTTP&#34; width=&#34;1024&#34; height=&#34;639&#34; class=&#34;alignnone size-large wp-image-12803&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/04/AzureAppSettings1-1024x639.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2016/04/AzureAppSettings1-300x187.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2016/04/AzureAppSettings1.png 1488w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;][6]

In Heroku, you need to go to your app&#39;s Settings, and then add a new Config Var with the same key-value pair:  
[&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/04/HerokuConfigVars-1024x489.png&#34; alt=&#34;Heroku Config Vars PARSE_DASHBOARD_ALLOW_INSECURE_HTTP&#34; width=&#34;1024&#34; height=&#34;489&#34; class=&#34;alignnone size-large wp-image-12797&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/04/HerokuConfigVars-1024x489.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2016/04/HerokuConfigVars-300x143.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2016/04/HerokuConfigVars.png 1142w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;][7]

&lt;a name=&#34;related&#34; class=&#34;jump-target&#34;&gt;&lt;/a&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;https://www.andrewcbancroft.com/2016/04/05/fix-query-in-parse-cloud-code-returns-unauthorized-error/&#34; title=&#34;FIX – Query in Parse Cloud Code Returns Unauthorized Error&#34;&gt;FIX – Query in Parse Cloud Code Returns Unauthorized Error&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;

&lt;a name=&#34;share&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;

 [1]: https://github.com/ParsePlatform/parse-dashboard
 [2]: http://www.bitbucket.com
 [3]: https://azure.microsoft.com/en-us/
 [4]: https://www.andrewcbancroft.com/wp-content/uploads/2016/04/AzureGitDeployment1.png
 [5]: https://devcenter.heroku.com/articles/getting-started-with-nodejs#introduction
 [6]: https://www.andrewcbancroft.com/wp-content/uploads/2016/04/AzureAppSettings1.png
 [7]: https://www.andrewcbancroft.com/wp-content/uploads/2016/04/HerokuConfigVars.png</description>
    </item>
    
    <item>
      <title>FIX – Query in Parse Cloud Code Returns Unauthorized Error</title>
      <link>https://www.andrewcbancroft.com/2016/04/05/fix-query-in-parse-cloud-code-returns-unauthorized-error/</link>
      <pubDate>Wed, 06 Apr 2016 04:27:49 +0000</pubDate>
      
      <guid>https://www.andrewcbancroft.com/2016/04/05/fix-query-in-parse-cloud-code-returns-unauthorized-error/</guid>
      <description>&lt;p&gt;The Parse migration process has begun – I&amp;rsquo;ve got a couple of small apps that used this backend as a service because the backend was simple and Parse was free.&lt;/p&gt;
&lt;p&gt;The migration has gone fairly smooth so far, but I hit a wall and have been struggling to figure out the solution for about 3 hours. 3 hours too many, haha!&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;problem-overview&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;problem-overview&#34;&gt;Problem Overview&lt;/h1&gt;
&lt;p&gt;Any time I did a query in my Parse Cloud Code, the query would fail. Every time I&amp;rsquo;d make a request to my Cloud Code function, I&amp;rsquo;d get &amp;ldquo;500 Internal Server Error” as my response.&lt;/p&gt;
&lt;p&gt;I finally wised up and adjusted my query so that I could see the specific &amp;ldquo;internal server error” that was occurring:&lt;/p&gt;
&lt;pre class=&#34;lang:js mark:8 decode:true &#34; title=&#34;Parse Query&#34; &gt;var query = new Parse.Query(&#34;NameOfParseClassImFetching&#34;);

query.find({
    success: function(results){
        // Do things with the results
    },
    error: function(error) {
        response.error(error);
    }
});
```

When the details came back as to what was failing, the JSON object in the response looked like this:

&lt;pre class=&#34;lang:js decode:true &#34; &gt;{
  &#34;code&#34;: 141,
  &#34;error&#34;: {
    &#34;message&#34;: &#34;unauthorized&#34;
  }
}
```

I got to searching and found an [issue on GitHub][1] that pointed me in the right direction, but to spare you reading through the entire thread, here&#39;s the gist:

&lt;a name=&#34;solution&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;

# Solution

I happened to be using a version of the Parse Server Example that was missing a critical line of configuration in index.js. When you configure your Parse Server by calling `new ParseServer`, you need to make sure that there&#39;s a `serverURL` property that&#39;s set to the URL of your Parse Server&#39;s API endpoint.

My config didn&#39;t have this, but the moment I added it, my Cloud Code queries worked perfectly as they did before. Here&#39;s a look at my final index.js file with the relevant line highlighted:

&lt;pre class=&#34;lang:js mark:18 decode:true &#34; title=&#34;index.js&#34; &gt;// Example express application adding the parse-server module to expose Parse
// compatible API routes.

var express = require(&#39;express&#39;);
var ParseServer = require(&#39;parse-server&#39;).ParseServer;

var databaseUri = process.env.DATABASE_URI || process.env.MONGOLAB_URI

if (!databaseUri) {
  console.log(&#39;DATABASE_URI not specified, falling back to localhost.&#39;);
}

var api = new ParseServer({
  databaseURI: databaseUri || &#39;mongodb://localhost:27017/dev&#39;,
  cloud: process.env.CLOUD_CODE_MAIN || __dirname + &#39;/cloud/main.js&#39;,
  appId: process.env.APP_ID || &#39;myAppId&#39;,
  masterKey: process.env.MASTER_KEY || &#39;&#39;, //Add your master key here. Keep it secret!
  serverurl: &#39;http://url-to-your-parse-api-endpoint/parse&#39;
});
// Client-keys like the javascript key or the .NET key are not necessary with parse-server
// If you wish you require them, you can set them as options in the initialization above:
// javascriptKey, restAPIKey, dotNetKey, clientKey

var app = express();

// Serve the Parse API on the /parse URL prefix
var mountPath = process.env.PARSE_MOUNT || &#39;/parse&#39;;
app.use(mountPath, api);

// Parse Server plays nicely with the rest of your web routes
app.get(&#39;/&#39;, function(req, res) {
  res.status(200).send(&#39;I dream of being a web site.&#39;);
});

var port = process.env.PORT || 1337;
app.listen(port, function() {
    console.log(&#39;parse-server-example running on port &#39; + port + &#39;.&#39;);
});
```

I hope this little breadcrumb finds its way to someone before _they_ spend 3 hours on a silly configuration issue.

&lt;a name=&#34;share&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;

 [1]: https://github.com/ParsePlatform/parse-server/issues/356</description>
    </item>
    
    <item>
      <title>NSNotificationCenter vs Delegation – An Analysis</title>
      <link>https://www.andrewcbancroft.com/2015/02/05/nsnotificationcenter-vs-delegation-analysis/</link>
      <pubDate>Thu, 05 Feb 2015 13:00:26 +0000</pubDate>
      
      <guid>https://www.andrewcbancroft.com/2015/02/05/nsnotificationcenter-vs-delegation-analysis/</guid>
      <description>&lt;p&gt;In &lt;a href=&#34;http://www.andrewcbancroft.com/2014/10/08/fundamentals-of-nsnotificationcenter-in-swift/&#34;&gt;Fundamentals of NSNotificationCenter in Swift&lt;/a&gt;, a &lt;a href=&#34;http://www.andrewcbancroft.com/2014/10/08/fundamentals-of-nsnotificationcenter-in-swift/#comment-1762533966&#34;&gt;commenter asked&lt;/a&gt; me to elaborate on a response I&amp;rsquo;d given to a dialog going on below the blog post. I had stated:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;If you need a more structured environment around your [instance]-to-[instance] communication, delegates … are probably a better choice [than NSNotificationCenter].&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;I&amp;rsquo;ve been thinking for some time since I responded with that comment. What &lt;em&gt;do&lt;/em&gt; I mean when I say &amp;ldquo;if you need a more structured environment”… What does that even look like? Why are delegates a better choice when I need such &amp;ldquo;structure”?&lt;/p&gt;
&lt;h3 id=&#34;structured-environment-whats-that&#34;&gt;Structured environment? What&amp;rsquo;s that?&lt;/h3&gt;
&lt;p&gt;&amp;ldquo;Structured environment” may be a bit vague. Here&amp;rsquo;s what I was thinking when I wrote it: At the time of the comment, I was imagining what a solution implemented with NSNotificationCenter, and a solution implemented with a delegate look like….&lt;/p&gt;
&lt;h3 id=&#34;questions&#34;&gt;Questions&lt;/h3&gt;
&lt;p&gt;First, I tried to step into the role of each instance, and in a role-playing sort of way, ask:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;As a &lt;strong&gt;notifier&lt;/strong&gt; / &lt;strong&gt;delegator&lt;/strong&gt; instance: &amp;ldquo;What do I expect to happen as I send this notification or invoke this method on my delegate? What clues from my execution context inform that expectation?”&lt;/li&gt;
&lt;li&gt;As a &lt;strong&gt;notifier&lt;/strong&gt; / &lt;strong&gt;delegator&lt;/strong&gt; instance: &amp;ldquo;What control do I appear to have over the sequence of events that happen as a result of sending this notification or invoking this method on my delegate?”&lt;/li&gt;
&lt;li&gt;As a &lt;strong&gt;listener&lt;/strong&gt; / &lt;strong&gt;delegate&lt;/strong&gt; instance: &amp;ldquo;What impact does acting on this notification or executing this delegate method have on the system as a whole?”&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;And then shifting out of the role-playing mentality, stepping back and asking a question of clarity:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&amp;ldquo;Which strategy seems to provide greater clarity and structure to the &lt;em&gt;entire application environment&lt;/em&gt;?”&lt;/li&gt;
&lt;li&gt;&amp;ldquo;Which strategy would most help another developer who might see this code and try to trace the logic and impact of the code?”&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The measurement of a more or less &amp;ldquo;structured environment”, then, would be influenced by the answers to the questions of &lt;strong&gt;expectations&lt;/strong&gt;, &lt;strong&gt;perceived control&lt;/strong&gt;, &lt;strong&gt;impact&lt;/strong&gt;, and &lt;strong&gt;clarity&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s explore some of those answers from the perspective of each communication strategy, starting with NSNotificationCenter.&lt;/p&gt;
&lt;h3 id=&#34;answers-from-the-nsnotificationcenter-perspective&#34;&gt;Answers from the NSNotificationCenter Perspective&lt;/h3&gt;
&lt;p&gt;With NSNotificationCenter as an instance-to-instance communication strategy, we have the following environment:&lt;br&gt;
&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/02/Notification_Center_Environment.png&#34;&gt;&lt;img src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/02/Notification_Center_Environment.png&#34; alt=&#34;Notification Center Environment&#34; width=&#34;945&#34; height=&#34;374&#34; class=&#34;alignnone size-full wp-image-11252&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/02/Notification_Center_Environment.png 945w, https://www.andrewcbancroft.com/wp-content/uploads/2015/02/Notification_Center_Environment-300x119.png 300w&#34; sizes=&#34;(max-width: 945px) 100vw, 945px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Note that Listeners 1 to n may or may not exist. The graphic is assuming that 1+ Listener instances have &amp;ldquo;tuned in” to a particular notification key.&lt;/p&gt;
&lt;h5 id=&#34;expectations&#34;&gt;Expectations&lt;/h5&gt;
&lt;p&gt;What do I expect to happen? Well, the most reasonable thing I (as a &amp;ldquo;notifier instance”) can expect is for some other &amp;ldquo;listener instance” somewhere to tune in to the notification key I&amp;rsquo;m broadcasting. I have no way of knowing what would happen after that. It&amp;rsquo;s up to the listener to do something intelligent with the notification that [x event] occurred. I, as a notifier, can expect nothing more.&lt;/p&gt;
&lt;h5 id=&#34;perceived-control&#34;&gt;Perceived Control&lt;/h5&gt;
&lt;p&gt;It appears that I, as a notifier, have no control over the sequence of events that would occur as a result of my broadcast. That&amp;rsquo;s by design – the interaction between me and any listener is weak at best.&lt;/p&gt;
&lt;p&gt;This can be a fantastic thing! There&amp;rsquo;s freedom in saying &amp;ldquo;Hey! This happened!” and then being done. But it&amp;rsquo;s also &amp;ldquo;less structured”, as I&amp;rsquo;m terming it.&lt;/p&gt;
&lt;h5 id=&#34;impact&#34;&gt;Impact&lt;/h5&gt;
&lt;p&gt;The impact on the system as a whole has the potential to be significant. With NSNotificationCenter, it depends on how many listener instances there are and what each of them does in response to the notification. One could design the system to localize the impacts to the context of the listening instance. I&amp;rsquo;ve heard of and seen ugly situations that trigger cascading effects that make deciphering the impact of a notification much harder.&lt;/p&gt;
&lt;h5 id=&#34;clarity&#34;&gt;Clarity&lt;/h5&gt;
&lt;p&gt;We lose a good deal of clarity when heavy usage of NSNotificationCenter occurs in an application. There may be appropriate times to use NSNotificationCenter in your app. Keep in mind that it becomes much more difficult to sort out various interactions when more and more listener instances are responding a notification. Trying to reason about how the system as a whole arrived at its current state isn&amp;rsquo;t as easy when NSNotificationCenter enters the picture. Other developers with less knowledge of the app as a whole would suffer from this loss of clarity.&lt;/p&gt;
&lt;h3 id=&#34;answers-from-the-delegate-perspective&#34;&gt;Answers from the Delegate Perspective&lt;/h3&gt;
&lt;p&gt;With a delegate, we have a significantly different strategy at hand. To put it before us:&lt;br&gt;
&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/02/Delegate_Environment.png&#34;&gt;&lt;img src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/02/Delegate_Environment.png&#34; alt=&#34;Delegate Environment&#34; width=&#34;948&#34; height=&#34;497&#34; class=&#34;alignnone size-full wp-image-11251&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/02/Delegate_Environment.png 948w, https://www.andrewcbancroft.com/wp-content/uploads/2015/02/Delegate_Environment-300x157.png 300w&#34; sizes=&#34;(max-width: 948px) 100vw, 948px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h5 id=&#34;expectations-1&#34;&gt;Expectations&lt;/h5&gt;
&lt;p&gt;The delegation strategy deals with protocols. Protocols by nature give us reliable a way to…&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Count on the implementation of needed/expected behavior, and&lt;/li&gt;
&lt;li&gt;Predict what the behavior of the adopter of that protocol will be. By practicing good naming conventions, I, as a delegator, find it reasonable to expect that invoking a method on my delegate will do whatever the name of that method implies.&lt;/li&gt;
&lt;/ol&gt;
&lt;h5 id=&#34;perceived-control-1&#34;&gt;Perceived Control&lt;/h5&gt;
&lt;p&gt;It would appear that as a delegator, I can control the sequence of events that need to take place by choosing when to invoke methods on my delegate. A strategy using NSNotificationCenter can only send out a notification into the ether, and hope that something acts on it. A strategy using delegation has a &lt;em&gt;delegate&lt;/em&gt; which adopts a protocol. Every method is at my disposal to call in whatever order makes sense.&lt;/p&gt;
&lt;h5 id=&#34;impact-1&#34;&gt;Impact&lt;/h5&gt;
&lt;p&gt;The impact on the system as a whole still has the potential to be significant. Since there&amp;rsquo;s one-to-one communication going on between a delegator and its delegate, the determining factor on how broad the impact is tends to lean on the design of the system as a whole. Design patterns that minimize or eliminate global state reduce this potentially broad impact.&lt;/p&gt;
&lt;p&gt;One advantage that the delegation pattern has is that the delegate conforms to a protocol and a protocol, along with its specified methods, have &lt;em&gt;names&lt;/em&gt;. However small that knowledge is, it could give us clues about what impact of executing the delegate&amp;rsquo;s methods might have on the system.&lt;/p&gt;
&lt;h5 id=&#34;clarity-1&#34;&gt;Clarity&lt;/h5&gt;
&lt;p&gt;With the advantage of dealing with intelligently named protocols and clearly outlined method names, the delegation strategy would win the battle of clarity in my opinion. I can look at the delegator instance and say, &amp;ldquo;When execution of this instance&amp;rsquo;s logic gets to this point, reliance on the delegate kicks in and [x, y, and z] happens. I can jump over to the delegate&amp;rsquo;s implementation and say, &amp;ldquo;[x] does this, [y] does that, and [z] does this other thing.” Other developers with less knowledge of the app as a whole would enjoy this added clarity quite readily.&lt;/p&gt;
&lt;h3 id=&#34;in-summary&#34;&gt;In Summary&lt;/h3&gt;
&lt;p&gt;Here, I&amp;rsquo;ve analyzed NSNotificationCenter, side by side with the delegation pattern, by imagining myself in the role of each instance (notifier, listener | delegator, delegate). I assessed each strategy in terms of expectations, perceived control, impact, and clarity, attempting to shed light on what it means for an environment to be &amp;ldquo;more structured” or &amp;ldquo;less structured”. My hope was to shed light on my use of the term &amp;ldquo;structured environment”, and to share my thoughts on some of the implications of using each strategy.&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/2015/03/26/what-is-delegation-a-swift-developers-guide/&#34; title=&#34;What is Delegation? – A Swift Developer’s Guide&#34;&gt;What is Delegation? – A Swift Developer’s Guide&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/2015/04/08/how-delegation-works-a-swift-developer-guide/&#34; title=&#34;How Delegation Works – A Swift Developer’s Guide&#34;&gt;How Delegation Works – A Swift Developer’s Guide&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/10/08/fundamentals-of-nsnotificationcenter-in-swift/&#34; title=&#34;Fundamentals of NSNotificationCenter in Swift&#34;&gt;Fundamentals of NSNotificationCenter in Swift&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;https://www.andrewcbancroft.com/2016/02/15/fundamentals-of-callbacks-for-swift-developers/&#34; title=&#34;Fundamentals of Callbacks for Swift Developers&#34;&gt;Fundamentals of Callbacks for Swift Developers&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>Unit Testing Model Layer with Core Data and Swift</title>
      <link>https://www.andrewcbancroft.com/2015/01/13/unit-testing-model-layer-core-data-swift/</link>
      <pubDate>Tue, 13 Jan 2015 13:00:37 +0000</pubDate>
      
      <guid>https://www.andrewcbancroft.com/2015/01/13/unit-testing-model-layer-core-data-swift/</guid>
      <description>&lt;p&gt;&lt;small&gt;Updated on November 19, 2015 – Swift 2.0&lt;/small&gt;&lt;/p&gt;
&lt;p&gt;As I approached testing my Core Data model, I have to admit I was apprehensive. How in the world was I going to write unit tests for my model layer that depended on a database. Past experience with trying to write tests with databases was painful. I feared the same would be the case with Core Data.&lt;/p&gt;
&lt;p&gt;To my surprise, unit testing my Core Data model layer has been… well… amazing. With little effort, I’ve been able to write the unit tests I’ve wanted. The process went something like this for me:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/2014/07/17/implement-nsmanagedobject-subclass-in-swift/&#34;&gt;Create an &lt;code&gt;NSManagedObject&lt;/code&gt; subclass&lt;/a&gt; of the Core Data entity that I need in my unit test. (This just makes things easier from an Xcode code-completion standpoint)&lt;/li&gt;
&lt;li&gt;Write a helper function to set up an in-memory &lt;code&gt;NSManagedObjectContext&lt;/code&gt;. Avoiding the need to use an actual sqlite database is pretty handy. It allows for quick-running tests and easy iterations over the data model itself.&lt;/li&gt;
&lt;li&gt;Write unit tests using the in-memory &lt;code&gt;NSManagedObjectContext&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/2014/07/17/implement-nsmanagedobject-subclass-in-swift/&#34;&gt;I’ve already written about creating an &lt;code&gt;NSManagedObject&lt;/code&gt; subclass&lt;/a&gt;, so I will unpack steps 2 and 3 in this blog entry.&lt;/p&gt;
&lt;h3 id=&#34;set-up-an-in-memory-nsmanagedobjectcontext&#34;&gt;Set up an in-memory NSManagedObjectContext&lt;/h3&gt;
&lt;p&gt;A &lt;a href=&#34;http://stackoverflow.com/questions/1849802/how-to-unit-test-my-models-now-that-i-am-using-core-data&#34;&gt;Stack Overflow&lt;/a&gt; question+answer sparked some thoughts. The idea and the code both came from there. The answer uses Objective-C, so my contribution is that I&amp;rsquo;ve written it in Swift. In my project, I created a new Swift file called &amp;ldquo;CoreDataHelpers.swift” in my tests target. Here&amp;rsquo;s a look at the helper function:&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;import&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;CoreData&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&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;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;setUpInMemoryManagedObjectContext&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;n&#34;&gt;NSManagedObjectContext&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; 4&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;managedObjectModel&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSManagedObjectModel&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;mergedModelFromBundles&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;([&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;NSBundle&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;mainBundle&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()])&lt;/span&gt;&lt;span class=&#34;o&#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;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;persistentStoreCoordinator&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSPersistentStoreCoordinator&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;managedObjectModel&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;managedObjectModel&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&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;k&#34;&gt;do&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;try&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;persistentStoreCoordinator&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;addPersistentStoreWithType&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;NSInMemoryStoreType&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;configuration&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;kc&#34;&gt;nil&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;url&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;kc&#34;&gt;nil&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;options&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;kc&#34;&gt;nil&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;10&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;catch&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;11&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;bp&#34;&gt;print&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;Adding in-memory persistent store failed&amp;#34;&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;12&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;13&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;14&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;managedObjectContext&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSManagedObjectContext&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;15&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;n&#34;&gt;managedObjectContext&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;persistentStoreCoordinator&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;persistentStoreCoordinator&lt;/span&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&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;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;managedObjectContext&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;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h4 id=&#34;observations&#34;&gt;Observations&lt;/h4&gt;
&lt;p&gt;I&amp;rsquo;ll be honest, I&amp;rsquo;m only starting to put together the pieces involved in setting up the Core Data stack. Working through these unit testing techniques has solidified a &lt;em&gt;lot&lt;/em&gt;. Analyzing the helper method from the bottom up has made some sense out of how to configure everything:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;I need an &lt;code&gt;NSManagedObjectContext&lt;/code&gt; whose &lt;code&gt;NSPersistentStoreCoordinator&lt;/code&gt; property uses an in-memory store.&lt;/li&gt;
&lt;li&gt;To get such an &lt;code&gt;NSManagedObjectContext&lt;/code&gt;, I need to add a persistent store with a type of &lt;code&gt;NSInMemoryStoreType&lt;/code&gt; to an &lt;code&gt;NSPersistentStoreCoordinator&lt;/code&gt; instance. (note the line that&amp;rsquo;s highlighted)&lt;/li&gt;
&lt;li&gt;Of course, in order to do &lt;em&gt;that&lt;/em&gt;, I need an &lt;code&gt;NSPersistentStoreCoordinator&lt;/code&gt; &lt;em&gt;instance&lt;/em&gt;, and I can&amp;rsquo;t get one of &lt;em&gt;those&lt;/em&gt; unless I initialize it with an &lt;code&gt;NSManagedObjectModel&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;To get an &lt;code&gt;NSManagedObjectModel&lt;/code&gt;, I use the class method, &lt;code&gt;mergedModelFromBundles()&lt;/code&gt; to grab it from my main bundle.&lt;/li&gt;
&lt;li&gt;Fast-forwarding now: With a proper &lt;code&gt;NSManagedObjectModel&lt;/code&gt; instance, I can create an &lt;code&gt;NSPersistentStoreCoordinator&lt;/code&gt; instance with it and add an &lt;code&gt;NSInMemoryStoreType&lt;/code&gt; to that &lt;code&gt;persistentStoreCoordinator&lt;/code&gt;. Finally, I can initialize an &lt;code&gt;NSManagedObjectContext&lt;/code&gt;, assign the configured &lt;code&gt;persistentStoreCoordinator&lt;/code&gt; to the context, and return it.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Whew! This whole process felt a lot like reading &lt;a href=&#34;http://en.wikipedia.org/wiki/If_You_Give_a_Mouse_a_Cookie&#34;&gt;If You Give a Mouse a Cookie&lt;/a&gt;, but that may be because I&amp;rsquo;ve read it a few hundred times to my 2 year old. :]&lt;/p&gt;
&lt;h3 id=&#34;writing-the-unit-tests&#34;&gt;Writing the unit test(s)&lt;/h3&gt;
&lt;p&gt;With the ability to get an &lt;code&gt;NSManagedObjectContext&lt;/code&gt; instance that&amp;rsquo;s using an in-memory store, the unit tests using Entities from my Core Data model are quite easy.&lt;/p&gt;
&lt;p&gt;Here&amp;rsquo;s a sample test:&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;import&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;CoreData&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&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;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;TestsUsingModelFromCoreData&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;XCTestCase&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; 4&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;testSomethingUsingCoreData&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; 5&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;managedObjectContext&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;setUpInMemoryManagedObjectContext&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;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;entity&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSEntityDescription&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;insertNewObjectForEntityForName&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;EntityName&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;inManagedObjectContext&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;managedObjectContext&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&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;c1&#34;&gt;// model setup&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&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;c1&#34;&gt;// XCTAssert    &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 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;12&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;h3 id=&#34;conclusion&#34;&gt;Conclusion&lt;/h3&gt;
&lt;p&gt;I was so surprised at how straightforward the test was. The helper function makes a world of difference for me. I hope it does for you, too!&lt;/p&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;https://www.andrewcbancroft.com/2016/07/10/using-a-core-data-model-in-swift-playgrounds/&#34; title=&#34;Using a Core Data Model in Swift Playgrounds&#34;&gt;Using a Core Data Model in Swift Playgrounds&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;https://www.andrewcbancroft.com/2015/08/25/sharing-a-core-data-model-with-a-swift-framework/&#34; title=&#34;Sharing a Core Data Model with a Swift Framework&#34;&gt;Sharing a Core Data Model with a Swift Framework&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;https://www.andrewcbancroft.com/2015/02/18/core-data-cheat-sheet-for-swift-ios-developers/&#34; title=&#34;Core Data Cheat Sheet for Swift iOS Developers&#34;&gt;Core Data Cheat Sheet for Swift iOS Developers&lt;/a&gt;
  &lt;/li&gt;
&lt;/ul&gt;&lt;/div&gt; 
&lt;p&gt;&lt;a name=&#34;course&#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;
    Resources
  &lt;/div&gt;
  &lt;ul class=&#34;resources-content&#34;&gt;
    &lt;li&gt;
      &lt;i class=&#34;fas fa-video&#34;&gt;&lt;/i&gt; &lt;a href=&#34;http://bit.ly/ps-core-data-swift&#34; target=&#34;_blank&#34;&gt;Core Data Fundamentals with Swift&lt;/a&gt;&lt;br /&gt; &lt;a href=&#34;http://bit.ly/ps-core-data-swift&#34; target=&#34;_blank&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2017/04/ps-core-data-fundamentals-swift-1024x576.png&#34; alt=&#34;Core Data Fundamentals with Swift&#34; width=&#34;1024&#34; height=&#34;576&#34; class=&#34;alignnone size-large wp-image-13163&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2017/04/ps-core-data-fundamentals-swift-1024x576.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2017/04/ps-core-data-fundamentals-swift-300x169.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2017/04/ps-core-data-fundamentals-swift-768x432.png 768w, https://www.andrewcbancroft.com/wp-content/uploads/2017/04/ps-core-data-fundamentals-swift.png 1539w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;
</description>
    </item>
    
    <item>
      <title>Immutable Types with Changing State in Swift</title>
      <link>https://www.andrewcbancroft.com/2015/01/06/immutable-types-changing-state-swift/</link>
      <pubDate>Tue, 06 Jan 2015 12:00:50 +0000</pubDate>
      
      <guid>https://www.andrewcbancroft.com/2015/01/06/immutable-types-changing-state-swift/</guid>
      <description>&lt;p&gt;Dabble in Swift for long and the functional programming paradigm will most certainly appear on your radar. From conferences to books to blog posts, I&amp;rsquo;ve seen a lot in the functional programming arena as it relates to Swift.&lt;/p&gt;
&lt;p&gt;As I seek to improve my functional programming thought processes and to practice what I&amp;rsquo;m learning, I found myself struggling with a fundamental concept: &lt;strong&gt;immutability&lt;/strong&gt;. &lt;em&gt;Especially&lt;/em&gt; when I&amp;rsquo;m designing something that, at the very least, &lt;em&gt;appears&lt;/em&gt; to require the ability to change state at some point in time.&lt;/p&gt;
&lt;h3 id=&#34;immutable-changes--a-contradiction-in-terms&#34;&gt;Immutable changes – A contradiction in terms?&lt;/h3&gt;
&lt;p&gt;Immutability and changing state was a real struggle for me. The two seemed contradictory actually. How am I supposed to handle changes with things that &lt;em&gt;can&amp;rsquo;t change&lt;/em&gt;??!&lt;/p&gt;
&lt;p&gt;And then, with the help of Stack Overflow, a lightbulb came on. As I was perusing FP topics, I ran across a question that expressed exactly what I was feeling. &lt;a href=&#34;http://stackoverflow.com/questions/1020653/how-can-you-do-anything-useful-without-mutable-state&#34;&gt;&amp;ldquo;How can you do anything useful without mutable state?”&lt;/a&gt;, the questioner asked. The answer is what illuminated things for me. The key quote from the Stack Overflow answer:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;In functional languages, rather than mutating the state of objects, we simply return a new object with the changes we want.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;So… instead of thinking about state changes as &lt;em&gt;mutations&lt;/em&gt;, think of them as reasons to create something &lt;em&gt;new&lt;/em&gt;.&lt;/p&gt;
&lt;h3 id=&#34;getting-practical-with-swift&#34;&gt;Getting practical with Swift&lt;/h3&gt;
&lt;p&gt;If I&amp;rsquo;m going to get pragmatic with this, I have some design decisions to make if I&amp;rsquo;m going to try and avoid mutating the state of my objects. Rather than design my Types with the intent to update stored properties in-place, I should think about designing them so that the state-change process would actually produce a whole &lt;em&gt;new&lt;/em&gt; instance with the transformed (updated) value.&lt;/p&gt;
&lt;h3 id=&#34;see-it-in-action&#34;&gt;See it in action&lt;/h3&gt;
&lt;p&gt;I&amp;rsquo;m a visual person, so for those of you who need to see it in action like me, here&amp;rsquo;s a short example: Suppose that you&amp;rsquo;re designing a class that stores a counter (or a score or a total… something like that), and provides a function to let you increment that count.&lt;/p&gt;
&lt;h3 id=&#34;the-mutating-way&#34;&gt;The mutating way&lt;/h3&gt;
&lt;p&gt;We could design this class in one of two ways: declare a variable stored property to hold the count. The method would then reassign incremented values to it. This is the &lt;em&gt;opposite&lt;/em&gt; of immutable changes:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;class Scorekeeper {
    var runningScore: Int
    
    init (score: Int = 0) {
        self.runningScore = score
    }
    
    func incrementScoreBy(points: Int) {
        runningScore += points
    }
}

let scoreKeeper = Scorekeeper()
scoreKeeper.incrementScoreBy(5)
println(scoreKeeper.runningScore)
// prints 5
&lt;/code&gt;&lt;/pre&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;    &amp;lt;p&amp;gt;&amp;lt;/p&amp;gt;
    
    
    ### The immutable way
    
    
    OR, rather than mutating the existing variable, we could go an alternate route:  The second way to design the class is to declare a constant stored property.  The method would then produce _new_ instance that stored the new state:
    
    
        class Scorekeeper {
            let runningScore: Int
            
            init (score: Int = 0) {
                self.runningScore = score
            }
            
            func incrementScoreBy(points: Int) -&amp;gt; Scorekeeper {
                return Scorekeeper(score: self.runningScore + points)
            }
        }
        
        let scorekeeper = Scorekeeper()
        let scorekeeperWithIncreasedScore = scorekeeper.incrementScoreBy(5)
        println(scorekeeperWithIncreasedScore.runningScore)
        // prints 5
        
&lt;/code&gt;&lt;/pre&gt;&lt;pre&gt;&lt;code&gt;    &amp;lt;p&amp;gt;&amp;lt;/p&amp;gt;
    
    
    ### Observations
    
    
    As I look at the code I&#39;ve just presented, I notice a few things as I contrast the two examples:
    
    
    
    
    
      * The first example uses var to declare the stored property of my Scorekeeper class.  It _has_ to be var so that the value of the property can be changed (mutated).
    
    
      * The second example uses let exclusively.  No in-place mutations occur, so constants are perfectly reasonable to use.
    
    
    
    * * *
    
    
    
    
    
      * The first example&#39;s design lends itself to produce interesting and unpredictable side-effects.  If multiple clients hold a reference to my scorekeeper instance, there are two ways for the runningScore to change out from underneath any of those clients:  First, I could simply reassign a value to the runningScore directly.  Second, I could call incrementScoreBy().  Either way, unintended consequences may arise because of the mutation.
    
    
      * In the second example, it&#39;s _impossible_ to cause those unintended consequences.  runningScore can&#39;t be changed directly (it&#39;s a constant), and incrementScoreBy() returns a whole _new_ instance, so all clients would be dealing with the instance that they expect to be dealing with.  No values can be changed out from underneath them.
    
    
    
    * * *
    
    
    
    
    
      * The first example&#39;s incrementScoreBy method has no return value.  While I can envision writing a unit test for this, it&#39;s not obvious at first glance what I should do.  The method produces a side-effect in my existing instance that I need to know about in order to get the XCTAssert right.
    
    
      * The second example&#39;s incrementScoreBy method returns a new Scorekeeper instance.  The unit test for this (to me) is a little more clear.  I simply inspect the value of runningScore of the new instance, and assert that it is [x] points higher than the old instance.  I still have both the old scorekeeper instance _and_ the new scorekeeperWithIncreasedScore, so everything I&#39;d need to ensure the correct point increase occurred is at my disposal.
    
    
    
    ### Conclusion
    
    
    I hear so much benefit that comes from avoiding mutable state, so it was satisfying to finally let my mind reconcile how to manage the state of my own Types immutably.  With the iOS frameworks we have to work with, immutability is a challenge, and _total_ immutable state is not possible (think of the user interface layer where state is stored and updated out of necessity because of how _Apple&#39;s_ frameworks and tools are designed). Nonetheless, I found this discovery to be really exciting all the same.
&lt;/code&gt;&lt;/pre&gt;
</description>
    </item>
    
    <item>
      <title>Getting Started with Unit Testing in Swift</title>
      <link>https://www.andrewcbancroft.com/2014/12/29/getting-started-unit-testing-swift/</link>
      <pubDate>Mon, 29 Dec 2014 12:00:50 +0000</pubDate>
      
      <guid>https://www.andrewcbancroft.com/2014/12/29/getting-started-unit-testing-swift/</guid>
      <description>&lt;p&gt;Getting set up to write unit tests in Swift, while not &lt;em&gt;difficult&lt;/em&gt;, isn&amp;rsquo;t exactly &lt;em&gt;intuitive&lt;/em&gt;. I&amp;rsquo;ve written &lt;a href=&#34;http://www.andrewcbancroft.com/2014/12/19/swift-unit-testing-resources/&#34;&gt;several posts and gathered a few resources on unit testing in Swift&lt;/a&gt;, but I haven&amp;rsquo;t yet published a &amp;ldquo;getting started” guide until now.&lt;/p&gt;
&lt;p&gt;Where does one get started with unit testing in Swift when they&amp;rsquo;ve never gotten started before? Here are 5 steps to help you successfully begin unit testing in Swift:&lt;/p&gt;
&lt;h3 id=&#34;1--create-your-project&#34;&gt;1 – Create your project&lt;/h3&gt;
&lt;p&gt;The obvious first step. If you don&amp;rsquo;t already have a project going, create a new one to organize the code for your app idea. Xcode 6 will automatically generate two &amp;ldquo;targets” for you. One will be your app&amp;rsquo;s main module and contains all the code which, when compiled, is meant to be run in the iOS simulator / on an iOS device.&lt;/p&gt;
&lt;p&gt;The other will be your test target, which, as the name implies, is where you write your unit test code.&lt;/p&gt;
&lt;p&gt;It&amp;rsquo;s important to note that there are &lt;em&gt;two compilation targets&lt;/em&gt; that you&amp;rsquo;ll be working with as you unit test. Source code that&amp;rsquo;s part of one compilation target isn&amp;rsquo;t part of the other by default. This is important to at least &lt;em&gt;know&lt;/em&gt; as we move forward in this walkthrough.&lt;/p&gt;
&lt;h3 id=&#34;2--framework-considerations-toggle-defines-module8221-in-your-build-settings&#34;&gt;2 – Framework considerations: toggle &amp;ldquo;Defines Module” in your build settings&lt;/h3&gt;
&lt;p&gt;This step becomes especially critical if you&amp;rsquo;re using or plan to use a &lt;a href=&#34;https://github.com/Quick/Quick&#34;&gt;unit testing framework like Quick&lt;/a&gt;. It&amp;rsquo;s not required to use something like Quick, but it can enhance your testing experience if you prefer something other than the out-of-the-box &lt;code&gt;XCTestCase&lt;/code&gt; setup.&lt;/p&gt;
&lt;p&gt;When you’re working with frameworks, &lt;a href=&#34;https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html#//apple_ref/doc/uid/TP40014216-CH10-XID_88&#34;&gt;Apple recommends&lt;/a&gt; that you make sure the &amp;ldquo;Defines Module” build setting, found under Packaging, is set to Yes.&lt;/p&gt;
&lt;p&gt;If you are relying on, or &lt;em&gt;could&lt;/em&gt; be relying on &amp;ldquo;non-standard” .framework files for your app, make sure you toggle this flag in your Xcode build settings:&lt;br&gt;
&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2014/12/GetStartedWithUnitTesting_Swift_xcodeproj.png&#34;&gt;&lt;img src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2014/12/GetStartedWithUnitTesting_Swift_xcodeproj-1024x679.png&#34; alt=&#34;Build Settings - Defines Module&#34; width=&#34;730&#34; height=&#34;484&#34; class=&#34;alignnone size-large wp-image-10371&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2014/12/GetStartedWithUnitTesting_Swift_xcodeproj-1024x679.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2014/12/GetStartedWithUnitTesting_Swift_xcodeproj-300x199.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2014/12/GetStartedWithUnitTesting_Swift_xcodeproj-1200x796.png 1200w, https://www.andrewcbancroft.com/wp-content/uploads/2014/12/GetStartedWithUnitTesting_Swift_xcodeproj.png 1387w&#34; sizes=&#34;(max-width: 730px) 100vw, 730px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;3--import-your-main-project-module-into-your-test-files&#34;&gt;3 – Import your main project (module) into your test file(s)&lt;/h3&gt;
&lt;p&gt;In your test target, at the top of each of your .swift files that contain your XCTestCase classes, write an import statement to bring in your main project&amp;rsquo;s module. In normal scenarios, your app&amp;rsquo;s module is named the same as your Xcode project file.&lt;br&gt;
&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2014/12/GetStartedWithUnitTesting_SwiftTests_swift.png&#34;&gt;&lt;img src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2014/12/GetStartedWithUnitTesting_SwiftTests_swift-1024x671.png&#34; alt=&#34;Import Module for Testing&#34; width=&#34;730&#34; height=&#34;478&#34; class=&#34;alignnone size-large wp-image-10381&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2014/12/GetStartedWithUnitTesting_SwiftTests_swift-1024x671.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2014/12/GetStartedWithUnitTesting_SwiftTests_swift-300x196.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2014/12/GetStartedWithUnitTesting_SwiftTests_swift-1200x786.png 1200w, https://www.andrewcbancroft.com/wp-content/uploads/2014/12/GetStartedWithUnitTesting_SwiftTests_swift.png 1393w&#34; sizes=&#34;(max-width: 730px) 100vw, 730px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a id=&#34;public-testable&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;4--access-control-public--testable&#34;&gt;4 – Access control: Public == Testable&lt;/h3&gt;
&lt;p&gt;Any classes/structs/enums/methods that you need to use in your tests must have a &lt;code&gt;public&lt;/code&gt; access control modifier. &lt;code&gt;Internal&lt;/code&gt; (default) and &lt;code&gt;private&lt;/code&gt; Types and functions can&amp;rsquo;t be &amp;ldquo;seen” by your unit tests, so you need to go the &lt;code&gt;public&lt;/code&gt; route if you want to test those things.&lt;/p&gt;
&lt;p&gt;Previously I&amp;rsquo;d written on &lt;a href=&#34;http://www.andrewcbancroft.com/2014/07/22/swift-access-control-implications-for-unit-testing/&#34;&gt;Swift access control and its implications for unit testing&lt;/a&gt;. There, I proposed simply adding your main project&amp;rsquo;s .swift source files to your test target, but it turns out that this can lead to some &lt;a href=&#34;https://github.com/Quick/Quick/issues/91&#34;&gt;really obscure issues&lt;/a&gt;. An enlightening &lt;a href=&#34;https://twitter.com/modocache/status/549042409838219264&#34;&gt;Twitter conversation&lt;/a&gt; also shed some light on the subject, and pointed to the solution of testing only publicly accessible &lt;em&gt;behavior&lt;/em&gt; that your Types expose, rather than trying to test internal implementation. That probably deserves a blog entry of its own, but for now, I&amp;rsquo;ll leave it to say that I&amp;rsquo;d recommend &lt;em&gt;not&lt;/em&gt; adding your .swift source files to your test target, but rather to adjust the access control modifiers of the things you want to test to &lt;code&gt;public&lt;/code&gt;.&lt;/p&gt;
&lt;h3 id=&#34;5--write-tests&#34;&gt;5 – Write tests!&lt;/h3&gt;
&lt;p&gt;With your main project imported into a test file, and your Types and functions declared with appropriate &lt;code&gt;public&lt;/code&gt; accessibility, you&amp;rsquo;re all set to begin writing tests in Swift!&lt;/p&gt;
&lt;h3 id=&#34;summary&#34;&gt;Summary&lt;/h3&gt;
&lt;p&gt;We&amp;rsquo;ve gone from 0 to ready-to-test in this getting started guide to unit testing in Swift. From here, you may be interested in pursuing other topics related to unit testing in Swift. &lt;a href=&#34;http://www.andrewcbancroft.com/2014/12/19/swift-unit-testing-resources/&#34;&gt;Check out my ever-growing unit testing resource list for more information&lt;/a&gt;!&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Swift Unit Testing – Verifying Method Calls</title>
      <link>https://www.andrewcbancroft.com/2014/12/22/swift-unit-testing-verifying-method-calls/</link>
      <pubDate>Mon, 22 Dec 2014 13:09:28 +0000</pubDate>
      
      <guid>https://www.andrewcbancroft.com/2014/12/22/swift-unit-testing-verifying-method-calls/</guid>
      <description>&lt;p&gt;In this unit testing screencast for Swift developers, we explore how to use Test Driven Development to verify method calls.&lt;/p&gt;
&lt;h3 id=&#34;getting-started-guide&#34;&gt;Getting Started Guide&lt;/h3&gt;
&lt;p&gt;If you&amp;rsquo;re new to unit testing or are trying to get set up with unit testing in a Swift project, you might &lt;a href=&#34;http://www.andrewcbancroft.com/2014/12/29/getting-started-unit-testing-swift/&#34;&gt;check out my getting started guide&lt;/a&gt; before jumping into the screencast.&lt;/p&gt;
&lt;h3 id=&#34;screencast&#34;&gt;Screencast&lt;/h3&gt;
&lt;p&gt;&lt;span class=&#34;text-center&#34;&gt;&lt;a href=&#34;http://youtu.be/hC2Dni9SAWY&#34; target=&#34;_blank&#34;&gt;Large / Full-Screen Viewing&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;
&lt;h3 id=&#34;github-example&#34;&gt;GitHub Example&lt;/h3&gt;
&lt;p&gt;&lt;a href=&#34;https://github.com/andrewcbancroft/SwiftTDDVerifyMethodCalls&#34;&gt;GitHub repo&lt;/a&gt; of the example developed in the screencast.&lt;/p&gt;
&lt;h3 id=&#34;screencast-transcript&#34;&gt;Screencast Transcript&lt;/h3&gt;
&lt;h4 id=&#34;001&#34;&gt;[0:01]&lt;/h4&gt;
&lt;p&gt;Hi, I&amp;rsquo;m Andrew from andrewcbancroft.com, and I&amp;rsquo;m bringing you a test driven development example for iOS in Swift.&lt;/p&gt;
&lt;p&gt;The goal of this video is to teach you how to write a unit test to verify that a method was called.&lt;/p&gt;
&lt;h4 id=&#34;021&#34;&gt;[0:21]&lt;/h4&gt;
&lt;p&gt;I&amp;rsquo;ll begin in Xcode 6.1.1 with a side-by-side view of my TestCase class, and my primary View Controller class. This is what Xcode generated for me when I chose to create a new Single View Application.&lt;/p&gt;
&lt;h4 id=&#34;038&#34;&gt;[0:38]&lt;/h4&gt;
&lt;p&gt;Imagine a scenario with me: Suppose that as part of your application&amp;rsquo;s requirements, you mush show an Alert View after your primary view loads. How would we go about using Test Driven Development to implement this &amp;ldquo;feature”?&lt;/p&gt;
&lt;h4 id=&#34;053&#34;&gt;[0:53]&lt;/h4&gt;
&lt;p&gt;Well, we&amp;rsquo;d write a test, of course – I&amp;rsquo;ll name it something appropriate like &amp;ldquo;testUIAlertViewShowsAfterViewLoads”&lt;/p&gt;
&lt;h4 id=&#34;104&#34;&gt;[1:04]&lt;/h4&gt;
&lt;p&gt;Next, I need to create an instance of my ViewController class so that I can test it.&lt;br&gt;
But immediately, I run into trouble. It seems that my TestCase class can&amp;rsquo;t &amp;ldquo;see” my View Controller class.&lt;/p&gt;
&lt;h4 id=&#34;117&#34;&gt;[1:17]&lt;/h4&gt;
&lt;p&gt;Thankfully, it&amp;rsquo;s a simple fix: Simply add the View Controller class to your Test target.&lt;/p&gt;
&lt;h4 id=&#34;126&#34;&gt;[1:26]&lt;/h4&gt;
&lt;p&gt;With everything compiling now, we can move to the next line of test code.&lt;/p&gt;
&lt;h4 id=&#34;132&#34;&gt;[1:32]&lt;/h4&gt;
&lt;p&gt;First off, in order to test my View Controller&amp;rsquo;s Alert View functionality, the Alert View has got to be visible to my test. The easiest thing for me to do at this point is to assume that there will be a property on my View Controller that I can set. This allows me to perform a kind of dependency injection known as &amp;ldquo;setter injection”. All it really means is that the property is dual-purpose. When the app runs on my iPhone, it&amp;rsquo;ll use a real UIAlertView. But when I run it in my tests, I can plug in a UIAlertView that I control the behavior of, so that I can verify what I need to in my tests.&lt;/p&gt;
&lt;h4 id=&#34;216&#34;&gt;[2:16]&lt;/h4&gt;
&lt;p&gt;The ability to swap in a kind of Alert View that I control really is the &amp;ldquo;magic sauce” to this whole test-driven operation. In order to know whether or not a UI element was &amp;ldquo;shown” in a unit test without actually showing something on the screen in a simulator or device, I need to invent something known as a test-double. A fake object, if you will.&lt;/p&gt;
&lt;h4 id=&#34;244&#34;&gt;[2:44]&lt;/h4&gt;
&lt;p&gt;My Fake Alert View will have some special capabilities that allow me to know whether the &amp;ldquo;show()” method was called on it. At the same time, it needs to be able to be substituted in my View Controller for a real UIAlertView.&lt;/p&gt;
&lt;h4 id=&#34;300&#34;&gt;[3:00]&lt;/h4&gt;
&lt;p&gt;Since Swift supports object-oriented design, we have inheritance at our disposal here.&lt;/p&gt;
&lt;h4 id=&#34;307&#34;&gt;[3:07]&lt;/h4&gt;
&lt;p&gt;I&amp;rsquo;ll create a nested class inside my test function called FakeAlertView. Notice that it&amp;rsquo;s a subclass of UIAlertView. What&amp;rsquo;s great about this is that it meets both of my testing requirements: I can control it&amp;rsquo;s behavior, and it can be substituted anywhere a UIAlertView is needed.&lt;/p&gt;
&lt;h4 id=&#34;328&#34;&gt;[3:28]&lt;/h4&gt;
&lt;p&gt;I&amp;rsquo;ll finish fleshing out this fake object in a minute. Now that Xcode isn&amp;rsquo;t complaining about not knowing what a FakeAlertView is, I&amp;rsquo;ll turn my attention to a new compiler complaint: I don&amp;rsquo;t have an alertView property on my View Controller yet, so I&amp;rsquo;ll add one.&lt;/p&gt;
&lt;h4 id=&#34;350&#34;&gt;[3:50]&lt;/h4&gt;
&lt;p&gt;There&amp;rsquo;s just a little more setup that&amp;rsquo;s needed in order to be able to verify that the show method was called. Since show() doesn&amp;rsquo;t return anything, we need some way to know that its logic was executed. I&amp;rsquo;ll do two things to expose this:&lt;/p&gt;
&lt;h4 id=&#34;406&#34;&gt;[4:06]&lt;/h4&gt;
&lt;ol&gt;
&lt;li&gt;I&amp;rsquo;ll have a boolean property called showWasCalled on my FakeAlertView that is initially set to false.&lt;/li&gt;
&lt;li&gt;I&amp;rsquo;ll override the show() method in this fake UIAlertView subclass. Inside the method body, I&amp;rsquo;ll reassign the value of showWasCalled to true. That will be enough for me to use inside an XCTAssert, which is coming up.&lt;/li&gt;
&lt;/ol&gt;
&lt;h4 id=&#34;435&#34;&gt;[4:35]&lt;/h4&gt;
&lt;p&gt;We&amp;rsquo;re nearing the finish line here. All that&amp;rsquo;s left is to call my View Controller&amp;rsquo;s viewDidLoad method, and write my assertion.&lt;/p&gt;
&lt;h4 id=&#34;444&#34;&gt;[4:44]&lt;/h4&gt;
&lt;p&gt;The only thing I&amp;rsquo;d tell you to make note of is that we need to cast the View Controller&amp;rsquo;s UIAlertView instance to a FakeAlertView so that we can access the showWasCalled property.&lt;/p&gt;
&lt;h4 id=&#34;457&#34;&gt;[4:57]&lt;/h4&gt;
&lt;p&gt;Running the test at this point should produce a failing test, which is exactly what we want (because there&amp;rsquo;s no code that calls the alertView&amp;rsquo;s show() method in viewDidLoad()).&lt;/p&gt;
&lt;h4 id=&#34;508&#34;&gt;[5:08]&lt;/h4&gt;
&lt;p&gt;The last step to this adventure is to write the production code to pass the test. In viewDidLoad, I call my alertView&amp;rsquo;s show method and re-run the tests.&lt;/p&gt;
&lt;h4 id=&#34;519&#34;&gt;[5:19]&lt;/h4&gt;
&lt;p&gt;And we&amp;rsquo;re green! Which means we&amp;rsquo;ve managed to successfully verify that a method was called.&lt;/p&gt;
&lt;h4 id=&#34;527&#34;&gt;[5:27]&lt;/h4&gt;
&lt;p&gt;Thanks for watching – I have other resources related to Swift and iOS development at andrewcbancroft.com.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Swift Unit Testing Resources</title>
      <link>https://www.andrewcbancroft.com/2014/12/19/swift-unit-testing-resources/</link>
      <pubDate>Fri, 19 Dec 2014 12:00:33 +0000</pubDate>
      
      <guid>https://www.andrewcbancroft.com/2014/12/19/swift-unit-testing-resources/</guid>
      <description>&lt;p&gt;As I&amp;rsquo;ve researched Test Driven Development practices in general, and for iOS / Mac applications, I&amp;rsquo;ve been helped by several sources. I&amp;rsquo;ve also discovered a few things the hard way, so I&amp;rsquo;m combining and maintaining this list of resources to help point you in some helpful directions as you adventure in unit testing your iOS apps.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;pluralsight&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;pluralsight-courses&#34;&gt;Pluralsight Courses&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;http://www.pluralsight.com/courses/play-by-play-wilson-tdd&#34;&gt;Play by Play: TDD with Brad Wilson&lt;/a&gt; – Awesome, awesome course. I learned some really great techniques from this one. The course is geared toward .Net development using Visual Studio, C#, and Moq, but you should still watch it if you&amp;rsquo;ve got a subscription – the techniques used can be done on other platforms. I&amp;rsquo;ve been practicing the techniques in Swift (minus the mocking framework).&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;http://www.pluralsight.com/courses/tdd-as-design-tool&#34;&gt;TDD as a Design Tool&lt;/a&gt; – Geared toward web development with JavaScript / AngularJS, but has some useful patterns.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;a name=&#34;blogs&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;blogs&#34;&gt;Blogs&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;http://roadfiresoftware.com/2014/06/unit-testing-with-swift/&#34;&gt;How to write unit tests in Swift with XCTest&lt;/a&gt; – Josh Brown gives a nice overview of the unit testing capabilities offered in Xcode and how Swift can be used as the testing language of choice.&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;http://natashatherobot.com/swift-unit-testing-tips-and-tricks/&#34;&gt;Swift: Unit Testing Tips and Tricks&lt;/a&gt; – &lt;a href=&#34;https://twitter.com/NatashaTheRobot&#34;&gt;@NatashaTheRobot&lt;/a&gt; offers some practical advice in a tips and tricks style blog post.&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;http://iosunittesting.com/&#34;&gt;iOS Unit Testing&lt;/a&gt; – Some good introductory articles to browse. Offers a few things in Swift, but much of his content is in Objective-C.&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://swiftcast.tv/articles/introduction-to-xctest&#34;&gt;Write your first Unit Test in Swift&lt;/a&gt; – Learn how to write the most basic, but still useful, Unit Tests in Swift with XCTest.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;a name=&#34;user-group&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;user-group-talks&#34;&gt;User Group Talks&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;http://vimeo.com/102163542&#34;&gt;Adam Leonard – Test-Driven Development in Swift using Quick (July 2014)&lt;/a&gt; – Talk given by &lt;a href=&#34;https://twitter.com/adamjleonard&#34;&gt;@adamjleonard&lt;/a&gt; at a Brooklyn Swift Developers Meetup in July.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;a name=&#34;at-acb&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;at-andrewcbancroftcom&#34;&gt;At andrewcbancroft.com&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/2014/12/10/dont-write-legacy-swift/&#34;&gt;Don&amp;rsquo;t Write Legacy Swift&lt;/a&gt; – Motivations for testing your code.&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/2014/12/16/tdd-ios-swift-whats-goal/&#34;&gt;TDD for iOS in Swift – What’s the Goal?&lt;/a&gt; – Before actually doing Test Driven Development (TDD) for your iOS / Mac app, it’s really important to ask a very fundamental question: What’s the goal? I discuss two goals for testing your iOS / Mac apps.&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/2015/06/10/improvements-to-unit-testing-in-swift-with-xcode-7/&#34; title=&#34;Improvements to Unit Testing in Swift with Xcode 7&#34;&gt;Improvements to Unit Testing in Swift with Xcode 7&lt;/a&gt; Much of the pain involved in getting set up to write unit tests in Swift has been alleviated by improvements in Xcode 7. This article analyzes these improvements to ease your unit testing workflow.&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/2014/12/29/getting-started-unit-testing-swift&#34;&gt;Getting Started with Unit Testing in Swift&lt;/a&gt; – While getting set up to write unit tests in Swift isn&amp;rsquo;t &lt;em&gt;difficult&lt;/em&gt;, it isn&amp;rsquo;t exactly &lt;em&gt;intuitive&lt;/em&gt;. This 5-step guide will get your project set up to write unit tests in Swift.&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/2015/03/10/an-almost-tdd-workflow-in-swift/&#34; title=&#34;An (Almost) TDD Workflow in Swift&#34;&gt;An (Almost) TDD Workflow in Swift&lt;/a&gt; – When I find myself staring at the screen, paralyzed because I’m “not supposed to write actual production code until the test is written”, I often turn to the workflow that describe in this post, to help me break through to being productive.&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/2015/01/13/unit-testing-model-layer-core-data-swift/&#34; title=&#34;Unit Testing Model Layer with Core Data and Swift&#34;&gt;Unit Testing Model Layer with Core Data and Swift&lt;/a&gt; – Exploration of testing when your project uses Core Data. This post provides a walk-through of setting up an in-memory data store to test with NSManagedObjects.&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/2014/12/22/swift-unit-testing-verifying-method-calls/&#34;&gt;Swift Unit Testing – Verifying Method Calls&lt;/a&gt; – Screencast showing how to verify method calls in Swift unit tests.&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/2014/07/22/swift-access-control-implications-for-unit-testing/&#34;&gt;Swift Access Control – Implications for Unit Testing&lt;/a&gt; – Pointer on how to make code from your main project visible in your Test project.&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/2014/07/15/how-to-create-mocks-and-stubs-in-swift/&#34;&gt;Creating &amp;ldquo;Mocks” and &amp;ldquo;Stubs” in Swift&lt;/a&gt; – Essentially a discovery I made about how to create fake objects and override those objects&amp;rsquo; methods. You&amp;rsquo;d do this to isolate and control the object&amp;rsquo;s behavior in order to enable or ease the testing process.&lt;/li&gt;
&lt;/ul&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>TDD for iOS in Swift – What’s the Goal?</title>
      <link>https://www.andrewcbancroft.com/2014/12/16/tdd-ios-swift-whats-goal/</link>
      <pubDate>Tue, 16 Dec 2014 12:00:10 +0000</pubDate>
      
      <guid>https://www.andrewcbancroft.com/2014/12/16/tdd-ios-swift-whats-goal/</guid>
      <description>&lt;p&gt;Before actually &lt;em&gt;doing&lt;/em&gt; Test Driven Development (TDD) for your iOS / Mac app, it&amp;rsquo;s really important to ask a very fundamental question: What&amp;rsquo;s the goal? What am I aiming for when I say I want to drive my iOS development with tests?&lt;/p&gt;
&lt;h2 id=&#34;2-goals-of-tdd-in-ios&#34;&gt;2 Goals of TDD in iOS&lt;/h2&gt;
&lt;p&gt;A recent course I watched on &lt;a href=&#34;http://www.pluralsight.com&#34; title=&#34;Pluralsight&#34;&gt;Pluralsight&lt;/a&gt; called &lt;a href=&#34;http://www.pluralsight.com/courses/tdd-as-design-tool&#34; title=&#34;Pluralsight - TDD as a Design Tool&#34;&gt;TDD as a Design Tool&lt;/a&gt; gave me some insight on two goals for doing Test Driven Development:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Make sure my code is in the right place&lt;/li&gt;
&lt;li&gt;Make sure my logic is correct&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Let&amp;rsquo;s take them one at a time…&lt;/p&gt;
&lt;h3 id=&#34;a-place-for-everything-and-everything-in-its-place&#34;&gt;A place for everything, and everything in its place&lt;/h3&gt;
&lt;p&gt;Believe it or not, Test Driven Development is an extremely powerful tool for ensuring that code is written in the right place. But what do I mean by &amp;ldquo;the right place”?&lt;/p&gt;
&lt;p&gt;In the object-oriented world, &amp;ldquo;places” are data structures, such as classes and structs, and their publicly accessible methods.&lt;/p&gt;
&lt;p&gt;When I employ TDD in a project, I will tend to be driven to making sure my code ends up in the right place. I&amp;rsquo;ll give you an example:&lt;/p&gt;
&lt;h4 id=&#34;starting-places&#34;&gt;Starting Places&lt;/h4&gt;
&lt;p&gt;When I create a new iOS project, Xcode sets me up with a Storyboard, a blank Scene, and a View Controller. Xcode also generates a Test target for me… BUT a what am I most aware of right from the onset? The Storyboard and the View Controller.&lt;/p&gt;
&lt;p&gt;The natural inclination, then is to start dragging things onto the design surface and wiring them up to the controller as Outlets and Actions, and off I go!&lt;/p&gt;
&lt;p&gt;It&amp;rsquo;s like I&amp;rsquo;m lead to the ever-common temptation to put &lt;em&gt;all&lt;/em&gt; my code for a given screen in the application inside its corresponding View Controller.&lt;/p&gt;
&lt;h4 id=&#34;tdd-tension&#34;&gt;TDD Tension&lt;/h4&gt;
&lt;p&gt;While I may be led there, and while it may seem convenient, it would seem that TDD wants to start me off in a different &amp;ldquo;place” altogether. Since TDD asserts that I should not write any code unless there&amp;rsquo;s a test requiring it to be written, I&amp;rsquo;d be driven away from my main project into my Test project. My canvas at that point is a fresh XCTestCase class.&lt;/p&gt;
&lt;p&gt;UI at this point is not on my mind. Here, I care more about the &lt;em&gt;foundations&lt;/em&gt; of the app itself. I begin to consider the application&amp;rsquo;s domain, and its behavior &lt;em&gt;apart&lt;/em&gt; from its user interface. This is very important if I want to truly write decoupled, modular, maintainable code.&lt;/p&gt;
&lt;p&gt;Rather than weigh down my View Controller with tons of responsibility, TDD drives me to try and build &lt;em&gt;separate&lt;/em&gt; classes to steward &lt;em&gt;small&lt;/em&gt; bits of my application. Those small classes can be tested &lt;strong&gt;much&lt;/strong&gt; more easily than trying to get an enormous View Controller instantiated and configured in my test suite.&lt;/p&gt;
&lt;p&gt;This is just one small example of how TDD can get you off on the right track to putting code in its proper place.&lt;/p&gt;
&lt;h3 id=&#34;your-logic-was-impeccable-captain&#34;&gt;Your logic was impeccable, Captain&lt;/h3&gt;
&lt;p&gt;The second goal of TDD in iOS is making sure my logic is correct, or, as Spock would say, &amp;ldquo;impeccable”.&lt;/p&gt;
&lt;p&gt;Does my application&amp;rsquo;s code do what it &lt;em&gt;should&lt;/em&gt; do? Can I write my code in such a way that I can easily verify it? TDD, by nature, pushes me in the direction of being able to verify the accuracy of my code&amp;rsquo;s logical outcomes… that is, how it behaves.&lt;/p&gt;
&lt;h4 id=&#34;fascinating-is-a-word-i-use-for-the-unexpected&#34;&gt;Fascinating is a word I use for the unexpected&lt;/h4&gt;
&lt;p&gt;Testing has this way of setting expectations. One thing I&amp;rsquo;ve really enjoyed about TDD&amp;rsquo;s notorious red-green-refactor cycle is that I know certainly and immediately when I&amp;rsquo;ve messed up (ie, run across a &amp;ldquo;fascinating” situation in Spock terms).&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;When I write the test, it should fail the first time. If it doesn&amp;rsquo;t, I&amp;rsquo;ve messed up.&lt;/li&gt;
&lt;li&gt;When I write the code to pass the test and the test fails, I &lt;em&gt;also&lt;/em&gt; know I&amp;rsquo;ve messed up.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;next-steps&#34;&gt;Next Steps&lt;/h2&gt;
&lt;p&gt;With the fundamental goals of TDD in place, I feel more prepared from a foundational standpoint to venture into actuall test-driven practices for iOS.&lt;/p&gt;
&lt;p&gt;I am learning so much in the area of testing – it&amp;rsquo;s a technique I&amp;rsquo;m practicing regularly, both in Swift and in C# (for fun and for work, respectively), so as I grow and discover ways to optimize the TDD experience in Xcode / Swift, I&amp;rsquo;ll be sharing them. Stay tuned!&lt;/p&gt;
&lt;div class=&#34;related-posts&#34;&gt;
  You might also enjoy&lt;/p&gt; 
  &lt;ul&gt;
    &lt;li&gt;
      &lt;a href=&#34;http://www.andrewcbancroft.com/2014/12/19/swift-unit-testing-resources/&#34; title=&#34;Swift Unit Testing Resources&#34;&gt;Swift Unit Testing Resources&lt;/a&gt;
    &lt;/li&gt;
    &lt;li&gt;
      &lt;a href=&#34;http://www.andrewcbancroft.com/2014/12/22/swift-unit-testing-verifying-method-calls/&#34; title=&#34;Swift Unit Testing – Verifying Method Calls&#34;&gt;Swift Unit Testing – Verifying Method Calls&lt;/a&gt;
    &lt;/li&gt;
    &lt;li&gt;
      &lt;a href=&#34;http://www.andrewcbancroft.com/2014/07/15/how-to-create-mocks-and-stubs-in-swift/&#34; title=&#34;How to Create Mocks and Stubs in Swift&#34;&gt;How to Create Mocks and Stubs in Swift&lt;/a&gt;
    &lt;/li&gt;
    &lt;li&gt;
      &lt;a href=&#34;http://www.andrewcbancroft.com/2014/07/22/swift-access-control-implications-for-unit-testing/&#34; title=&#34;Swift Access Control – Implications for Unit Testing&#34;&gt;Swift Access Control – Implications for Unit Testing&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;
</description>
    </item>
    
    <item>
      <title>Don’t Write Legacy Swift</title>
      <link>https://www.andrewcbancroft.com/2014/12/10/dont-write-legacy-swift/</link>
      <pubDate>Wed, 10 Dec 2014 12:00:51 +0000</pubDate>
      
      <guid>https://www.andrewcbancroft.com/2014/12/10/dont-write-legacy-swift/</guid>
      <description>&lt;p&gt;“Legacy Swift”.  Is that an odd phrase to you, too?&lt;/p&gt;
&lt;p&gt;“Legacy” has connotations such as “old”, “difficult-to-change”, “outdated”, etc. Juxtaposing “legacy” and “Swift” would never have crossed my mind until I read Michael Feathers’, &lt;a href=&#34;http://www.amazon.com/Working-Effectively-Legacy-Michael-Feathers/dp/0131177052&#34; title=&#34;Working Effectively With Legacy Code&#34;&gt;Working Effectively With Legacy Code&lt;/a&gt;, where he asserts,&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;font face=&#34;Thread-00004318-Id-00000000&#34;&gt;Legacy code is simply code without tests.&lt;/font&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;What an impact those words had on me as I’ve transitioned over to Swift from Objective-C.  If it’s true that legacy code is code without tests, it’s easy to see how you and I could be writing legacy Swift, even though Swift is a brand new language!&lt;/p&gt;
&lt;p&gt;And whether you agree with Feathers’ definition of “legacy code” or not (he readily admits the potential for controversy with the definition), I think we can &lt;em&gt;all&lt;/em&gt; concede that unit testing our code has many benefits and leads to the ability to &lt;em&gt;consistently,&lt;/em&gt; &lt;em&gt;objectively&lt;/em&gt; &lt;em&gt;verify&lt;/em&gt;  the quality of our code at the end of the day.&lt;/p&gt;
&lt;h3 id=&#34;legacy-swift--bad-swift&#34;&gt;Legacy Swift == Bad Swift&lt;/h3&gt;
&lt;p&gt;Unashamedly, Feathers writes,&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;font face=&#34;Thread-00004318-Id-00000000&#34;&gt;Code without tests is bad code…With tests, we can change the behavior of our code quickly and verifiably. Without them, we really don’t know if our code is getting better or worse.&lt;/font&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;font size=&#34;6&#34; face=&#34;Thread-00004318-Id-00000001&#34;&gt;My take-away from that quote is this:  The &lt;em&gt;only&lt;/em&gt;  way to consistently, objectively verify whether my code is getting better or worse is to execute it and see if the results are what I expected.  I can do this in the Simulator / on my device, over and &lt;em&gt;over&lt;/em&gt; &lt;strong&gt;&lt;em&gt;and over&lt;/em&gt;&lt;/strong&gt;, opening myself up to a never-ending, time-consuming, error-prone testing cycle, OR I can write automated unit tests and run them every time as I make incremental changes to my software.&lt;/font&gt;&lt;/p&gt;
&lt;h3 id=&#34;testing-is-hard-for-me&#34;&gt;Testing is hard (for me)&lt;/h3&gt;
&lt;p&gt;I have not always been the most successful at writing unit tests.  Test-driven development (TDD) is difficult for me at times.  I often find myself sitting, staring at the screen as if looking into an abyss, unsure of what I’m supposed to do next.  (Any of you TDD-ers who’ve figured out how to imagine the structure of your production code &lt;em&gt;prior to it being written&lt;/em&gt; so that a test can be crafted &lt;em&gt;first&lt;/em&gt;, let me know your tricks!)&lt;/p&gt;
&lt;p&gt;I’m growing in this area of writing testable code, and the tests that go along with it.  Admittedly, I’m still in the stages of “this is hard”, sometimes. &lt;/p&gt;
&lt;p&gt;I &lt;em&gt;have&lt;/em&gt; noticed, though, that coding and testing patterns are emerging in my toolbox that are making this process more and more fluid for me.&lt;/p&gt;
&lt;h3 id=&#34;testing-is-worth-it&#34;&gt;Testing is worth it&lt;/h3&gt;
&lt;p&gt;However difficult it is for me, I’m on board with believing that the effort put in to writing tests is worth it in the end.  I wrestle with my code to tame it with tests &lt;em&gt;from the start&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;If I’m unable to figure out test-first development on a particular problem, I strive to write the tests as jointly with my production code as possible.  At all costs, I try to avoid writing the code and saying to myself, “I’ll come back to the tests later”, because I don’t trust myself to keep that promise.&lt;/p&gt;
&lt;p&gt;With every product I’ve written and tested, I can testify to the fact that I feel good about that code.  I know it works how I intended it to work.  I rest assured that when I make changes, I haven’t broken anything I programmed in another session.  The tests help me remember what I intended for my software to do.  I’m confident that the patterns that evolved out of the tested code are better than what I’d have implemented if I did without the tests.  It’s worth it.&lt;/p&gt;
&lt;h3 id=&#34;what-kind-of-swift-will-you-write&#34;&gt;What kind of Swift will you write?&lt;/h3&gt;
&lt;p&gt;The main goal of this post was to get you thinking.  Feathers got &lt;em&gt;me&lt;/em&gt; thinking, so I thought I’d pass along the challenge:  Don’t write legacy Swift! &lt;/p&gt;
&lt;p&gt;The good news is that, as Feathers writes,&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;[Legacy code is code without tests] is a good working definition, and it points to a solution.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;And that solution is straightforward:  Begin writing tests for your code, and you’ll avoid writing legacy Swift.&lt;/p&gt;
&lt;div class=&#34;related-posts&#34;&gt;
  You might also enjoy&lt;/p&gt; 
  &lt;ul&gt;
    &lt;li&gt;
      &lt;a href=&#34;http://www.andrewcbancroft.com/2014/07/15/how-to-create-mocks-and-stubs-in-swift/&#34; title=&#34;How to Create Mocks and Stubs in Swift&#34;&gt;How to Create Mocks and Stubs in Swift&lt;/a&gt;
    &lt;/li&gt;
    &lt;li&gt;
      &lt;a href=&#34;http://www.andrewcbancroft.com/2014/07/22/swift-access-control-implications-for-unit-testing/&#34; title=&#34;Swift Access Control – Implications for Unit Testing&#34;&gt;Swift Access Control – Implications for Unit Testing&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&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>
    
    <item>
      <title>Swift UITableViewDataSource Cheat Sheet</title>
      <link>https://www.andrewcbancroft.com/2014/11/24/swift-uitableviewdatasource-cheat-sheet/</link>
      <pubDate>Mon, 24 Nov 2014 22:04:28 +0000</pubDate>
      
      <guid>https://www.andrewcbancroft.com/2014/11/24/swift-uitableviewdatasource-cheat-sheet/</guid>
      <description>&lt;p&gt;iOS developers will quickly recognize that there are a set of methods that always tend to get implemented when dealing with &lt;code&gt;UITableViews&lt;/code&gt; . The problem I consistently face is &lt;em&gt;remembering&lt;/em&gt; that set of methods that belong to the &lt;code&gt;UITableViewDataSource&lt;/code&gt; (and &lt;code&gt;UITableViewDelegate&lt;/code&gt; ) protocols. I find myself option-clicking the protocol name to remember the method signatures I need, since Xcode doesn&amp;rsquo;t have a way to stub out the methods involved with a protocol (C# developers working in Visual Studio like myself are spoiled!).&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;m sure cheat sheets like this already exist, but I thought, &amp;ldquo;Why not have one that I can reference from my &lt;em&gt;own&lt;/em&gt; blog?”… In fact, I&amp;rsquo;ve gone ahead and turned the code below into a snippet in Xcode, but just in case I ever lose that, I&amp;rsquo;ve got this post, which stubs out dummy implementations of the three most common &lt;code&gt;UITableViewDataSource&lt;/code&gt; protocol methods.&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;d be delighted if it helped my readers as well, so without further ado, here&amp;rsquo;s my &lt;code&gt;UITableViewDataSource&lt;/code&gt; cheat sheet, which should allow you (and me) to copy and paste directly into our &lt;code&gt;UITableViewDataSource&lt;/code&gt; class for a quick start:&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;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;numberOfSectionsInTableView&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;tableView&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UITableView&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; 2&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;mi&#34;&gt;1&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;// This was put in mainly for my own unit testing&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;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; 4&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; 5&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;tableView&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;tableView&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UITableView&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;numberOfRowsInSection&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;section&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;-&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; 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;n&#34;&gt;dataSourceArray&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;bp&#34;&gt;count&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;// Most of the time my data source is an array of something...  will replace with the actual name of the data source&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&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;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;tableView&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;tableView&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UITableView&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;cellForRowAtIndexPath&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;indexPath&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSIndexPath&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;n&#34;&gt;UITableViewCell&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;10&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Note:  Be sure to replace the argument to dequeueReusableCellWithIdentifier with the actual identifier string!&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 class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;cell&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;tableView&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;dequeueReusableCellWithIdentifier&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;ReplaceWithCellIdentifier&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;as&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;!&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UITableViewCell&lt;/span&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&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;c1&#34;&gt;// set cell&amp;#39;s textLabel.text property&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;c1&#34;&gt;// set cell&amp;#39;s detailTextLabel.text property&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;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;cell&lt;/span&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;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</description>
    </item>
    
    <item>
      <title>The 5 W’s of Swift Extensions</title>
      <link>https://www.andrewcbancroft.com/2014/11/03/the-5-ws-of-swift-extensions/</link>
      <pubDate>Mon, 03 Nov 2014 19:04:19 +0000</pubDate>
      
      <guid>https://www.andrewcbancroft.com/2014/11/03/the-5-ws-of-swift-extensions/</guid>
      <description>&lt;p&gt;For newcomers to Swift, the concept of extending a Type may be new, so I thought I&amp;rsquo;d share a high-level overview of Swift extensions in the style of asking the 5 W&amp;rsquo;s: Who? What? When? Where? Why?&lt;/p&gt;
&lt;h3 id=&#34;who&#34;&gt;Who&lt;/h3&gt;
&lt;p&gt;&amp;ldquo;Swift developers” is a pretty obvious answer, but here are a scenarios to ask yourself about to see if you could make use of Swift extensions:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&amp;ldquo;I am relying on Types that are found in 3rd party frameworks or libraries and I wish I could add a little bit of functionality that isn&amp;rsquo;t there out of the box. Can I do that easily?”&lt;/li&gt;
&lt;li&gt;&amp;ldquo;In utilizing Apple&amp;rsquo;s frameworks I find that a Type could be more powerful if it could &lt;em&gt;just do&lt;/em&gt; [x]. Can I add that power to the Apple framework Type?”&lt;/li&gt;
&lt;li&gt;&amp;ldquo;I have written a framework for myself / my team that is used in multiple apps, but I desire to add behavior to one of the framework&amp;rsquo;s Types that is only relevant in a specific area of one particular app. Can I add that special functionality without changing the framework source?”&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If you related to any or all of those scenarios, you could potentially make use of Swift extensions to speed your workflow and make your more organized and clean. Read on for more!&lt;/p&gt;
&lt;h3 id=&#34;what&#34;&gt;What&lt;/h3&gt;
&lt;blockquote&gt;
&lt;p&gt;Extensions add new functionality to an &lt;strong&gt;existing&lt;/strong&gt; class, structure, or enumeration type.&lt;br&gt;
– &lt;a href=&#34;https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Extensions.html&#34; title=&#34;Apple Swift Developer Referenc&#34;&gt;Apple Swift Developer Reference&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;The key word is &lt;strong&gt;existing&lt;/strong&gt;. Hopefully the scenarios listed under &amp;ldquo;Who” make a little more sense with this information in mind. In all of those cases, you wish to add functionality to Types that have &lt;em&gt;already&lt;/em&gt; been designed and implemented.&lt;/p&gt;
&lt;p&gt;What I have found to be amazing about Swift extensions is that the word &amp;ldquo;existing” &lt;em&gt;also&lt;/em&gt; applies to Types for which you don&amp;rsquo;t have access to the original source code. &lt;em&gt;That&lt;/em&gt; is powerful!&lt;/p&gt;
&lt;p&gt;So extensions are especially useful if you want to add behavior to a Type found in any of the Cocoa / Cocoa Touch frameworks, or a 3rd party&amp;rsquo;s framework. Additionally, if you&amp;rsquo;ve created your own libraries and frameworks and you&amp;rsquo;d like to add behavior without modifying the original source, extensions are a good choice for adding that behavior, especially if what you&amp;rsquo;re adding doesn&amp;rsquo;t apply to every single usage of the Type you&amp;rsquo;re extending.&lt;/p&gt;
&lt;h3 id=&#34;when&#34;&gt;When&lt;/h3&gt;
&lt;p&gt;The most logical time to use Swift extensions is when you have a piece of functionality that you want to be able to &lt;em&gt;reuse&lt;/em&gt;, and the most reasonable place to put that code is within an already-existing Type. Ideally, the piece of functionality is &lt;em&gt;so closely related&lt;/em&gt; to that Type that it just makes sense to extend that Type&amp;rsquo;s behavior to include your new functionality… &amp;ldquo;If &lt;em&gt;only&lt;/em&gt; I could peek inside that Type&amp;rsquo;s implementation to inject my new code”, you think to yourself.&lt;/p&gt;
&lt;p&gt;If you&amp;rsquo;re pondering along those lines, then chances are, Swift extensions will aid you in your effort to create more reusable, clean, and organized code.&lt;/p&gt;
&lt;p&gt;Perhaps a good question to ask here is, &lt;strong&gt;&amp;ldquo;When &lt;em&gt;not&lt;/em&gt;?”&lt;/strong&gt;. When I see phrases like, &amp;ldquo;most reasonable place to put that code”, it begs the question, &amp;ldquo;What does ‘most reasonable&amp;rsquo; mean?”. The answer isn&amp;rsquo;t black and white. I&amp;rsquo;ll offer what I&amp;rsquo;ve done in the past as general advice:&lt;/p&gt;
&lt;p&gt;As a general rule of thumb, extensions are great for small, simple additions to a Type, rather than long, complex functionality.&lt;/p&gt;
&lt;p&gt;If you&amp;rsquo;re implementing a &lt;em&gt;set&lt;/em&gt; of behavior (more than a handful of methods or computed properties), creating a new Type to encapsulate that behavior is most appropriate. After all, that&amp;rsquo;s what classes and structs &lt;em&gt;do&lt;/em&gt;. When deciding on extension vs new Type, they key word that comes to my mind is &lt;strong&gt;relatedness&lt;/strong&gt;. &lt;em&gt;How related&lt;/em&gt; is this behavior to the Type I think I want to extend?&lt;/p&gt;
&lt;p&gt;Additionally, extensions are limited in the area of maintaining state. Extensions cannot add new &lt;a href=&#34;https://developer.apple.com/library/ios/documentation/swift/conceptual/Swift_Programming_Language/Properties.html#//apple_ref/doc/uid/TP40014097-CH14-XID_381&#34;&gt;stored properties&lt;/a&gt; (only &lt;a href=&#34;https://developer.apple.com/library/ios/documentation/swift/conceptual/Swift_Programming_Language/Properties.html#//apple_ref/doc/uid/TP40014097-CH14-XID_386&#34;&gt;computed ones&lt;/a&gt;). Creating a new Type to encapsulate the stored properties and the behavior that utilizes them is your alternative solution in this case.&lt;/p&gt;
&lt;h3 id=&#34;where&#34;&gt;Where&lt;/h3&gt;
&lt;p&gt;So you&amp;rsquo;ve decided that you want to create an extension to some class, struct, or enumeration. Where do you put it? My thought on this has always been to create a new .swift file, give it a name that indicates which Type you&amp;rsquo;re extending, and create the extension inside that file.&lt;/p&gt;
&lt;p&gt;So if I want to extend, say, String to have a new method called &amp;ldquo;sayHello” which printed &amp;ldquo;Hello” when invoked, I would create a new .swift file called StringExtensions.swift. I&amp;rsquo;d then place my extension inside:&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;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;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;sayHello&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;3&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;bp&#34;&gt;println&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;Hello&amp;#34;&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;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 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;Hey, awesome – you got a bonus &lt;em&gt;How&lt;/em&gt; with that quick example! Creating an extension in Swift is &lt;em&gt;that easy&lt;/em&gt;.&lt;/p&gt;
&lt;h3 id=&#34;why&#34;&gt;Why&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Swift extensions are powerful. They empower you to add behavior to &lt;em&gt;any&lt;/em&gt; class, struct, or enumeration, even if you don&amp;rsquo;t have access to the original source code.&lt;/li&gt;
&lt;li&gt;They are simple and convenient to create.&lt;/li&gt;
&lt;li&gt;They encourage code re-use by encapsulating behavior that will be used more than once in your project in a single location.&lt;/li&gt;
&lt;li&gt;Additionally, they promote good code organization, leading to cleaner and more readable code when used to add behavior that&amp;rsquo;s closely related to the Type they&amp;rsquo;re extending.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;summary&#34;&gt;Summary&lt;/h3&gt;
&lt;p&gt;We&amp;rsquo;ve just explored Swift extensions by analyzing &lt;em&gt;who&lt;/em&gt; should use them, &lt;em&gt;what&lt;/em&gt; they are, &lt;em&gt;when&lt;/em&gt; they&amp;rsquo;re appropriate, &lt;em&gt;where&lt;/em&gt; to put them, and &lt;em&gt;why&lt;/em&gt; they&amp;rsquo;re useful to your development efforts. As a bonus, &lt;em&gt;how&lt;/em&gt; to program an extension was given as a simple example.&lt;/p&gt;
&lt;p&gt;If you&amp;rsquo;ve found Swift extensions to be useful to your development efforts, sound off in the comments below! I&amp;rsquo;d love to hear how you&amp;rsquo;re utilizing this feature of Swift.&lt;/p&gt;
&lt;div class=&#34;related-posts&#34;&gt;
  You might also enjoy&lt;/p&gt; 
  &lt;ul&gt;
    &lt;li&gt;
      &lt;a href=&#34;http://www.andrewcbancroft.com/2014/07/27/fade-in-out-animations-as-class-extensions-with-swift/&#34; title=&#34;Fade In / Out Animations as Class Extensions in Swift&#34;&gt;Fade In / Out Animations as Class Extensions in Swift&lt;/a&gt;
    &lt;/li&gt;
    &lt;li&gt;
      &lt;a href=&#34;http://www.andrewcbancroft.com/2014/10/15/rotate-animation-in-swift/&#34; title=&#34;Rotate Animation in Swift&#34;&gt;Rotate Animation in Swift&lt;/a&gt;
    &lt;/li&gt;
    &lt;li&gt;
      &lt;a href=&#34;http://www.andrewcbancroft.com/2014/09/24/slide-in-animation-in-swift/&#34; title=&#34;Slide In Animation in Swift&#34;&gt;Slide In Animation in Swift&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;
</description>
    </item>
    
    <item>
      <title>Send Text Message In-App – Using MFMessageComposeViewController with Swift</title>
      <link>https://www.andrewcbancroft.com/2014/10/28/send-text-message-in-app-using-mfmessagecomposeviewcontroller-with-swift/</link>
      <pubDate>Tue, 28 Oct 2014 11:00:37 +0000</pubDate>
      
      <guid>https://www.andrewcbancroft.com/2014/10/28/send-text-message-in-app-using-mfmessagecomposeviewcontroller-with-swift/</guid>
      <description>&lt;p&gt;In a &lt;a href=&#34;http://www.andrewcbancroft.com/2014/08/25/send-email-in-app-using-mfmailcomposeviewcontroller-with-swift/&#34;&gt;previous walkthrough on sending e-mails in-app&lt;/a&gt;, I explored how to use &lt;code&gt;MFMailComposeViewController&lt;/code&gt; to allow a user to compose an e-mail without ever leaving your app. I then &lt;a href=&#34;http://www.andrewcbancroft.com/2014/08/26/pick-a-delegate-clean-view-controllers-in-swift/&#34;&gt;followed up with a proposal for better-segregating the responsibilities&lt;/a&gt; of composing an e-mail and responding to the delegate callbacks out of the View Controller.&lt;/p&gt;
&lt;p&gt;I say all this because today, I&amp;rsquo;d like to walk you through sending a &lt;em&gt;text message&lt;/em&gt; in-app using the same pattern as the &lt;em&gt;second&lt;/em&gt; article. This walkthrough should be fairly straight-forward on its own, but if you&amp;rsquo;d like to reference &lt;a href=&#34;http://www.andrewcbancroft.com/2014/08/26/pick-a-delegate-clean-view-controllers-in-swift/&#34;&gt;my previous post on clean view controllers&lt;/a&gt;, it could be helpful in understanding why I&amp;rsquo;m not just shoving everything into the View Controller and calling it good.&lt;/p&gt;
&lt;h3 id=&#34;the-gist&#34;&gt;The Gist&lt;/h3&gt;
&lt;p&gt;Here&amp;rsquo;s the gist of the components we&amp;rsquo;ll need in order to accomplish the task of using &lt;code&gt;MFMessageComposeViewController&lt;/code&gt; to send text messages in-app. Presumably, you&amp;rsquo;d like to allow your users to send a text message to a pre-defined recipient, or maybe you want to pre-populate a message and allow your users to text it to whomever they desire. Any and all of this can be accomplished by implementing this general outline:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Create a class (I&amp;rsquo;ll call it &lt;code&gt;MessageComposer&lt;/code&gt;) that will handle the responsibility of creating/configuring a text message composer.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;MessageComposer&lt;/code&gt; will also handle the &lt;code&gt;MFMessageComposeViewControllerDelegate&lt;/code&gt; callback method (&lt;br&gt;
&lt;code&gt;messageComposeViewController:didFinishWithResult:&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Finally, I&amp;rsquo;ll program a View Controller to create an instance of this &lt;code&gt;MessageComposer&lt;/code&gt; class and present it based upon some user action, such as tapping a button.&lt;/li&gt;
&lt;li&gt;Note that to see the example in action, you&amp;rsquo;ll have to run it on an actual device, because the Simulator is unable to send text messages…&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For those who just like to dive in and explore, feel free to &lt;a href=&#34;https://github.com/andrewcbancroft/SwiftMFMessageComposeViewController&#34;&gt;head over to GitHub&lt;/a&gt; and grab the example project now!&lt;/p&gt;
&lt;h3 id=&#34;the-details&#34;&gt;The Details&lt;/h3&gt;
&lt;p&gt;With the general idea in mind, let&amp;rsquo;s jump in to discover how to implement the solution…&lt;/p&gt;
&lt;h4 id=&#34;messagecomposer&#34;&gt;MessageComposer&lt;/h4&gt;
&lt;p&gt;While it&amp;rsquo;s &lt;em&gt;possible&lt;/em&gt; to simply put all of this code inside the View Controller, &lt;a href=&#34;http://www.andrewcbancroft.com/2014/08/26/pick-a-delegate-clean-view-controllers-in-swift/&#34;&gt;I&amp;rsquo;d recommend doing your best to avoid it&lt;/a&gt;. While this simple example doesn&amp;rsquo;t add a ton of complexity if you just write it all into your View Controller, a real-world app will undoubtedly be more complex.&lt;/p&gt;
&lt;p&gt;In my experience, it&amp;rsquo;s best to try and segregate out as many responsibilities of functionality from the View Controller where it&amp;rsquo;s possible. Thankfully, simple examples like this highlight the ease of segregating these responsibilities so that you can begin to employ the pattern for &lt;em&gt;other&lt;/em&gt; components that you use within your app.&lt;/p&gt;
&lt;p&gt;So in keeping with this idea of nurturing a clean View Controller, I&amp;rsquo;ve decided to create a new custom class called &lt;code&gt;MessageComposer&lt;/code&gt;. Take a look:&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;import&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;Foundation&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;import&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;MessageUI&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;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;textMessageRecipients&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 class=&#34;s&#34;&gt;&amp;#34;1-800-867-5309&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;// for pre-populating the recipients list (optional, depending on your needs)&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;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;MessageComposer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSObject&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;MFMessageComposeViewControllerDelegate&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&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;c1&#34;&gt;// A wrapper function to indicate whether or not a text message can be sent from the user&amp;#39;s device&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;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;canSendText&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;Bool&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;10&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;MFMessageComposeViewController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;canSendText&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;11&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;12&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;13&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Configures and returns a MFMessageComposeViewController instance&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;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;configuredMessageComposeViewController&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;n&#34;&gt;MFMessageComposeViewController&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;15&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;messageComposeVC&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;MFMessageComposeViewController&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;16&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;messageComposeVC&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;messageComposeDelegate&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;kc&#34;&gt;self&lt;/span&gt;  &lt;span class=&#34;c1&#34;&gt;//  Make sure to set this property to self, so that the controller can be dismissed!&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;n&#34;&gt;messageComposeVC&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;recipients&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;textMessageRecipients&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;n&#34;&gt;messageComposeVC&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;body&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;Hey friend - Just sending a text message in-app using Swift!&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;19&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;messageComposeVC&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;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;21&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;22&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// MFMessageComposeViewControllerDelegate callback - dismisses the view controller when the user is finished with it&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;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;messageComposeViewController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;controller&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;MFMessageComposeViewController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;!,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;didFinishWithResult&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;result&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;MessageComposeResult&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;24&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;controller&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;dismissViewControllerAnimated&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;kc&#34;&gt;true&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;completion&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;kc&#34;&gt;nil&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;25&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;26&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;Taking a close look through the comments, you&amp;rsquo;ll notice the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;You need to import the &lt;code&gt;MessageUI&lt;/code&gt; module.&lt;/li&gt;
&lt;li&gt;Rather than hard-code the recipients list, I&amp;rsquo;ve declared a constant at a global scope for easy access/changeability in the future. It&amp;rsquo;s actually optional to even supply a recipients list to your composer… it&amp;rsquo;s just dependent on how you want to use it. Notice that it&amp;rsquo;s an &lt;strong&gt;array of Strings&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;MessageComposer&lt;/code&gt; inherits from &lt;code&gt;NSObject&lt;/code&gt;. This is a requirement of the &lt;code&gt;MFMessageComposeViewControllerDelegate&lt;/code&gt; protocol, which &lt;code&gt;MessageComposer&lt;/code&gt; conforms to.&lt;/li&gt;
&lt;li&gt;There&amp;rsquo;s a function in there called &lt;code&gt;canSendText&lt;/code&gt; that becomes important later on for testing whether or not sending a text message is even &lt;em&gt;possible&lt;/em&gt; at the moment on the user&amp;rsquo;s device. It wraps &lt;code&gt;MFMessageComposeViewController&lt;/code&gt;‘s &lt;code&gt;canSendText&lt;/code&gt; method to avoid needing to import the &lt;code&gt;MessageUI&lt;/code&gt; module in other places (like the View Controller that uses instances of this class).&lt;/li&gt;
&lt;li&gt;&lt;code&gt;configuredMessageComposeViewController&lt;/code&gt; does what it says it does – it returns an instance of a &lt;code&gt;MFMessageComposeViewController&lt;/code&gt; that&amp;rsquo;s been configured with a list of recipients and a body.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;messageComposeViewController:didFinishWithResult:&lt;/code&gt; is what gets called with the user either sends the text, or cancels sending a text from the message composer. I&amp;rsquo;ve written code in that method&amp;rsquo;s body to simply dismiss the instance of the view controller that called the method.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;We&amp;rsquo;re now ready to head to the View Controller and wire up the components allowing a user to display a &lt;code&gt;MessageComposer&lt;/code&gt;!&lt;/p&gt;
&lt;h3 id=&#34;view-controller&#34;&gt;View Controller&lt;/h3&gt;
&lt;p&gt;The View Controller is very simple for this example:&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;import&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;UIKit&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&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;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;ViewController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIViewController&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; 4&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Create a MessageComposer&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;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;messageComposer&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;MessageComposer&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&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;kr&#34;&gt;@IBAction&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;sendTextMessageButtonTapped&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;sender&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIButton&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; 8&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;c1&#34;&gt;// Make sure the device can send text messages&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;if&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;messageComposer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;canSendText&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;10&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;            &lt;span class=&#34;c1&#34;&gt;// Obtain a configured MFMessageComposeViewController&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 class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;messageComposeVC&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;messageComposer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;configuredMessageComposeViewController&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;12&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;13&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;            &lt;span class=&#34;c1&#34;&gt;// Present the configured MFMessageComposeViewController instance&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;c1&#34;&gt;// Note that the dismissal of the VC will be handled by the messageComposer instance,&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;c1&#34;&gt;// since it implements the appropriate delegate call-back&lt;/span&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;n&#34;&gt;presentViewController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;messageComposeVC&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;animated&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;kc&#34;&gt;true&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;completion&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;kc&#34;&gt;nil&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;17&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;else&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;c1&#34;&gt;// Let the user know if his/her device isn&amp;#39;t able to send text messages&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;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;errorAlert&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIAlertView&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;title&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;Cannot Send Text Message&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;message&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;Your device is not able to send text messages.&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;delegate&lt;/span&gt;&lt;span class=&#34;p&#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;cancelButtonTitle&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;OK&amp;#34;&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;n&#34;&gt;errorAlert&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;show&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;21&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;22&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;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;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;ul&gt;
&lt;li&gt;You&amp;rsquo;ll notice that I create an instance of &lt;code&gt;MessageComposer&lt;/code&gt; (the custom class we just defined in previous steps). It&amp;rsquo;s declared at a scope that can be seen throughout the lifetime of the View Controller (so that the delegate callback can be invoked when appropriate).&lt;/li&gt;
&lt;li&gt;&lt;code&gt;sendTextMessageButtonTapped&lt;/code&gt; is wired up to a button on my storyboard, and will be executed when the user taps the button.&lt;/li&gt;
&lt;li&gt;Checking to make sure the device can send a text message is critical – Note that running the example in the Simulator will execute the &lt;em&gt;else&lt;/em&gt; block, because the Simulator cannot send text messages.&lt;/li&gt;
&lt;li&gt;If the device &lt;em&gt;can&lt;/em&gt; send texts, a configured &lt;code&gt;MFMessageComposeViewController&lt;/code&gt; is obtained from the &lt;code&gt;MessageComposer&lt;/code&gt; instance. It&amp;rsquo;s then displayed.&lt;/li&gt;
&lt;li&gt;If the device &lt;em&gt;can&amp;rsquo;t&lt;/em&gt; send texts, it&amp;rsquo;s probably a good idea to alert the user somehow… I&amp;rsquo;ve chosen a simple &lt;code&gt;UIAlertView&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;summary&#34;&gt;Summary&lt;/h3&gt;
&lt;p&gt;In this walkthrough, I&amp;rsquo;ve demonstrated the mechanics of configuring, displaying, and dismissing a &lt;code&gt;MFMessageComposeViewController&lt;/code&gt;, which enables your users to send a text message in-app. Additionally, I&amp;rsquo;ve attempted to show how to keep the View Controller from handling more than it really should, by segregating out the &lt;code&gt;MFMessageComposeViewController&lt;/code&gt; configuration and delegate protocol conformance to another class.&lt;/p&gt;
&lt;p&gt;Happy texting!&lt;/p&gt;
&lt;div class=&#34;related-posts&#34;&gt;
  You might also enjoy&lt;/p&gt; 
  &lt;ul&gt;
    &lt;li&gt;
      &lt;a href=&#34;http://www.andrewcbancroft.com/2014/08/25/send-email-in-app-using-mfmailcomposeviewcontroller-with-swift/&#34; title=&#34;Send Email In-App – Using MFMailComposeViewController with Swift&#34;&gt;Send Email In-App – Using MFMailComposeViewController with Swift&lt;/a&gt;
    &lt;/li&gt;
    &lt;li&gt;
      &lt;a href=&#34;http://www.andrewcbancroft.com/2014/08/26/pick-a-delegate-clean-view-controllers-in-swift/&#34; title=&#34;Pick a Delegate… Any Delegate… On Clean View Controllers in Swift&#34;&gt;Pick a Delegate… Any Delegate… On Clean View Controllers in Swift&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;
</description>
    </item>
    
    <item>
      <title>Fundamentals of NSNotificationCenter in Swift</title>
      <link>https://www.andrewcbancroft.com/2014/10/08/fundamentals-of-nsnotificationcenter-in-swift/</link>
      <pubDate>Wed, 08 Oct 2014 18:27:47 +0000</pubDate>
      
      <guid>https://www.andrewcbancroft.com/2014/10/08/fundamentals-of-nsnotificationcenter-in-swift/</guid>
      <description>&lt;p&gt;&lt;small&gt;Updated on September 19, 2016 – Xcode 8 &amp;amp; Swift 3.0&lt;/small&gt;&lt;/p&gt;
&lt;p&gt;The goal of this post is to help you grasp the fundamentals of using &lt;code&gt;NSNotificationCenter&lt;/code&gt; in your Swift iOS app. The topic isn&amp;rsquo;t necessarily new, and there&amp;rsquo;s no difference except syntax between Objective-C and Swift implementations involving &lt;code&gt;NSNotificationCenter&lt;/code&gt;. Even still, I&amp;rsquo;m hoping to add to the understanding of this useful tool by giving a fully-working example on GitHub (&lt;a title=&#34;GitHub - Swift NSNotificationCenter Example&#34; href=&#34;https://github.com/andrewcbancroft/SwiftNSNotificationCenter/tree/swift-2.3&#34; target=&#34;_blank&#34;&gt;Swift 2.3&lt;/a&gt; and &lt;a title=&#34;GitHub - Swift NSNotificationCenter Example&#34; href=&#34;https://github.com/andrewcbancroft/SwiftNSNotificationCenter&#34; target=&#34;_blank&#34;&gt;Swift 3.0&lt;/a&gt;), and by sharing the insight that&amp;rsquo;s clicked in my own brain as I explain the example.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Code in the main article below is written in Swift 3.0, but code examples for Swift 2.3 are found in the &lt;a href=&#34;https://github.com/andrewcbancroft/SwiftNSNotificationCenter/tree/swift-2.3&#34;&gt;example project&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;what-is-it&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;first-things-first-what-_is_-this-thing&#34;&gt;First things first: What &lt;em&gt;is&lt;/em&gt; this thing?&lt;/h3&gt;
&lt;p&gt;When I first heard &lt;code&gt;NSNotificationCenter&lt;/code&gt;, I thought &amp;ldquo;push notifications”. However, &lt;code&gt;NSNotificationCenter&lt;/code&gt; has nothing to do with sending or receiving push notifications in iOS. Rather, it is a communication tool internal to your app. It provides a way for one instance of a class/struct to notify one or more &lt;em&gt;other&lt;/em&gt; class/struct instances about something. The goal in doing this is to enable those 1+ other class or struct instances to take appropriate action based the communication they receive. An common analogy comes to mind: think &amp;ldquo;radio tower”. &lt;code&gt;NSNotificationCenter&lt;/code&gt; is the central hub that acts as a &lt;em&gt;broadcaster&lt;/em&gt; of notifications. If that still seems vague, hang tight – seeing the example below should help it all come together for you.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;should-i-use-it&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;should-i-use-it&#34;&gt;Should I use it?&lt;/h3&gt;
&lt;p&gt;Before diving into the basic workflow, it&amp;rsquo;s worth asking, &amp;ldquo;Should I use &lt;code&gt;NSNotificationCenter&lt;/code&gt;, or is there some other option that&amp;rsquo;s more appropriate for my app&amp;rsquo;s internal communication needs?”&lt;/p&gt;
&lt;p&gt;It is often the case that there is more than one way to solve a problem in software development, and it&amp;rsquo;s no different with &lt;code&gt;NSNotificationCenter&lt;/code&gt;. There is more than one way to accomplish instance-to-instance communication at run-time. I&amp;rsquo;ve written about two such forms of communication in my post titled &lt;a href=&#34;https://www.andrewcbancroft.com/2015/02/05/nsnotificationcenter-vs-delegation-analysis/&#34;&gt;NSNotificationCenter vs Delegation – An Analysis&lt;/a&gt;. It may be worth reading up on, just to make sure you&amp;rsquo;re using the right tool for the right kind of communication.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;basic-workflow&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;basic-workflow&#34;&gt;Basic Workflow&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;NSNotificationCenter.default&lt;/code&gt; is at the center of it all when it comes to this discussion.&lt;/p&gt;
&lt;p&gt;The basic workflow of using &lt;code&gt;NSNotificationCenter&lt;/code&gt; goes something like this:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Since `NSNotificationCenter` is in the business of broadcasting notifications, the first thing to do is to specify a way to uniquely identify a notification. This is most easily accomplished by defining a constant string value (using `let` syntax in Swift) at a globally-visible scope. The string constant you define here will serve as a &amp;ldquo;notification key”. If you have several notification keys that you need to define, perhaps creating a new .swift file called &amp;ldquo;NSNotificationCenterKeys.swift” is a clean way to do this.&lt;/li&gt;
&lt;li&gt;Steps 2 and 3 of the workflow go hand-in-hand. Without implementing the one, implementing the other doesn&amp;rsquo;t make much sense… Step 2 is to &lt;em&gt;post&lt;/em&gt; a notification to `NSNotificationCenter.default` identified by the key that was created in step 1. In radio terms, the class or struct instance desiring to post a notification asks the default notification center to broadcast the notification key defined in step 1… this class or struct instance chooses the right situations to request that the notification key be sent out based on business logic specific to the project&amp;rsquo;s requirements.&lt;/li&gt;
&lt;li&gt;Step 3 is to set up 1+ class or struct instances to be &lt;em&gt;listeners&lt;/em&gt;, or more properly, &lt;em&gt;observers&lt;/em&gt; of a particular notification. Such an observer will be able to tell that it&amp;rsquo;s &amp;ldquo;heard” the notification, because it will be &amp;ldquo;listening for” a notification that uses the same key that was created in step 1, which is the key used in step 2 to &lt;em&gt;post&lt;/em&gt; the notification. In radio terms, the listener is tuned in to the default notification center&amp;rsquo;s station that&amp;rsquo;s identified by that special key defined in step 1 and posted in step 2. So you see now why steps 2 and 3 go hand-in-hand. With no posts to the notification center on that station, tuning in will do no good. Likewise, posting a notification but having no listeners accomplishes nothing.&lt;/li&gt;
&lt;li&gt;Finally, what should the observing instance &lt;em&gt;do&lt;/em&gt; once it&amp;rsquo;s detected the notification? Well, when signing up to be an observer, the instance must also specify the name of a function that will be called upon receipt of the notification it&amp;rsquo;s listening for. Whatever action or routine is appropriate to perform at that time is what that function&amp;rsquo;s implementation should contain.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;a name=&#34;visualize&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;visualizing-nsnotificationcenter-by-example&#34;&gt;Visualizing NSNotificationCenter by Example&lt;/h3&gt;
&lt;p&gt;If you&amp;rsquo;re like me, you need less talk and more example, so I&amp;rsquo;ve created a simple Xcode project that you can grab over at GitHub (&lt;a title=&#34;GitHub - Swift NSNotificationCenter Example&#34; href=&#34;https://github.com/andrewcbancroft/SwiftNSNotificationCenter&#34; target=&#34;_blank&#34;&gt;Swift 2.3&lt;/a&gt; and &lt;a title=&#34;GitHub - Swift NSNotificationCenter Example&#34; href=&#34;https://github.com/andrewcbancroft/SwiftNSNotificationCenter/tree/swift-3.0&#34; target=&#34;_blank&#34;&gt;Swift 3.0&lt;/a&gt;). Running the project will allow you to click through a series of tabs in a tab view controller, post a notification, and see the results. It looks much like this:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2014/10/NSNotificationCenterExample.gif&#34;&gt;&lt;img class=&#34;size-full wp-image-5461 aligncenter&#34; src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2014/10/NSNotificationCenterExample.gif&#34; alt=&#34;NS Notification Center Example&#34; width=&#34;356&#34; height=&#34;636&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Working back through the basic workflow with this example you can find the following key aspects of implementing a &lt;code&gt;NSNotificationCenter&lt;/code&gt; solution (lines of code to pay special attention to will be highlighted).&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;steps1-2&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;steps-1-and-2&#34;&gt;Steps 1 and 2:&lt;/h4&gt;
&lt;p&gt;I&amp;rsquo;ve chosen my &lt;code&gt;FirstViewController&lt;/code&gt; class to be the one that defines the global constant with a unique notification key and tells the default notification center to post that notification when the &amp;ldquo;Notify!” button is tapped:&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;// 1. Globally define a &amp;#34;special notification key&amp;#34; constant that can be broadcast / tuned in to...&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;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;mySpecialNotificationKey&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;com.andrewcbancroft.specialNotificationKey&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; 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;kd&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;FirstViewController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIViewController&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; 5&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kr&#34;&gt;@IBOutlet&lt;/span&gt; &lt;span class=&#34;kr&#34;&gt;weak&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;sentNotificationLabel&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UILabel&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&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;kr&#34;&gt;override&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;viewDidLoad&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; 8&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;kc&#34;&gt;super&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;viewDidLoad&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;n&#34;&gt;NotificationCenter&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;k&#34;&gt;default&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;addObserver&lt;/span&gt;&lt;span class=&#34;p&#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;selector&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;#selector&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;FirstViewController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;updateNotificationSentLabel&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;NSNotification&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;rawValue&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;mySpecialNotificationKey&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;),&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;object&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;kc&#34;&gt;nil&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;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;c1&#34;&gt;// 2. Post notification using &amp;#34;special notification key&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;13&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kr&#34;&gt;@IBAction&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;notify&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;14&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;NotificationCenter&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;k&#34;&gt;default&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;post&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;Notification&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;rawValue&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;mySpecialNotificationKey&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;),&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;object&lt;/span&gt;&lt;span class=&#34;p&#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&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;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&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;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;updateNotificationSentLabel&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;18&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;sentNotificationLabel&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;text&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;Notification sent!&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;19&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;20&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;&lt;a name=&#34;steps3-4&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;steps-3-and-4&#34;&gt;Steps 3 and 4:&lt;/h4&gt;
&lt;p&gt;Both of these steps are implemented in very similar ways in my &lt;code&gt;SecondViewController&lt;/code&gt; and &lt;code&gt;ThirdViewController&lt;/code&gt; classes. The code should speak for itself:&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;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;SecondViewController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIViewController&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;kr&#34;&gt;@IBOutlet&lt;/span&gt; &lt;span class=&#34;kr&#34;&gt;weak&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;notificationLabel&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UILabel&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;kr&#34;&gt;override&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;viewDidLoad&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; 5&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;kc&#34;&gt;super&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;viewDidLoad&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&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;c1&#34;&gt;// 3.  Observe (listen for) &amp;#34;special notification key&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; 8&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;NotificationCenter&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;k&#34;&gt;default&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;addObserver&lt;/span&gt;&lt;span class=&#34;p&#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;selector&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;#selector&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;SecondViewController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;actOnSpecialNotification&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;NSNotification&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;rawValue&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;mySpecialNotificationKey&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;),&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;object&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;kc&#34;&gt;nil&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;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;10&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;11&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// 4.  Implement function to act on that notification&lt;/span&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;c1&#34;&gt;// The name of this function must match the selector argument you specified when you called addObserver()&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;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;actOnSpecialNotification&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;14&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;notificationLabel&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;text&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;I heard the notification!&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;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;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;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;17&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;18&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;19&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;ThirdViewController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIViewController&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;kr&#34;&gt;@IBOutlet&lt;/span&gt; &lt;span class=&#34;kr&#34;&gt;weak&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;notificationLabel&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UILabel&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;21&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;22&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kr&#34;&gt;override&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;viewDidLoad&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;23&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;kc&#34;&gt;super&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;viewDidLoad&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;24&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;NotificationCenter&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;k&#34;&gt;default&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;addObserver&lt;/span&gt;&lt;span class=&#34;p&#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;selector&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;#selector&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;ThirdViewController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;actOnSpecialNotification&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;NSNotification&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;rawValue&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;mySpecialNotificationKey&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;),&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;object&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;kc&#34;&gt;nil&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;25&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;26&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;27&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;actOnSpecialNotification&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;28&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;notificationLabel&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;text&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;I heard the notification, too!&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;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;Finally, to reiterate the explanation in visual form and for easy reference, I&amp;rsquo;ve annotated a screen shot taken from the project&amp;rsquo;s code:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2014/10/NSNotificationCenterExplanation_Swift3.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2014/10/NSNotificationCenterExplanation_Swift3-1024x825.png&#34; alt=&#34;NSNotificationCenter Explanation&#34; width=&#34;1024&#34; height=&#34;825&#34; class=&#34;alignnone size-large wp-image-13039&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2014/10/NSNotificationCenterExplanation_Swift3-1024x825.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2014/10/NSNotificationCenterExplanation_Swift3-300x242.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2014/10/NSNotificationCenterExplanation_Swift3.png 1100w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;remove-observer&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;removing-an-observer&#34;&gt;Removing an observer&lt;/h3&gt;
&lt;p&gt;One final requirement for working with &lt;code&gt;NSNotificationCenter&lt;/code&gt; is to remove an observer when it no longer needs to listen for notifications. When might this situation arise?&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;When the observer is no longer referenced and thus deallocated from memory, it should tell the default notification center that it can be removed&lt;/li&gt;
&lt;li&gt;When some condition occurs in your application that renders it no longer relevant for an instance to listen for notifications&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The first situation is the most common, and is for sure the scenario that we want to protect against, so that the default notification center doesn&amp;rsquo;t continue to manage observers that are no longer capable of actively listening. To satisfy the requirement of removing an observer when an instance is deallocated, we could provide something like the following &lt;code&gt;deinit&lt;/code&gt; method:&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;deinit&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;n&#34;&gt;NSNotificationCenter&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;k&#34;&gt;default&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;removeObserver&lt;/span&gt;&lt;span class=&#34;p&#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&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;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;For the second bullet point, you can simply write the same line of code that&amp;rsquo;s highlighted in the above snippet, wherever it makes sense for you to tell the default notification center when it&amp;rsquo;s appropriate to stop sending messages to the instance in question.&lt;/p&gt;
&lt;h3 id=&#34;summary&#34;&gt;Summary&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;NSNotificationCenter&lt;/code&gt; is particularly useful when there are multiple class or struct instances that need to take action based on something that happens elsewhere in your application. For this type of scenario, &lt;code&gt;NSNotificationCenter&lt;/code&gt; can be a great tool to wield as you develop apps in Swift for iOS.&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;https://www.andrewcbancroft.com/2015/02/05/nsnotificationcenter-vs-delegation-analysis/&#34; title=&#34;NSNotificationCenter vs Delegation – An Analysis&#34;&gt;NSNotificationCenter vs Delegation – An Analysis&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>Swift Alternative to Objective-C Macros</title>
      <link>https://www.andrewcbancroft.com/2014/10/01/swift-alternative-to-objective-c-macros/</link>
      <pubDate>Wed, 01 Oct 2014 17:58:46 +0000</pubDate>
      
      <guid>https://www.andrewcbancroft.com/2014/10/01/swift-alternative-to-objective-c-macros/</guid>
      <description>&lt;p&gt;I&amp;rsquo;ve previously written about &lt;a title=&#34;Replace Magic Strings with Enumerations in Swift&#34; href=&#34;http://www.andrewcbancroft.com/2014/09/02/replace-magic-strings-with-enumerations-in-swift/&#34; target=&#34;_blank&#34;&gt;using enumerations in Swift to encapsulate special values&lt;/a&gt; that could end up falling into the &amp;ldquo;magic string” category if they were to simply be scattered in-line throughout your code. The primary example I proposed for such a &amp;ldquo;magic string” replacement was Storyboard Segue Identifiers.  These special identifiers have such a specific purpose that felt to me like a natural fit to create a Type in the form of an enumeration (which I called SegueIdentifier) to group them all together in one place so that I could easily find them and modify them, should I ever need to do so.&lt;/p&gt;
&lt;p&gt;I still like that solution for &lt;em&gt;groups&lt;/em&gt; of things, but it&amp;rsquo;s a lot of &amp;ldquo;ceremony” to use enumerations for encapsulating &lt;em&gt;everything&lt;/em&gt; that may have been implemented as a macro expression or a static global constant in Objective-C.&lt;/p&gt;
&lt;p&gt;I ran across this in the &lt;a title=&#34;Apple Developer Documentation - Using Swift with Cocoa and Objective-C&#34; href=&#34;https://developer.apple.com/library/ios/documentation/swift/conceptual/buildingcocoaapps/InteractingWithCAPIs.html#//apple_ref/doc/uid/TP40014216-CH8-XID_19&#34; target=&#34;_blank&#34;&gt;Swift developer documentation&lt;/a&gt; that I think will be of help to folks who want to avoid &amp;ldquo;magic values” throughout their code, but don&amp;rsquo;t want to employ enumerations where they&amp;rsquo;re not the best fit.  Here&amp;rsquo;s a snippet:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2014/09/Macro-Alternatives-in-Swift.png&#34;&gt;&lt;img class=&#34;alignnone size-full wp-image-5231&#34; src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2014/09/Macro-Alternatives-in-Swift.png&#34; alt=&#34;Macro Alternatives in Swift&#34; width=&#34;705&#34; height=&#34;149&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2014/09/Macro-Alternatives-in-Swift.png 705w, https://www.andrewcbancroft.com/wp-content/uploads/2014/09/Macro-Alternatives-in-Swift-300x63.png 300w&#34; sizes=&#34;(max-width: 705px) 100vw, 705px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;So there you have it, folks!  One easy alternative to your former &lt;code&gt;#define&lt;/code&gt; habits when you&amp;rsquo;re working in Swift is to simply declare a &lt;em&gt;constant&lt;/em&gt; (using the &lt;code&gt;let&lt;/code&gt; keyword) instead.  The word &amp;ldquo;constant” is &lt;em&gt;key&lt;/em&gt; – the last thing you want to do is declare a &lt;em&gt;variable&lt;/em&gt; in some global scope (using the &lt;code&gt;var&lt;/code&gt; keyword) where the value of the identifier could be changed somehow, if even by accident.&lt;/p&gt;
&lt;h3 id=&#34;organizing-define-replacement-constants&#34;&gt;Organizing #define Replacement Constants&lt;/h3&gt;
&lt;p&gt;The question that naturally comes next is, &amp;ldquo;Where do I declare a constant that I&amp;rsquo;m using to replace a #define?”&lt;/p&gt;
&lt;p&gt;The answer is not black and white – a few factors play into your decision of where to declare them.&lt;/p&gt;
&lt;p&gt;In general, I would probably declare such a constant in the location that&amp;rsquo;s closest to the context in which it is used.  Here are a few examples to consider:&lt;/p&gt;
&lt;li style=&#34;text-align: left;&#34;&gt;
  If it&#39;s only going to be used in a single function, it&#39;s reasonable to declare the constant locally at the top of that function, or anywhere near where it will be used.
&lt;/li&gt;
&lt;li style=&#34;text-align: left;&#34;&gt;
  If it&#39;s only used in a single class/struct, perhaps declaring it at the top of that class/struct is a good idea.
&lt;/li&gt;
&lt;li style=&#34;text-align: left;&#34;&gt;
  If it&#39;s going to be a value that&#39;s used in &lt;em&gt;multiple&lt;/em&gt; classes/structs, it may be time to create a new .swift file and place it there so that you can find it again.
&lt;/li&gt;
&lt;li style=&#34;text-align: left;&#34;&gt;
  Start with a very small scope, and as that constant broadens in its usage throughout your project, gradually move it to more and more globally visible locations.
&lt;/li&gt;
&lt;p&gt;Using a globally-defined constant is exactly what I did for &lt;a title=&#34;Swift iOS Version Checking&#34; href=&#34;http://www.andrewcbancroft.com/2014/09/17/swift-ios-version-check/&#34; target=&#34;_blank&#34;&gt;checking the iOS version number of a user&amp;rsquo;s device&lt;/a&gt;.  I simply created a new file called &amp;ldquo;&lt;span style=&#34;color: #404040;&#34;&gt;iOSVersions.swift”, placed my global constant definitions in it, and was able to reference those constant names everywhere in my project that I needed to perform conditional logic based on the iOS version number.  &lt;/span&gt;&lt;/p&gt;
&lt;h3 id=&#34;summary&#34;&gt;Summary&lt;/h3&gt;
&lt;p&gt;A simple Swift alternative to a &lt;span class=&#34;lang:objc decode:true  crayon-inline &#34;&gt;#define&lt;/span&gt; macro in Objective-C it to define a constant at a scope that&amp;rsquo;s appropriate for where you plan to use that constant.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Slide In Animation in Swift</title>
      <link>https://www.andrewcbancroft.com/2014/09/24/slide-in-animation-in-swift/</link>
      <pubDate>Wed, 24 Sep 2014 18:25:52 +0000</pubDate>
      
      <guid>https://www.andrewcbancroft.com/2014/09/24/slide-in-animation-in-swift/</guid>
      <description>&lt;p&gt;In a previous post about &lt;a title=&#34;Fade In / Out Animations as Class Extensions in Swift&#34; href=&#34;http://www.andrewcbancroft.com/2014/07/27/fade-in-out-animations-as-class-extensions-with-swift/&#34; target=&#34;_blank&#34;&gt;fade animations&lt;/a&gt; in Swift, I demonstrated how to use a class extension to add the ability for any UIView instance to easily call &lt;code&gt;fadeIn()&lt;/code&gt; or &lt;code&gt;fadeOut()&lt;/code&gt; on itself. This strategy was nice – the animations, while simple, would have cluttered my code each time I used them, had I not encapsulated them &lt;em&gt;somewhere&lt;/em&gt;. Employing class extensions in Swift seemed a natural way to provide this functionality to UIViews.&lt;/p&gt;
&lt;p&gt;Well, I liked the idea so much that when it came time for me to implement a slide animation, I kept the same strategy, and I&amp;rsquo;d like to share my implementation with you today.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;animation-demo&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;animation-demo&#34;&gt;Animation demo&lt;/h3&gt;
&lt;p&gt;To start off with, here&amp;rsquo;s a sample of what the animation does:&lt;/p&gt;
&lt;p style=&#34;text-align: center;&#34;&gt;
  &lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2014/09/SlideAnimationSample2.gif&#34;&gt;&lt;img class=&#34;alignnone size-full wp-image-5171&#34; src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2014/09/SlideAnimationSample2.gif&#34; alt=&#34;Slide Animation Example&#34; width=&#34;285&#34; height=&#34;509&#34; /&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;The easiest way to get the gist of what I&amp;rsquo;ve done is to &lt;a title=&#34;GitHub - SwiftSlideAnimation&#34; href=&#34;https://github.com/andrewcbancroft/SwiftSlideAnimation&#34; target=&#34;_blank&#34;&gt;head over to GitHub and download the example Xcode Project&lt;/a&gt;. In real life, I&amp;rsquo;ve combined the fade animations and the slide animation into the same UIView extension, but for education&amp;rsquo;s sake, I&amp;rsquo;ve split them out so you can easily see the moving parts.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;overview&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;implementation-overview&#34;&gt;Implementation overview&lt;/h3&gt;
&lt;p&gt;There are three major parts to this example implementation, which I&amp;rsquo;ll explain in detail shortly. Here&amp;rsquo;s the overview:&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;overview-create-extension&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;1--create-the-uiview-extension&#34;&gt;1 – Create the UIView extension&lt;/h4&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;import&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;UIKit&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&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;extension&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;UIView&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; 4&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Name this function in a way that makes sense to you... &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;c1&#34;&gt;// slideFromLeft, slideRight, slideLeftToRight, etc. are great alternative names&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;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;slideInFromLeft&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;duration&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSTimeInterval&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;mf&#34;&gt;1.0&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;completionDelegate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;AnyObject&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 class=&#34;kc&#34;&gt;nil&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;c1&#34;&gt;// Create a CATransition animation&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;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;slideInFromLeftTransition&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;CATransition&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&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;c1&#34;&gt;// Set its callback delegate to the completionDelegate that was provided (if any)&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 class=&#34;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;delegate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;AnyObject&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;completionDelegate&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;12&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;            &lt;span class=&#34;n&#34;&gt;slideInFromLeftTransition&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;delegate&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;delegate&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;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&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;c1&#34;&gt;// Customize the animation&amp;#39;s properties&lt;/span&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;n&#34;&gt;slideInFromLeftTransition&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;type&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;kCATransitionPush&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;n&#34;&gt;slideInFromLeftTransition&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;subtype&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;kCATransitionFromLeft&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;n&#34;&gt;slideInFromLeftTransition&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;duration&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;duration&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;n&#34;&gt;slideInFromLeftTransition&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;timingFunction&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;CAMediaTimingFunction&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;kCAMediaTimingFunctionEaseInEaseOut&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;n&#34;&gt;slideInFromLeftTransition&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;fillMode&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;kCAFillModeRemoved&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&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;c1&#34;&gt;// Add the animation to the View&amp;#39;s layer&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;kc&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;layer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;addAnimation&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;slideInFromLeftTransition&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;forKey&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;slideInFromLeftTransition&amp;#34;&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;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 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;&lt;a name=&#34;overview-setup-storyboard&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;2--set-up-the-storyboard&#34;&gt;2 – Set up the storyboard&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;From the Utilities panel, drag over a regular View to act as a wrapper for the sliding text (use a &lt;em&gt;regular&lt;/em&gt; View, not a Container View)&lt;/li&gt;
&lt;li&gt;From the Utilities panel, drag a Label into the wrapper view and create an IBOutlet to your View Controller for the Label&lt;/li&gt;
&lt;li&gt;Set up auto layout constraints for both the wrapper and the Label&lt;/li&gt;
&lt;li&gt;Set the wrapper view&amp;rsquo;s Clip Subviews property to &lt;em&gt;checked&lt;/em&gt; in the Attributes Inspector&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;a name=&#34;overview-code-vc&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;3--code-the-view-controller--initiate-slide-animation&#34;&gt;3 – Code the View Controller – initiate slide animation.&lt;/h4&gt;
&lt;p&gt;In my example, I wired the trigger up to a button&amp;rsquo;s &lt;code&gt;touchUpInside&lt;/code&gt; action. For you, it may be something different that triggers the animation to begin. Whatever it may be, call &lt;code&gt;slideInFromLeft()&lt;/code&gt; on your UIView instance (in my case, the UILabel).&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;kr&#34;&gt;@IBAction&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;slideTextButtonTapped&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;sender&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIButton&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;2&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;slidingTextLabel&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;slideInFromLeft&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;kc&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;slidingTextLabel&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;text&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;Sliding Text!&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;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;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Notice that directly after the call to &lt;code&gt;slideInFromLeft()&lt;/code&gt;, I change the label&amp;rsquo;s text property to contain the new text that I want to slide in.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;details&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;implementation-details&#34;&gt;Implementation details&lt;/h3&gt;
&lt;p&gt;The real work all happens inside the UIViewExtensions.swift file where I add the &lt;code&gt;slideInFromLeft()&lt;/code&gt; &lt;span class=&#34;crayon-sy&#34;&gt;function to a UIView&amp;rsquo;s arsenal.`&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;details-uiviewextensions-swift&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;uiviewextensionsswift&#34;&gt;UIViewExtensions.swift&lt;/h4&gt;
&lt;p&gt;First to note is that I&amp;rsquo;ve provided a few default values in the function&amp;rsquo;s signature so that the animation can be initiated as simply as writing &lt;code&gt;viewInstance.slideInFromLeft()&lt;/code&gt;, or as &amp;ldquo;complicated” as providing argument values to both duration and completionDelegate:&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;UIView&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;slideInFromLeft&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;duration&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSTimeInterval&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;mf&#34;&gt;1.0&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;completionDelegate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;AnyObject&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 class=&#34;kc&#34;&gt;nil&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;3&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Implementation...&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Next, I create a &lt;code&gt;CATransition&lt;/code&gt; instance, and set its delegate property if a &lt;code&gt;completionDelegate&lt;/code&gt; is passed when the function is called:&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;UIView&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;slideInFromLeft&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;duration&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSTimeInterval&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;mf&#34;&gt;1.0&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;completionDelegate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;AnyObject&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 class=&#34;kc&#34;&gt;nil&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;3&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;slideInFromLeftTransition&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;CATransition&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;4&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;5&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;delegate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;AnyObject&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;completionDelegate&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;n&#34;&gt;slideInFromLeftTransition&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;delegate&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;delegate&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;c1&#34;&gt;// Remaining implementation...&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;I then go about configuring the animation&amp;rsquo;s properties. To achieve the &amp;ldquo;slide in from left” animation, I set the &lt;code&gt;type&lt;/code&gt; and the &lt;code&gt;subtype&lt;/code&gt; properties to &lt;code&gt;kCATransitionPush&lt;/code&gt; and &lt;code&gt;kCATransitionFromLeft&lt;/code&gt;, respectively. These two combined create the &amp;ldquo;slide in” effect. Other properties that I set are &lt;code&gt;duration&lt;/code&gt;, &lt;code&gt;timingFunction&lt;/code&gt;, and &lt;code&gt;fillMode&lt;/code&gt;:&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;UIView&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 class=&#34;n&#34;&gt;slideInFromLeftTransition&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;type&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;kCATransitionPush&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;n&#34;&gt;slideInFromLeftTransition&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;subtype&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;kCATransitionFromLeft&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;n&#34;&gt;slideInFromLeftTransition&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;duration&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;duration&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;n&#34;&gt;slideInFromLeftTransition&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;timingFunction&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;CAMediaTimingFunction&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;kCAMediaTimingFunctionEaseInEaseOut&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;n&#34;&gt;slideInFromLeftTransition&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;fillMode&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;kCAFillModeRemoved&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&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;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;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 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;To keep things simple, I only allow myself to customize the &lt;code&gt;duration&lt;/code&gt; property, and optionally provide a &lt;code&gt;completionDelegate&lt;/code&gt; when I call the function… the other properties are more fundamental to how the animation should perform, so I encapsulate the implementation here so that it&amp;rsquo;s an abstraction when I actually call &lt;code&gt;slideInFromLeft()&lt;/code&gt; later in my View Controller.&lt;/p&gt;
&lt;p&gt;The last thing I do is add the animation that I just created and configured to the view&amp;rsquo;s layer property. &amp;ldquo;The view” here would be the instance of UIView that has &lt;em&gt;calls&lt;/em&gt; &lt;code&gt;slideInFromLeft()&lt;/code&gt;:&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;UIView&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 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;layer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;addAnimation&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;slideInFromLeftTransition&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;forKey&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;slideInFromLeftTransition&amp;#34;&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;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 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;&lt;a name=&#34;details-storyboard-setup&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;storyboard-setup-details&#34;&gt;Storyboard setup details&lt;/h4&gt;
&lt;p&gt;This is probably the more challenging piece, just because it takes some tweaking to get the auto layout constraints just right.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;My general setup in this example is a Label wrapped inside a containing UIView. I made sure to simply drag over a regular View (not a Container View) from the utilities pane on the right. I then dragged a Label inside the &amp;ldquo;wrapper” view.&lt;/li&gt;
&lt;li&gt;I set wrapper view&amp;rsquo;s width to a value that was less than the entire screen&amp;rsquo;s width. This was so that the sliding view didn&amp;rsquo;t appear to slide in from off-screen and slide out off-screen. Instead it appears to slide in from underneath the wrapper view… You&amp;rsquo;re welcome to customize this how you want (or avoid the containing view altogether if you find it&amp;rsquo;s not needed for your specific implementation).&lt;/li&gt;
&lt;li&gt;For the wrapper view, I&amp;rsquo;ve set constraints for
&lt;ul&gt;
&lt;li&gt;Center Horizontally in Container&lt;/li&gt;
&lt;li&gt;Top Space to Top Layout Guide&lt;/li&gt;
&lt;li&gt;Width&lt;/li&gt;
&lt;li&gt;Height&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2014/09/Wrapper-View-Constraints.png&#34;&gt;&lt;img class=&#34;alignnone size-large wp-image-5091&#34; src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2014/09/Wrapper-View-Constraints-1024x644.png&#34; alt=&#34;Wrapper View Constraints&#34; width=&#34;730&#34; height=&#34;459&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2014/09/Wrapper-View-Constraints-1024x644.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2014/09/Wrapper-View-Constraints-300x188.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2014/09/Wrapper-View-Constraints-1200x754.png 1200w, https://www.andrewcbancroft.com/wp-content/uploads/2014/09/Wrapper-View-Constraints.png 1469w&#34; sizes=&#34;(max-width: 730px) 100vw, 730px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Next, I made sure that my label that will be sliding is &lt;em&gt;inside&lt;/em&gt; the wrapper view. I set its constraints to&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Center Horizontally in Container&lt;/li&gt;
&lt;li&gt;Center Vertically in Container&lt;/li&gt;
&lt;li&gt;Equal Widths&lt;/li&gt;
&lt;li&gt;Equal Heights&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2014/09/Label-Constraints.png&#34;&gt;&lt;img class=&#34;alignnone size-large wp-image-5081&#34; src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2014/09/Label-Constraints-1024x642.png&#34; alt=&#34;Label Constraints&#34; width=&#34;730&#34; height=&#34;457&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2014/09/Label-Constraints-1024x642.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2014/09/Label-Constraints-300x188.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2014/09/Label-Constraints-1200x752.png 1200w, https://www.andrewcbancroft.com/wp-content/uploads/2014/09/Label-Constraints.png 1471w&#34; sizes=&#34;(max-width: 730px) 100vw, 730px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The last thing I do in the storyboard before wiring things in the View Controller is to select the wrapper view and make sure it&amp;rsquo;s Clip Subviews property is &lt;em&gt;checked&lt;/em&gt;. As an experiment, toggle this option and watch the effect it has on the animation&amp;rsquo;s appearance:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2014/09/Clib-Subviews-Checked.png&#34;&gt;&lt;img class=&#34;alignnone size-large wp-image-5071&#34; src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2014/09/Clib-Subviews-Checked-1024x646.png&#34; alt=&#34;Clib Subviews Checked&#34; width=&#34;730&#34; height=&#34;460&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2014/09/Clib-Subviews-Checked-1024x646.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2014/09/Clib-Subviews-Checked-300x189.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2014/09/Clib-Subviews-Checked-1200x757.png 1200w, https://www.andrewcbancroft.com/wp-content/uploads/2014/09/Clib-Subviews-Checked.png 1475w&#34; sizes=&#34;(max-width: 730px) 100vw, 730px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;details-vc-implementation&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;view-controller-implementation&#34;&gt;View controller implementation&lt;/h4&gt;
&lt;p&gt;The final piece of the setup is to wire things up to the View Controller and animate the Label.&lt;/p&gt;
&lt;p&gt;Since I needed to reference the Label containing the text that I&amp;rsquo;d like to animate, I created an &lt;code&gt;IBOutlet&lt;/code&gt; from my storyboard to my View Controller:&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;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;ViewController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIViewController&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;kr&#34;&gt;@IBOutlet&lt;/span&gt; &lt;span class=&#34;kr&#34;&gt;weak&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;slidingTextLabel&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UILabel&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;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;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;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Next, I needed a way to initiate the slide in animation – I decided that wiring it to a button&amp;rsquo;s &lt;code&gt;touchUpInside&lt;/code&gt; action would be sufficient for the example, so I created an &lt;code&gt;IBAction&lt;/code&gt; from my Storyboard to my View Controller for that purpose.&lt;/p&gt;
&lt;p&gt;Within the body of that &lt;code&gt;IBAction&lt;/code&gt;, I wrote the call to &lt;code&gt;slideInFromLeft()&lt;/code&gt; on my &lt;code&gt;slidingTextLabel&lt;/code&gt; instance:&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;import&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;UIKit&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&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;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;ViewController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIViewController&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; 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;kr&#34;&gt;@IBAction&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;slideTextButtonTapped&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;sender&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIButton&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; 6&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;slidingTextLabel&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;slideInFromLeft&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;c1&#34;&gt;//      self.slidingTextLabel.slideInFromLeft(duration: 1.0, completionDelegate: self) // Use this line to specify a duration or completionDelegate&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;slidingTextLabel&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;text&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;Sliding Text!&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; 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;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&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 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;12&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;If you have need to specify a &lt;code&gt;duration&lt;/code&gt; or a &lt;code&gt;completionDelegate&lt;/code&gt;, there&amp;rsquo;s a commented out line of code there that shows an example of passing those arguments to the &lt;code&gt;slideInFromLeft()&lt;/code&gt; function.&lt;/p&gt;
&lt;p&gt;If you specify a &lt;code&gt;completionDelegate&lt;/code&gt; to the &lt;code&gt;slideInFromLeft()&lt;/code&gt; function, a method called &lt;code&gt;animationDidStop()&lt;/code&gt; will be called when the animation finishes. Inside this callback function, you can write code to perform any action you&amp;rsquo;d like to have happen after the animation has finished. If you don&amp;rsquo;t set a completionDelegate, there&amp;rsquo;s no need to have this method override in your code:&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;kr&#34;&gt;override&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;animationDidStop&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;anim&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;CAAnimation&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;!,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;finished&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;flag&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;Bool&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;2&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;bp&#34;&gt;println&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;Animation stopped&amp;#34;&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;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;That&amp;rsquo;s a wrap, folks! Hope this strategy is helpful for you as you think about where to place code for your UIView animations.&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;https://www.andrewcbancroft.com/2016/02/22/fade-views-inout-with-fadeable-a-swift-protocol-extension/&#34; title=&#34;Fade Views In/Out with Fadeable – A Swift Protocol Extension&#34;&gt;Fade Views In/Out with Fadeable – A Swift Protocol Extension&lt;/a&gt;
    &lt;/li&gt;
    &lt;li&gt;
      &lt;i class=&#34;fa fa-angle-right&#34;&gt;&lt;/i&gt; &lt;a title=&#34;The 5 W’s of Swift Extensions&#34; href=&#34;http://www.andrewcbancroft.com/2014/11/03/the-5-ws-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 title=&#34;Fade In / Out Animations as Class Extensions in Swift&#34; href=&#34;http://www.andrewcbancroft.com/2014/07/27/fade-in-out-animations-as-class-extensions-with-swift/&#34; target=&#34;_blank&#34;&gt;Fade In / Out Animations as Class Extensions in Swift&lt;/a&gt;
    &lt;/li&gt;
    &lt;li&gt;
      &lt;i class=&#34;fa fa-angle-right&#34;&gt;&lt;/i&gt; &lt;a title=&#34;Rotate Animation in Swift&#34; href=&#34;http://www.andrewcbancroft.com/2014/10/15/rotate-animation-in-swift/&#34; target=&#34;_blank&#34;&gt;Rotate Animation in Swift&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>Swift iOS Version Checking</title>
      <link>https://www.andrewcbancroft.com/2014/09/17/swift-ios-version-check/</link>
      <pubDate>Thu, 18 Sep 2014 04:44:36 +0000</pubDate>
      
      <guid>https://www.andrewcbancroft.com/2014/09/17/swift-ios-version-check/</guid>
      <description>&lt;p&gt;While iOS 8 is now officially in the wild, it may take a bit of time to reach the level of user adoption that iOS 7 had accumulated over the past year.  If you plan to target the widest number of users for your app (at least in these early days of the iOS 8 roll-out), it&amp;rsquo;d probably be wise to include iOS 7 in your target audience.&lt;/p&gt;
&lt;p&gt;When accommodating iOS 7 users, you&amp;rsquo;ll inevitably run into instances where you need to check which iOS version the device is running, so that you can implement a fall-back plan for older versions of iOS.&lt;/p&gt;
&lt;p&gt;In Objective-C, I&amp;rsquo;ve seen this accomplished by using pre-processor directives, or with introspection.  Swift, however, &lt;a title=&#34;Apple Developer Documentation - No Swift Preprocessor Directives&#34; href=&#34;https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/InteractingWithCAPIs.html#//apple_ref/doc/uid/TP40014216-CH8-XID_20&#34; target=&#34;_blank&#34;&gt;has no pre-processor directives in v1.0&lt;/a&gt;, and &lt;a title=&#34;Apple Developer Documentation - NSObject Protocol&#34; href=&#34;https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Protocols/NSObject_Protocol/index.html&#34; target=&#34;_blank&#34;&gt;only objects which conform to the NSObject protocol&lt;/a&gt; can utilize the &lt;code&gt;respondsToSelector:&lt;/code&gt; method call.  What to do?&lt;/p&gt;
&lt;p&gt;As it turns out, &lt;a title=&#34;Apple Developer Documentation - Conditionally Load Resources&#34; href=&#34;https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/TransitionGuide/SupportingEarlieriOS.html#//apple_ref/doc/uid/TP40013174-CH14-SW1&#34; target=&#34;_blank&#34;&gt;Apple has guidance&lt;/a&gt; that would actually work for both Objective-C &lt;em&gt;and&lt;/em&gt; Swift – It involves simply checking the &lt;code&gt;NSFoundationVersionNumber&lt;/code&gt; of the device against one of the pre-defined values defined in NSObjCRuntime.h.&lt;/p&gt;
&lt;p&gt;To accomplish this in Swift, you can create a new Swift file (I called mine &amp;ldquo;iOSVersions.swift”) to hold the following code:&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;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;iOS7&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;floor&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;NSFoundationVersionNumber&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;floor&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;NSFoundationVersionNumber_iOS_7_1&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;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;iOS8&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;floor&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;NSFoundationVersionNumber&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;floor&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;NSFoundationVersionNumber_iOS_7_1&lt;/span&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;Whenever you need to check which iOS version the device is running, you can simply use the iOS8 / iOS7 constants that you just defined – they&amp;rsquo;re accessible in other Swift files throughout your project:&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;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;iOS8&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;//Do some iOS 8-specific things that may not be supported in older versions&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;p&#34;&gt;}&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;else&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;4&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;  &lt;span class=&#34;c1&#34;&gt;//Implement your fall-back plan for older versions of iOS&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;With these little snippets, you should be empowered to support iOS 7 while taking advantage of new iOS 8-only features and APIs.  You can also begin to move away from deprecated ways of doing things, while not breaking your app for iOS 7 users, so long as iOS 7 retains a significant slice of the iOS version pie.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Create Documentation for your Swift Playground</title>
      <link>https://www.andrewcbancroft.com/2014/09/09/create-documentation-for-your-swift-playground/</link>
      <pubDate>Wed, 10 Sep 2014 03:44:16 +0000</pubDate>
      
      <guid>https://www.andrewcbancroft.com/2014/09/09/create-documentation-for-your-swift-playground/</guid>
      <description>&lt;p&gt;As I opened Apple&amp;rsquo;s latest &lt;a title=&#34;Apple Swift Developer Blog - Patterns Playground&#34; href=&#34;https://developer.apple.com/swift/blog/?id=13&#34; target=&#34;_blank&#34;&gt;&amp;ldquo;Patterns Playground” blog post&lt;/a&gt;, I was impressed with the look and feel.  Rather than create the documentation as comment blocks, they managed to put in sharp-looking notes and explanations before each code example / experiment.  It&amp;rsquo;s like an interactive book, reminiscent of the Swift Programming Language iBook!  Here&amp;rsquo;s a snippet of what their Playground looks like:&lt;/p&gt;
&lt;p&gt;[&lt;img class=&#34;alignnone size-large wp-image-4771&#34; src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2014/09/Patterns_playground-1024x511.png&#34; alt=&#34;Apple&#39;s Patterns Playground&#34; width=&#34;730&#34; height=&#34;364&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2014/09/Patterns_playground-1024x511.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2014/09/Patterns_playground-300x149.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2014/09/Patterns_playground.png 1080w&#34; sizes=&#34;(max-width: 730px) 100vw, 730px&#34; /&gt;][1]&lt;/p&gt;
&lt;p&gt;Of course, my immediate question / thought was, &amp;ldquo;How&amp;rsquo;d they &lt;em&gt;do&lt;/em&gt; that?!  Can &lt;em&gt;I&lt;/em&gt; do that?  I want to do that!”.  I didn&amp;rsquo;t know it at the time of this publication, but apparently Apple has some &lt;a title=&#34;Apple Developer Documentation - Interactive Learning Playgrounds&#34; href=&#34;https://developer.apple.com/library/prerelease/ios/documentation/Swift/Reference/Playground_Ref/Chapters/InteractiveLearning.html&#34; target=&#34;_blank&#34;&gt;pre-release documentation out on developer.apple.com&lt;/a&gt;, detailing how all this is done.  I (as usual) did it the hard way and hacked my way through it.  I figured I&amp;rsquo;d go ahead and share my discoveries alongside the official documentation from Apple.&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;m already a believer in using Playgrounds for teaching.  The read-example-experiment loop is so easy in the Playground environment.  I think using this documentation technique has the potential to enhance the Playground experience &lt;em&gt;even more&lt;/em&gt; to create professional, sharp-looking educational material_.  _&lt;/p&gt;
&lt;p&gt;For this post, I wanted to take a simple example (the File -&amp;gt; New Playground playground) and convert the comment-style documentation into &amp;ldquo;fancy” documentation.&lt;/p&gt;
&lt;h3 id=&#34;before&#34;&gt;Before:&lt;/h3&gt;
&lt;p&gt;[&lt;img class=&#34;alignnone size-large wp-image-4751&#34; src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2014/09/MyPlayground_WithDocumentation_playground_before-1024x108.png&#34; alt=&#34;Before view of Playground&#34; width=&#34;730&#34; height=&#34;76&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2014/09/MyPlayground_WithDocumentation_playground_before-1024x108.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2014/09/MyPlayground_WithDocumentation_playground_before-300x31.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2014/09/MyPlayground_WithDocumentation_playground_before.png 1142w&#34; sizes=&#34;(max-width: 730px) 100vw, 730px&#34; /&gt;][2]&lt;/p&gt;
&lt;h3 id=&#34;after&#34;&gt;After:&lt;/h3&gt;
&lt;p&gt;[&lt;img class=&#34;alignnone size-large wp-image-4761&#34; src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2014/09/MyPlayground_WithDocumentation_playground_after-1024x239.png&#34; alt=&#34;After view of Playground&#34; width=&#34;730&#34; height=&#34;170&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2014/09/MyPlayground_WithDocumentation_playground_after-1024x239.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2014/09/MyPlayground_WithDocumentation_playground_after-300x70.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2014/09/MyPlayground_WithDocumentation_playground_after.png 1139w&#34; sizes=&#34;(max-width: 730px) 100vw, 730px&#34; /&gt;][3]&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s explore.&lt;/p&gt;
&lt;h2 id=&#34;the-gist&#34;&gt;The Gist&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;Create yourself a playground (and note where you save it).&lt;/li&gt;
&lt;li&gt;Navigate to where the .playground file is saved in Finder.&lt;/li&gt;
&lt;li&gt;Right-click the .playground file, and choose &amp;ldquo;Show Package Contents”.&lt;/li&gt;
&lt;li&gt;Add a new folder named &amp;ldquo;Documentation”.&lt;/li&gt;
&lt;li&gt;Create a new HTML file in the Documentation folder.  It should contain the HTML markup and the text explanation that you&amp;rsquo;d like to enhance your playground with.  Make sure it is a well-formed HTML document by the time you&amp;rsquo;re finished creating it. I describe a quick way to jumpstart your documentation process at the end of this article…&lt;/li&gt;
&lt;li&gt;Although optional, I&amp;rsquo;d recommend styling your HTML file with appropriate CSS.  Adding a CSS file and referencing it within the HTML file you created is probably a good idea (and this is allowed for Playground documentation).&lt;/li&gt;
&lt;li&gt;Open contents.xcplayground with the text editor of your choice by right-clicking and choosing &amp;ldquo;Open With”.&lt;/li&gt;
&lt;li&gt;Modify the XML so that the &lt;sections&gt; element contains a &lt;span class=&#34;lang:default decode:true  crayon-inline &#34;&gt;&lt;documentation&gt;&lt;/span&gt; node as shown in the highlighted code lines in the example below.  Be sure to replace the relative-path value with the name of the HTML file you created in step 5 (I named mine &amp;ldquo;doc-fragment-0.html”): &lt;pre class=&#34;lang:xhtml mark:2-3 decode:true &#34; title=&#34;contents.xcplayground Snippet&#34;&gt;&lt;sections&gt;
&lt;documentation relative-path=&#39;doc-fragment-0.html&#39;&gt;
&lt;/documentation&gt;
&lt;code source-file-name=&#39;section-2.swift&#39;/&gt;
&lt;/sections&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;
  9.  Save, and open the playground in Xcode 6.  Your Playground should now contain documentation!

As a way to jump-start my own Playground documentation, I simply borrowed one of the HTML files and the CSS file from Apple&amp;#39;s Patterns Playground.  Then I modified the markup to contain the educational material I wanted for _my_ playground, customized the styles to what I wanted, added the &amp;lt;span class=&amp;#34;lang:default decode:true  crayon-inline &amp;#34;&amp;gt;&amp;lt;documentation&amp;gt;&amp;lt;/span&amp;gt; node to the contents.xcplayground file, saved, and voila:  Fancy documentation!

&amp;amp;nbsp;

 [1]: http://www.andrewcbancroft.com/wp-content/uploads/2014/09/Patterns_playground.png
 [2]: http://www.andrewcbancroft.com/wp-content/uploads/2014/09/MyPlayground_WithDocumentation_playground_before.png
 [3]: http://www.andrewcbancroft.com/wp-content/uploads/2014/09/MyPlayground_WithDocumentation_playground_after.png
&lt;/code&gt;&lt;/pre&gt;</description>
    </item>
    
    <item>
      <title>Replace Magic Strings with Enumerations in Swift</title>
      <link>https://www.andrewcbancroft.com/2014/09/02/replace-magic-strings-with-enumerations-in-swift/</link>
      <pubDate>Wed, 03 Sep 2014 01:55:54 +0000</pubDate>
      
      <guid>https://www.andrewcbancroft.com/2014/09/02/replace-magic-strings-with-enumerations-in-swift/</guid>
      <description>&lt;p&gt;&amp;ldquo;What can I do to avoid these ‘magic strings&amp;rsquo; in my code?” – This was the question I asked myself recently as I found myself in &lt;code&gt;prepareForSegue&lt;/code&gt; comparing &lt;code&gt;segue.segueIdentifier&lt;/code&gt; to in-line hard-coded Strings.  This kind of in-line hard-coding of a String for comparison purposes is what I mean by &amp;ldquo;magic strings” in this article.  I knew this felt like a bad idea, but the solution to a cleaner option wasn&amp;rsquo;t readily apparent to me.&lt;/p&gt;
&lt;p&gt;I &lt;em&gt;used&lt;/em&gt; to do things like create static string constants, or &lt;code&gt;#define&lt;/code&gt; expressions so that I could easily change these values if I ever needed to update them for some reason.  You know… back in a former Objective-C developer life when these tactics were available to me.  But these options don&amp;rsquo;t exist in Swift.  What to do??&lt;/p&gt;
&lt;h2 id=&#34;enumerations-to-the-rescue&#34;&gt;Enumerations to the rescue!&lt;/h2&gt;
&lt;p&gt;Specifically, &lt;a title=&#34;Apple Developer Documentation - Enumerations with Raw Values&#34; href=&#34;https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Enumerations.html#//apple_ref/doc/uid/TP40014097-CH12-XID_228&#34; target=&#34;_blank&#34;&gt;Enumerations with pre-populated default values (called &lt;em&gt;raw values&lt;/em&gt;)&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;By creating an Enumeration that stores raw String values, I was able to encapsulate what would otherwise be &amp;ldquo;magic strings” in a type-safe construct for easier, cleaner use in my code.&lt;/p&gt;
&lt;h2 id=&#34;thegist&#34;&gt;The Gist&lt;/h2&gt;
&lt;p&gt;Consider this fabricated example:&lt;/p&gt;
&lt;p&gt;I have a storyboard with one main View Controller that connects to three other View Controllers through three segues:  &amp;ldquo;otherScreenSegue1”, &amp;ldquo;otherScreenSegue2”, and &amp;ldquo;otherScreenSegue3” as defined in the utilities panel in Xcode.&lt;/p&gt;
&lt;p&gt;An Enumeration encapsulating those segue identifiers might look 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;enum&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;SegueIdentifier&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;2&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;OtherScreenSegue1&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;otherScreenSegue1&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;3&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;OtherScreenSegue2&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;otherScreenSegue2&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;4&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;OtherScreenSegue3&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;otherScreenSegue3&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;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;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;With this Enumeration defined (perhaps in its own .swift file – wherever you deem would be strategic and findable again), the &lt;code&gt;prepareForSegue&lt;/code&gt; override can become &amp;ldquo;magic string”-free:&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;kr&#34;&gt;override&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;prepareForSegue&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;segue&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIStoryboardSegue&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;!,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;sender&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;AnyObject&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; 2&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;n&#34;&gt;segue&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;identifier&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;case&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;SegueIdentifier&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;OtherScreenSegue1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;toRaw&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; 4&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;bp&#34;&gt;println&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;Going to other screen 1&amp;#34;&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; 5&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;SegueIdentifier&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;OtherScreenSegue2&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;toRaw&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;bp&#34;&gt;println&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;Going to other screen 2&amp;#34;&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;case&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;SegueIdentifier&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;OtherScreenSegue3&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;toRaw&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;bp&#34;&gt;println&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;Going to other screen 3&amp;#34;&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;default&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;10&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;bp&#34;&gt;println&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;Going somewhere else&amp;#34;&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;11&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;12&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;Alternatively, if you prefer to compare the enum values themselves, you could do the following (thank you to &lt;a title=&#34;Twitter - Brandon Knope&#34; href=&#34;https://twitter.com/bknope&#34; target=&#34;_blank&#34;&gt;Brandon Knope&lt;/a&gt; for pointing this out – I think it looks even cleaner than my original code above!):&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;kr&#34;&gt;override&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;prepareForSegue&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;segue&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIStoryboardSegue&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;!,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;sender&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;AnyObject&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; 2&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;identifier&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;SegueIdentifier&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;fromRaw&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;segue&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;identifier&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; 3&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;n&#34;&gt;identifier&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; 4&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;OtherScreenSegue1&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; 5&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;            &lt;span class=&#34;bp&#34;&gt;println&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;Going to other screen 1&amp;#34;&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;case&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;OtherScreenSegue2&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;bp&#34;&gt;println&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;Going to other screen 2&amp;#34;&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;k&#34;&gt;case&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;OtherScreenSegue3&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;bp&#34;&gt;println&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;Going to other screen 3&amp;#34;&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;10&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;default&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;11&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;            &lt;span class=&#34;bp&#34;&gt;println&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;Going somewhere else&amp;#34;&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;12&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;13&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;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;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This strategy of encapsulating my various segue identifiers in an Enumeration provides me a one-stop-shop for reviewing, and if need-be, updating the String values to match what I&amp;rsquo;ve set up in my storyboard.&lt;/p&gt;
&lt;h2 id=&#34;the-details&#34;&gt;The Details&lt;/h2&gt;
&lt;p&gt;I&amp;rsquo;ve chosen an &lt;a title=&#34;Apple Developer Documentation - Enumerations with Raw Values&#34; href=&#34;https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Enumerations.html#//apple_ref/doc/uid/TP40014097-CH12-XID_228&#34; target=&#34;_blank&#34;&gt;Enumeration with Raw Values&lt;/a&gt;, because the other two kinds (&lt;a title=&#34;Apple Developer Documentation - Enumerations as Inherent Types&#34; href=&#34;https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Enumerations.html#//apple_ref/doc/uid/TP40014097-CH12-XID_224&#34; target=&#34;_blank&#34;&gt;Enumerations as Inherent Types&lt;/a&gt;, or &lt;a title=&#34;Apple Developer Documentation - Enumerations with Associated Values&#34; href=&#34;https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Enumerations.html#//apple_ref/doc/uid/TP40014097-CH12-XID_227&#34; target=&#34;_blank&#34;&gt;Enumerations with Associated Values&lt;/a&gt;) don&amp;rsquo;t allow me to do String comparison, or don&amp;rsquo;t allow me to define a value at declaration-time, respectively.&lt;/p&gt;
&lt;p&gt;Notice one critical aspect of the Enumeration:  because default raw values are defined, &lt;em&gt;all&lt;/em&gt; of the raw values must be of the same Type, as explicitly specified in the declaration line:  &lt;code&gt;enum SegueIdentifier: String // All of the enum cases must be Strings&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;The next important thing to understand is that in order to do actual comparisons with the raw value itself (see the &lt;code&gt;switch&lt;/code&gt; statement in my code example above), I needed to call &lt;code&gt;toRaw()&lt;/code&gt; on the Enumeration value being used (first code example), or call &lt;code&gt;fromRaw()&lt;/code&gt; to convert the &lt;code&gt;segue.identifier&lt;/code&gt; string to an Enumeration value (second code example):&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;n&#34;&gt;SegueIdentifier&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;OtherScreenSegue1&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;// Enum value of type SegueIdentifier&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;n&#34;&gt;SegueIdentifier&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;OtherScreenSegue1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;toRaw&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;// String value, &amp;#34;otherScreen1Segue&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;3&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;n&#34;&gt;SegueIdentifier&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;fromRaw&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;otherScreenSegue1&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;!&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;// Unwrapped Enum value of type SegueIdentifier&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&#34;conclusion&#34;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;In addition to segue identifiers, I&amp;rsquo;m considering using raw value Enumerations to wrap &lt;code&gt;NSNotificationCenter&lt;/code&gt; keys as well.  Share if you find other nice uses of raw value Enumerations!&lt;/p&gt;
&lt;p&gt;So far, this solution has provided me a nice, straight-forward, type-safe way to encapsulate groups of Strings where the urge to fall back to &amp;ldquo;magic strings” would otherwise be high.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Pick a Delegate… Any Delegate… On Clean View Controllers in Swift</title>
      <link>https://www.andrewcbancroft.com/2014/08/26/pick-a-delegate-clean-view-controllers-in-swift/</link>
      <pubDate>Wed, 27 Aug 2014 04:43:29 +0000</pubDate>
      
      <guid>https://www.andrewcbancroft.com/2014/08/26/pick-a-delegate-clean-view-controllers-in-swift/</guid>
      <description>&lt;p&gt;The delegation pattern is ubiquitous in iOS development – the pattern is a &amp;ldquo;&lt;a title=&#34;Cocoa Core Competencies&#34; href=&#34;https://developer.apple.com/library/ios/documentation/general/conceptual/DevPedia-CocoaCore/Delegation.html&#34; target=&#34;_blank&#34;&gt;core competency&lt;/a&gt;” for developing in Cocoa, and if you program with the iOS SDK for any length of time and you&amp;rsquo;ll end up writing some code that resembles &lt;code&gt;someInstance.delegate = someDelegate&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;One of the toughest things that I&amp;rsquo;ve experienced is choosing what &lt;code&gt;someDelegate&lt;/code&gt; is.  All too often, a View Controller ends up being assigned the responsibility of being the delegate for &lt;em&gt;everything&lt;/em&gt; in its hierarchy.  My question is:  Is there a cleaner way?&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s pick up on the example I proposed in my &lt;a href=&#34;http://www.andrewcbancroft.com/2014/08/25/send-email-in-app-using-mfmailcomposeviewcontroller-with-swift/&#34; title=&#34;Send Email In-App – Using MFMailComposeViewController with Swift&#34;&gt;recent post about sending e-mails in-app&lt;/a&gt;.  For &amp;ldquo;quick and dirty” pragmatism, I just crammed everything into the View Controller with the promise of coming back and (hopefully) showing a cleaner way.  &lt;a title=&#34;Send Email In-App – Using MFMailComposeViewController with Swift&#34; href=&#34;http://www.andrewcbancroft.com/2014/08/25/send-email-in-app-using-mfmailcomposeviewcontroller-with-swift#//acbref-MFMailComposeViewControllerExample&#34; target=&#34;_blank&#34;&gt;Here is a quick link to example posed before&lt;/a&gt; if you&amp;rsquo;d like to review it before proceeding.&lt;/p&gt;
&lt;h2 id=&#34;_what-if8230_&#34;&gt;&lt;em&gt;What if…&lt;/em&gt;&lt;/h2&gt;
&lt;p&gt;What if we could make some adjustments so that the View Controller was trimmed down to the example on the &lt;em&gt;right (click for larger view)&lt;/em&gt;:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2014/08/Clean-View-Controller-Comparison.png&#34;&gt;&lt;img class=&#34;alignnone wp-image-4321 size-large&#34; src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2014/08/Clean-View-Controller-Comparison-1024x258.png&#34; alt=&#34;Clean View Controller Comparison&#34; width=&#34;730&#34; height=&#34;183&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2014/08/Clean-View-Controller-Comparison-1024x258.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2014/08/Clean-View-Controller-Comparison-300x75.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2014/08/Clean-View-Controller-Comparison-1200x303.png 1200w&#34; sizes=&#34;(max-width: 730px) 100vw, 730px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;ve created a &lt;a title=&#34;Swift Email Composer - GitHub&#34; href=&#34;https://github.com/andrewcbancroft/SwiftEmailComposer&#34; target=&#34;_blank&#34;&gt;fully-working example on GitHub&lt;/a&gt; if you&amp;rsquo;d like to download it and play.&lt;/p&gt;
&lt;p&gt;So the question at hand:  Is the class labeled &amp;ldquo;Clean Example” &lt;em&gt;preferable (&lt;em&gt;ie&lt;/em&gt;, better)&lt;/em&gt;?  First, let&amp;rsquo;s explore how I accomplished the &amp;ldquo;clean” View Controller.  Then I&amp;rsquo;ll tip my hand on and share what I like about this approach…&lt;/p&gt;
&lt;h2 id=&#34;emailcomposer&#34;&gt;EmailComposer&lt;/h2&gt;
&lt;p&gt;In order to accomplish the self-declared Clean View Controller above, I placed all of the configuration processes and the delegate method for the &lt;code&gt;MFMailComposeViewController&lt;/code&gt; in a &lt;em&gt;new&lt;/em&gt; class called &lt;code&gt;EmailComposer&lt;/code&gt;.  It should look familiar if you recall the &lt;a title=&#34;Send Email In-App – Using MFMailComposeViewController with Swift&#34; href=&#34;http://www.andrewcbancroft.com/2014/08/25/send-email-in-app-using-mfmailcomposeviewcontroller-with-swift#//acbref-MFMailComposeViewControllerExample&#34; target=&#34;_blank&#34;&gt;previous example&lt;/a&gt;:&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;import&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;Foundation&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;import&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;MessageUI&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;kd&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;EmailComposer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSObject&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;MFMailComposeViewControllerDelegate&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; 5&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Did this in order to mitigate needing to import MessageUI in my View Controller&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;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;canSendMail&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;Bool&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;MFMailComposeViewController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;canSendMail&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;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&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;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;configuredMailComposeViewController&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;n&#34;&gt;MFMailComposeViewController&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;11&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;mailComposerVC&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;MFMailComposeViewController&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;12&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;mailComposerVC&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;mailComposeDelegate&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;kc&#34;&gt;self&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&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;n&#34;&gt;mailComposerVC&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;setToRecipients&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;([&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;someone@somewhere.com&amp;#34;&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;15&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;mailComposerVC&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;setSubject&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;Sending you an in-app e-mail...&amp;#34;&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;16&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;mailComposerVC&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;setMessageBody&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;Sending e-mail in-app is not so bad!&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;isHTML&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;kc&#34;&gt;false&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;17&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;18&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;mailComposerVC&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;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&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;c1&#34;&gt;// &lt;/span&gt;&lt;span class=&#34;cs&#34;&gt;MARK:&lt;/span&gt;&lt;span class=&#34;c1&#34;&gt; MFMailComposeViewControllerDelegate Method&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;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;mailComposeController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;controller&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;MFMailComposeViewController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;!,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;didFinishWithResult&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;result&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;MFMailComposeResult&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;error&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSError&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;23&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;controller&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;dismissViewControllerAnimated&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;kc&#34;&gt;true&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;completion&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;kc&#34;&gt;nil&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;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 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;So literally, the only thing I did is&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Cut the function definitions for &lt;code&gt;configuredMailComposeViewController&lt;/code&gt;, and the &lt;code&gt;MFMailComposeViewControllerDelegate&lt;/code&gt; method.&lt;/li&gt;
&lt;li&gt;Paste them into the new &lt;code&gt;EmailComposer&lt;/code&gt;  class, which inherits from &lt;code&gt;NSObject&lt;/code&gt;  (a requirement for this particular delegate protocol&amp;rsquo;s conformity), and conforms to the &lt;code&gt;MFMailComposeViewControllerDelegate&lt;/code&gt;  protocol.&lt;/li&gt;
&lt;li&gt;Adjust my View Controller to create an instance of &lt;code&gt;EmailComposer&lt;/code&gt; , obtain a configured &lt;code&gt;MFMailComposeViewController&lt;/code&gt;, and present it whenever the user taps on a button in my UI.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;conclusions&#34;&gt;Conclusions&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;The View Controller in its final version is &lt;em&gt;focused&lt;/em&gt;.  It&amp;rsquo;s primary concern is presentation and handling of user interaction with the View itself, rather than needing to worry with configuring an &lt;code&gt;MFMailComposeViewController&lt;/code&gt; and its delegate callback.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;EmailComposer&lt;/code&gt; is less of a hassle to test, in the sense that &lt;span style=&#34;line-height: 1.5;&#34;&gt;I no longer need to instantiate a View Controller in my &lt;/span&gt;&lt;code&gt;XCTestCase&lt;/code&gt;&lt;span style=&#34;line-height: 1.5;&#34;&gt; class just to test my &lt;/span&gt;&lt;code&gt;MFMailComposeViewController&lt;/code&gt;&lt;span style=&#34;line-height: 1.5;&#34;&gt; stuff&lt;/span&gt;&lt;span style=&#34;line-height: 1.5;&#34;&gt;.  It&amp;rsquo;s a real pain to test an actual View Controller instance, so I like that I can easily create an instance of &lt;code&gt;EmailComposer&lt;/code&gt; and test away without the bulk.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;No need to import MessageUI in my View Controller.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;All in all, this is the cleanest, simplest, most balanced solution (that I could think of) to factoring out some logic to another class, so as to make my View Controller as clean as possible.&lt;/p&gt;
&lt;p&gt;The goal was to make sure the appropriate responsibilities are assigned to the right classes.  Presentation logic is all in the View Controller.  Configuration and delegate callback implementation is done in &lt;code&gt;EmailComposer&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;m thinking through applying this same idea to other more complicated examples (UITableViewDataSource and UITableViewDelegate come to mind), and I think it would do us a &lt;em&gt;lot&lt;/em&gt; of good to strategize on how to avoid making the View Controller the &amp;ldquo;catch-all” delegate / data source class for everything that&amp;rsquo;s currently on the screen_._&lt;/p&gt;
&lt;p&gt;Hopefully these thoughts spark some ideas in the Swift community.  This post has already been revised slightly based on feedback that I&amp;rsquo;ve received from folks on Twitter.  If you have additional ideas in regards to choosing the right delegate, holler my way!  I&amp;rsquo;d love to hear from you.&lt;/p&gt;
&lt;p&gt;Thanks for reading.&lt;/p&gt;
&lt;div class=&#34;related-posts&#34;&gt;
  &lt;p&gt;
    You might also enjoy
  &lt;/p&gt;
  &lt;ul&gt;
    &lt;li&gt;
      &lt;a title=&#34;Clean Coding in Swift – Functions&#34; href=&#34;http://www.andrewcbancroft.com/2014/08/07/clean-coding-in-swift-functions/&#34;&gt;Clean Coding in Swift – Functions&lt;/a&gt;
    &lt;/li&gt;
    &lt;li&gt;
      &lt;a title=&#34;Clean Coding in Swift – Type Inference&#34; href=&#34;http://www.andrewcbancroft.com/2014/08/12/clean-coding-in-swift-type-inference/&#34;&gt;Clean Coding in Swift – Type Inference&lt;/a&gt;
    &lt;/li&gt;
    &lt;li&gt;
      &lt;a title=&#34;Expanded Thoughts on Swift’s Type Inference&#34; href=&#34;http://www.andrewcbancroft.com/2014/08/20/expanded-thoughts-on-swifts-type-inference/&#34;&gt;Expanded Thoughts on Swift’s Type Inference&lt;/a&gt;
    &lt;/li&gt;
    &lt;li&gt;
      &lt;a title=&#34;Send Email In-App – Using MFMailComposeViewController with Swift&#34; href=&#34;http://www.andrewcbancroft.com/2014/08/25/send-email-in-app-using-mfmailcomposeviewcontroller-with-swift/&#34;&gt;Send Email In-App – Using MFMailComposeViewController with Swift&lt;/a&gt;
    &lt;/li&gt;
    &lt;li&gt;
      &lt;a title=&#34;Send Text Message In-App – Using MFMessageComposeViewController with Swift&#34; href=&#34;http://www.andrewcbancroft.com/2014/10/28/send-text-message-in-app-using-mfmessagecomposeviewcontroller-with-swift/&#34;&gt;Send Text Message In-App – Using MFMessageComposeViewController with Swift&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;
</description>
    </item>
    
    <item>
      <title>Send Email In-App – Using MFMailComposeViewController with Swift</title>
      <link>https://www.andrewcbancroft.com/2014/08/25/send-email-in-app-using-mfmailcomposeviewcontroller-with-swift/</link>
      <pubDate>Tue, 26 Aug 2014 03:48:30 +0000</pubDate>
      
      <guid>https://www.andrewcbancroft.com/2014/08/25/send-email-in-app-using-mfmailcomposeviewcontroller-with-swift/</guid>
      <description>&lt;p&gt;&lt;small&gt;Updated on October 11, 2016 – Swift 3.0&lt;/small&gt;&lt;/p&gt;
&lt;p&gt;In this writing, I want explore how to use &lt;code&gt;MFMailComposeViewController&lt;/code&gt; with Swift to send e-mails within your app as a walkthrough. My focus here is &amp;ldquo;quick and dirty” pragmatism, so that we can easily see what the inter-working components of &lt;code&gt;MFMailComposeViewController&lt;/code&gt; are. That being said, here&amp;rsquo;s an important &lt;em&gt;disclaimer&lt;/em&gt; – I&amp;rsquo;m going to overload the View Controller&amp;rsquo;s responsibilities in the examples to follow.&lt;/p&gt;
&lt;p&gt;&lt;a title=&#34;Pick a Delegate… Any Delegate… On Clean View Controllers in Swift&#34; href=&#34;http://www.andrewcbancroft.com/2014/08/26/pick-a-delegate-clean-view-controllers-in-swift/&#34; target=&#34;_blank&#34;&gt;An op-ed with my thoughts and experimentation on how to keep the View Controller clean&lt;/a&gt; by factoring out some of the configuration and delegate methods to another class is now live as well.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;define-requirements&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;defining-the-requirements&#34;&gt;Defining the requirements&lt;/h2&gt;
&lt;p&gt;As part of your app requirements, you need to be able to send an e-mail &lt;em&gt;within&lt;/em&gt; your app without leaving it. Additionally, you need to pre-populate some standard e-mail fields such as &amp;ldquo;To”, &amp;ldquo;Subject”, and &amp;ldquo;Body”.&lt;/p&gt;
&lt;p&gt;Not only is this possible, the API for accomplishing it is pretty straight forward.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;implementation-overview&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;implementation-overview&#34;&gt;Implementation overview&lt;/h2&gt;
&lt;p&gt;In order to implement the solution for this requirement, you need a few things:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;A View Controller from which your user will initiate the display of the email composer screen, presumably by tapping on a button or something else that&amp;rsquo;s wired up to an `@IBAction`.&lt;/li&gt;
&lt;li&gt;A configured `MFMailComposeViewController` to present.&lt;/li&gt;
&lt;li&gt;An `MFMailCompseViewControllerDelegate` to handle dismissing the email composer screen.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Note that you may have trouble in the iOS 8 Simulator, with symptoms of the composer presenting itself and immediately dismissing. Running the app on an actual device running iOS 8 should work fine, as the problem seems to be isolated to the simulator, only.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;//acbref-MFMailComposeViewControllerExample&#34;&gt;&lt;/a&gt;An example View Controller class that implements the three steps above is proposed here. All that would be left for you to do is to design a user interface and wire up the &lt;code&gt;@IBAction&lt;/code&gt;. Lines of code that are of special importance, such as module imports, protocol conformance and assignment, checking for the ability to send e-mail, and the protocol method implementation are highlighted. Take a look:&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;import&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;Foundation&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;import&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;UIKit&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;import&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;MessageUI&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&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;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;ViewController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIViewController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;MFMailComposeViewControllerDelegate&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&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;kr&#34;&gt;override&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;viewDidLoad&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; 8&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;kc&#34;&gt;super&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;viewDidLoad&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;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;10&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;11&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kr&#34;&gt;@IBAction&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;sendEmailButtonTapped&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;sender&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;AnyObject&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;12&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;mailComposeViewController&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;configuredMailComposeViewController&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;if&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;MFMailComposeViewController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;canSendMail&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;14&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;present&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;mailComposeViewController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;animated&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;kc&#34;&gt;true&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;completion&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;kc&#34;&gt;nil&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;15&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;else&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;16&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;showSendMailErrorAlert&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;17&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;18&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;19&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;20&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;configuredMailComposeViewController&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;n&#34;&gt;MFMailComposeViewController&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;21&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;mailComposerVC&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;MFMailComposeViewController&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;n&#34;&gt;mailComposerVC&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;mailComposeDelegate&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;kc&#34;&gt;self&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;// Extremely important to set the --mailComposeDelegate-- property, NOT the --delegate-- property&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&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;n&#34;&gt;mailComposerVC&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;setToRecipients&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;([&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;someone@somewhere.com&amp;#34;&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;25&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;mailComposerVC&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;setSubject&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;Sending you an in-app e-mail...&amp;#34;&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;26&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;mailComposerVC&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;setMessageBody&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;Sending e-mail in-app is not so bad!&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;isHTML&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;kc&#34;&gt;false&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&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;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;mailComposerVC&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&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;31&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;showSendMailErrorAlert&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;32&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;sendMailErrorAlert&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIAlertView&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;title&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;Could Not Send Email&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;message&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;Your device could not send e-mail.  Please check e-mail configuration and try again.&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;delegate&lt;/span&gt;&lt;span class=&#34;p&#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;cancelButtonTitle&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;OK&amp;#34;&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;33&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;sendMailErrorAlert&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;show&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;34&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;35&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;36&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// &lt;/span&gt;&lt;span class=&#34;cs&#34;&gt;MARK:&lt;/span&gt;&lt;span class=&#34;c1&#34;&gt; MFMailComposeViewControllerDelegate Method&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;37&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;mailComposeController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;kc&#34;&gt;_&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;controller&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;MFMailComposeViewController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;didFinishWith&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;result&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;MFMailComposeResult&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;error&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Error&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;38&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;controller&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;dismiss&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;animated&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;kc&#34;&gt;true&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;completion&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;kc&#34;&gt;nil&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;39&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;40&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;&lt;a name=&#34;step-by-step-implementation&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;step-by-step-implementation&#34;&gt;Step-by-step implementation&lt;/h2&gt;
&lt;p&gt;With the above example in front of you, let&amp;rsquo;s explore what&amp;rsquo;s going on here in detail.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;vc-setup&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;setting-up-the-view-controller&#34;&gt;Setting up the View Controller&lt;/h3&gt;
&lt;p&gt;First of all, we need to import the &lt;code&gt;MessageUI&lt;/code&gt; module.&lt;/p&gt;
&lt;p&gt;Second, we need to specify that the View Controller will conform to the &lt;code&gt;MFMailComposeViewControllerDelegate&lt;/code&gt; protocol. Later, we&amp;rsquo;ll actually implement the method that this protocol outlines, which will allow us to make the email composer screen go away once the user is finished either sending an e-mail or cancels out of sending one.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;send-button-tapped&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;sendemailbuttontapped&#34;&gt;sendEmailButtonTapped()&lt;/h3&gt;
&lt;p&gt;This is the method that responds to the user tapping on a button. Assuming this is wired up to an appropriate element in the UI, it kicks off everything related to creating and showing the email composer screen. The logic is as follows:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Obtain a configured `MFMailComposeViewController` instance&lt;/li&gt;
&lt;li&gt;Check to make sure the device can send e-mail at this moment
&lt;ul&gt;
&lt;li&gt;If it can, present the configured `MFMailComposeViewController`&lt;/li&gt;
&lt;li&gt;Otherwise, show an alert with an error message&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;a name=&#34;configured-controller&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;configuredmailcomposeviewcontroller&#34;&gt;configuredMailComposeViewController()&lt;/h3&gt;
&lt;p&gt;I decided to encapsulate the configuration of an &lt;code&gt;MFMailComposeViewController&lt;/code&gt; instance inside a function. I found that it made things a little more readable, perhaps more testable, and kept the spirit of &lt;a href=&#34;http://www.andrewcbancroft.com/2014/08/07/clean-coding-in-swift-functions/&#34; title=&#34;Clean Coding in Swift – Functions&#34;&gt;decomposing sub-steps of a process into individual, single-responsibility functions&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;One vital property to set is the &lt;code&gt;mailComposeDelegate&lt;/code&gt; property (otherwise, you can never get rid of the e-mail composer screen after it&amp;rsquo;s presented). Now, there&amp;rsquo;s a &amp;ldquo;gotcha” here – &lt;code&gt;MFMailComposeViewController&lt;/code&gt; instances &lt;em&gt;also&lt;/em&gt; have a property named &lt;code&gt;delegate&lt;/code&gt; . &lt;strong&gt;The &lt;em&gt;delegate&lt;/em&gt;&lt;/strong&gt; &lt;strong&gt;property is &lt;em&gt;not&lt;/em&gt; the one to set&lt;/strong&gt; (I did this at first and wondered why my implemented delegate &amp;ldquo;callback” method never got called). Set the &lt;code&gt;mailComposeDelegate&lt;/code&gt; property to the instance of whatever you want to handle dismissing the email composer screen once the user is finished sending an e-mail or cancels. In the example, I set it to &lt;code&gt;self&lt;/code&gt;, since the View Controller itself will implement the appropriate delegate method (&lt;a title=&#34;Pick a Delegate… Any Delegate… On Clean View Controllers in Swift&#34; href=&#34;http://www.andrewcbancroft.com/2014/08/26/pick-a-delegate-clean-view-controllers-in-swift/&#34; target=&#34;_blank&#34;&gt;Read my thoughts on cleaning this up a bit&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;As you can see, setting up the &amp;ldquo;To”, &amp;ldquo;Subject”, and &amp;ldquo;Body” are simply a matter of setting properties of an &lt;code&gt;MFMailComposeViewController&lt;/code&gt; instance. Notice that &lt;code&gt;setToRecipients()&lt;/code&gt; accepts an &lt;em&gt;array&lt;/em&gt; of e-mail address strings, so don&amp;rsquo;t forget to wrap that argument in an array, even for a single recipient. The same would work for Cc, and Bcc recipients, had I configured those.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;show-send-mail-error&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;showsendmailerroralert&#34;&gt;showSendMailErrorAlert()&lt;/h3&gt;
&lt;p&gt;This method shows a simple UIAlertView if the user&amp;rsquo;s device cannot send an e-mail at the moment.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;delegate-method&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;mfmailcomposeviewcontrollers-delegate-method&#34;&gt;MFMailComposeViewController&amp;rsquo;s delegate method&lt;/h3&gt;
&lt;p&gt;The implementation of this delegate method simply dismisses the email composer screen.&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/08/26/pick-a-delegate-clean-view-controllers-in-swift/&#34; title=&#34;Pick a Delegate… Any Delegate… On Clean View Controllers in Swift&#34;&gt;Pick a Delegate… Any Delegate… On Clean View Controllers in Swift&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/10/28/send-text-message-in-app-using-mfmessagecomposeviewcontroller-with-swift/&#34; title=&#34;Send Text Message In-App – Using MFMessageComposeViewController with Swift&#34;&gt;Send Text Message In-App – Using MFMessageComposeViewController with Swift&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>Expanded Thoughts on Swift’s Type Inference</title>
      <link>https://www.andrewcbancroft.com/2014/08/20/expanded-thoughts-on-swifts-type-inference/</link>
      <pubDate>Thu, 21 Aug 2014 02:19:45 +0000</pubDate>
      
      <guid>https://www.andrewcbancroft.com/2014/08/20/expanded-thoughts-on-swifts-type-inference/</guid>
      <description>&lt;p&gt;In my recent &lt;a title=&#34;Clean Coding in Swift – Type Inference&#34; href=&#34;http://www.andrewcbancroft.com/2014/08/12/clean-coding-in-swift-type-inference/&#34; target=&#34;_blank&#34;&gt;op-ed on clean coding in Swift focused on Type Inference&lt;/a&gt;, I began by saying,&lt;/p&gt;
&lt;p&gt;Quick!  Tell me!  What is the Type of the &lt;code&gt;birdDetails&lt;/code&gt; constant in this code example:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;let birdDetails = birdDetailsFromStorage()&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;With no additional context to glean information from, the correct answer to the question is, &amp;ldquo;I have absolutely no clue…”&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;But is that concluding assertion &lt;em&gt;true&lt;/em&gt;?  Hmm…&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;I&amp;rsquo;m learning&lt;/em&gt;, and as I&amp;rsquo;ve weighed a recent Twitter conversation and thought on a &lt;a title=&#34;Rob Napier Comment&#34; href=&#34;http://www.andrewcbancroft.com/2014/08/12/clean-coding-in-swift-type-inference/#comment-1551252721&#34; target=&#34;_blank&#34;&gt;comment thread that Rob Napier made&lt;/a&gt; on the post quoted above, I&amp;rsquo;m compelled to expand a little on my first post on Type Inference as it relates to clean code in Swift.&lt;/p&gt;
&lt;p&gt;Something struck me as I read &lt;a title=&#34;Rob&#39;s Comment&#34; href=&#34;http://www.andrewcbancroft.com/2014/08/12/clean-coding-in-swift-type-inference/#comment-1551252721&#34; target=&#34;_blank&#34;&gt;Rob&amp;rsquo;s comment&lt;/a&gt;:  Why &lt;em&gt;wouldn&amp;rsquo;t&lt;/em&gt; I know the Type that would be inferred from what is returned by &lt;code&gt;birdDetailsFromStorage()&lt;/code&gt; and assigned to &lt;code&gt;birdDetails&lt;/code&gt;?  Presumably, I named the function what I named it intentionally.  The part I missed, was that if I had &lt;em&gt;designed&lt;/em&gt; well and created a Type called &lt;code&gt;BirdDetails&lt;/code&gt; (say, a Struct as Rob proposed), then all of a sudden, an inference can be made by both the compiler &lt;em&gt;and&lt;/em&gt; a human that the &lt;code&gt;birdDetails&lt;/code&gt; constant is… well… an instance of &lt;code&gt;BirdDetails&lt;/code&gt;.  Imagine that!&lt;/p&gt;
&lt;p&gt;To quote Rob (emphasis added):&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;span style=&#34;color: #3f4549;&#34;&gt;Type inference should only be used when the result is unambiguous, but &lt;strong&gt;the best solution is to&lt;/strong&gt; &lt;strong&gt;make the result unambiguous&lt;/strong&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Bingo.  The &lt;em&gt;best&lt;/em&gt; solution is to &lt;em&gt;make&lt;/em&gt; the result unambiguous.  You and I as code authors are in charge of the clarity or ambiguity of our code – it&amp;rsquo;s up to us to &lt;em&gt;make&lt;/em&gt; the results of our function evaluations unambiguous.&lt;/p&gt;
&lt;p&gt;When I named the function &lt;code&gt;birdDetailsFromStorage()&lt;/code&gt;, I heavily implied something about its return Type in the name.  I even implied it in the name of the &lt;em&gt;constant&lt;/em&gt;.  I was expecting the return Type to be something that encapsulated whatever &amp;ldquo;bird details” are – I just didn&amp;rsquo;t realize it at the time (although it&amp;rsquo;s super obvious now)!&lt;/p&gt;
&lt;p&gt;The very &lt;em&gt;name&lt;/em&gt; of a thing sets expectations for you and the readers of your code.  It&amp;rsquo;s our job to set ourselves up for that expectation to be &lt;em&gt;fulfilled&lt;/em&gt;!  B&lt;span style=&#34;color: #3f4549;&#34;&gt;e predictable with the return Type of your functions for your own sake.  A function signature should be such that when you run across &lt;code&gt;birdDetailsFromStorage()&lt;/code&gt;in some piece of code, you are able to legitimately expect it to return a &lt;code&gt;BirdDetails&lt;/code&gt;.  &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style=&#34;color: #3f4549;&#34;&gt;Using this predictable, &lt;/span&gt;convention-based approach to give humans the right clues about what the compiler is going to compute a Type to be makes Type Inference a totally legitimate language feature to embrace for the sake of your code&amp;rsquo;s clarity and simplicity.&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;ll leave you with a final quote from Rob&amp;rsquo;s comments:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;span style=&#34;color: #3f4549;&#34;&gt;Don&amp;rsquo;t go implying that you&amp;rsquo;re returning one thing when you&amp;rsquo;re returning another. If you must, then yes, explicit types are your punishment. 😀&lt;/span&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Thank you Rob for your insight and feedback.  You can read more of Rob&amp;rsquo;s work at &lt;a title=&#34;Rob Napier&#39;s Blog&#34; href=&#34;http://robnapier.net&#34; target=&#34;_blank&#34;&gt;&lt;a href=&#34;http://robnapier.net&#34;&gt;http://robnapier.net&lt;/a&gt;&lt;/a&gt;.&lt;/p&gt;
&lt;div class=&#34;related-posts&#34;&gt;
  &lt;p&gt;
    You might also enjoy
  &lt;/p&gt;
  &lt;ul&gt;
    &lt;li&gt;
      &lt;a title=&#34;Clean Coding in Swift – Functions&#34; href=&#34;http://www.andrewcbancroft.com/2014/08/07/clean-coding-in-swift-functions/&#34; target=&#34;_blank&#34;&gt;Clean Coding in Swift – Functions&lt;/a&gt;
    &lt;/li&gt;
    &lt;li&gt;
      &lt;a title=&#34;Clean Coding in Swift – Type Inference&#34; href=&#34;http://www.andrewcbancroft.com/2014/08/12/clean-coding-in-swift-type-inference/&#34; target=&#34;_blank&#34;&gt;Clean Coding in Swift – Type Inference&lt;/a&gt;
    &lt;/li&gt;
    &lt;li&gt;
      &lt;a title=&#34;Pick a Delegate… Any Delegate… On Clean View Controllers in Swift&#34; href=&#34;http://www.andrewcbancroft.com/2014/08/26/pick-a-delegate-clean-view-controllers-in-swift/&#34; target=&#34;_blank&#34;&gt;Pick a Delegate… Any Delegate… On Clean View Controllers in Swift&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;</description>
    </item>
    
    <item>
      <title>Sort It Out – Sorting an Array in Swift</title>
      <link>https://www.andrewcbancroft.com/2014/08/16/sort-yourself-out-sorting-an-array-in-swift/</link>
      <pubDate>Sat, 16 Aug 2014 18:06:26 +0000</pubDate>
      
      <guid>https://www.andrewcbancroft.com/2014/08/16/sort-yourself-out-sorting-an-array-in-swift/</guid>
      <description>&lt;p&gt;I had a question come to me today regarding sorting an array of integers that are actually &lt;em&gt;encoded&lt;/em&gt; as strings in the array.  Data comes to is in a variety of encodings/types, so it&amp;rsquo;s quite common to need to adjust things to the right state for working with.  Let&amp;rsquo;s take a look at how to solve this one.&lt;/p&gt;
&lt;h2 id=&#34;the-dilemma&#34;&gt;The Dilemma&lt;/h2&gt;
&lt;p&gt;Given an array 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;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;arrayOfIntsAsStrings&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 class=&#34;s&#34;&gt;&amp;#34;103&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;2&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;1&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;50&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;55&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;98&amp;#34;&lt;/span&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 question arises:  &amp;ldquo;How do I sort this in numerical order so that my output array is still an array of strings, but sorted like this &lt;code&gt;[&amp;quot;1&amp;amp;#8221;, &amp;quot;2&amp;amp;#8221;, &amp;quot;50&amp;amp;#8221;, &amp;quot;55&amp;amp;#8221;, &amp;quot;98&amp;amp;#8221;, &amp;quot;103&amp;amp;#8221;, &amp;quot;1000&amp;amp;#8221;]&lt;/code&gt; (integer comparison), not this &lt;code&gt;[&amp;quot;1&amp;amp;#8221;, &amp;quot;1000&amp;amp;#8221;, &amp;quot;103&amp;amp;#8221;, &amp;quot;2&amp;amp;#8221;, &amp;quot;50&amp;amp;#8221;, &amp;quot;55&amp;amp;#8221;, &amp;quot;98&amp;amp;#8221;]&lt;/code&gt;  (string comparison)?” Enter Swift&amp;rsquo;s &lt;code&gt;sorted&lt;/code&gt; function:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;“Swift’s standard library provides a function called sorted, which sorts an array of values of a known type, based on the output of a sorting closure that you provide. Once it completes the sorting process, the sorted function returns a new array of the same type and size as the old one, with its elements in the correct sorted order. The original array is not modified by the sorted function.” Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. &lt;a title=&#34;Swift iBook&#34; href=&#34;https://itun.es/us/jEUH0.l&#34; target=&#34;_blank&#34;&gt;&lt;a href=&#34;https://itun.es/us/jEUH0.l&#34;&gt;https://itun.es/us/jEUH0.l&lt;/a&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;This is exactly what we need to do the job here.&lt;/p&gt;
&lt;h2 id=&#34;the-gist&#34;&gt;The Gist&lt;/h2&gt;
&lt;p&gt;Given the original array outlined above, we can create a new_ sorted_ array 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;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;sortedArray&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;bp&#34;&gt;sorted&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;arrayOfIntsAsStrings&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;2&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;str1&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;str2&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;p&#34;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;Bool&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;in&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;str1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;toInt&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;str2&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;toInt&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;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;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This code produces our desired output:  &lt;code&gt;[&amp;quot;1&amp;amp;#8221;, &amp;quot;2&amp;amp;#8221;, &amp;quot;50&amp;amp;#8221;, &amp;quot;55&amp;amp;#8221;, &amp;quot;98&amp;amp;#8221;, &amp;quot;103&amp;amp;#8221;, &amp;quot;1000&amp;amp;#8221;]&lt;/code&gt;&lt;/p&gt;
&lt;h2 id=&#34;thenbspdetails&#34;&gt;The Details&lt;/h2&gt;
&lt;p&gt;Swift&amp;rsquo;s sorted function takes two arguments:  an Array, and a Closure.  The part that may be confusing is the closure argument.  Isolated it looks 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;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;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;str1&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;str2&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;p&#34;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;Bool&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;in&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;str1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;toInt&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;str2&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;toInt&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;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;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Swift provides several &lt;a title=&#34;Apple Developer Documentation - Swift Closures&#34; href=&#34;https://developer.apple.com/library/prerelease/mac/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html&#34; target=&#34;_blank&#34;&gt;shorthand forms of the closure syntax&lt;/a&gt;, but I&amp;rsquo;ve chosen the longest-form here just for full exposure and clarity.  The closure I&amp;rsquo;ve written simply needs to compute a Bool value representing the result of a comparison between two values – in this case, I&amp;rsquo;m wanting to do &lt;em&gt;integer&lt;/em&gt; comparison, so I write this:  &lt;code&gt;str1.toInt()&lt;/code&gt; and &lt;code&gt;str2.toInt()&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;You can choose whatever parameter names you&amp;rsquo;d like, and you can actually rely on Type Inference in the parameter section — the compiler can work out what Types your parameters are.  But if it helps clue you in to what the code is doing, you can &lt;a title=&#34;Clean Coding in Swift – Type Inference&#34; href=&#34;http://www.andrewcbancroft.com/2014/08/12/clean-coding-in-swift-type-inference/&#34; target=&#34;_blank&#34;&gt;&lt;em&gt;specify&lt;/em&gt; the Types in the closure&amp;rsquo;s parameter section, as I wrote about recently&lt;/a&gt;, and as I did in my example when I wrote &lt;code&gt;str1: String, str2: String&lt;/code&gt; (I could have left off the &lt;code&gt;: String&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;Within the closure&amp;rsquo;s body, you can put in as many lines of logic as are necessary in order to produce an appropriate comparison result to get your Array in the right order.  If it gets &lt;em&gt;too&lt;/em&gt; complicated to do it in-line, think about encapsulating that logic inside one or more functions in the spirit of writing &lt;a title=&#34;Clean Coding in Swift – Functions&#34; href=&#34;http://www.andrewcbancroft.com/2014/08/07/clean-coding-in-swift-functions/&#34; target=&#34;_blank&#34;&gt;clean code&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;To produce an appropriate comparison result, you&amp;rsquo;ll have to consider the Types of the data you&amp;rsquo;re working with within the closure and consider any casting or manipulation you&amp;rsquo;ll need to do to produce the correct result like we did in our example. Other than that, it&amp;rsquo;s pretty straightforward to sort it out!&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Clean Coding in Swift – Type Inference</title>
      <link>https://www.andrewcbancroft.com/2014/08/12/clean-coding-in-swift-type-inference/</link>
      <pubDate>Wed, 13 Aug 2014 04:22:25 +0000</pubDate>
      
      <guid>https://www.andrewcbancroft.com/2014/08/12/clean-coding-in-swift-type-inference/</guid>
      <description>&lt;p&gt;Quick!  Tell me!  What is the Type of the &lt;code&gt;birdDetails&lt;/code&gt;constant in this code example:&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;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;birdDetails&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;birdDetailsFromStorage&lt;/span&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;With no additional context to glean information from, the correct answer to the question is, &amp;ldquo;I have absolutely no clue…”  &lt;a title=&#34;Expanded Thoughts on Swift’s Type Inference&#34; href=&#34;http://www.andrewcbancroft.com/2014/08/20/expanded-thoughts-on-swifts-type-inference/&#34; target=&#34;_blank&#34;&gt;&lt;em&gt;Or is it?&lt;/em&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Not fair!”, you say.  &amp;ldquo;In the real world, I&amp;rsquo;d have the ability to option-click and learn the type from a pop-up tooltip, or learn this information by inspection from Xcode&amp;rsquo;s utilities panel.”&lt;/p&gt;
&lt;p&gt;Truth.  We would.  &lt;em&gt;But…&lt;/em&gt; should we &lt;em&gt;have&lt;/em&gt; to for an example like the one above?  Could we have helped ourselves out a bit by being explicit about the type of &lt;code&gt;birdDetails&lt;/code&gt;?&lt;/p&gt;
&lt;p&gt;This was the question I asked myself as I set out to determine how and when I prefer to explicitly specify the Types of my variables and constants at declaration-time, rather than letting the compiler &lt;em&gt;infer&lt;/em&gt; the Type for me.&lt;/p&gt;
&lt;p&gt;&lt;a title=&#34;Apple Documentation - Type Inference&#34; href=&#34;https://developer.apple.com/library/prerelease/mac/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html#//apple_ref/doc/uid/TP40014097-CH5-XID_468&#34; target=&#34;_blank&#34;&gt;Type Inference&lt;/a&gt; is powerful and convenient.  It enables us to leave off explicit type specifications when we declare a variable or constant, leaving us with nice, terse, clean-&lt;em&gt;looking&lt;/em&gt; code.  But in reality, for examples like the one posed above, is clean-&lt;em&gt;looking&lt;/em&gt; truly &amp;ldquo;clean code”?&lt;/p&gt;
&lt;p&gt;From Apple&amp;rsquo;s documentation:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;span style=&#34;color: #414141;&#34;&gt;Type inference enables a compiler to deduce the type of a particular expression automatically when it compiles your code, simply by examining the values you provide.&lt;/span&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;The key phrase that stuck out to me was this:  &amp;ldquo;Type inference enables a &lt;strong&gt;&lt;em&gt;compiler&lt;/em&gt;&lt;/strong&gt; to deduce the type of a particular expression…”.  Humans are not compilers!&lt;/p&gt;
&lt;p&gt;I don&amp;rsquo;t know about you, but I could use all the help I can get when it comes to figuring out simple things like the Type resulting from an expression.  Sure, I could rely on the IDE, but in debugging, or in trying to simply read and understand what the intention of my code is when I come back to it sometime later, I want to focus on the &lt;em&gt;code&lt;/em&gt;, not on pop-up tool tips or inspector panels.&lt;/p&gt;
&lt;p&gt;For instances where deducing the type is not simple for a human to do (and I mean &lt;em&gt;really&lt;/em&gt; simple), I&amp;rsquo;m getting to where I prefer to specify the Type up-front.  We&amp;rsquo;re used to doing this in Objective-C, and I even do it in C# when using &lt;span class=&#34;lang:c# decode:true  crayon-inline &#34;&gt;var&lt;/span&gt; would obscure things.  When a language gives me the option to be clear about Types, I try to take advantage of that valuable language feature for all but the simplest of situations.&lt;/p&gt;
&lt;h3 id=&#34;the-simplest-of-situations&#34;&gt;The Simplest of Situations&lt;/h3&gt;
&lt;p&gt;Let&amp;rsquo;s think for a moment about this so-called &amp;ldquo;simplest of situations”.  I would define such a situation to be when the Type resulting from an expression can easily, at a glance, be inferred by a human being &lt;em&gt;without&lt;/em&gt; assistance from an IDE.  It&amp;rsquo;s all about context here, and for these simplest of situations, I love Type Inference.&lt;/p&gt;
&lt;p&gt;Compare the following two lines of code:&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;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;birdDetails&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;Swift&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;and&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;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;birdDetails&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;s&#34;&gt;&amp;#34;Swift&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In the example just presented, typing &amp;ldquo;&lt;code&gt;: String&lt;/code&gt; ” to explicitly specify &lt;code&gt;birdDetails&lt;/code&gt;‘ type is superfluous in my opinion (and I prefer not to be repetitively redundant when I can).  It&amp;rsquo;s crystal clear that &lt;code&gt;birdDetails&lt;/code&gt; in this example is a &lt;code&gt;String&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;However&lt;/em&gt;, in situations like the one at the beginning of the article where, by simply &lt;em&gt;looking,&lt;/em&gt; I would have to answer, &amp;ldquo;I have no clue what the Type of this is”, my preference / proposal for your consideration would be to go ahead and specify the Type at declaration time.  Consider:&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;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;birdDetails&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 class=&#34;n&#34;&gt;genusSpecies&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;commonName&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;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;birdDetailsFromStorage&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&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;//... somewhere else in a Swift file far, far away ...&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&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;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;birdDetailsFromStorage&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;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;genusSpecies&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;commonName&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;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;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;Apus apus&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;Swift&amp;#34;&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;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;When I&amp;rsquo;m writing this the first time, I can spend the time looking up the function&amp;rsquo;s return Type and specify it when I declare my constant.  It will make my life so much easier down the road.&lt;/p&gt;
&lt;p&gt;Don&amp;rsquo;t stumble over the fact that &lt;code&gt;birdDetails&lt;/code&gt; is a tuple type &lt;code&gt;(String, String)&lt;/code&gt;.  The point is that the function could have returned &lt;em&gt;anything&lt;/em&gt;, and it would still have been impossible for me to tell you what the Type of the constant was just by looking, had I not specified it upon its declaration.&lt;/p&gt;
&lt;p&gt;Being explicit about the type in the declaration has great potential to immediately help us get our bearings around a particular set of code when we return to it after any length of time.  And it seems to me that one of the principal goals of writing clean code is to help ourselves and our teams make sense of code quicker so that everyone&amp;rsquo;s happier and more productive.&lt;/p&gt;
&lt;p&gt;Whew.  That was a long-winded exploration of some things to think about when relying on (or avoiding) Type Inference in your own code.  Thanks for reading!&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;Since publishing this op-ed, I&amp;rsquo;ve &lt;a title=&#34;Expanded Thoughts on Swift’s Type Inference&#34; href=&#34;http://www.andrewcbancroft.com/2014/08/20/expanded-thoughts-on-swifts-type-inference/&#34; target=&#34;_blank&#34;&gt;expanded my thoughts on Type Inference&lt;/a&gt; as it relates to clean coding practices in Swift.  I highly recommend that this post be read and considered alongside my latest musings,which were heavily influenced by Rob Napier&amp;rsquo;s comments below.&lt;/p&gt;
&lt;div class=&#34;related-posts&#34;&gt;
  &lt;p&gt;
    You might also enjoy
  &lt;/p&gt;
  &lt;ul&gt;
    &lt;li&gt;
      &lt;a title=&#34;Clean Coding in Swift – Functions&#34; href=&#34;http://www.andrewcbancroft.com/2014/08/07/clean-coding-in-swift-functions/&#34; target=&#34;_blank&#34;&gt;Clean Coding in Swift – Functions&lt;/a&gt;
    &lt;/li&gt;
    &lt;li&gt;
      &lt;a title=&#34;Expanded Thoughts on Swift’s Type Inference&#34; href=&#34;http://www.andrewcbancroft.com/2014/08/20/expanded-thoughts-on-swifts-type-inference/&#34; target=&#34;_blank&#34;&gt;Expanded Thoughts on Swift&#39;s Type Inference&lt;/a&gt;
    &lt;/li&gt;
    &lt;li&gt;
      &lt;a title=&#34;Pick a Delegate… Any Delegate… On Clean View Controllers in Swift&#34; href=&#34;http://www.andrewcbancroft.com/2014/08/26/pick-a-delegate-clean-view-controllers-in-swift/&#34; target=&#34;_blank&#34;&gt;Pick a Delegate… Any Delegate… On Clean View Controllers in Swift&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;</description>
    </item>
    
    <item>
      <title>Clean Coding in Swift – Functions</title>
      <link>https://www.andrewcbancroft.com/2014/08/07/clean-coding-in-swift-functions/</link>
      <pubDate>Thu, 07 Aug 2014 18:16:23 +0000</pubDate>
      
      <guid>https://www.andrewcbancroft.com/2014/08/07/clean-coding-in-swift-functions/</guid>
      <description>&lt;p&gt;I&amp;rsquo;ve been thinking a lot about how the principles of clean coding (Bob Martin&amp;rsquo;s &amp;ldquo;&lt;a title=&#34;Amazon - Clean Code&#34; href=&#34;http://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882&#34; target=&#34;_blank&#34;&gt;Clean Code&lt;/a&gt;&amp;rdquo;) apply in Swift. How do I express clean code in this language? Conversely, how do I &lt;em&gt;avoid&lt;/em&gt; writing &lt;em&gt;cryptic&lt;/em&gt; code in Swift? What language features help me write clear and self-explanatory code and what language features present the potential for tempting me to write obscure code in Swift?&lt;/p&gt;
&lt;p&gt;I am beginning a commentary series that I hope will encourage clean coding practice in myself and in the Swift developer community. Dialog on these topics is welcomed – I have not &amp;ldquo;arrived”, so please – help me help the Swift community for the better!&lt;/p&gt;
&lt;p&gt;The first in the series is on writing clean functions in Swift. As it turns out, Swift provides some really great mechanisms for defining self-explanatory, clear-purposed functions. We&amp;rsquo;ll start by analyzing the features around naming functions and their parameters, and will conclude on thinking through function decomposition.&lt;/p&gt;
&lt;h5 id=&#34;function-names&#34;&gt;Function Names&lt;/h5&gt;
&lt;p&gt;Something that I really enjoyed about Objective-C was that while method names were often long and verbose, they were extremely descriptive. My code often read like a narrative, and I enjoyed that. Some may have hated it, but I found it extremely helpful in facilitating my recollection of a method&amp;rsquo;s intended purpose and the expectations around its argument requirements.&lt;/p&gt;
&lt;p&gt;I do my best to think hard about the names that I give my functions, ascribing to them a name that is specific, targeted, and focused on the single &amp;ldquo;thing” that each one does. Nothing in Swift mandates that we leave the verbosity of Objective-C naming conventions behind. In fact, the exact opposite is true!  One example is that swift intentionally provides us with the ability to add external parameter names &lt;em&gt;so that&lt;/em&gt; we can be as descriptive as we need to be about the names of our functions.&lt;/p&gt;
&lt;h5 id=&#34;parameter-names&#34;&gt;Parameter Names&lt;/h5&gt;
&lt;p&gt;&lt;a title=&#34;Apple Developer Documentation - External Parameter Names&#34; href=&#34;https://developer.apple.com/library/prerelease/mac/documentation/Swift/Conceptual/Swift_Programming_Language/Functions.html#//apple_ref/doc/uid/TP40014097-CH10-XID_255&#34; target=&#34;_blank&#34;&gt;Swift&amp;rsquo;s external parameter names&lt;/a&gt;, in my opinion, are wonderful for helping write self-documenting code. They simply help guide my mind back to what a given function does and needs in order to do its job. Anything to help me get back in the zone is worth spending a few extra keystrokes on, especially since Xcode&amp;rsquo;s auto-completion assists me so well when calling functions with long signatures.&lt;/p&gt;
&lt;p&gt;Apple recommends the following:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;span style=&#34;color: #414141;&#34;&gt;Consider using external parameter names whenever the purpose of a function’s arguments would be unclear to someone reading your code for the first time.&lt;/span&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;I would only add that sometimes, that &lt;em&gt;someone&lt;/em&gt; &amp;ldquo;reading your code for the first time” is &lt;em&gt;you&lt;/em&gt;. Not technically, of course, but think about this: Write some code, leave it, and come back to it some time later. Will it make sense? Will you immediately go, &amp;ldquo;Ah, I know what I meant there”, or will you do like I&amp;rsquo;ve often done and say, &amp;ldquo;&lt;em&gt;WHAT&lt;/em&gt; in the &lt;em&gt;WORLD&lt;/em&gt; was I &lt;em&gt;THINKING&lt;/em&gt;??!?”.&lt;/p&gt;
&lt;p&gt;Spending a few seconds on typing a few more words to save brain power and potentially &lt;em&gt;more&lt;/em&gt; seconds/minutes some time later is a worthy investment. And if you work on a team, your teammates will appreciate the extra care you put in to providing as many hints as possible through inventing good names. Good naming includes parameter names. Why &lt;em&gt;not&lt;/em&gt; take advantage of the fact that Swift provides you this opportunity to write self-documenting code?&lt;/p&gt;
&lt;h5 id=&#34;function-decomposition&#34;&gt;Function Decomposition&lt;/h5&gt;
&lt;blockquote&gt;
&lt;p&gt;In order to make sure our functions are doing &amp;ldquo;one thing”, we need to make sure that the statements within our function are all at the same level of abstraction. -Bob Martin, Clean Code pg. 36&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;This is just general advice without respect to a specific language. A couple of things to think about:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt; Watch out for key words like &amp;ldquo;and” / &amp;ldquo;or” in your function names. These red-flag words often indicate that your function is doing more than one thing and can be further decomposed. Consider:&lt;/li&gt;
&lt;/ol&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;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;washAndDryCar&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;2&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Logic to wash and dry a car&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;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;A preferred decomposition could look 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;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;washCar&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;2&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Logic to wash a car&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;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;4&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;5&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;dryCar&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;6&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Logic to dry a car&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;ol&gt;
&lt;li&gt; Doing one thing can still involve multiple steps. But if each step takes a few steps of its own, that group of steps can be extracted out into another function. Consider:&lt;/li&gt;
&lt;/ol&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;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;washCar&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;2&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Pre-rinse code (example implementation - 4 lines)&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;c1&#34;&gt;// Soap code (example implementation - 5 lines)&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;c1&#34;&gt;// Rinse code (exmple implementation - 4 lines)&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;A preferred decomposition might look 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;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;washCar&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;2&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;n&#34;&gt;rinse&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;durationInSeconds&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;mf&#34;&gt;25.0&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;n&#34;&gt;soapCar&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;4&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;n&#34;&gt;rinse&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;durationInSeconds&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;mf&#34;&gt;60.0&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;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;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;What&amp;rsquo;s neat about the final result of the decomposition is that not only is my &lt;code&gt;washCar&lt;/code&gt; function shorter and more readable (from 13 lines to 3 lines), but I got code re-use by making the &lt;code&gt;rinse&amp;lt;&lt;/code&gt; function take a duration argument.&lt;/p&gt;
&lt;p&gt;Hopefully these thoughts spark a few ideas in you.  Constructive feedback is welcome!  How are &lt;em&gt;you&lt;/em&gt; thinking about writing clean code in Swift?&lt;/p&gt;
&lt;div class=&#34;related-posts&#34;&gt;
  &lt;p&gt;
    You might also enjoy
  &lt;/p&gt;
  &lt;ul&gt;
    &lt;li&gt;
      &lt;a title=&#34;Clean Coding in Swift – Type Inference&#34; href=&#34;http://www.andrewcbancroft.com/2014/08/12/clean-coding-in-swift-type-inference/&#34; target=&#34;_blank&#34;&gt;Clean Coding in Swift Type Inference&lt;/a&gt;
    &lt;/li&gt;
    &lt;li&gt;
      &lt;a title=&#34;Expanded Thoughts on Swift’s Type Inference&#34; href=&#34;http://www.andrewcbancroft.com/2014/08/20/expanded-thoughts-on-swifts-type-inference/&#34; target=&#34;_blank&#34;&gt;Expanded Thoughts on Swift&#39;s Type Inference&lt;/a&gt;
    &lt;/li&gt;
    &lt;li&gt;
      &lt;a title=&#34;Pick a Delegate… Any Delegate… On Clean View Controllers in Swift&#34; href=&#34;http://www.andrewcbancroft.com/2014/08/26/pick-a-delegate-clean-view-controllers-in-swift/&#34; target=&#34;_blank&#34;&gt;Pick a Delegate… Any Delegate… On Clean View Controllers in Swift&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;</description>
    </item>
    
    <item>
      <title>Fade In / Out Animations as Class Extensions in Swift</title>
      <link>https://www.andrewcbancroft.com/2014/07/27/fade-in-out-animations-as-class-extensions-with-swift/</link>
      <pubDate>Sun, 27 Jul 2014 20:13:53 +0000</pubDate>
      
      <guid>https://www.andrewcbancroft.com/2014/07/27/fade-in-out-animations-as-class-extensions-with-swift/</guid>
      <description>&lt;p&gt;&lt;small&gt;Updated on December 6, 2016 – Xcode 8 &amp;amp; Swift 3.0&lt;/small&gt;&lt;/p&gt;
&lt;p&gt;The question has been &lt;a title=&#34;Fade In / Out - Stack Overflow&#34; href=&#34;http://stackoverflow.com/questions/20891614/fade-in-fade-out-animation&#34; target=&#34;_blank&#34;&gt;asked (and solved) on StackOverflow in Objective-C&lt;/a&gt;, but my aim in this post is to take the Objective-C implementation and leverage Swift &lt;em&gt;extensions&lt;/em&gt; to make this job even easier to achieve and reuse.&lt;/p&gt;
&lt;p&gt;Fade animations basically involve adjusting a &lt;code&gt;UIView&lt;/code&gt;‘s alpha value from 1.0 to 0.0 (fade out) or 0.0 to 1.0 (fade in) over a specified duration using some kind of easing option (like starting fast, then slowing down at the end of the animation, or starting slow and speeding up at the end of the animation).&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;ve &lt;a title=&#34;Swift Fade Animations - GitHub Project&#34; href=&#34;https://github.com/andrewcbancroft/SwiftFadeAnimations&#34; target=&#34;_blank&#34;&gt;published an example Xcode project to GitHub&lt;/a&gt; with the final working version of the code below if you&amp;rsquo;d like to just see it. Read on for the full explanation.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Edit: 2/23/2016&lt;/strong&gt; – A new idea flowing out of my &lt;a href=&#34;https://www.pluralsight.com/courses/cocoapods-xcode-project-dependencies&#34;&gt;Pluralsight Course&lt;/a&gt; involves a similar implementation, but using protocol extensions instead. This article&amp;rsquo;s implementation still works though, so feel free to check out either the contents of this blog entry, or the new one!&lt;/p&gt;
&lt;div class=&#34;resources&#34;&gt;
  &lt;div class=&#34;resources-header&#34;&gt;
    New article using protocol extensions
  &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;https://www.andrewcbancroft.com/2016/02/22/fade-views-inout-with-fadeable-a-swift-protocol-extension/&#34; title=&#34;Fade Views In/Out with Fadeable – A Swift Protocol Extension&#34;&gt;Fade Views In/Out with Fadeable – A Swift Protocol Extension&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;
&lt;p&gt;&lt;a name=&#34;fade-without-extension&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;fade-_without_-an-extension&#34;&gt;Fade &lt;em&gt;without&lt;/em&gt; an extension&lt;/h3&gt;
&lt;p&gt;Below is an example of how my view controller may look if I want to click a button and have it fade out a label, set the text, and fade it back in again:&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;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;ViewController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIViewController&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;kr&#34;&gt;@IBOutlet&lt;/span&gt; &lt;span class=&#34;kr&#34;&gt;weak&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;birdTypeLabel&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UILabel&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;kr&#34;&gt;override&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;viewDidLoad&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; 5&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;kc&#34;&gt;super&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;viewDidLoad&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;c1&#34;&gt;// Do any additional setup that your app requires&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&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;kr&#34;&gt;@IBAction&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;updateBirdTypeLabel&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;kc&#34;&gt;_&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;sender&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIButton&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;10&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;c1&#34;&gt;// Fade out to set the text&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 class=&#34;n&#34;&gt;UIView&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;animate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;withDuration&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;mf&#34;&gt;1.0&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;delay&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;mf&#34;&gt;0.0&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;options&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIViewAnimationOptions&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;curveEaseOut&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;animations&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;12&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;birdTypeLabel&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;alpha&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;mf&#34;&gt;0.0&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;p&#34;&gt;},&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;completion&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;14&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;                &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;finished&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;Bool&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;Void&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;in&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;//Once the label is completely invisible, set the text and fade it back in&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;kc&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;birdTypeLabel&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;text&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;Bird Type: Swift&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;18&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;19&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;                &lt;span class=&#34;c1&#34;&gt;// Fade in&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;n&#34;&gt;UIView&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;animate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;withDuration&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;mf&#34;&gt;1.0&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;delay&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;mf&#34;&gt;0.0&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;options&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIViewAnimationOptions&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;curveEaseIn&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;animations&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;21&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;birdTypeLabel&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;alpha&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;mf&#34;&gt;1.0&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;p&#34;&gt;},&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;completion&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;kc&#34;&gt;nil&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;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 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;What I don&amp;rsquo;t like about this implementation is that if I want to perform this same kind of animation again elsewhere in my app, I&amp;rsquo;ve got to write the bulk of that algorithm again each time I want to fade something in or out. I&amp;rsquo;d like it to be in one place for easier maintainability. I&amp;rsquo;d also like to be able to fade in / out simply by doing something like &lt;code&gt;self.birdTypeLabel.fadeIn()&lt;/code&gt; or &lt;code&gt;self.birdTypeLabel.fadeOut()&lt;/code&gt; &lt;em&gt;optionally&lt;/em&gt; setting parameters for duration, delay, and completion. With these goals in mind, let&amp;rsquo;s see what Swift extensions provide us in terms of simplifying the process.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;refactoring-using-swift-extensions&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;refactoring-using-swift-extensions&#34;&gt;Refactoring using Swift extensions&lt;/h3&gt;
&lt;p&gt;&lt;a name=&#34;create-uiviewextensions&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;step-1--create-uiviewextensionsswift&#34;&gt;Step 1 – Create UIViewExtensions.swift&lt;/h4&gt;
&lt;p&gt;Create a new Swift file and name it something like UIViewExtensions.swift&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;move-fadeout-fadein&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;step-2--move-fadeout-and-fadein-to-uiviewextensionsswift&#34;&gt;Step 2 – Move fadeOut and fadeIn to UIViewExtensions.swift&lt;/h4&gt;
&lt;p&gt;Use the previously-written &lt;code&gt;fadeOut()&lt;/code&gt; and `fadeIn() algorithms in the new UIViewExtensions.swift file.&lt;/p&gt;
&lt;p&gt;We can leverage what we wrote before with a few modifications. Take a look (I&amp;rsquo;ve written some comments to help identify some of the tweaks for the extension version):&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;import&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;Foundation&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;import&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;UIKit&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;kd&#34;&gt;extension&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;UIView&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; 5&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;fadeIn&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; 6&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;c1&#34;&gt;// Move our fade out code from earlier&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;n&#34;&gt;UIView&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;animate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;withDuration&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;mf&#34;&gt;1.0&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;delay&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;mf&#34;&gt;0.0&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;options&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIViewAnimationOptions&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;curveEaseIn&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;animations&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; 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;alpha&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;mf&#34;&gt;1.0&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;// Instead of a specific instance of, say, birdTypeLabel, we simply set [thisInstance] (ie, self)&amp;#39;s alpha&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 class=&#34;n&#34;&gt;completion&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;kc&#34;&gt;nil&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;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;fadeOut&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;13&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;UIView&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;animate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;withDuration&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;mf&#34;&gt;1.0&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;delay&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;mf&#34;&gt;0.0&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;options&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIViewAnimationOptions&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;curveEaseOut&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;animations&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;14&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;alpha&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;mf&#34;&gt;0.0&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 class=&#34;n&#34;&gt;completion&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;kc&#34;&gt;nil&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;16&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;17&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;With this extension in place, we can now call &lt;code&gt;self.birdTypeLabel.fadeIn()&lt;/code&gt; or &lt;code&gt;self.birdTypeLabel.fadeOut()&lt;/code&gt; . To gain a little more control (if I so choose), I can outfit the &lt;code&gt;fadeIn&lt;/code&gt; and &lt;code&gt;fadeOut&lt;/code&gt; extension functions with parameters with default values defined so that I can call them with or without parameters as I need.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;parameters-default-values&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;step-3--provide-parameters-with-default-values&#34;&gt;Step 3 – Provide parameters with default values&lt;/h4&gt;
&lt;p&gt;In Step 2, we simply hard-coded values for duration, delay, and completion. Below is the final version of the extension that provides parameters for you to (optionally) pass arguments to.&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;import&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;Foundation&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;import&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;UIKit&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;kd&#34;&gt;extension&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;UIView&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; 5&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;fadeIn&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;kc&#34;&gt;_&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;duration&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;TimeInterval&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;mf&#34;&gt;1.0&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;delay&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;TimeInterval&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;mf&#34;&gt;0.0&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;completion&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 class=&#34;n&#34;&gt;escaping&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;((&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;Bool&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;Void&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 class=&#34;p&#34;&gt;{(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;finished&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;Bool&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;Void&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;in&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; 6&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;UIView&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;animate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;withDuration&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;duration&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;delay&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;delay&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;options&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIViewAnimationOptions&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;curveEaseIn&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;animations&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;alpha&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;mf&#34;&gt;1.0&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 class=&#34;n&#34;&gt;completion&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;completion&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; 9&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;10&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;fadeOut&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;kc&#34;&gt;_&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;duration&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;TimeInterval&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;mf&#34;&gt;1.0&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;delay&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;TimeInterval&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;mf&#34;&gt;0.0&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;completion&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 class=&#34;n&#34;&gt;escaping&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;Bool&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;Void&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 class=&#34;n&#34;&gt;finished&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;Bool&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;Void&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;in&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;11&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;UIView&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;animate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;withDuration&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;duration&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;delay&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;delay&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;options&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIViewAnimationOptions&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;curveEaseIn&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;animations&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;12&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;alpha&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;mf&#34;&gt;0.0&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;p&#34;&gt;},&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;completion&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;completion&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;With this now in place, the final version of my view controller becomes much simpler and clean:&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;import&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;UIKit&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&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;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;ViewController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIViewController&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; 4&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kr&#34;&gt;@IBOutlet&lt;/span&gt; &lt;span class=&#34;kr&#34;&gt;weak&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;birdTypeLabel&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UILabel&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; 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;kr&#34;&gt;override&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;viewDidLoad&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;super&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;viewDidLoad&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;c1&#34;&gt;// Do any additional setup that your app requires&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;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&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 class=&#34;kr&#34;&gt;@IBAction&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;updateBirdTypeLabel&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;kc&#34;&gt;_&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;sender&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIButton&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;12&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;birdTypeLabel&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;fadeOut&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;completion&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;13&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;            &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;finished&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;Bool&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;Void&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;in&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;kc&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;birdTypeLabel&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;text&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;Bird Type: Swift&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;15&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;birdTypeLabel&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;fadeIn&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;16&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;17&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;18&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;By employing Swift extensions to encapsulate the fade in / out animation logic, I was able to&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Define the animation logic in one place for easy maintainability&lt;/li&gt;
&lt;li&gt;Make my view controller&amp;rsquo;s code simpler and clean&lt;/li&gt;
&lt;li&gt;Provide a more natural way to perform the animation on any UIView instance by simply calling fadeIn() or fadeOut()&lt;/li&gt;
&lt;li&gt;Give myself the option to specify a different duration, delay, or completion closure if I need extra control&lt;/li&gt;
&lt;/ul&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;https://www.andrewcbancroft.com/2016/02/22/fade-views-inout-with-fadeable-a-swift-protocol-extension/&#34; title=&#34;Fade Views In/Out with Fadeable – A Swift Protocol Extension&#34;&gt;Fade Views In/Out with Fadeable – A Swift Protocol Extension&lt;/a&gt;
    &lt;/li&gt;
    &lt;li&gt;
      &lt;i class=&#34;fa fa-angle-right&#34;&gt;&lt;/i&gt; &lt;a title=&#34;The 5 W’s of Swift Extensions&#34; href=&#34;http://www.andrewcbancroft.com/2014/11/03/the-5-ws-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 title=&#34;Slide In Animation in Swift&#34; href=&#34;http://www.andrewcbancroft.com/2014/09/24/slide-in-animation-in-swift/&#34; target=&#34;_blank&#34;&gt;Slide In Animation in Swift&lt;/a&gt;
    &lt;/li&gt;
    &lt;li&gt;
      &lt;i class=&#34;fa fa-angle-right&#34;&gt;&lt;/i&gt; &lt;a title=&#34;Rotate Animation in Swift&#34; href=&#34;http://www.andrewcbancroft.com/2014/10/15/rotate-animation-in-swift/&#34; target=&#34;_blank&#34;&gt;Rotate Animation in Swift&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>Swift Optionals?  Don’t Forget to Unwrap!</title>
      <link>https://www.andrewcbancroft.com/2014/07/25/swift-optionals-dont-forget-to-unwrap/</link>
      <pubDate>Sat, 26 Jul 2014 02:23:53 +0000</pubDate>
      
      <guid>https://www.andrewcbancroft.com/2014/07/25/swift-optionals-dont-forget-to-unwrap/</guid>
      <description>&lt;p&gt;There is a compiler error that throws me off every time I see it.  It takes the form,&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;‘ClassName?&amp;rsquo; does not have a member named ‘memberName&#39;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;This can happen when you&amp;rsquo;ve declared a variable as an optional, but forget to &lt;em&gt;unwrap&lt;/em&gt; that optional when you attempt to call a method on it.&lt;/p&gt;
&lt;p&gt;For example, given this class definition:&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;class&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&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;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;family&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;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;color&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; 4&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; 5&lt;/span&gt;&lt;span class=&#34;cl&#34;&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;family&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;color&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;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;kc&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;family&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;family&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;color&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;color&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&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;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;isSwift&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;Bool&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;11&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;kc&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;family&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;==&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;Apodidae&amp;#34;&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;?&lt;/span&gt; &lt;span class=&#34;kc&#34;&gt;true&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;kc&#34;&gt;false&lt;/span&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;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;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;If, say in a ViewController, I declare a variable that I intend to reference an &lt;em&gt;optional&lt;/em&gt; &lt;code&gt;Bird&lt;/code&gt; instance 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;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;birdInstance&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Bird&lt;/span&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;And then I later initialize this variable with a &lt;code&gt;Bird&lt;/code&gt; instance, perhaps in &lt;code&gt;viewDidLoad()&lt;/code&gt; :&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;n&#34;&gt;birdInstance&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Bird&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;family&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;Apodidae&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;color&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;Black&amp;#34;&lt;/span&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;If I attempt to invoke the &lt;code&gt;isSwift&lt;/code&gt; method on the &lt;code&gt;birdInstance&lt;/code&gt; later  on, I&amp;rsquo;ll get a compiler error:&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;bp&#34;&gt;println&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;birdInstance&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;isSwift&lt;/span&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;blockquote&gt;
&lt;p&gt;error: ‘Bird?&amp;rsquo; does not have a member named ‘isSwift&#39;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;This may seem pretty basic — after all, I declared the &lt;code&gt;birdInstance&lt;/code&gt; as an optional and I know optionals need special treatment.  How could I make this mistake??  Perhaps this is just a consequence of my current stage in life, trying to code in 15-30 minute spurts with my 1 1/2 year old running around, haha.&lt;/p&gt;
&lt;p&gt;We all deal with this though:  it&amp;rsquo;s fairly easy to write some code and come back to it later and not remember how you declared your variable in an earlier coding session.  Then when you&amp;rsquo;re presented a message saying that your class doesn&amp;rsquo;t have a member named &amp;ldquo;___”, you immediately go to the class definition and see the function there, plain as day.  It&amp;rsquo;s easy to spend 3-5 minutes scratching your head thinking, &amp;ldquo;What in the world??!” … And then you realize – it&amp;rsquo;s that &lt;em&gt;optional declaration&lt;/em&gt; that you forgot to handle.&lt;/p&gt;
&lt;p&gt;To fix this, of course, you can do any number of things, depending on your situation.&lt;/p&gt;
&lt;h5 id=&#34;force-unwrap-the-optional-and-invoke-the-method&#34;&gt;Force unwrap the optional and invoke the method:&lt;/h5&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;n&#34;&gt;birdInstance&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;!.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;isSwift&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;// Force unwrapped -- **CAUTION** make sure that birdInstance gets instantiated before you do this,&amp;amp;nbsp;or you&amp;#39;ll get a runtime error&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h5 id=&#34;employ-optional-chaining-and-invoke-the-method&#34;&gt;Employ optional chaining and invoke the method:&lt;/h5&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;n&#34;&gt;birdInstance&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;?.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;isSwift&lt;/span&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;h5 id=&#34;declare-the-variable-as-implicitly-unwrapped-optional-then-invoke-the-method-later-without-extra-exclamation-or-question-marks&#34;&gt;Declare the variable as implicitly unwrapped optional, then invoke the method later without extra exclamation or question marks:&lt;/h5&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;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;birdInstance&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Bird&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;!&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;// Implicitly unwrapped -- **CAUTION** make sure that birdInstance gets instantiated before you use it, or you&amp;#39;ll get a runtime error&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;n&#34;&gt;birdInstance&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Bird&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;family&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;Apodidae&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;color&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;Black&amp;#34;&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;// Some time later, invoke isSwift&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;bp&#34;&gt;println&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;birdInstance&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;isSwift&lt;/span&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;&lt;a title=&#34;CompileSwift - Optionals&#34; href=&#34;http://www.compileswift.com/intermediate/optionals/?utm_content=bufferfba01&amp;utm_medium=social&amp;utm_source=twitter.com&amp;utm_campaign=buffer&#34; target=&#34;_blank&#34;&gt;A blog post by Peter Witham over at CompileSwift&lt;/a&gt; was the article that caused me to think, &amp;ldquo;OH!  I haven&amp;rsquo;t done anything with my optional…&lt;em&gt;that&amp;rsquo;s&lt;/em&gt; the problem”.  Credit to you, sir, for your post!&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Swift Access Control – Implications for Unit Testing</title>
      <link>https://www.andrewcbancroft.com/2014/07/22/swift-access-control-implications-for-unit-testing/</link>
      <pubDate>Wed, 23 Jul 2014 04:04:43 +0000</pubDate>
      
      <guid>https://www.andrewcbancroft.com/2014/07/22/swift-access-control-implications-for-unit-testing/</guid>
      <description>&lt;p&gt;If you find yourself with broken unit tests, failing to build with the error, “Use of unresolved identifier…”, you&amp;rsquo;re not alone!&lt;/p&gt;
&lt;h3 id=&#34;unit-tests-and-swift-access-control&#34;&gt;Unit Tests and Swift Access Control&lt;/h3&gt;
&lt;p&gt;When Swift access control came into the picture, we suddenly had a little more to consider. From the Apple docs:&lt;/p&gt;
&lt;p&gt;Swift access control has three access levels:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;private entities can only be accessed from within the source file where they are defined.&lt;/li&gt;
&lt;li&gt;internal entities can be accessed anywhere within the target where they are defined.&lt;/li&gt;
&lt;li&gt;public entities can be accessed from anywhere within the target and from any other context&lt;br&gt;
that imports the current target’s module.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;By default, most entities in a source file have internal access.&lt;br&gt;
So given the following…&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Out of the box, your unit tests are part of a separate test target&lt;/li&gt;
&lt;li&gt;The default access control for a class is &lt;em&gt;internal&lt;/em&gt;, (meaning that if you do not explicitly specify an access control on the class / properties / functions, they&amp;rsquo;re marked internal behind the scenes)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;… we now know why the unit tests break, unless we make a few tweaks:  classes marked internal are only seen &lt;em&gt;within a set of specified targets&lt;/em&gt; and our unit tests are in a separate target that our class is not a part of by default.&lt;/p&gt;
&lt;h3 id=&#34;options&#34;&gt;Options&lt;/h3&gt;
&lt;p&gt;It seems to me that we have two options:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Change the access control on our class to &lt;em&gt;public&lt;/em&gt;.  Additionally, mark any methods we intend to test with &lt;em&gt;public&lt;/em&gt; also.&lt;/li&gt;
&lt;li&gt;Add the class(es) you want to be able to write unit tests for to the tests target.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&#34;solution&#34;&gt;Solution&lt;/h3&gt;
&lt;p&gt;I found option #2 to be the easiest to implement at first. &lt;em&gt;However&lt;/em&gt;, it turns out that this can lead to some &lt;a href=&#34;https://github.com/Quick/Quick/issues/91&#34;&gt;really obscure issues&lt;/a&gt;. An &lt;a href=&#34;https://twitter.com/modocache/status/549042409838219264&#34;&gt;enlightening Twitter conversation&lt;/a&gt; also shed some light on the subject, and pointed to the solution of testing only publicly accessible behavior that your Types expose, rather than trying to test internal implementation. That probably deserves a blog entry of its own, but for now, I’ll leave it to say that I’d recommend not adding your .swift source files to your test target, but rather to adjust the access control modifiers of the things you want to test to public (ie, Option # 1).&lt;/p&gt;
&lt;div class=&#34;related-posts&#34;&gt;
  You might also enjoy&lt;/p&gt; 
  &lt;ul&gt;
    &lt;li&gt;
      &lt;a href=&#34;http://www.andrewcbancroft.com/2014/12/10/dont-write-legacy-swift/&#34; title=&#34;Don’t Write Legacy Swift&#34;&gt;Don&#39;t Write Legacy Swift&lt;/a&gt;
    &lt;/li&gt;
    &lt;li&gt;
      &lt;a href=&#34;http://www.andrewcbancroft.com/2014/12/29/getting-started-unit-testing-swift/&#34; title=&#34;Getting Started with Unit Testing in Swift&#34;&gt;Getting Started with Unit Testing in Swift&lt;/a&gt;
    &lt;/li&gt;
    &lt;li&gt;
      &lt;a href=&#34;http://www.andrewcbancroft.com/2014/12/19/swift-unit-testing-resources/&#34; title=&#34;Swift Unit Testing Resources&#34;&gt;Swift Unit Testing Resources&lt;/a&gt;
    &lt;/li&gt;
    &lt;li&gt;
      &lt;a href=&#34;http://www.andrewcbancroft.com/2014/12/16/tdd-ios-swift-whats-goal/&#34; title=&#34;TDD for iOS in Swift – What’s the Goal?&#34;&gt;TDD for iOS in Swift – What’s the Goal?&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;
</description>
    </item>
    
    <item>
      <title>Implement NSManagedObject Subclass in Swift</title>
      <link>https://www.andrewcbancroft.com/2014/07/17/implement-nsmanagedobject-subclass-in-swift/</link>
      <pubDate>Fri, 18 Jul 2014 04:08:12 +0000</pubDate>
      
      <guid>https://www.andrewcbancroft.com/2014/07/17/implement-nsmanagedobject-subclass-in-swift/</guid>
      <description>&lt;p&gt;&lt;small&gt;Updated on July 8, 2016 – Xcode 7&lt;/small&gt;&lt;/p&gt;
&lt;p&gt;My goal with this blog entry is to help get you set up to create NSManagedObject subclasses in Swift for the Entities in your Core Data model.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;example&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;example&#34;&gt;Example&lt;/h1&gt;
&lt;p&gt;Let&amp;rsquo;s look at a fabricated example: Say that you&amp;rsquo;ve got a Core Data project and you&amp;rsquo;re creating Entities.  For my simple project, I&amp;rsquo;ll create an Entity called &amp;ldquo;MyEntity” with an attribute called &amp;ldquo;myAttribute”.&lt;/p&gt;
&lt;p&gt;After you create an NSManagedObject subclass for the Entity and come back to the data model screen to specify the &amp;ldquo;Class” in the inspector area, you &lt;em&gt;must&lt;/em&gt; prefix the name of the class with &amp;ldquo;YourProjectName.” (don&amp;rsquo;t forget the dot).  Forgetting to do this will lead to run-time errors when you start interacting with instances of your NSManagedObject subclass.&lt;/p&gt;
&lt;p&gt;&lt;a title=&#34;Documentation specifying module name prefix requirement&#34; href=&#34;https://developer.apple.com/library/prerelease/mac/documentation/Swift/Conceptual/BuildingCocoaApps/WritingSwiftClassesWithObjective-CBehavior.html&#34; target=&#34;_blank&#34;&gt;Apple specifies this in their documentation page&lt;/a&gt;, but it was a subtle mention at the end of the document and I just happened upon it as I was troubleshooting this:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;span style=&#34;color: #414141;&#34;&gt;Swift classes are namespaced—they’re scoped to the module (typically, the project) they are compiled in. To use a Swift subclass of the &lt;/span&gt;&lt;code&gt;NSManagedObject&lt;/code&gt;&lt;span style=&#34;color: #414141;&#34;&gt; class with your Core Data model, prefix the class name in the Class field in the model entity inspector with the name of your module.&lt;/span&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;a name=&#34;walkthrough&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;walk-through&#34;&gt;Walk-through&lt;/h1&gt;
&lt;p&gt;&lt;a name=&#34;create-entity&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;create-an-entity&#34;&gt;Create an Entity&lt;/h2&gt;
&lt;p&gt;In your .xcdatamodeld file, create an Entity to your liking.  In my example, I named the Entity &amp;ldquo;MyEntity” and I gave it an attribute called &amp;ldquo;myAttribute” with a data type of String.&lt;br&gt;
&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2014/07/NSManagedObjectSubclassExample.png&#34;&gt;&lt;img src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2014/07/NSManagedObjectSubclassExample-1024x783.png&#34; alt=&#34;Create Entity and Attribute&#34; width=&#34;730&#34; height=&#34;558&#34; class=&#34;alignnone size-large wp-image-1901&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2014/07/NSManagedObjectSubclassExample-1024x783.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2014/07/NSManagedObjectSubclassExample-300x229.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2014/07/NSManagedObjectSubclassExample.png 1202w&#34; sizes=&#34;(max-width: 730px) 100vw, 730px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;create-nsmanagedobject-subclass&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;create-an-nsmanagedobject-subclass-for-that-entity&#34;&gt;Create an NSManagedObject Subclass for that Entity&lt;/h2&gt;
&lt;p&gt;On the Menu, click Editor, then &amp;ldquo;Create NSManagedObject Subclass…”&lt;br&gt;
&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2014/07/Fullscreen_7_17_14__9_59_PM.png&#34;&gt;&lt;img src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2014/07/Fullscreen_7_17_14__9_59_PM-1024x700.png&#34; alt=&#34;Create NSManagedObject Subclass&#34; width=&#34;730&#34; height=&#34;499&#34; class=&#34;alignnone size-large wp-image-1961&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2014/07/Fullscreen_7_17_14__9_59_PM-1024x700.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2014/07/Fullscreen_7_17_14__9_59_PM-300x205.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2014/07/Fullscreen_7_17_14__9_59_PM.png 1524w&#34; sizes=&#34;(max-width: 730px) 100vw, 730px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Make sure you choose &amp;ldquo;Swift” as your language of choice as you click Next through the wizard and Xcode will generate you a file that is appropriate for your Entity.  The files it created for me (Xcode 7.1.1) look like this:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2014/07/NSManaged_1.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2014/07/NSManaged_1-1024x298.png&#34; alt=&#34;NSManagedObject Class file&#34; width=&#34;1024&#34; height=&#34;298&#34; class=&#34;alignnone size-large wp-image-12993&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2014/07/NSManaged_1-1024x298.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2014/07/NSManaged_1-300x87.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2014/07/NSManaged_1.png 1390w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2014/07/NSManaged_2.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2014/07/NSManaged_2-1024x274.png&#34; alt=&#34;NSManagedObject Properties file&#34; width=&#34;1024&#34; height=&#34;274&#34; class=&#34;alignnone size-large wp-image-12992&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2014/07/NSManaged_2-1024x274.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2014/07/NSManaged_2-300x80.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2014/07/NSManaged_2.png 1395w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;verify-class-module&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;verify-nsmanagedobject-class-in-the-data-model-inspector8221&#34;&gt;Verify NSManagedObject class in the &amp;ldquo;Data Model Inspector”&lt;/h2&gt;
&lt;p&gt;Make sure that you have your .xcdatamodeld file selected in the Navigator panel.  Then make sure your Utilities panel is visible.&lt;/p&gt;
&lt;p&gt;Click the &amp;ldquo;Data Model Inspector” icon.  This will be the last icon in the inspector of Xcode.  You should see a section for &amp;ldquo;Entity” and within this section, two textboxes:  one for Name and one for Class. You should also see a drop-down for the Module that the NSManagedObject subclass is found in.&lt;/p&gt;
&lt;p&gt;You&amp;rsquo;ll be verifying the &lt;em&gt;Class&lt;/em&gt; and the &lt;em&gt;Module&lt;/em&gt; values:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2014/07/NSManaged_verify_class.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2014/07/NSManaged_verify_class-1024x674.png&#34; alt=&#34;Verify class and module&#34; width=&#34;1024&#34; height=&#34;674&#34; class=&#34;alignnone size-large wp-image-12991&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2014/07/NSManaged_verify_class-1024x674.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2014/07/NSManaged_verify_class-300x197.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2014/07/NSManaged_verify_class.png 1393w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;According to the &lt;a title=&#34;Documentation specifying module name prefix requirement&#34; href=&#34;https://developer.apple.com/library/prerelease/mac/documentation/Swift/Conceptual/BuildingCocoaApps/WritingSwiftClassesWithObjective-CBehavior.html&#34; target=&#34;_blank&#34;&gt;Swift documentation&lt;/a&gt;, Swift class namespaces are scoped to the module they&amp;rsquo;re compiled in (usually the project you&amp;rsquo;re working in).  &lt;/p&gt;
&lt;p&gt;To use the NSManagedObject subclass in your project, you just need to verify that the Module setting is set to &amp;ldquo;Current Product Module”, assuming that the NSManagedObject subclass you&amp;rsquo;re wiring this Entity to is found in that module. If it&amp;rsquo;s in &lt;em&gt;another&lt;/em&gt; module, you&amp;rsquo;ll need to adjust the Module value in the inspector appropriately.&lt;/p&gt;
&lt;p&gt;Once the Class and Module values are verified (or set), you&amp;rsquo;ll be able to use this NSManagedObject subclass anywhere in your project.&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;https://www.andrewcbancroft.com/2015/02/18/core-data-cheat-sheet-for-swift-ios-developers/&#34; title=&#34;Core Data Cheat Sheet for Swift iOS Developers&#34;&lt;/a&gt;Core Data Cheat Sheet for Swift iOS Developers
    &lt;/li&gt;
    &lt;li&gt;
      &lt;i class=&#34;fa fa-angle-right&#34;&gt;&lt;/i&gt; &lt;a href=&#34;https://www.andrewcbancroft.com/2015/02/25/using-swift-to-seed-a-core-data-database/&#34; title=&#34;Using Swift to Seed a Core Data Database&#34;&lt;/a&gt;Using Swift to Seed a Core Data Database
    &lt;/li&gt;
    &lt;li&gt;
      &lt;i class=&#34;fa fa-angle-right&#34;&gt;&lt;/i&gt; &lt;a href=&#34;https://www.andrewcbancroft.com/2015/03/05/displaying-data-with-nsfetchedresultscontroller-and-swift/&#34; title=&#34;Displaying Data With NSFetchedResultsController and Swift&#34;&lt;/a&gt;Displaying Data With NSFetchedResultsController and Swift
    &lt;/li&gt;
    &lt;li&gt;
      &lt;i class=&#34;fa fa-angle-right&#34;&gt;&lt;/i&gt; &lt;a href=&#34;https://www.andrewcbancroft.com/2015/05/28/sync-table-view-data-nsfetchedresultscontroller-swift/&#34; title=&#34;Sync Table View Data: NSFetchedResultsController and Swift&#34;&lt;/a&gt;Sync Table View Data: NSFetchedResultsController and Swift
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;
&lt;p&gt;&lt;a name=&#34;course&#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;
    Resources
  &lt;/div&gt;
  &lt;ul class=&#34;resources-content&#34;&gt;
    &lt;li&gt;
      &lt;i class=&#34;fas fa-video&#34;&gt;&lt;/i&gt; &lt;a href=&#34;http://bit.ly/ps-core-data-swift&#34; target=&#34;_blank&#34;&gt;Core Data Fundamentals with Swift&lt;/a&gt;&lt;br /&gt; &lt;a href=&#34;http://bit.ly/ps-core-data-swift&#34; target=&#34;_blank&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2017/04/ps-core-data-fundamentals-swift-1024x576.png&#34; alt=&#34;Core Data Fundamentals with Swift&#34; width=&#34;1024&#34; height=&#34;576&#34; class=&#34;alignnone size-large wp-image-13163&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2017/04/ps-core-data-fundamentals-swift-1024x576.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2017/04/ps-core-data-fundamentals-swift-300x169.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2017/04/ps-core-data-fundamentals-swift-768x432.png 768w, https://www.andrewcbancroft.com/wp-content/uploads/2017/04/ps-core-data-fundamentals-swift.png 1539w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;&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>How to Create Mocks and Stubs in Swift</title>
      <link>https://www.andrewcbancroft.com/2014/07/15/how-to-create-mocks-and-stubs-in-swift/</link>
      <pubDate>Tue, 15 Jul 2014 05:12:16 +0000</pubDate>
      
      <guid>https://www.andrewcbancroft.com/2014/07/15/how-to-create-mocks-and-stubs-in-swift/</guid>
      <description>&lt;p&gt;Without 100% support for a mocking framework like &lt;a title=&#34;OCMock&#34; href=&#34;http://ocmock.org/&#34; target=&#34;_blank&#34;&gt;OCMock&lt;/a&gt;, I found myself needing to get creative when building mock objects and method stubs in Swift unit tests.  The great thing about testing is that you&amp;rsquo;re…well… &lt;em&gt;testing things out&lt;/em&gt; to see if they&amp;rsquo;ll work, and I found a solution that I&amp;rsquo;m pretty happy with for now.  I&amp;rsquo;m open to better ways, so leave a comment if you&amp;rsquo;ve had good results using a different design!&lt;/p&gt;
&lt;p&gt;The process is essentially this (example to follow):&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Ensure that the class that you would like to test is designed so that you can substitute your mock for the real one that&amp;rsquo;s used in your class&amp;rsquo; implementation&lt;/li&gt;
&lt;li&gt;Create an &lt;code&gt;XCTestCase&lt;/code&gt;  class with a test function in your unit test project&lt;/li&gt;
&lt;li&gt;Within the function body create a &lt;em&gt;nested&lt;/em&gt; class&lt;/li&gt;
&lt;li&gt;Make the nested class inherit from the real object you&amp;rsquo;re trying to mock / create a method stub for&lt;/li&gt;
&lt;li&gt;You can give the nested class a name such as Mock[ObjectName]&lt;/li&gt;
&lt;li&gt;Configure the mock object however you need by setting its properties or overriding its function implementations with stubbed implementations – no need to override every function… only the one(s) that your class calls during the test at hand&lt;/li&gt;
&lt;li&gt;Instantiate the class you&amp;rsquo;re testing and pass in an instance of the mock object you just nested in the test function to your class somehow (either through its initializer, by setting a property on the class, or by passing it into the method under test via parameter — however you intended to ‘inject&amp;rsquo; the mock from step 1 is what you should do)&lt;/li&gt;
&lt;li&gt;XCTAssert…&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Let&amp;rsquo;s see those 8 steps in action for those of us who are more visually inclined.&lt;/p&gt;
&lt;p&gt;EDIT:  July 22, 2014 – I&amp;rsquo;ve added a simple Xcode Project to GitHub for those interested in seeing the setup directly in Xcode at  &lt;a title=&#34;GitHub - MocksAndStubs&#34; href=&#34;https://github.com/andrewcbancroft/MocksAndStubs&#34; target=&#34;_blank&#34;&gt;&lt;a href=&#34;https://github.com/andrewcbancroft/MocksAndStubs&#34;&gt;https://github.com/andrewcbancroft/MocksAndStubs&lt;/a&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The scenario that I&amp;rsquo;d like to use a mock class in is this:  I have a CoreData application and I&amp;rsquo;d like to be able to mock the &lt;code&gt;NSManagedObjectContext&lt;/code&gt;  so that instead of making actual database fetch requests, I can just provide stubs of various sorts with the kinds of responses I&amp;rsquo;d expect from the real database calls to ensure my class will do the right thing based on predictable results.  To do this I begin at step 1…&lt;/p&gt;
&lt;h4 id=&#34;1-nbspensure-that-thenbspclass-that-you-would-like-to-test-is-designed-so-that-you-can-substitute-your-mock-for-the-real-one-thats-used-in-your-class-implementation&#34;&gt;1.  Ensure that the class that you would like to test is designed so that you can substitute your mock for the real one that&amp;rsquo;s used in your class&amp;rsquo; implementation&lt;/h4&gt;
&lt;p&gt;In the example class below, I intend to provide the &lt;code&gt;NSManagedObjectContext&lt;/code&gt;  dependency through the class&amp;rsquo; initializer which will set a property that is used by my class&amp;rsquo; methods later on, but you could easily use some other way of performing &amp;ldquo;dependency injection”.  The initializer strategy just makes it super clear in &lt;em&gt;my&lt;/em&gt; mind what the class&amp;rsquo; dependencies are, so that&amp;rsquo;s what I&amp;rsquo;m going to do here.  Have a look:&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;import&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;Foundation&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;import&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;CoreData&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;kd&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;MyClass&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; 5&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;context&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSManagedObjectContext&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;init&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;managedObjectContext&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSManagedObjectContext&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; 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;context&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;managedObjectContext&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;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;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Now, let&amp;rsquo;s say that my example class has a member function called &lt;code&gt;databaseHasRecordsForSomeEntity&lt;/code&gt;  that returns a &lt;code&gt;Bool&lt;/code&gt;  value of &lt;strong&gt;true&lt;/strong&gt; if the resulting array of a fetch request contains objects, and a &lt;code&gt;Bool&lt;/code&gt;  value of &lt;strong&gt;false&lt;/strong&gt; if the result array of a fetch request is empty.  The completed class looks 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;import&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;Foundation&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;import&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;CoreData&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;kd&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;MyClass&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; 5&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;context&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSManagedObjectContext&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;init&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;managedObjectContext&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSManagedObjectContext&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; 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;context&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;managedObjectContext&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;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&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 class=&#34;c1&#34;&gt;// If the array returned from executing a fetch request contains objects, return true; if empty, return false&lt;/span&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;databaseHasRecordsForSomeEntity&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;Bool&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;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;fetchRequest&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSFetchRequest&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;entityName&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;SomeEntity&amp;#34;&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;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;fetchRequestResults&lt;/span&gt; &lt;span class=&#34;p&#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;context&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;executeFetchRequest&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;fetchRequest&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;error&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;kc&#34;&gt;nil&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;// May want to do something with the error in real life...&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;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;fetchRequestResults&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;?.&lt;/span&gt;&lt;span class=&#34;bp&#34;&gt;count&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;0&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;16&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;17&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;I want to test if &lt;code&gt;databaseHasRecordsForSomeEntity&lt;/code&gt;  does what I intend it to do. So…&lt;/p&gt;
&lt;h4 id=&#34;2-nbspcreate-annbspxctestcasenbspnbspclass-with-a-test-function-in-your-unit-test-project&#34;&gt;2.  Create an &lt;code&gt;XCTestCase&lt;/code&gt;  class with a test function in your unit test project&lt;/h4&gt;
&lt;p&gt;Just listing this for completeness&lt;/p&gt;
&lt;p&gt;Next comes the way to make the mock.  Read steps 3-5 and then look below for a code example of what the skeleton will look like.&lt;/p&gt;
&lt;h4 id=&#34;3-nbspwithin-the-function-body-createnbspanbsp_nested_nbspclass&#34;&gt;3.  Within the function body create a &lt;em&gt;nested&lt;/em&gt; class&lt;/h4&gt;
&lt;h4 id=&#34;4-nbspmake-the-nested-class-inherit-from-the-real-object-youre-trying-to-mock--create-a-method-stub-for&#34;&gt;4.  Make the nested class inherit from the real object you&amp;rsquo;re trying to mock / create a method stub for&lt;/h4&gt;
&lt;h4 id=&#34;5-nbspyou-can-give-the-nested-class-a-name-such-as-mockobjectname&#34;&gt;5.  You can give the nested class a name such as Mock[ObjectName]&lt;/h4&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;import&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;UIKit&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;import&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;XCTest&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;import&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;CoreData&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;// &amp;lt;-- Make sure to import CoreData or you will get errors when you try to use NSManagedObjectContext&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&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;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;MyClassTests&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;XCTestCase&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&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;kr&#34;&gt;override&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;setUp&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; 8&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;kc&#34;&gt;super&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;setUp&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;c1&#34;&gt;// Put setup code here. This method is called before the invocation of each test method in the class.&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;kr&#34;&gt;override&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;tearDown&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;13&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;c1&#34;&gt;// Put teardown code here. This method is called after the invocation of each test method in the class.&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;kc&#34;&gt;super&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;tearDown&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;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;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&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;c1&#34;&gt;// Yay for verbose test names!  :]&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;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;testDatabaseHasRecordsForSomeEntityReturnsTrueWhenFetchRequestReturnsNonEmptyArray&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;19&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;kd&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;MockNSManagedObjectContext&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSManagedObjectContext&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&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;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;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;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;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h4 id=&#34;nbsp6-nbspconfigure-the-mock-object-however-you-need-by-setting-its-properties-or-overriding-its-function-implementations-with-stubbed-implementations--no-need-to-override-every-function8230-only-the-ones-that-your-class-calls-during-the-test-at-hand&#34;&gt; 6.  Configure the mock object however you need by setting its properties or overriding its function implementations with stubbed implementations – no need to override every function… only the one(s) that your class calls during the test at hand&lt;/h4&gt;
&lt;p&gt;For my example, I&amp;rsquo;m going to stub out the &lt;code&gt;executeFetchRequest&lt;/code&gt;  method so that it returns an array with one object in it.  This is really the part where you have to determine what you&amp;rsquo;re testing and what you expect the stubbed results to be.  Whatever you decide, the way to stub a method is simply to override it in the mock you&amp;rsquo;re implementing.  Here&amp;rsquo;s how I implemented the &lt;code&gt;executeFetchRequest&lt;/code&gt;  stub for my example:&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;// Yay for verbose test names!  :]&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;testDatabaseHasRecordsForSomeEntityReturnsTrueWhenFetchRequestReturnsNonEmptyArray&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;3&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;kd&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;MockNSManagedObjectContext&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSManagedObjectContext&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;4&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;            &lt;span class=&#34;kr&#34;&gt;override&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;executeFetchRequest&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;request&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSFetchRequest&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;error&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;NSErrorPointer&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;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;AnyObject&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;5&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;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;object 1&amp;#34;&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;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;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;We&amp;rsquo;re ready to perform the test and assert the results.  Read steps 7-8 and take a look at the code example below step 8:&lt;/p&gt;
&lt;h4 id=&#34;7-nbspinstantiate-the-class-youre-testing-and-pass-in-an-instance-of-thenbspmock-object-you-just-nested-in-the-test-function-to-yournbspclass-somehow-either-through-its-initializer-by-setting-a-property-on-the-class-or-by-passing-it-into-the-method-under-test-via-parameter-8212-however-you-intended-to-8216inject-the-mock-from-step-1-is-what-you-should-do&#34;&gt;7.  Instantiate the class you&amp;rsquo;re testing and pass in an instance of the mock object you just nested in the test function to your class somehow (either through its initializer, by setting a property on the class, or by passing it into the method under test via parameter — however you intended to ‘inject&amp;rsquo; the mock from step 1 is what you should do)&lt;/h4&gt;
&lt;h4 id=&#34;8-nbspxctassert8230&#34;&gt;8.  XCTAssert…&lt;/h4&gt;
&lt;p&gt;From step 1, I intended to pass an NSManagedObjectContext instance to the initializer of MyClass, so that&amp;rsquo;s what I&amp;rsquo;ll do in my test.  I&amp;rsquo;ll then perform the XCTAssert on the return value of my method under test:&lt;/p&gt;
&lt;p&gt; &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;// Yay for verbose test names!  :]&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;testDatabaseHasRecordsForSomeEntityReturnsTrueWhenFetchRequestReturnsNonEmptyArray&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; 3&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;kd&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;MockNSManagedObjectContext&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSManagedObjectContext&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; 4&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;            &lt;span class=&#34;kr&#34;&gt;override&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;executeFetchRequest&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;request&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSFetchRequest&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;error&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;NSErrorPointer&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;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;AnyObject&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; 5&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;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;object 1&amp;#34;&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;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;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&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;c1&#34;&gt;// Instantiate mock&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;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;mockContext&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;MockNSManagedObjectContext&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;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;c1&#34;&gt;// Instantiate class under test and pass it the mockContext object&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;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;myClassInstance&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;MyClass&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;managedObjectContext&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;mockContext&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&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;c1&#34;&gt;// Call the method under test and store its return value for XCTAssert&lt;/span&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;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;returnValue&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;myClassInstance&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;databaseHasRecordsForSomeEntity&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;17&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;18&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;XCTAssertTrue&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;returnValue&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;==&lt;/span&gt; &lt;span class=&#34;kc&#34;&gt;true&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;The return value should be been true&amp;#34;&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;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Running the tests at this point should produce a passing test using the mock object in place of a real NSManagedObjectContext that calls a database!&lt;/p&gt;
&lt;p&gt;Now, if I wanted to test the &amp;ldquo;false” branch of my class&amp;rsquo; method, I could simply create another test method following the same steps, only this time, I&amp;rsquo;d provide a new implementation for the overridden &lt;code&gt;executeFetchRequest&lt;/code&gt;  method that&amp;rsquo;s appropriate:&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;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;testDatabaseHasRecordsForSomeEntityReturnsFalseWhenFetchRequestReturnsEMPTYArray&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; 2&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;kd&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;MockNSManagedObjectContext&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSManagedObjectContext&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;kr&#34;&gt;override&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;executeFetchRequest&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;request&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSFetchRequest&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;error&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;NSErrorPointer&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;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;AnyObject&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; 4&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;p&#34;&gt;[]&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;// Provided a different stub implementation to test the &amp;#34;false&amp;#34; branch of my method under test&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 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&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;c1&#34;&gt;// Instantiate mock&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;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;mockContext&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;MockNSManagedObjectContext&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;10&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;11&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;c1&#34;&gt;// Instantiate class under test&lt;/span&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;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;myClassInstance&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;MyClass&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;managedObjectContext&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;mockContext&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&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;c1&#34;&gt;// Call the method under test and store its return value for XCTAssert&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;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;returnValue&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;myClassInstance&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;databaseHasRecordsForSomeEntity&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;16&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;17&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;XCTAssertTrue&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;returnValue&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;==&lt;/span&gt; &lt;span class=&#34;kc&#34;&gt;false&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;The return value should be been false&amp;#34;&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;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;And that&amp;rsquo;s a wrap – happy mocking and stubbing in Swift!&lt;/p&gt;
&lt;p&gt;EDIT:  July 22, 2014 – I&amp;rsquo;ve added a simple Xcode Project to GitHub for those interested in seeing the setup directly in Xcode at  &lt;a title=&#34;GitHub - MocksAndStubs&#34; href=&#34;https://github.com/andrewcbancroft/MocksAndStubs&#34; target=&#34;_blank&#34;&gt;&lt;a href=&#34;https://github.com/andrewcbancroft/MocksAndStubs&#34;&gt;https://github.com/andrewcbancroft/MocksAndStubs&lt;/a&gt;&lt;/a&gt;&lt;/p&gt;
&lt;div class=&#34;related-posts&#34;&gt;
  You might also enjoy&lt;/p&gt; 
  &lt;ul&gt;
    &lt;li&gt;
      &lt;a href=&#34;http://www.andrewcbancroft.com/2014/12/10/dont-write-legacy-swift/&#34; title=&#34;Don’t Write Legacy Swift&#34;&gt;Don&#39;t Write Legacy Swift&lt;/a&gt;
    &lt;/li&gt;
    &lt;li&gt;
      &lt;a href=&#34;http://www.andrewcbancroft.com/2014/07/22/swift-access-control-implications-for-unit-testing/&#34; title=&#34;Swift Access Control – Implications for Unit Testing&#34;&gt;Swift Access Control – Implications for Unit Testing&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;</description>
    </item>
    
    <item>
      <title>iOS KeychainItemWrapper – Obstacles Overcome, Inconsistencies Resolved</title>
      <link>https://www.andrewcbancroft.com/2014/05/21/ios-keychainitemwrapper-obstacles-overcome-inconsistencies-resolved/</link>
      <pubDate>Wed, 21 May 2014 18:20:56 +0000</pubDate>
      
      <guid>https://www.andrewcbancroft.com/2014/05/21/ios-keychainitemwrapper-obstacles-overcome-inconsistencies-resolved/</guid>
      <description>&lt;p&gt;I had a major case of &amp;ldquo;struggleface” when trying to work with the &lt;span class=&#34;lang:default decode:true  crayon-inline&#34;&gt;KeychainItemWrapper&lt;/span&gt;  for iOS.  I wanted it to be simple – just take a username and password and put it in the iOS Keychain for secure retrieval later.  Turns out that I had to spend an entire work day and do a lot of Googling to get it working so I&amp;rsquo;m posting this to try and bring all the pieces together.&lt;/p&gt;
&lt;h4 id=&#34;obstacle-1-automatic-reference-counting-arc&#34;&gt;Obstacle #1:  Automatic Reference Counting (ARC)&lt;/h4&gt;
&lt;p&gt;My project uses ARC for its memory management.  As it turns out, the &lt;span class=&#34;lang:default decode:true  crayon-inline&#34;&gt;KeychainItemWrapper&lt;/span&gt;  class from Apple does not support ARC out of the box.  Step one for me should have been to find an ARC-friendly version of the class, but I had no idea until I got in there.  I attempted to retrofit the one provided by Apple, but there are a few nuances with toll-free bridging that I don&amp;rsquo;t fully understand, so I went in search of someone who&amp;rsquo;s already invented the wheel.  There&amp;rsquo;s a GitHub Gist repository that provided exactly what I needed:  &lt;a title=&#34;KeychainItemWrapper ARCified&#34; href=&#34;https://gist.github.com/dhoerl/1170641&#34; target=&#34;_blank&#34;&gt;KeychainItemWrapper ARCified&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;obstacle-2-error-message-obscurity&#34;&gt;Obstacle #2:  Error Message Obscurity&lt;/h4&gt;
&lt;p&gt;Somehow in my novice experimentation with storing credentials in the keychain I ended up getting a &lt;span class=&#34;lang:default decode:true  crayon-inline &#34;&gt;keychainItem&lt;/span&gt;  on the keychain for a particular test user that couldn&amp;rsquo;t be updated or removed.  I kept getting a cryptic error code (–25299) as I debugged the app when attempting to sign in with that user&amp;rsquo;s credentials.  I spent…well…too long trying to find what the keychain error codes meant.&lt;/p&gt;
&lt;p&gt;While I started off reviewing the &lt;a title=&#34;Keychain Services Developer Documentation&#34; href=&#34;https://developer.apple.com/library/ios/documentation/Security/Reference/keychainservices/Reference/reference.html&#34; target=&#34;_blank&#34;&gt;Keychain Services developer documentation&lt;/a&gt; page, I didn&amp;rsquo;t see the section describing the result codes.  &lt;a title=&#34;Keychain Services Result Codes Section&#34; href=&#34;https://developer.apple.com/library/ios/documentation/Security/Reference/keychainservices/Reference/reference.html#jumpTo_124&#34; target=&#34;_blank&#34;&gt;This is the section&lt;/a&gt; that was most helpful in determining what &amp;ldquo;-25299” meant:  &lt;span class=&#34;lang:default decode:true  crayon-inline &#34;&gt;errSecDuplicateItem&lt;/span&gt;  – The item already exists.  In my case, the item was in there but the keychain search method didn&amp;rsquo;t find it, so it tried to add it again, causing a &amp;ldquo;duplicate keychain item” error message.&lt;/p&gt;
&lt;h4 id=&#34;inconsistent-behavior&#34;&gt;Inconsistent Behavior&lt;/h4&gt;
&lt;p&gt;Eventually I got things to store to the keychain fine for &lt;em&gt;some&lt;/em&gt; test user accounts but my primary test account (the one I started with at the beginning of the day) still ran into the &amp;ldquo;duplicate keychain item” error no matter what I tried.  Signing out of my app didn&amp;rsquo;t work.  Sending a &lt;span class=&#34;lang:default decode:true  crayon-inline &#34;&gt;resetKeychainItem&lt;/span&gt;  message to my &lt;span class=&#34;lang:default decode:true  crayon-inline &#34;&gt;KeychainItemWrapper&lt;/span&gt;  instance didn&amp;rsquo;t work.  Even deleting the app from the simulator didn&amp;rsquo;t work.  I ended up just needing to reset my simulator completely.  This was simple to do:&lt;/p&gt;
&lt;p&gt;iOS Simulator -&amp;gt; Reset Content and Settings…&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2014/05/Screen-Shot-2014-05-21-at-12.59.48-PM.png&#34;&gt;&lt;img class=&#34;alignnone wp-image-1051 size-full&#34; src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2014/05/Screen-Shot-2014-05-21-at-12.59.48-PM.png&#34; alt=&#34;Reset iOS Simulator&#39;s Content and Settings&#34; width=&#34;295&#34; height=&#34;208&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Things began to work perfectly after the reset.  Unfortunately, I spent the majority of the day spinning over the error message trying to find ways to programmatically either remove the &lt;span class=&#34;lang:default decode:true  crayon-inline &#34;&gt;keychainItem&lt;/span&gt;  or reset it so that it would no longer be a duplicate.  I&amp;rsquo;d have been finished hours earlier if I would have just reset the simulator.  In fact, &lt;em&gt;since&lt;/em&gt; I reset the simulator, I&amp;rsquo;ve been unable to reproduce the scenario I&amp;rsquo;d gotten myself into.  Otherwise I would list the obstacles I was trying to work through for the rest of the day as well.&lt;/p&gt;
&lt;p&gt;As far as I can tell, it was all stemming from whatever I did at the beginning of the day to get the malformed &lt;span class=&#34;lang:default decode:true  crayon-inline &#34;&gt;keychainItem&lt;/span&gt;  in the keychain.  The simulator reset did the trick.&lt;/p&gt;
&lt;p&gt;Commit.&lt;br&gt;
Breathe.&lt;br&gt;
Go home happy.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>How Old is Silas Allen?</title>
      <link>https://www.andrewcbancroft.com/2013/03/31/how-old-is-silas-allen/</link>
      <pubDate>Sun, 31 Mar 2013 14:36:07 +0000</pubDate>
      
      <guid>https://www.andrewcbancroft.com/2013/03/31/how-old-is-silas-allen/</guid>
      <description>&lt;p&gt;&amp;ldquo;Ooooohh, now… how old is he??”  I love this question.  Really!  I do!  Every time I&amp;rsquo;m asked I get to talk about my kid, and that&amp;rsquo;s always fun.  But today, things just got even &lt;em&gt;more&lt;/em&gt; fun.  Now, instead of just telling people his approximate age, I can &lt;strong&gt;show&lt;/strong&gt; them his &lt;em&gt;precise&lt;/em&gt; age down to the second!&lt;/p&gt;
&lt;p&gt;As some of you know, I&amp;rsquo;ve been nerding out on learning to build iOS apps for iPhone and iPad.  Well, to celebrate Silas turning another month old, I built a little app that tells me how many years, months, weeks, days, hours, minutes, and seconds he is old.  I also built in functionality so that I can see how much longer it will be until his birthday (actually, I think I just thought of a bug with that part, but it won&amp;rsquo;t show itself until 2014 so I have time…it shouldn&amp;rsquo;t be nearly as bad as the Y2K panic – but date math is non-trivial, just so you know). _&lt;br&gt;
_&lt;/p&gt;
&lt;p&gt;And yes, for those of you who are just as OCD as I am, I did consider the fact that he was born at 12:06 AM (sorry…they didn&amp;rsquo;t give us the exact millisecond of his debut or I&amp;rsquo;d have that in there too).&lt;/p&gt;
&lt;p&gt;Check out this video of the app showing his age as he turned 3 months old this morning!&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://vimeo.com/63031780&#34;&gt;Silas Allen Turns 3 Months Old!&lt;/a&gt; from &lt;a href=&#34;http://vimeo.com/user8393753&#34;&gt;Andrew Bancroft&lt;/a&gt; on &lt;a href=&#34;http://vimeo.com&#34;&gt;Vimeo&lt;/a&gt;.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Journey in iOS Development Series – In the Beginning…</title>
      <link>https://www.andrewcbancroft.com/2013/03/16/learning-ios-development/</link>
      <pubDate>Sat, 16 Mar 2013 19:51:59 +0000</pubDate>
      
      <guid>https://www.andrewcbancroft.com/2013/03/16/learning-ios-development/</guid>
      <description>&lt;p&gt;Today I&amp;rsquo;m starting a series related to my journey in learning to develop iOS apps. As lightbulbs come on for me, I plan to write up what I&amp;rsquo;m learning, if nothing else for my own reference. But I do hope my readers enjoy the discoveries I make along the way.&lt;/p&gt;
&lt;p&gt;These first few blogs will be somewhat autobiographical, so hopefully I don&amp;rsquo;t bore you to death. I&amp;rsquo;ll try and make them quick. More technical stuff will appear very soon. I thought to write a short article about how I got started. If I had an &amp;ldquo;In the beginning…” moment, what would that be?&lt;/p&gt;
&lt;h2 id=&#34;the-early-days&#34;&gt;The Early Days&lt;/h2&gt;
&lt;p&gt;It seems like just yesterday I was a little boy visiting my Grandpa&amp;rsquo;s house in Lawton, OK, eager to play on Grandpa&amp;rsquo;s computer. Computers in the early 90&amp;rsquo;s were still not quite the commodity they are in 2013. We didn&amp;rsquo;t own one yet, but &lt;em&gt;Grandpa&lt;/em&gt; did.&lt;/p&gt;
&lt;p&gt;If I had to point to an &amp;ldquo;In the beginning…” stage, it&amp;rsquo;d be the times when Grandpa sat down with me and taught me about computers. Not just games (although I did enjoy Submarine and Wheel of Fortune for DOS a &lt;em&gt;lot&lt;/em&gt;) – he taught me the basics of how to instruct a computer.  He taught me to program. He was a Master of DOS and a QBasic Guru. If I had to guess, he still prefers elements of both today. :]&lt;/p&gt;
&lt;p&gt;He is, quite simply, an amazing man. He is now a retired entrepreneur, but when he was running his music store there were always those &amp;ldquo;things that needed to be done”.  He viewed the &amp;ldquo;gotta do it&amp;rsquo;s” as an opportunity to automate by programming a computer to do it &lt;em&gt;for&lt;/em&gt; __him, rather than do it by hand. He&amp;rsquo;s showed me how he programmed apps to produce his financial statements, track his budget, print invoices, and all kinds of other &amp;ldquo;by hand” stuff that he was able to make &amp;ldquo;automagically” with a computer. It was (and still is) fascinating to me.&lt;/p&gt;
&lt;p&gt;In addition to those early years of influence, I&amp;rsquo;ve learned from Grandpa that even though &amp;ldquo;there&amp;rsquo;s an app for that” already, it doesn&amp;rsquo;t mean it&amp;rsquo;ll work for you how you want it to work. Grandpa could be happy with 99% of an app&amp;rsquo;s functionality, but there&amp;rsquo;d always be that &lt;em&gt;one thing&lt;/em&gt; that bugged him. What did he do? He wrote his own. He made it work how &lt;em&gt;he&lt;/em&gt; wanted it to work. He may not have been inventing a new wheel every time, but because he could program, he could improve the existing wheel to his liking.&lt;/p&gt;
&lt;p&gt;All of this has served to motivate me in my pursuit of programming in general.  I love my Grandpa.  My Grandpa loves computers.  The little kid inside of me is yelling, &amp;ldquo;Me too!  Me too!” to this day.&lt;/p&gt;
&lt;p&gt;My Grandpa just turned 81 and he still loves the computing world.  It&amp;rsquo;s been great fun to watch him use his latest computer: his iPhone.  It&amp;rsquo;s been even &lt;em&gt;more&lt;/em&gt; fun to show him the little apps I&amp;rsquo;m making as I learn to program for iOS.&lt;/p&gt;
&lt;p&gt;Next up will be a post on how I&amp;rsquo;ve approached learning to program for iOS.  What&amp;rsquo;s my strategy and how is it going so far?  See you again soon with all that and more.&lt;/p&gt;
</description>
    </item>
    
  </channel>
</rss>