<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Andrew Bancroft</title>
    <link>https://www.andrewcbancroft.com/</link>
    <description>Recent content about iOS development with Swift  from Andrew Bancroft.</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-us</language>
    <lastBuildDate>Wed, 22 Jul 2026 09:00:00 -0500</lastBuildDate>
    
        <atom:link href="https://www.andrewcbancroft.com/index.xml" rel="self" type="application/rss+xml" />
    
    
    
    <item>
      <title>How I Learned to Copy and Paste, and Why I Never Do</title>
      <link>https://www.andrewcbancroft.com/blog/ai/how-i-learned-to-copy-and-paste/</link>
      <pubDate>Wed, 22 Jul 2026 09:00:00 -0500</pubDate>
      <guid>https://www.andrewcbancroft.com/blog/ai/how-i-learned-to-copy-and-paste/</guid>
      <description>&lt;p&gt;It was the mid 1990s and I was about thirteen. These were the days of Netscape Navigator and internet connections that made sound (if you know, you know) and delivered web pages slower than molasses flowin&amp;rsquo; up hill on a cold winter&amp;rsquo;s day. So&amp;hellip; the pages were simple. And I had a Star Wars fan page I loved. I would look at it and think, &amp;ldquo;I want to know how to make something like that.&amp;rdquo;&lt;/p&gt;
&lt;p&gt;I had a friend who was, like me, a tad bit on the nerdy side. One day the two of us decided we wanted to learn how to build a web page. He had already figured out &lt;em&gt;the secret&lt;/em&gt;:&lt;/p&gt;
&lt;p&gt;You can go to any web page, right-click it, and &lt;strong&gt;view the source&lt;/strong&gt;. 🤯&lt;/p&gt;
&lt;p&gt;I could not believe that was allowed. But I was in tinker nerd heaven. I could go to &lt;em&gt;any web page&lt;/em&gt; on the &lt;em&gt;entire Internet&lt;/em&gt; and view its source to see what was under the hood.&lt;/p&gt;
&lt;p&gt;So&amp;hellip;what do you think I did? &lt;em&gt;Nailed it.&lt;/em&gt; I went straight to my favorite Star Wars page, right-clicked, View(ed) Source, and &lt;strong&gt;behold&lt;/strong&gt;. There it was in all its HTML glory. Every piece of what made that page &lt;em&gt;that page&lt;/em&gt;, sitting right out in the open before my eyes.&lt;/p&gt;
&lt;p&gt;My next thought was, &amp;ldquo;I&amp;rsquo;m going to remake this. And then I&amp;rsquo;m going to make it &lt;em&gt;mine&lt;/em&gt;.&amp;rdquo;&lt;/p&gt;
&lt;p&gt;So here&amp;rsquo;s what I did: I put the source code on one side of my screen. I opened up plain old Notepad on the other. And I started typing. Exactly what I saw. Every. Single. Character.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Angle bracket, HTML, close angle bracket. Angle bracket, body, close angle bracket.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;And on, and on, until I had typed out by hand every single line of markup that made up that page.&lt;/p&gt;
&lt;p&gt;Most people would consider that painstaking, but I was on a dopamine high. I&amp;rsquo;d type a few lines, save my HTML file, and refresh my browser to see the changes I&amp;rsquo;d just made. That immediate feedback loop was exactly the hit my thirteen-year-old brain was chasing.&lt;/p&gt;
&lt;p&gt;Fast forward.&lt;/p&gt;
&lt;p&gt;A few days later my friend and I got together again. I showed him the page I had built, and I walked him through how I did it. &amp;ldquo;You right-click, you view source, and then you just&amp;hellip; type it all into a new Notepad file with a .html extension,&amp;rdquo; I remember saying with enthusiasm!&lt;/p&gt;
&lt;p&gt;He looked at me. &amp;ldquo;Bro. Why didn&amp;rsquo;t you just&amp;hellip; &lt;em&gt;copy and paste&lt;/em&gt;?&amp;rdquo;&lt;/p&gt;
&lt;p&gt;And I said, in a true &amp;ldquo;bless your heart&amp;rdquo; moment, &amp;ldquo;What&amp;rsquo;s&amp;hellip; copy and paste? 👀&amp;rdquo;&lt;/p&gt;
&lt;p&gt;He looked at me like I was a complete idiot. But I wasn&amp;rsquo;t playing dumb.&lt;/p&gt;
&lt;p&gt;So&amp;hellip;kind friend as he was, he showed me. &amp;ldquo;You select the text you want, Control-C. You put your cursor where you want a copy of that text to go and press Control-V. Because obviously, &amp;lsquo;V&amp;rsquo; is for paste.&amp;rdquo;&lt;/p&gt;
&lt;p&gt;Control-C, Control-V. So easy!&lt;/p&gt;
&lt;p&gt;In a millisecond, my friend re-created the web page it had taken me &lt;em&gt;days&lt;/em&gt; to hand type.&lt;/p&gt;
&lt;p&gt;But here is what I walked away with that he didn&amp;rsquo;t.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;I knew how to build the thing.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;He could replicate it. I could &lt;em&gt;extend&lt;/em&gt; it.&lt;/p&gt;
&lt;p&gt;I could take the page apart and put it back together and evolve my &lt;em&gt;own&lt;/em&gt; from it, because every single line had passed through my fingertips on the way to the screen.&lt;/p&gt;
&lt;p&gt;I know how this sounds. The kid who hand-typed a webpage because he didn&amp;rsquo;t know &lt;code&gt;Ctrl+C&lt;/code&gt; is now going to tell you there&amp;rsquo;s something to doing it the slow way. Fair enough. But it stuck!&lt;/p&gt;
&lt;p&gt;And by &amp;ldquo;it stuck&amp;rdquo; I mean, &lt;strong&gt;this became my method&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;As a technology practitioner, of course I (used to) search the Internet. Not too long ago, I&amp;rsquo;d find help on Stack Overflow or someone&amp;rsquo;s blog or in a forum post. Would I copy and paste what I found? Honestly, &lt;em&gt;sometimes&lt;/em&gt;. Sorry&amp;hellip; the title was a lie. BUT. Here&amp;rsquo;s the truth: when I actually want to understand something&amp;hellip; when I want it to live in my own head and stick around, &lt;code&gt;Ctrl+C&lt;/code&gt; is a forbidden key combination for me.&lt;/p&gt;
&lt;p&gt;And now, all these years later, the most tempting copy-and-paste machine anyone has ever built is built into my IDEs and running in all my terminals.&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;m no longer writing a lot of code &amp;ldquo;by hand&amp;rdquo;. I&amp;rsquo;m describing goals, intentions, and motivations. I&amp;rsquo;m creating plans so that I can let a swarm of AI agents tackle the implementation.&lt;/p&gt;
&lt;p&gt;Whenever I hand a problem to an AI, the AI hands me back way more than a line of copy-pasteable code. In many cases, my AI hands me the &lt;strong&gt;whole thing&lt;/strong&gt;, not just snippets.&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;m not here to wag my finger at the robots or those of us who now rely on them. I&amp;rsquo;m also not going to pretend that the fast path isn&amp;rsquo;t sitting right there in front of us. So I let my AIs generate. Truly, I do.&lt;/p&gt;
&lt;p&gt;And then I do what I&amp;rsquo;ve always done: I slow down, and I pull up some of the generated code on one side of my screen, and an empty editor on the other. And just like I typed out that Star Wars page in Notepad, I hand-type what I see, asking at every line, &amp;ldquo;Do I understand why this one is here? Could I have written it myself?&amp;rdquo; And if my honest answer is, &amp;ldquo;No,&amp;rdquo; I pause and ask my AI Friend to be my tutor and have them explain until I get it.&lt;/p&gt;
&lt;p&gt;So while AI can build an entire solution to my problem in minutes, I&amp;rsquo;m still in the business of trying to become someone who could have built it on my own.&lt;/p&gt;
&lt;p&gt;And for me that means copy and paste (and &amp;ldquo;gen it and forget it&amp;rdquo;) are forbidden.&lt;/p&gt;
&lt;p&gt;When I want to understand, it&amp;rsquo;s &amp;ldquo;by hand&amp;rdquo;.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Download macOS Beta Installer</title>
      <link>https://www.andrewcbancroft.com/blog/musings/download-macos-beta-installer/</link>
      <pubDate>Sun, 16 Jun 2024 00:00:00 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/blog/musings/download-macos-beta-installer/</guid>
      <description>&lt;p&gt;When you log into the Apple Developer portal, you can easily download a restore image (ipsw file) for beta versions of macOS, but it’s not helpful if you want to install macOS on a separate volume on your existing Mac.&lt;/p&gt;
&lt;p&gt;The actual macOS installer is much more useful. The only issue is that it’s not exactly intuitive to download.&lt;/p&gt;
&lt;p&gt;Here&amp;rsquo;s how to do it:&lt;/p&gt;
&lt;h2 id=&#34;step-1-turn-on-beta-updates&#34;&gt;Step 1: Turn on Beta Updates&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Open the Settings app on your mac&lt;/li&gt;
&lt;li&gt;Click General&lt;/li&gt;
&lt;li&gt;Click Software Update&lt;/li&gt;
&lt;li&gt;Turn on Beta Updates for macOS Sequoia&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;step-2-use-terminal-to-download-the-installer&#34;&gt;Step 2: Use Terminal to download the installer&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Open a terminal window&lt;/li&gt;
&lt;li&gt;Run &lt;code&gt;softwareupdate --download --fetch-full-installer --full-installer-version 15.0&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;When you run that command, you&amp;rsquo;ll see a message in the console:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Scanning for 15.0 installer&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;If you skipped Step 1 and didn&amp;rsquo;t already opt in to receiving beta updates from Apple, you may get an error message:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Install failed with error: Update not found&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;If you&amp;rsquo;ve got Beta updates turned on though, you should see the softwareupdate utility begin to download the installer.&lt;/p&gt;
&lt;p&gt;Don&amp;rsquo;t worry if you see the words &amp;ldquo;Installing xx%&amp;rdquo;. It&amp;rsquo;s not actually going to update your primary operating system.
It&amp;rsquo;s actually just downloading the installer to your Applications folder.&lt;/p&gt;
&lt;h2 id=&#34;step-3-find-the-installer-in-your-applications-folder&#34;&gt;Step 3: Find the installer in your Applications folder&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Open Finder and navigate to your Applications folder&lt;/li&gt;
&lt;li&gt;You should be able to find the &lt;code&gt;Install macOS 15 beta&lt;/code&gt; application if the download was successful&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Once you’ve got the installer, you can install macOS on a separate volume on the same Mac that you’re running the currently-released version of macOS!&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>SwiftUI Fundamentals</title>
      <link>https://www.andrewcbancroft.com/pluralsight/2024-04-08-swiftui-fundamentals/</link>
      <pubDate>Mon, 08 Apr 2024 00:00:00 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/pluralsight/2024-04-08-swiftui-fundamentals/</guid>
      <description>&lt;p&gt;Developing for iOS is a valuable and in-demand skill, but if you haven&amp;rsquo;t developed in the Apple world before, it can be surprisingly different.&lt;/p&gt;
&lt;p&gt;In this course, SwiftUI Fundamentals, you&amp;rsquo;ll quickly get up and running using Xcode 15, Swift, and SwiftUI to establish the foundational skills you need to build great user interfaces for iOS.&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;https://bit.ly/swiftui-fundamentals&#34; target=&#34;_blank&#34;&gt;SwiftUI Fundamentals&lt;/a&gt;&lt;a href=&#34;https://bit.ly/swiftui-fundamentals&#34; target=&#34;_blank&#34;&gt;&lt;br /&gt; &lt;img src=&#34;https://www.andrewcbancroft.com/images/social-assets/swiftui-fundamentals-title.png&#34; alt=&#34;SwiftUI Fundamentals&#34; width=&#34;1024&#34; height=&#34;576&#34; class=&#34;alignnone size-large wp-image-13737&#34;/&gt;&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;
&lt;h1 id=&#34;course-outline&#34;&gt;Course Outline&lt;/h1&gt;
&lt;p&gt;First, you&amp;rsquo;ll see the tools and learn how to use SwiftUI to create single-screen applications with adaptive user interfaces that work on multiple devices.&lt;/p&gt;
&lt;p&gt;Next, you’ll learn how to bring your own style to a SwiftUI user interface by customizing its appearance.&lt;/p&gt;
&lt;p&gt;Finally, you&amp;rsquo;ll explore working with data-driven controls and understand navigation options for multi-screen applications.&lt;/p&gt;
&lt;p&gt;When you’re finished with this course, you&amp;rsquo;ll know current best practices, iOS architecture, and the most important ideas you need to feel comfortable bringing your developer skills into the iOS and Apple developer world.&lt;/p&gt;
&lt;h2 id=&#34;1---building-single-view-applications&#34;&gt;1 - Building Single-view Applications&lt;/h2&gt;
&lt;p&gt;Topics in this module include&amp;hellip;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Version Check: What You Need to Get Started&lt;/li&gt;
&lt;li&gt;The Night Watch Scenario&lt;/li&gt;
&lt;li&gt;Creating a Single-view App&lt;/li&gt;
&lt;li&gt;Understanding the SwiftUI Framework&lt;/li&gt;
&lt;li&gt;Creating Xcode Previews&lt;/li&gt;
&lt;li&gt;Understanding Views&lt;/li&gt;
&lt;li&gt;Combining and Laying out Views&lt;/li&gt;
&lt;li&gt;Understanding the SwiftUI Layout System&lt;/li&gt;
&lt;li&gt;Aligning and Positioning Views&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;2---customizing-ui-appearance&#34;&gt;2 - Customizing UI Appearance&lt;/h2&gt;
&lt;p&gt;Topics in this module include&amp;hellip;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Applying View Modifiers&lt;/li&gt;
&lt;li&gt;Developing a SwiftUI Mindset&lt;/li&gt;
&lt;li&gt;Using Icons and Symbols&lt;/li&gt;
&lt;li&gt;Implementing Custom View Modifiers&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;3---creating-data-driven-lists&#34;&gt;3 - Creating Data-driven Lists&lt;/h2&gt;
&lt;p&gt;Topics in this module include&amp;hellip;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Preparing to Work with Lists&lt;/li&gt;
&lt;li&gt;Understanding Lists&lt;/li&gt;
&lt;li&gt;Creating a List&lt;/li&gt;
&lt;li&gt;Controlling a List with Data&lt;/li&gt;
&lt;li&gt;Using Loops to Supply Content for Lists&lt;/li&gt;
&lt;li&gt;Adding Sections and Headings&lt;/li&gt;
&lt;li&gt;Creating Grid Layouts&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;4---building-ios-apps-with-multiple-screens&#34;&gt;4 - Building iOS Apps with Multiple Screens&lt;/h2&gt;
&lt;p&gt;Topics in this module include&amp;hellip;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Defining Apps with Multiple Screens&lt;/li&gt;
&lt;li&gt;Implementing Navigation Stacks&lt;/li&gt;
&lt;li&gt;Adding Navigation to an Existing Project&lt;/li&gt;
&lt;li&gt;Extracting Reusable Subviews&lt;/li&gt;
&lt;li&gt;Creating a Tabbed Application&lt;/li&gt;
&lt;li&gt;Recapping Multi-view Applications&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;5---adding-behavior-and-working-with-data&#34;&gt;5 - Adding Behavior and Working With Data&lt;/h2&gt;
&lt;p&gt;Topics in this module include&amp;hellip;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Preparing to Add Basic Behavior&lt;/li&gt;
&lt;li&gt;Extending the Data Model&lt;/li&gt;
&lt;li&gt;Understanding the View Update Cycle in SwiftUI&lt;/li&gt;
&lt;li&gt;Adding Basic Behavior&lt;/li&gt;
&lt;li&gt;Connecting a Subview to a Parent View’s Data&lt;/li&gt;
&lt;li&gt;Using Classes as Data Models&lt;/li&gt;
&lt;li&gt;Sharing a Data Model through the SwiftUI Environment&lt;/li&gt;
&lt;li&gt;Bringing the Data Model and Behavior to the Night Watch App&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;6---putting-it-all-together&#34;&gt;6 - Putting It All Together&lt;/h2&gt;
&lt;p&gt;Topics in this module include&amp;hellip;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Applying What You’ve Learned&lt;/li&gt;
&lt;li&gt;Integrating the Extended Data Model&lt;/li&gt;
&lt;li&gt;Adding the Mark Complete Behavior&lt;/li&gt;
&lt;li&gt;Adding a Toolbar and Toggle Control&lt;/li&gt;
&lt;li&gt;Implementing Swipe and Move Actions for a List&lt;/li&gt;
&lt;li&gt;Resetting the List&lt;/li&gt;
&lt;li&gt;Showing an Alert&lt;/li&gt;
&lt;li&gt;Adding Images to a SwiftUI Application&lt;/li&gt;
&lt;li&gt;Continuing your iOS Developer Journey&lt;/li&gt;
&lt;/ul&gt;
</description>
    </item>
    
    <item>
      <title>iOS 17 Fundamentals</title>
      <link>https://www.andrewcbancroft.com/pluralsight/2023-12-10-ios-17-fundamentals/</link>
      <pubDate>Sun, 10 Dec 2023 00:00:00 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/pluralsight/2023-12-10-ios-17-fundamentals/</guid>
      <description>&lt;p&gt;Developing for iOS is a valuable and in-demand skill, but it can be surprisingly different if you haven&amp;rsquo;t developed for Apple platforms before.&lt;/p&gt;
&lt;p&gt;In this course, iOS 17 Fundamentals, you&amp;rsquo;ll quickly get up and running using Xcode 15, Swift 5, and SwiftUI to build great iOS apps and tap into the latest features of iOS 17.&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;https://bit.ly/ios-17-fundamentals&#34; target=&#34;_blank&#34;&gt;iOS 17 Fundamentals&lt;/a&gt;&lt;a href=&#34;https://bit.ly/ios-17-fundamentals&#34; target=&#34;_blank&#34;&gt;&lt;br /&gt; &lt;img src=&#34;https://www.andrewcbancroft.com/images/social-assets/ios-17-fundamentals-title.png&#34; alt=&#34;iOS 17 Fundamentals&#34; width=&#34;1024&#34; height=&#34;576&#34; class=&#34;alignnone size-large wp-image-13737&#34;/&gt;&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;
&lt;h1 id=&#34;course-outline&#34;&gt;Course Outline&lt;/h1&gt;
&lt;p&gt;First, you’ll see the tools and learn the core principles for designing iOS apps with adaptive, accessible, and localized user interfaces that work on multiple devices.&lt;/p&gt;
&lt;p&gt;Next, you&amp;rsquo;ll explore how to tackle some of the most common scenarios you’ll run into when you’re building an app for iOS 17, like performing common networking tasks, managing data, adding push notifications to your application, and implementing widgets.&lt;/p&gt;
&lt;p&gt;Finally, you’ll learn how to take your app and publish it on the App Store. When you’re finished with this course, you&amp;rsquo;ll know current best practices, iOS architecture, and the most important ideas you need to feel comfortable bringing your app ideas to iOS 17.&lt;/p&gt;
&lt;h2 id=&#34;1---preparing-to-build-ios-17-applications&#34;&gt;1 - Preparing to Build iOS 17 Applications&lt;/h2&gt;
&lt;p&gt;Topics in this module include&amp;hellip;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Version Check: What You Need to Get Started&lt;/li&gt;
&lt;li&gt;How to Learn iOS Development&lt;/li&gt;
&lt;li&gt;Introducing “Improvado” - An App for Continuous Improvement&lt;/li&gt;
&lt;li&gt;Transitioning an App to iOS 17&lt;/li&gt;
&lt;li&gt;Supporting Older Versions of iOS&lt;/li&gt;
&lt;li&gt;What Every iOS 17 App Needs&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;2---designing-applications-for-ios-17&#34;&gt;2 - Designing Applications for iOS 17&lt;/h2&gt;
&lt;p&gt;Topics in this module include&amp;hellip;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Introducing Apple&amp;rsquo;s Human Interface Guidelines&lt;/li&gt;
&lt;li&gt;Making an Adaptive User Interface&lt;/li&gt;
&lt;li&gt;Adding and Customizing Layout Variations&lt;/li&gt;
&lt;li&gt;Supporting Dynamic Type&lt;/li&gt;
&lt;li&gt;Implementing Dark Mode&lt;/li&gt;
&lt;li&gt;Working with SFSymbols 5&lt;/li&gt;
&lt;li&gt;Making Apps Accessible&lt;/li&gt;
&lt;li&gt;Localizing an iOS Application&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;3---performing-common-networking-tasks&#34;&gt;3 - Performing Common Networking Tasks&lt;/h2&gt;
&lt;p&gt;Topics in this module include&amp;hellip;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Understanding URL Sessions&lt;/li&gt;
&lt;li&gt;Checking Network Connectivity&lt;/li&gt;
&lt;li&gt;Sending HTTP Requests&lt;/li&gt;
&lt;li&gt;Requesting Data from REST APIs&lt;/li&gt;
&lt;li&gt;Inspecting HTTP Responses&lt;/li&gt;
&lt;li&gt;Handling Networking Errors&lt;/li&gt;
&lt;li&gt;Organizing Networking Code&lt;/li&gt;
&lt;li&gt;Integrating Networking Tasks with SwiftUI&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;4---managing-data&#34;&gt;4 - Managing Data&lt;/h2&gt;
&lt;p&gt;Topics in this module include&amp;hellip;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Understanding Data Management Scenarios&lt;/li&gt;
&lt;li&gt;Encoding and Decoding Data&lt;/li&gt;
&lt;li&gt;Managing Preferences with UserDefaults&lt;/li&gt;
&lt;li&gt;Synchronizing Settings with iCloud Key-value Store&lt;/li&gt;
&lt;li&gt;Configuring Apps with Property Lists&lt;/li&gt;
&lt;li&gt;Performing File Management Tasks&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;5---adding-push-notification-support&#34;&gt;5 - Adding Push Notification Support&lt;/h2&gt;
&lt;p&gt;Topics in this module include&amp;hellip;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Understanding Notification Scenarios&lt;/li&gt;
&lt;li&gt;Requesting Permission to Deliver Notifications&lt;/li&gt;
&lt;li&gt;Sending Local Notifications&lt;/li&gt;
&lt;li&gt;Sidebar: Understanding the Delegation Design Pattern&lt;/li&gt;
&lt;li&gt;Responding to Local Notifications&lt;/li&gt;
&lt;li&gt;Configuring an Xcode Project for Remote Notifications&lt;/li&gt;
&lt;li&gt;Testing Remote Notifications&lt;/li&gt;
&lt;li&gt;Making Notifications Actionable&lt;/li&gt;
&lt;li&gt;Sending Silent Background Notifications&lt;/li&gt;
&lt;li&gt;Setting Up a Remote Notification Server&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;6---implementing-widgets&#34;&gt;6 - Implementing Widgets&lt;/h2&gt;
&lt;p&gt;Topics in this module include&amp;hellip;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Adding a Widget to an Xcode Project&lt;/li&gt;
&lt;li&gt;Configuring a Widget&lt;/li&gt;
&lt;li&gt;Sharing Data between an App and a Widget&lt;/li&gt;
&lt;li&gt;Making Widgets Configurable&lt;/li&gt;
&lt;li&gt;Creating Interactive Widgets&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;7---publishing-to-the-app-store&#34;&gt;7 - Publishing to the App Store&lt;/h2&gt;
&lt;p&gt;Topics in this module include&amp;hellip;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Preparing to Publish Your App&lt;/li&gt;
&lt;li&gt;Adding an App Icon&lt;/li&gt;
&lt;li&gt;Creating an iOS App Launch Screen&lt;/li&gt;
&lt;li&gt;Building a Release Version of Your App&lt;/li&gt;
&lt;li&gt;Configuring an App in App Store Connect&lt;/li&gt;
&lt;li&gt;Submitting an App for Review&lt;/li&gt;
&lt;/ul&gt;
</description>
    </item>
    
    <item>
      <title>Pre-populate a SwiftData Persistent Store</title>
      <link>https://www.andrewcbancroft.com/blog/ios-development/data-persistence/pre-populate-swiftdata-persistent-store/</link>
      <pubDate>Thu, 15 Jun 2023 00:00:00 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/blog/ios-development/data-persistence/pre-populate-swiftdata-persistent-store/</guid>
      <description>&lt;h2 id=&#34;building-blocks&#34;&gt;Building Blocks&lt;/h2&gt;
&lt;p&gt;These three building blocks will get you there:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;A &lt;code&gt;ModelContainer&lt;/code&gt; instance&lt;/li&gt;
&lt;li&gt;One or more instances of your &lt;code&gt;@Model&lt;/code&gt; class(es)&lt;/li&gt;
&lt;li&gt;Inserting those model instances into your &lt;code&gt;ModelContainer&lt;/code&gt;&amp;rsquo;s &lt;code&gt;mainContext&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Let’s take those building blocks one at a time.&lt;/p&gt;
&lt;h3 id=&#34;initialize-a-modelcontainer&#34;&gt;Initialize a &lt;code&gt;ModelContainer&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;Suppose that you’ve got a SwiftData model called &lt;code&gt;Item&lt;/code&gt; that looks something like this:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;8
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;@&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;Model&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kr&#34;&gt;final&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;Item&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;timestamp&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Date&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;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;timestamp&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Date&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;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;timestamp&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;timestamp&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;To pre-populate your SwiftData persistent store with instances of &lt;code&gt;Item&lt;/code&gt;, you need to initialize a &lt;code&gt;ModelContainer&lt;/code&gt;. A &lt;code&gt;ModelContainer&lt;/code&gt; manages everything about &lt;em&gt;where&lt;/em&gt; SwiftData will store instances of your &lt;code&gt;Item&lt;/code&gt; class.&lt;/p&gt;
&lt;p&gt;This code will initialize a &lt;code&gt;ModelContainer&lt;/code&gt; that tells SwiftData to persist instances of &lt;code&gt;Item&lt;/code&gt; to permanent storage:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;7
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;container&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;try&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ModelContainer&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;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;for&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Item&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;cl&#34;&gt;		&lt;span class=&#34;c1&#34;&gt;//...more code to come&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;    &lt;span class=&#34;bp&#34;&gt;fatalError&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;Failed to create container&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;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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;If you’d like SwiftData to persist instances of &lt;code&gt;Item&lt;/code&gt; in memory instead, this code will do it:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;hl&#34;&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;8
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;container&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;try&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ModelContainer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line hl&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;for&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Item&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;ModelConfiguration&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;inMemory&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&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;		&lt;span class=&#34;c1&#34;&gt;//...more code to come&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;    &lt;span class=&#34;bp&#34;&gt;fatalError&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;Failed to create container&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;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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;A &lt;code&gt;ModelContainer&lt;/code&gt; with an in-memory &lt;code&gt;ModelConfiguration&lt;/code&gt; will not retain any &lt;code&gt;Item&lt;/code&gt; instances that you save whenever someone re-launches your app. That being the case, using an in-memory &lt;code&gt;ModelConfiguration&lt;/code&gt; is more suited for unit testing, or for supplying data to your Xcode previews.&lt;/p&gt;
&lt;h3 id=&#34;initialize-instances-of-your-model-classes&#34;&gt;Initialize instances of your &lt;code&gt;@Model&lt;/code&gt; class(es)&lt;/h3&gt;
&lt;p&gt;Once you configure a &lt;code&gt;ModelContainer&lt;/code&gt;, you need some instances of your &lt;code&gt;@Model&lt;/code&gt; class to insert into the SwiftData persistent store.&lt;/p&gt;
&lt;p&gt;If you just need a few instances to seed your persistent store with, you can initialize them manually like this:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;5
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;items&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;cl&#34;&gt;    &lt;span class=&#34;n&#34;&gt;Item&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;timestamp&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Date&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;cl&#34;&gt;    &lt;span class=&#34;n&#34;&gt;Item&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;timestamp&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Date&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;cl&#34;&gt;	&lt;span class=&#34;n&#34;&gt;Item&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;timestamp&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Date&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;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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;If you need &lt;em&gt;more&lt;/em&gt; than a few instances, you may opt to decode instances of your &lt;code&gt;@Model&lt;/code&gt; class from something like a CSV file, a JSON file, or a property list that you include with your application’s main bundle.&lt;/p&gt;
&lt;h3 id=&#34;insert-those-model-instances-into-your-modelcontainers-maincontext&#34;&gt;Insert those model instances into your &lt;code&gt;ModelContainer&lt;/code&gt;&amp;rsquo;s &lt;code&gt;mainContext&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;The last step is to insert those instances of your model into your &lt;code&gt;ModelContainer&lt;/code&gt;&amp;rsquo;s &lt;code&gt;mainContext&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;This code loops over an array of &lt;code&gt;Item&lt;/code&gt; instances and inserts them into your SwiftData persistent store:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;k&#34;&gt;for&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;item&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;in&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;items&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;cl&#34;&gt;    &lt;span class=&#34;n&#34;&gt;container&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;mainContext&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;bp&#34;&gt;insert&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;n&#34;&gt;item&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;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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;h2 id=&#34;where-do-you-write-all-this-code&#34;&gt;Where do you write all this code?&lt;/h2&gt;
&lt;p&gt;You may be reading the building block code above and think, “Wait…where do I &lt;em&gt;write&lt;/em&gt; all this code?”&lt;/p&gt;
&lt;p&gt;The answer depends on what you’re trying to do.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Are you trying to pre-populate your SwiftData persistent store for your production app?&lt;/li&gt;
&lt;li&gt;Are you trying to pre-populate the persistent store for an Xcode preview?&lt;/li&gt;
&lt;li&gt;Are you trying to pre-populate it for your unit tests?&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;for-your-production-app&#34;&gt;For your production app&lt;/h3&gt;
&lt;p&gt;In this scenario, you can create a new Swift file to put your code in. A name like &lt;strong&gt;SwiftDataAppContainer&lt;/strong&gt; could work just fine. It’s also fine to add the code to your &lt;strong&gt;“AppName”App.swift&lt;/strong&gt; file where you initialize your SwiftUI &lt;code&gt;App&lt;/code&gt; instance.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;ModelContainer&lt;/code&gt;&amp;rsquo;s initializer can throw, but you can’t just write a &lt;code&gt;do-catch&lt;/code&gt; statement globally. You may consider wrapping the container creation code in a closure, and running that closure immediately by adding &lt;code&gt;()&lt;/code&gt; to the end of the closure expression:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;hl&#34;&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;hl&#34;&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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 hl&#34;&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;MainActor&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;appContainer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ModelContainer&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;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;cl&#34;&gt;        &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;container&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;try&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ModelContainer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;k&#34;&gt;for&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Item&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;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;cl&#34;&gt;				&lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;container&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;// this line wasn&amp;#39;t in any of the examples above&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;        &lt;span class=&#34;bp&#34;&gt;fatalError&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;Failed to create container&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;cl&#34;&gt;    &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line hl&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;}()&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;// The &amp;#34;()&amp;#34; here runs the closure immediately&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;You need to annotate the &lt;code&gt;appContainer&lt;/code&gt; declaration with the &lt;code&gt;@MainActor&lt;/code&gt; attribute because &lt;code&gt;container&lt;/code&gt;&amp;rsquo;s &lt;code&gt;mainContext&lt;/code&gt; is annotated with the &lt;code&gt;@MainActor&lt;/code&gt; attribute. You’ll get a compiler error like this if you don’t:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Main actor-isolated property &amp;lsquo;mainContext&amp;rsquo; can not be referenced from a non-isolated context&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Once you add &lt;code&gt;@MainActor&lt;/code&gt; to the &lt;code&gt;appContainer&lt;/code&gt;, you&amp;rsquo;re likely to see a warning if you&amp;rsquo;re using Swift 5.9:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Stored property &amp;lsquo;sharedModelContainer&amp;rsquo; within struct cannot have a global actor; this is an error in Swift 6&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;To silence this warning and prepare your app for Swift 6, you can move where you add &lt;code&gt;@MainActor&lt;/code&gt; from the &lt;code&gt;appContainer&lt;/code&gt; to the &lt;code&gt;App&lt;/code&gt; declaration:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;hl&#34;&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;9
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;@&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;main&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line hl&#34;&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;MainActor&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;struct&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;TrySwiftDataApp&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;App&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;body&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;some&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Scene&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;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;WindowGroup&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;cl&#34;&gt;            &lt;span class=&#34;n&#34;&gt;ContentView&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;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;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;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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;Now you can modify your SwiftUI view, or one of your app instance’s &lt;code&gt;WindowGroup&lt;/code&gt;s to have a model container:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;hl&#34;&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;@&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;main&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;MainActor&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;struct&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;TrySwiftDataApp&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;App&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;body&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;some&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Scene&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;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;WindowGroup&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;cl&#34;&gt;            &lt;span class=&#34;n&#34;&gt;ContentView&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line hl&#34;&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;cl&#34;&gt;        &lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;modelContainer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;appContainer&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;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;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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;Pre-populating your SwiftData persistent store for an app that you plan to publish to the App Store comes with a special consideration: You’ve got to handle the case where someone has already launched your app and the data store pre-population code has already done its work.&lt;/p&gt;
&lt;p&gt;In other words, you probably only want to pre-populate your persistent store on “first launch”.&lt;/p&gt;
&lt;p&gt;To prevent the persistent store from being pre-populated over and over again, do a check to make sure the store is empty before you start inserting model instances into the &lt;code&gt;mainContext&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;5
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;// Make sure the persistent store is empty. If it&amp;#39;s not, return the non-empty container.&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;itemFetchDescriptor&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;FetchDescriptor&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;Item&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;()&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;n&#34;&gt;itemFetchDescriptor&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;fetchLimit&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;&lt;span class=&#34;k&#34;&gt;guard&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;try&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;container&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;mainContext&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;fetch&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;itemFetchDescriptor&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;p&#34;&gt;==&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;0&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 class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;container&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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;You can keep things efficient by setting the &lt;code&gt;FetchDescriptor&lt;/code&gt; instance’s &lt;code&gt;fetchLimit&lt;/code&gt; to &lt;code&gt;1&lt;/code&gt;. In other words, you don’t need to fetch every &lt;code&gt;Item&lt;/code&gt; instance from the persistent store to decide whether the store is empty or not.&lt;/p&gt;
&lt;p&gt;As long as the &lt;code&gt;count&lt;/code&gt; of items that &lt;code&gt;fetch&lt;/code&gt; returns is &lt;code&gt;0&lt;/code&gt;, you can rest assured that the persistent store is empty and pre-populate it. If that’s not the case, the &lt;code&gt;guard&lt;/code&gt;&amp;rsquo;s &lt;code&gt;else&lt;/code&gt; branch will run and return the non-empty &lt;code&gt;ModelContainer&lt;/code&gt; instance.&lt;/p&gt;
&lt;p&gt;Here’s the full code example with that check included:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;12
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;13
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;14
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;15
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;16
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;17
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;18
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;19
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;20
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;21
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;22
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;23
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;24
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;25
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;26
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;27
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;@&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;MainActor&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;appContainer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ModelContainer&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;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;cl&#34;&gt;        &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;container&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;try&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ModelContainer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;k&#34;&gt;for&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Item&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;cl&#34;&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;c1&#34;&gt;// Make sure the persistent store is empty. If it&amp;#39;s not, return the non-empty container.&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;itemFetchDescriptor&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;FetchDescriptor&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;Item&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;()&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;itemFetchDescriptor&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;fetchLimit&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;guard&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;try&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;container&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;mainContext&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;fetch&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;itemFetchDescriptor&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;p&#34;&gt;==&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;0&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 class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;container&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;cl&#34;&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;c1&#34;&gt;// This code will only run if the persistent store is empty.&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;items&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;cl&#34;&gt;            &lt;span class=&#34;n&#34;&gt;Item&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;timestamp&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Date&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;cl&#34;&gt;            &lt;span class=&#34;n&#34;&gt;Item&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;timestamp&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Date&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;cl&#34;&gt;            &lt;span class=&#34;n&#34;&gt;Item&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;timestamp&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Date&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;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;cl&#34;&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;for&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;item&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;in&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;items&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;cl&#34;&gt;            &lt;span class=&#34;n&#34;&gt;container&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;mainContext&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;bp&#34;&gt;insert&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;n&#34;&gt;item&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;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;cl&#34;&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;container&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;        &lt;span class=&#34;bp&#34;&gt;fatalError&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;Failed to create container&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;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;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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;h3 id=&#34;for-an-xcode-preview&#34;&gt;For an Xcode preview&lt;/h3&gt;
&lt;p&gt;If you’re trying to pre-populate a SwiftData persistent store for an Xcode preview of your SwiftUI view, you can create a new Swift file to put your code in and name it something like &lt;strong&gt;SwiftDataPreviewContainer&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;In this file, you can write code that’s similar to the code you’d write for your production app but with a couple of changes:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;You don’t need to check for an empty store, because&lt;/li&gt;
&lt;li&gt;You’re going to initialize the &lt;code&gt;ModelContainer&lt;/code&gt; with an in-memory store&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Here’s the full code example of what the preview container could look like:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;hl&#34;&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;hl&#34;&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;12
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;13
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;14
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;15
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;16
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;17
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;18
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;19
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;20
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;21
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;22
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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 hl&#34;&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;MainActor&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;previewContainer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ModelContainer&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;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;cl&#34;&gt;        &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;container&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;try&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ModelContainer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line hl&#34;&gt;&lt;span class=&#34;cl&#34;&gt;            &lt;span class=&#34;k&#34;&gt;for&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Item&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;ModelConfiguration&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;inMemory&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&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;items&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;cl&#34;&gt;            &lt;span class=&#34;n&#34;&gt;Item&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;timestamp&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Date&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;cl&#34;&gt;            &lt;span class=&#34;n&#34;&gt;Item&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;timestamp&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Date&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;cl&#34;&gt;            &lt;span class=&#34;n&#34;&gt;Item&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;timestamp&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Date&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;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;cl&#34;&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;for&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;item&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;in&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;items&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;cl&#34;&gt;            &lt;span class=&#34;n&#34;&gt;container&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;mainContext&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;bp&#34;&gt;insert&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;n&#34;&gt;item&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;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;cl&#34;&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;container&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;        &lt;span class=&#34;bp&#34;&gt;fatalError&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;Failed to create container&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;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;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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;Then you can (in theory) use the new &lt;code&gt;#Preview&lt;/code&gt; macro if you’re using Xcode 15 or above:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;hl&#34;&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;hl&#34;&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;4
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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 hl&#34;&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;Preview&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;cl&#34;&gt;    &lt;span class=&#34;n&#34;&gt;ContentView&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line hl&#34;&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;modelContainer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;previewContainer&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;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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;I wrote, “in theory” because there’s a bug (as of June 14, 2023) with Xcode that causes the preview to crash when you use this macro.&lt;/p&gt;
&lt;p&gt;As a workaround — or if you’re not using Xcode 15 — you can embed this model creation code inside of a custom &lt;code&gt;PreviewProvider&lt;/code&gt; type:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;hl&#34;&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;hl&#34;&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;12
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;13
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;14
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;15
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;16
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;17
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;18
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;19
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;20
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;21
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;22
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;23
&lt;/span&gt;&lt;span class=&#34;hl&#34;&gt;&lt;span class=&#34;lnt&#34;&gt;24
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;hl&#34;&gt;&lt;span class=&#34;lnt&#34;&gt;25
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;hl&#34;&gt;&lt;span class=&#34;lnt&#34;&gt;26
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;hl&#34;&gt;&lt;span class=&#34;lnt&#34;&gt;27
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;28
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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 hl&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;struct&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;ContentView_Previews&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;PreviewProvider&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line hl&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;static&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;previewContainer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ModelContainer&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;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;cl&#34;&gt;            &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;container&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;try&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ModelContainer&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;cl&#34;&gt;                &lt;span class=&#34;k&#34;&gt;for&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Item&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;ModelConfiguration&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;inMemory&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&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;            
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;items&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;cl&#34;&gt;                &lt;span class=&#34;n&#34;&gt;Item&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;timestamp&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Date&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;cl&#34;&gt;                &lt;span class=&#34;n&#34;&gt;Item&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;timestamp&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Date&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;cl&#34;&gt;                &lt;span class=&#34;n&#34;&gt;Item&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;timestamp&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Date&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;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;cl&#34;&gt;            
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;            &lt;span class=&#34;k&#34;&gt;for&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;item&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;in&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;items&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;cl&#34;&gt;                &lt;span class=&#34;n&#34;&gt;container&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;mainContext&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;bp&#34;&gt;insert&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;n&#34;&gt;item&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;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;cl&#34;&gt;            
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;container&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;            &lt;span class=&#34;bp&#34;&gt;fatalError&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;Failed to create container&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;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;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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line hl&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;static&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;previews&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;some&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;View&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line hl&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;ContentView&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line hl&#34;&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;modelContainer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;previewContainer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line hl&#34;&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;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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;h3 id=&#34;for-your-unit-tests&#34;&gt;For your unit tests&lt;/h3&gt;
&lt;p&gt;Pre-populating your SwiftData persistent store for your unit tests is similar to doing so for your Xcode previews, in that (depending on what you’re testing), you probably don’t need to check for an empty persistent store before pre-populating it, and you’ll likely benefit from using an in-memory store.&lt;/p&gt;
&lt;p&gt;Here’s what you need to do:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Mark your &lt;code&gt;XCTestCase&lt;/code&gt; class as a &lt;code&gt;@MainActor&lt;/code&gt;:&lt;/li&gt;
&lt;/ol&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;hl&#34;&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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 hl&#34;&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;MainActor&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kr&#34;&gt;final&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;TrySwiftDataTests&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 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;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;ol start=&#34;2&#34;&gt;
&lt;li&gt;Add a stored property to your &lt;code&gt;XCTestCase&lt;/code&gt; that will hold your &lt;code&gt;ModelContainer&lt;/code&gt;:&lt;/li&gt;
&lt;/ol&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;testContainer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ModelContainer&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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;ol start=&#34;3&#34;&gt;
&lt;li&gt;Set the &lt;code&gt;testContainer&lt;/code&gt; property inside the &lt;code&gt;setUpWithError&lt;/code&gt; function:&lt;/li&gt;
&lt;/ol&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;12
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;13
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;14
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;15
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;16
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;17
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;18
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;19
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;20
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;21
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;22
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;23
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;24
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;25
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;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;setUpWithError&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;kr&#34;&gt;throws&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;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;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;testContainer&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;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;cl&#34;&gt;            &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;container&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;try&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ModelContainer&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;cl&#34;&gt;                &lt;span class=&#34;k&#34;&gt;for&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Item&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;ModelConfiguration&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;inMemory&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&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;            
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;items&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;cl&#34;&gt;                &lt;span class=&#34;n&#34;&gt;Item&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;timestamp&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Date&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;cl&#34;&gt;                &lt;span class=&#34;n&#34;&gt;Item&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;timestamp&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Date&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;cl&#34;&gt;                &lt;span class=&#34;n&#34;&gt;Item&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;timestamp&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Date&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;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;cl&#34;&gt;            
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;            &lt;span class=&#34;k&#34;&gt;for&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;item&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;in&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;items&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;cl&#34;&gt;                &lt;span class=&#34;n&#34;&gt;container&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;mainContext&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;bp&#34;&gt;insert&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;n&#34;&gt;item&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;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;cl&#34;&gt;            
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;container&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;            &lt;span class=&#34;bp&#34;&gt;fatalError&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;Failed to create container&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;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;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;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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;ol start=&#34;4&#34;&gt;
&lt;li&gt;Use the &lt;code&gt;testContainer&lt;/code&gt; in any tests that need it:&lt;/li&gt;
&lt;/ol&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;7
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;testExample&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;kr&#34;&gt;throws&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;fetchDescriptor&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;FetchDescriptor&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;Item&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;()&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;result&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;try&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;testContainer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;?.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;mainContext&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;fetch&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;fetchDescriptor&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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;n&#34;&gt;XCTAssertEqual&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&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;bp&#34;&gt;count&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;3&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;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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;</description>
    </item>
    
    <item>
      <title>Add SwiftData to an Existing App</title>
      <link>https://www.andrewcbancroft.com/blog/ios-development/data-persistence/add-swift-data-to-existing-app/</link>
      <pubDate>Thu, 08 Jun 2023 00:00:00 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/blog/ios-development/data-persistence/add-swift-data-to-existing-app/</guid>
      <description>&lt;h2 id=&#34;system-configuration&#34;&gt;System Configuration&lt;/h2&gt;
&lt;p&gt;First things first: you need the following before you can get started with SwiftData&amp;hellip;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;You need to install Xcode 15 on your Mac.&lt;/li&gt;
&lt;li&gt;You need to set your app&amp;rsquo;s minimum deployment target to macOS 14, iOS 17, tvOS 17, or watchOS 10.&lt;/li&gt;
&lt;li&gt;Your app needs to use SwiftUI &amp;ndash; SwiftData does not work with Storyboard apps. If you have a Storyboard app with some SwiftUI views integrated in, you &lt;em&gt;can&lt;/em&gt; use SwiftData with those SwiftUI views. In that situation, it may be best to stick with Core Data to keep data persistence simple and consistent.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;essential-pieces-of-swiftdata&#34;&gt;Essential Pieces of SwiftData&lt;/h2&gt;
&lt;p&gt;Once you configure your Mac, you need to understand these essential pieces of SwiftData:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The SwiftData import (&lt;code&gt;import SwiftData&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;@Model&lt;/code&gt; macro&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;.modelContainer()&lt;/code&gt; View modifier&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;@Query&lt;/code&gt; property wrapper&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;@Environment(\.modelContext)&lt;/code&gt; property wrapper&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;import-swiftdata&#34;&gt;Import SwiftData&lt;/h3&gt;
&lt;p&gt;Before you can use SwiftData in a Swift file, you must write &lt;code&gt;import SwiftData&lt;/code&gt; at the top of that file.&lt;/p&gt;
&lt;h3 id=&#34;the-model-macro&#34;&gt;The &lt;code&gt;@Model&lt;/code&gt; macro&lt;/h3&gt;
&lt;p&gt;Persisting data starts with defining a data model.&lt;/p&gt;
&lt;p&gt;This model needs to be a Swift &lt;code&gt;class&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Whenever you start a brand new Xcode project and select the option to use SwiftData for the storage of your app&amp;rsquo;s data, Xcode automatically includes an &lt;strong&gt;Item&lt;/strong&gt; file.&lt;/p&gt;
&lt;p&gt;In this file, Apple includes the following code:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;hl&#34;&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;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;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;import&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;SwiftData&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line hl&#34;&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;Model&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kr&#34;&gt;final&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;Item&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;timestamp&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Date&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;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;timestamp&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Date&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;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;timestamp&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;timestamp&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;On line 4, the &lt;code&gt;@Model&lt;/code&gt; macro gives the &lt;code&gt;Item&lt;/code&gt; class all the powers it needs to let you save instances of it in a persistent store.&lt;/p&gt;
&lt;p&gt;Curious to see what &lt;code&gt;@Model&lt;/code&gt; does? Right-click it, and select &lt;strong&gt;Expand Macro&lt;/strong&gt;. Here&amp;rsquo;s what you&amp;rsquo;ll see:&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;expand-model-macro.png&#34; alt=&#34;Expand Model Macro&#34;&gt;&lt;/p&gt;
&lt;p&gt;It&amp;rsquo;s not critical to understand the &lt;code&gt;@Model&lt;/code&gt; macro in detail. It&amp;rsquo;s OK to trust it to set up everything your model class needs to work with SwiftData. What you need to do is annotate any classes in your app that you want to persist in a data store with &lt;code&gt;@Model&lt;/code&gt;.&lt;/p&gt;
&lt;h3 id=&#34;the-modelcontainer-view-modifier&#34;&gt;The &lt;code&gt;.modelContainer()&lt;/code&gt; View modifier&lt;/h3&gt;
&lt;p&gt;Once you&amp;rsquo;ve annotated all your model classes with &lt;code&gt;@Model&lt;/code&gt;, you need to modify your SwiftUI view &amp;ndash; or even an entire &lt;code&gt;WindowGroup&lt;/code&gt; &amp;ndash; with a container that can store instances of all your model classes.&lt;/p&gt;
&lt;p&gt;Whenever you start a brand new Xcode project and select the option to use SwiftData for storage of your app&amp;rsquo;s data, Xcode automatically writes this code in your &amp;ldquo;AppName&amp;quot;App Swift file:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;hl&#34;&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;12
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;13
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;import&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;SwiftUI&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;SwiftData&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;@&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;main&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;struct&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;TrySwiftDataApp&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;App&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;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;body&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;some&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Scene&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;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;WindowGroup&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;cl&#34;&gt;            &lt;span class=&#34;n&#34;&gt;ContentView&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;cl&#34;&gt;        &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line hl&#34;&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;modelContainer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;k&#34;&gt;for&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Item&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;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;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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;If you have multiple model classes that you want to put into the container, replace &lt;code&gt;.modelContainer(for: Item.self)&lt;/code&gt; with an array like this:
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;5
&lt;/span&gt;&lt;span class=&#34;hl&#34;&gt;&lt;span class=&#34;lnt&#34;&gt;6
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;8
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;body&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;some&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Scene&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;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;WindowGroup&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;cl&#34;&gt;            &lt;span class=&#34;n&#34;&gt;ContentView&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;cl&#34;&gt;        &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line hl&#34;&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;modelContainer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;k&#34;&gt;for&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;ModelClass1&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;ModelClass2&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;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;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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;/p&gt;
&lt;h3 id=&#34;the-query-property-wrapper&#34;&gt;The &lt;code&gt;@Query&lt;/code&gt; property wrapper&lt;/h3&gt;
&lt;p&gt;After you modify your view or &lt;code&gt;WindowGroup&lt;/code&gt; with a &lt;code&gt;modelContainer&lt;/code&gt;, you need to get data from the container into your view. That&amp;rsquo;s where the &lt;code&gt;@Query&lt;/code&gt; property wrapper comes in.&lt;/p&gt;
&lt;p&gt;Suppose this was your SwiftData model:
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;8
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;@&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;Model&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kr&#34;&gt;final&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;Item&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;timestamp&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Date&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;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;timestamp&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Date&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;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;timestamp&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;timestamp&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;/p&gt;
&lt;p&gt;If you want to create a list of &lt;code&gt;Item&lt;/code&gt;s that you&amp;rsquo;ve stored with SwiftData, you&amp;rsquo;d write
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;@&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;Query&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;private&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;items&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;Item&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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;/p&gt;
&lt;p&gt;Then in your view&amp;rsquo;s &lt;code&gt;body&lt;/code&gt;, you could create a list out of the &lt;code&gt;items&lt;/code&gt; variable:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;n&#34;&gt;List&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;items&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;item&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;cl&#34;&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;Item at &lt;/span&gt;&lt;span class=&#34;si&#34;&gt;\(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;item&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;timestamp&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 class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;That&amp;rsquo;s the most basic way to get data &lt;em&gt;out&lt;/em&gt; of a SwiftData persistent store. But how do you put data &lt;em&gt;in&lt;/em&gt; &amp;ndash; or for that matter, update it or delete it from the persistent store?&lt;/p&gt;
&lt;h3 id=&#34;the-environmentmodelcontext-property-wrapper&#34;&gt;The &lt;code&gt;@Environment(\.modelContext)&lt;/code&gt; property wrapper&lt;/h3&gt;
&lt;p&gt;Suppose again that this was your SwiftData model:
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;8
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;@&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;Model&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kr&#34;&gt;final&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;Item&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;timestamp&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Date&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;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;timestamp&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Date&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;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;timestamp&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;timestamp&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;/p&gt;
&lt;p&gt;Any time you need to insert or delete data with SwiftData, add a line of code like this to your SwiftUI view:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;@&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;Environment&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;err&#34;&gt;\&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;modelContext&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;private&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;modelContext&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;With &lt;code&gt;modelContext&lt;/code&gt;, you can add data to your SwiftData store like this:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;newItem&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Item&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;timestamp&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Date&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;cl&#34;&gt;&lt;span class=&#34;n&#34;&gt;modelContext&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;bp&#34;&gt;insert&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;newItem&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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;If you want to delete data, this code will do it:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;k&#34;&gt;for&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;index&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;in&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;offsets&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;cl&#34;&gt;    &lt;span class=&#34;n&#34;&gt;modelContext&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;delete&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;items&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;index&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;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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;What about &lt;em&gt;updating&lt;/em&gt; data?&lt;/p&gt;
&lt;p&gt;In most cases, you don&amp;rsquo;t need to do anything extra. &lt;code&gt;modelContext&lt;/code&gt; has an autosave feature. When it&amp;rsquo;s enabled, &lt;code&gt;modelContext&lt;/code&gt; saves when system events, such as your app entering the foreground or background, occur. It also saves periodically while your app is running. No extra code required!&lt;/p&gt;
&lt;p&gt;&lt;code&gt;modelContext&lt;/code&gt; does have a &lt;code&gt;save()&lt;/code&gt; method that you can call if your app needs to do something like send data from SwiftData over to another system process, or share it with another person&amp;rsquo;s device. In situations like these, call &lt;code&gt;modelContext.save()&lt;/code&gt; before you perform the action.&lt;/p&gt;
&lt;h2 id=&#34;wrapping-up&#34;&gt;Wrapping Up&lt;/h2&gt;
&lt;p&gt;That takes care of adding SwiftData to one of your existing apps! There&amp;rsquo;s always more to explore, so &lt;a href=&#34;https://twitter.com/andrewcbancroft&#34;&gt;@ me on Twitter&lt;/a&gt; to continue the conversation.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Building iOS User Interfaces with SwiftUI</title>
      <link>https://www.andrewcbancroft.com/pluralsight/2021-08-22-building-ios-user-interfaces-swiftui/</link>
      <pubDate>Sun, 22 Aug 2021 00:00:00 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/pluralsight/2021-08-22-building-ios-user-interfaces-swiftui/</guid>
      <description>&lt;p&gt;Designing efficient and dynamic apps is essential for taking your iOS developer skills to the next level.&lt;/p&gt;
&lt;p&gt;In this course, Building iOS User Interfaces with SwiftUI, you&amp;rsquo;ll build upon the basic knowledge of iOS development you’ve already gained in order to build compelling iOS apps that engage your users.&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;https://bit.ly/building-ios-ui-swiftui&#34; target=&#34;_blank&#34;&gt;Building iOS User Interfaces with SwiftUI&lt;/a&gt;&lt;a href=&#34;https://bit.ly/building-ios-ui-swiftui&#34; target=&#34;_blank&#34;&gt;&lt;br /&gt; &lt;img src=&#34;https://www.andrewcbancroft.com/images/social-assets/building-ios-user-interfaces-swiftui.png&#34; alt=&#34;Building iOS User Interfaces with SwiftUI&#34; width=&#34;1024&#34; height=&#34;576&#34; class=&#34;alignnone size-large wp-image-13737&#34;/&gt;&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;
&lt;h1 id=&#34;course-outline&#34;&gt;Course Outline&lt;/h1&gt;
&lt;p&gt;First, you&amp;rsquo;ll see the tools and learn how to create iOS apps with adaptive user interfaces that work on multiple devices.&lt;/p&gt;
&lt;p&gt;Next, you’ll learn how to manage iOS projects and define the building blocks of your own iOS applications.&lt;/p&gt;
&lt;p&gt;Finally, you&amp;rsquo;ll explore working with data-driven controls and understand navigation options.&lt;/p&gt;
&lt;p&gt;When you’re finished with this course, you&amp;rsquo;ll know current best practices, iOS architecture, and the most important ideas you need to feel comfortable bringing your developer skills into the iOS and Apple developer world.&lt;/p&gt;
&lt;h2 id=&#34;1---designing-interfaces-with-swiftui&#34;&gt;1 - Designing Interfaces with SwiftUI&lt;/h2&gt;
&lt;p&gt;In this introductory module, we begin where the iOS Getting Started course left off and prepare you to handle next-level interface building with SwiftUI.&lt;/p&gt;
&lt;p&gt;Topics in this module include&amp;hellip;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Preparing to Design User Interfaces with SwiftUI&lt;/li&gt;
&lt;li&gt;The Snowhaven Renovation Scenario&lt;/li&gt;
&lt;li&gt;Defining SwiftUI Views&lt;/li&gt;
&lt;li&gt;Creating Basic Designs and Layouts&lt;/li&gt;
&lt;li&gt;Representing Data in User Interfaces&lt;/li&gt;
&lt;li&gt;Customizing the Appearance of Views&lt;/li&gt;
&lt;li&gt;Changing the Size and Scale of Views&lt;/li&gt;
&lt;li&gt;Adding Effects to Views&lt;/li&gt;
&lt;li&gt;Layering Views with ZStacks&lt;/li&gt;
&lt;li&gt;Implementing Navigation and Displaying a Modal View&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;2---handling-user-input&#34;&gt;2 - Handling User Input&lt;/h2&gt;
&lt;p&gt;There’s no such thing as an app without data.
In this module, I set you up for building dynamic, interactive applications by unpacking questions like:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;How do we model real-world objects and concepts as data that SwiftUI can work with?&lt;/li&gt;
&lt;li&gt;How do we integrate data into an app, so it’s usable in the right views at the right time?&lt;/li&gt;
&lt;li&gt;And how do we load data that’s been saved… display it… and, most importantly, for this module…change it using different editing controls with SwiftUI…&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Topics in this module include&amp;hellip;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Working with Data in SwiftUI&lt;/li&gt;
&lt;li&gt;Establishing an App’s Data Flow&lt;/li&gt;
&lt;li&gt;Loading and Displaying Data&lt;/li&gt;
&lt;li&gt;Editing Text Data with Text Fields&lt;/li&gt;
&lt;li&gt;Persisting (or Cancelling) Data Changes&lt;/li&gt;
&lt;li&gt;Working with Picker Views&lt;/li&gt;
&lt;li&gt;Switching Between Binary Options with Toggles&lt;/li&gt;
&lt;li&gt;Editing Numeric Data with Text Fields&lt;/li&gt;
&lt;li&gt;Displaying Editing Controls in a Form&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;3---bringing-apps-to-life-with-drawing-animation-and-gestures&#34;&gt;3 - Bringing Apps to Life with Drawing, Animation, and Gestures&lt;/h2&gt;
&lt;p&gt;SwiftUI provides you with all the tools necessary to make your apps even more dynamic by responding to user gestures like taps and swipes… The framework also gives you everything you need to express your creativity by drawing your own graphics, and adding animations throughout your apps to bring things to life.&lt;/p&gt;
&lt;p&gt;Topics in this module include&amp;hellip;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Understanding the SwiftUI Drawing System&lt;/li&gt;
&lt;li&gt;Drawing with Paths&lt;/li&gt;
&lt;li&gt;Using GeometryReader&lt;/li&gt;
&lt;li&gt;Applying Transformations to Paths&lt;/li&gt;
&lt;li&gt;Building Reusable Shapes&lt;/li&gt;
&lt;li&gt;Understanding Animations&lt;/li&gt;
&lt;li&gt;Adding Basic Animation&lt;/li&gt;
&lt;li&gt;Animating View Hierarchy Changes with Transitions&lt;/li&gt;
&lt;li&gt;Combining Multiple Animations and Transitions&lt;/li&gt;
&lt;li&gt;Responding to Gestures&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;4---integrating-swiftui-and-uikit&#34;&gt;4 - Integrating SwiftUI and UIKit&lt;/h2&gt;
&lt;p&gt;By now, you know that SwiftUI is wonderful, but&amp;hellip; it’s not the only Apple UI framework used in today’s Apple Developer world. &lt;strong&gt;UIKit&lt;/strong&gt; is still very widely used and even includes some UI components that are not currently available in the SwiftUI framework.&lt;/p&gt;
&lt;p&gt;As we wrap up the course, I explore the answer to this question: How can we, as developers, mix SwiftUI and UIKit to leverage the power of both frameworks?&lt;/p&gt;
&lt;p&gt;Topics in this module include&amp;hellip;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Preparing to Integrate SwiftUI and UIKit&lt;/li&gt;
&lt;li&gt;Representing UIKit ViewControllers in SwiftUI&lt;/li&gt;
&lt;li&gt;Making SwiftUI Views Compatible with UIKit&lt;/li&gt;
&lt;li&gt;Representing UIKit Views in SwiftUI&lt;/li&gt;
&lt;li&gt;Sharing State Between SwiftUI and UIKit&lt;/li&gt;
&lt;li&gt;Coordinating User Interaction Between UIViewControllers and SwiftUI&lt;/li&gt;
&lt;li&gt;Coordinating User Interaction Between UIViews and SwiftUI&lt;/li&gt;
&lt;li&gt;Applying the Finishing Touches&lt;/li&gt;
&lt;/ul&gt;
&lt;h1 id=&#34;feedback-welcome&#34;&gt;Feedback Welcome!&lt;/h1&gt;
&lt;p&gt;From the beginning my unwavering goal is to help you filter the noise and learn as efficiently as possible.  My hope is that this course benefits you in your iOS development career!&lt;/p&gt;
&lt;p&gt;I welcome feedback on this course, and on other iOS development courses you might be interested in seeing in the Pluralsight library. Happy learning!&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Swift Five - Week of February 14, 2021</title>
      <link>https://www.andrewcbancroft.com/blog/ios-development/swift-five/swift-five-2021-02-14/</link>
      <pubDate>Sun, 14 Feb 2021 00:00:00 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/blog/ios-development/swift-five/swift-five-2021-02-14/</guid>
      <description>&lt;h2 id=&#34;persistence-prevails-dave-shares-his-ios-developer-journey&#34;&gt;Persistence prevails: Dave shares his iOS developer journey&lt;/h2&gt;
&lt;p&gt;After watching &lt;a href=&#34;https://twitter.com/davejacobseniOS&#34;&gt;Dave Jacobsen&amp;rsquo;s&lt;/a&gt; story on YouTube, I found myself asking, &amp;ldquo;When would &lt;em&gt;I&lt;/em&gt; have given up if I were in Dave&amp;rsquo;s shoes?&amp;rdquo;&lt;/p&gt;
&lt;p&gt;He shares about the sobering reality of how much effort and dedication it really takes to transition careers.  But he also proves it&amp;rsquo;s possible if you persist!&lt;/p&gt;
&lt;h2 id=&#34;coreml-brings-the-beauty-of-braille-to-the-seeing&#34;&gt;CoreML brings the beauty of braille to the seeing&lt;/h2&gt;
&lt;p&gt;How cool is this? &lt;a href=&#34;https://twitter.com/azzoor&#34;&gt;Aaron Stephenson&lt;/a&gt; is learning braile. He&amp;rsquo;s also a developer.&lt;/p&gt;
&lt;p&gt;What does he do to check himself to make sure he&amp;rsquo;s reading things correctly? He trains a CoreML model to recognize braile letters and display them using augmented reality.&lt;/p&gt;
&lt;p&gt;Beautiful.&lt;/p&gt;
&lt;h2 id=&#34;streamlined-xctestcase-template&#34;&gt;Streamlined XCTestCase Template&lt;/h2&gt;
&lt;p&gt;&lt;a href=&#34;https://twitter.com/qcoding&#34;&gt;Jon Reid&lt;/a&gt; is an amazing teacher of testing. I came across his &lt;a href=&#34;https://qualitycoding.org/swift-unit-testing-template/&#34;&gt;Streamlined XCTestCase Template&lt;/a&gt; this week and had to share.&lt;/p&gt;
&lt;h2 id=&#34;kent-beck-on-the-goal-of-software-design&#34;&gt;Kent Beck on the goal of software design&lt;/h2&gt;
&lt;h2 id=&#34;ship-something-anything&#34;&gt;Ship something. Anything.&lt;/h2&gt;
&lt;p&gt;I&amp;rsquo;m thankful for this advice from &lt;a href=&#34;https://twitter.com/edouard_iosdev&#34;&gt;Eduardo&lt;/a&gt; and &lt;a href=&#34;https://twitter.com/JeffMorhous&#34;&gt;Jeff&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Perfection is overrated.  Ship something.  Anything.  Then improve it over time!&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>How to Download Old Versions of Xcode</title>
      <link>https://www.andrewcbancroft.com/blog/ios-development/xcode/how-to-download-old-versions-of-xcode/</link>
      <pubDate>Wed, 09 Dec 2020 04:21:19 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/blog/ios-development/xcode/how-to-download-old-versions-of-xcode/</guid>
      <description>&lt;h2 id=&#34;whyyy-would-i-need-an-old-version-of-xcode&#34;&gt;Whyyy would I need an old version of Xcode?&lt;/h2&gt;
&lt;p&gt;Valid question &amp;ndash; it&amp;rsquo;s not often you&amp;rsquo;ll need one.  Two reasons off the top of my head though:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;1) To compare with your previous experience&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Perhaps you&amp;rsquo;re just wanting to see what things &amp;ldquo;used to be like&amp;rdquo;.&lt;/p&gt;
&lt;p&gt;Maybe you&amp;rsquo;re struggling with the infamous &amp;ldquo;this code worked before&amp;hellip;now it doesn&amp;rsquo;t&amp;hellip;what changed?&amp;rdquo; and you just want to build your project with a prior version of Xcode in order to rule it out as the culprit.&lt;/p&gt;
&lt;p&gt;Grabbing an older version of Xcode could help you here.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;2) To upgrade a very old project to newer versions of Swift&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;I hit this one the other day. I needed to update the exercise files for my &lt;a href=&#34;http://bit.ly/core-data-swift&#34;&gt;Core Data Fundamentals with Swift&lt;/a&gt; course on &lt;a href=&#34;http://bit.ly/ps-author-page&#34;&gt;Pluralsight&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;I opened the project in Xcode 12 and this popped up:
&lt;img src=&#34;unsupported-version-of-swift.png&#34; width=&#34;50%&#34;/&gt;&lt;/p&gt;
&lt;p&gt;Xcode &lt;em&gt;10.1&lt;/em&gt;???  That was a &lt;em&gt;while&lt;/em&gt; ago. 😅&lt;/p&gt;
&lt;h2 id=&#34;how-to-download&#34;&gt;How to download&lt;/h2&gt;
&lt;p&gt;Regardless of your &lt;em&gt;reason&lt;/em&gt;, the good news is that you can download old versions of Xcode (and a lot of other tools) from the Apple Developer Center.&lt;/p&gt;
&lt;p&gt;The link is a bit hidden though, so I&amp;rsquo;ll leave it here for you as an easy-to-find breadcrumb:
&lt;a name=&#34;resources&#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-link&#34;&gt;&lt;/i&gt; &lt;a href=&#34;https://developer.apple.com/download/more/&#34; rel=&#34;nofollow&#34;&gt;https://developer.apple.com/download/more/&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;p&gt;You&amp;rsquo;ll need to sign in to your Apple Developer account, but once you&amp;rsquo;re in, you&amp;rsquo;ll have access to &lt;em&gt;lots&lt;/em&gt; of old stuff.&lt;/p&gt;
&lt;p&gt;Every version of Xcode is available &amp;ndash; all the way back to version 1.0, released in &lt;strong&gt;2003&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;You&amp;rsquo;ll also find prior versions of macOS, old versions of the command line tools, prior betas and release candidates of Xcode and macOS, and more.&lt;/p&gt;
&lt;p&gt;How cool is that?&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>iOS 14 Getting Started</title>
      <link>https://www.andrewcbancroft.com/pluralsight/2020-12-04-ios-14-getting-started/</link>
      <pubDate>Fri, 04 Dec 2020 00:00:00 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/pluralsight/2020-12-04-ios-14-getting-started/</guid>
      <description>&lt;p&gt;Developing for iOS is a valuable and in-demand skill, but if you haven&amp;rsquo;t developed in the Apple world before, it can be surprisingly different.&lt;/p&gt;
&lt;p&gt;In this course, iOS 14: Getting Started, you&amp;rsquo;ll quickly get up and running using Xcode 12, Swift 5, and SwiftUI to build great iOS apps and tap into the latest features in iOS 14.&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;https://bit.ly/iOS14-getting-started&#34; target=&#34;_blank&#34;&gt;iOS 14 Getting Started&lt;/a&gt;&lt;a href=&#34;https://bit.ly/iOS14-getting-started&#34; target=&#34;_blank&#34;&gt;&lt;br /&gt; &lt;img src=&#34;https://www.andrewcbancroft.com/images/social-assets/ios-14-getting-started.png&#34; alt=&#34;iOS 14 Getting Started&#34; width=&#34;1024&#34; height=&#34;576&#34; class=&#34;alignnone size-large wp-image-13737&#34;/&gt;&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;
&lt;h1 id=&#34;course-outline&#34;&gt;Course Outline&lt;/h1&gt;
&lt;p&gt;First, you&amp;rsquo;ll see the tools and learn how to create iOS apps with adaptive user interfaces that work on multiple devices.&lt;/p&gt;
&lt;p&gt;Next, you’ll learn how to manage iOS projects and define the building blocks of your own iOS applications.&lt;/p&gt;
&lt;p&gt;Finally, you&amp;rsquo;ll explore working with data-driven controls and understand navigation options.&lt;/p&gt;
&lt;p&gt;When you’re finished with this course, you&amp;rsquo;ll know current best practices, iOS architecture, and the most important ideas you need to feel comfortable bringing your developer skills into the iOS and Apple developer world.&lt;/p&gt;
&lt;h2 id=&#34;1--preparing-to-build-ios-applications&#34;&gt;1 – Preparing to Build iOS Applications&lt;/h2&gt;
&lt;p&gt;Prepare to implement iOS apps by understanding what’s involved from a big picture standpoint.&lt;/p&gt;
&lt;p&gt;We begin at the beginning to bring you up to speed and set the foundation for the remainder of the course.&lt;/p&gt;
&lt;p&gt;Topics in this module include&amp;hellip;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;What You Need for Building iOS Apps&lt;/li&gt;
&lt;li&gt;Creating an iOS Project with Xcode&lt;/li&gt;
&lt;li&gt;Exploring an iOS Project&lt;/li&gt;
&lt;li&gt;How to Learn iOS&lt;/li&gt;
&lt;li&gt;The Night Watch Scenario&lt;/li&gt;
&lt;li&gt;Working with Xcode Projects&lt;/li&gt;
&lt;li&gt;Grasping the Basics of the Xcode Development Environment&lt;/li&gt;
&lt;li&gt;Summary&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;2---building-single-view-applications&#34;&gt;2 - Building Single View Applications&lt;/h2&gt;
&lt;p&gt;Building single view applications is the starting point for most of us who are learning to build iOS apps because they allow us a simplified architecture that focuses on a single screen performing a single responsibility.&lt;/p&gt;
&lt;p&gt;Topics in this module include&amp;hellip;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Preparing to Build Single View Apps&lt;/li&gt;
&lt;li&gt;Understanding the SwiftUI Framework&lt;/li&gt;
&lt;li&gt;Understanding Views&lt;/li&gt;
&lt;li&gt;Combining and Laying out Views&lt;/li&gt;
&lt;li&gt;Understanding the SwiftUI Layout System&lt;/li&gt;
&lt;li&gt;Aligning and Positioning Views&lt;/li&gt;
&lt;li&gt;Customizing UI Appearance&lt;/li&gt;
&lt;li&gt;Developing a SwiftUI Mindset&lt;/li&gt;
&lt;li&gt;Using Icons and Symbols&lt;/li&gt;
&lt;li&gt;Managing Views with Groups&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;3---creating-data-driven-lists&#34;&gt;3 - Creating Data-driven Lists&lt;/h2&gt;
&lt;p&gt;Just about any day in which you use an iPhone or iPad for more than a few minutes, you&amp;rsquo;ll almost certainly end up in an app that uses a List View.&lt;/p&gt;
&lt;p&gt;It&amp;rsquo;s the most common way to show a structured, scrollable series of items, and List Views are a core competency for an iOS developer working in SwiftUI, so it&amp;rsquo;s the first complex control we dive into.&lt;/p&gt;
&lt;p&gt;Topics in this module include&amp;hellip;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Preparing to Work with Lists&lt;/li&gt;
&lt;li&gt;Understanding Lists&lt;/li&gt;
&lt;li&gt;Creating a List&lt;/li&gt;
&lt;li&gt;Controlling a List with Data&lt;/li&gt;
&lt;li&gt;Using ForEach to Supply List Content&lt;/li&gt;
&lt;li&gt;Adding Sections and Headings&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;4---building-ios-apps-with-multiple-screens&#34;&gt;4 - Building iOS Apps with Multiple Screens&lt;/h2&gt;
&lt;p&gt;Here I cover how to create apps multiple screens and how to think about and enable navigation between them.&lt;/p&gt;
&lt;p&gt;Topics in this module include&amp;hellip;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Defining Apps with Multiple Screens&lt;/li&gt;
&lt;li&gt;Implementing Navigation Views&lt;/li&gt;
&lt;li&gt;Adding Navigation to an Existing Project&lt;/li&gt;
&lt;li&gt;Extracting Reusable Subviews&lt;/li&gt;
&lt;li&gt;Creating a Tabbed Application&lt;/li&gt;
&lt;li&gt;Recapping Multi-view Applications&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;5---adding-behavior-and-working-with-data&#34;&gt;5 - Adding Behavior and Working With Data&lt;/h2&gt;
&lt;p&gt;The thing that makes an app come alive is the behavior we add, and the dynamic responses to that behavior that get reflected in the user interface.&lt;/p&gt;
&lt;p&gt;In any application, a user interface has one critical job: to accurately represent the &lt;strong&gt;data&lt;/strong&gt; of our application in what it displays on the screen.&lt;/p&gt;
&lt;p&gt;In SwiftUI, data and behavior go hand-in-hand, and I cover how to respond to user interaction in this module.&lt;/p&gt;
&lt;p&gt;Topics in this module include&amp;hellip;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Preparing to Add Basic Behavior&lt;/li&gt;
&lt;li&gt;Extending the Data Model&lt;/li&gt;
&lt;li&gt;Understanding the View Update Cycle in SwiftUI&lt;/li&gt;
&lt;li&gt;Adding Basic Behavior&lt;/li&gt;
&lt;li&gt;Connecting a Subview to a Parent View’s Data&lt;/li&gt;
&lt;li&gt;Using Classes as Data Models&lt;/li&gt;
&lt;li&gt;Sharing a Data Model through the SwiftUI Environment&lt;/li&gt;
&lt;li&gt;Bringing the Data Model and Behavior to the Night Watch App&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;6---putting-it-all-together&#34;&gt;6 - Putting It All Together&lt;/h2&gt;
&lt;p&gt;There’s nothing like practicing your skills in new contexts to refine them and solidify what you’ve learned.&lt;/p&gt;
&lt;p&gt;We go into some additional controls and techniques for building apps with SwiftUI before concluding the course.&lt;/p&gt;
&lt;p&gt;Topics in this module include&amp;hellip;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Applying What You’ve Learned&lt;/li&gt;
&lt;li&gt;Integrating the Extended Data Model&lt;/li&gt;
&lt;li&gt;Adding the Mark Complete Behavior&lt;/li&gt;
&lt;li&gt;Adding a Toolbar and Toggle Control&lt;/li&gt;
&lt;li&gt;Implementing Swipe and Move Actions for a List&lt;/li&gt;
&lt;li&gt;Resetting the List&lt;/li&gt;
&lt;li&gt;Showing an Alert&lt;/li&gt;
&lt;li&gt;Creating an Adaptive UI&lt;/li&gt;
&lt;li&gt;Adding and Customizing Layout Variations&lt;/li&gt;
&lt;li&gt;Getting Ready for the App Store&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;7---publishing-to-the-app-store&#34;&gt;7 - Publishing to the App Store&lt;/h2&gt;
&lt;p&gt;Finishing an app and getting it out the door to your users is one of the best feelings in the world.&lt;/p&gt;
&lt;p&gt;Beyond the emotional aspect of publishing an app though, more and more employers want to see job candidates who have published at least one app to the App Store.&lt;/p&gt;
&lt;p&gt;So in this final module, I complete the journey by setting you up to get an app into the App Store and bolster your resume!&lt;/p&gt;
&lt;p&gt;Topics in this module include&amp;hellip;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Preparing to Publish Your App&lt;/li&gt;
&lt;li&gt;Adding Images and App Icons&lt;/li&gt;
&lt;li&gt;Responding to Dark Mode&lt;/li&gt;
&lt;li&gt;Creating an iOS App Launch Screen&lt;/li&gt;
&lt;li&gt;Submitting an App for Review&lt;/li&gt;
&lt;li&gt;Continuing Your iOS Developer Journey&lt;/li&gt;
&lt;/ul&gt;
&lt;h1 id=&#34;feedback-welcome&#34;&gt;Feedback Welcome!&lt;/h1&gt;
&lt;p&gt;From the beginning my unwavering goal is to help you filter the noise and learn as efficiently as possible.  My hope is that this course benefits you in your iOS development career!&lt;/p&gt;
&lt;p&gt;I welcome feedback on this course, and on other iOS development courses you might be interested in seeing in the Pluralsight library. Happy learning!&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>What Are All of the Possible Environment Keys in SwiftUI?</title>
      <link>https://www.andrewcbancroft.com/blog/ios-development/ui-work/swiftui-what-environment-keys-possible/</link>
      <pubDate>Tue, 08 Sep 2020 05:11:48 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/blog/ios-development/ui-work/swiftui-what-environment-keys-possible/</guid>
      <description>&lt;p&gt;This is merely a breadcrumb for finding the Apple Developer documentation page where all of the possible SwiftUI Environment keys are listed.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;resources&#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;i class=&#34;fas fa-link&#34;&gt;&lt;/i&gt; &lt;a href=&#34;https://developer.apple.com/documentation/swiftui/environmentvalues&#34; rel=&#34;nofollow&#34; target=&#34;_blank&#34;&gt;Apple Documentation ➡️ SwiftUI ➡️ State and Data Flow ➡️ EnvironmentValues&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
</description>
    </item>
    
    <item>
      <title>WWDC - The Worldwide Developer Community</title>
      <link>https://www.andrewcbancroft.com/blog/musings/wwdc-worldwide-developer-community/</link>
      <pubDate>Mon, 29 Jun 2020 00:00:00 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/blog/musings/wwdc-worldwide-developer-community/</guid>
      <description>&lt;p&gt;June is an invigorating time for iOS developers worldwide.&lt;/p&gt;
&lt;p&gt;Invigorating&amp;hellip;  And exhausting.&lt;/p&gt;
&lt;p&gt;Me personally?  The flurry of new-ness often has me feeling overwhelmed.&lt;/p&gt;
&lt;h2 id=&#34;wwdc---the-worldwide-developer-chase&#34;&gt;WWDC - The Worldwide Developer Chase&lt;/h2&gt;
&lt;p&gt;When it comes time for WWDC, are you like me?  Do you conjure up a mental image of a race?&lt;/p&gt;
&lt;p&gt;It&amp;rsquo;s as if all of us who are enrolled in the Apple Developer Program line up at the starting line and wait for the pistol to fire, signaling the sprint that is the Worldwide Developer &lt;del&gt;Conference&lt;/del&gt; Chase.&lt;/p&gt;
&lt;p&gt;The race metaphor induces pressure.&lt;/p&gt;
&lt;p&gt;Pressure to figure things out as quickly as you can.&lt;br&gt;
Pressure to be one of the rist to download Xcode {next}.&lt;br&gt;
Pressure to install the new beta operating systems.&lt;br&gt;
Pressure to master the new APIs and features of Apple&amp;rsquo;s platforms.
Pressure to make a discovery, publish it out, and get noticed.&lt;/p&gt;
&lt;p&gt;For me, it can be discouraging to watch the Twitter feeds of others who seem to be &lt;strong&gt;much faster&lt;/strong&gt; than me at all of those things.&lt;/p&gt;
&lt;p&gt;I watch the race to &amp;ldquo;be first&amp;rdquo; go on around me and realize:  I can&amp;rsquo;t compete.&lt;/p&gt;
&lt;p&gt;But&amp;hellip; Why?  Why do this to ourselves?&lt;/p&gt;
&lt;h2 id=&#34;wwdc---the-worldwide-developer-community&#34;&gt;WWDC - The Worldwide Developer Community&lt;/h2&gt;
&lt;p&gt;What if we could shift the thinking?&lt;/p&gt;
&lt;p&gt;If you build apps because it&amp;rsquo;s something you love to do, here&amp;rsquo;s what I propose:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Nix the race metaphor&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;If writing Swift is something you enjoy because it&amp;rsquo;s fulfilling in itself, there&amp;rsquo;s no need to worry about how fast you&amp;rsquo;re able to grasp all the new-ness.&lt;/p&gt;
&lt;p&gt;Competitors are always going to be out there, always trying to one-up another.  That&amp;rsquo;s not the group of people I want to be a part of.&lt;/p&gt;
&lt;p&gt;Instead, I gravitate to the global body of like-minded Swifters who, like me, enjoy building apps and contributing our share of bytes to the Internet in order to nudge the world in a positive direction.  Thankfully, there are &lt;strong&gt;plenty&lt;/strong&gt; of these kinds of folks out there to befriend.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Bottom line:&lt;/strong&gt; Take your time.  Enjoy yourself.&lt;/p&gt;
&lt;p&gt;Build that example Xcode project and publish it to GitHub.&lt;/p&gt;
&lt;p&gt;Tweet that new discovery that someone else already discovered and tweeted about before you.&lt;/p&gt;
&lt;p&gt;Write that blog post without worrying with how much attention you garner from others.&lt;/p&gt;
&lt;p&gt;It&amp;rsquo;s okay!&lt;/p&gt;
&lt;p&gt;Do what you do because you love it.  I guarantee it will impact &lt;em&gt;somebody&lt;/em&gt;.  &lt;em&gt;&lt;a href=&#34;https://twitter.com/andrewcbancroft&#34;&gt;@me&lt;/a&gt;&lt;/em&gt; &amp;ndash; &lt;em&gt;I&amp;rsquo;d&lt;/em&gt; love to hear what you&amp;rsquo;re discovering!&lt;/p&gt;
&lt;p&gt;WWDC isn&amp;rsquo;t a Worldwide Developer &lt;strong&gt;Competition&lt;/strong&gt; when you&amp;rsquo;re contributing to the Worldwide Developer &lt;strong&gt;Community&lt;/strong&gt;.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>StoreKit Testing Swift Start</title>
      <link>https://www.andrewcbancroft.com/blog/ios-development/iap/storekit-testing-swift-start/</link>
      <pubDate>Thu, 25 Jun 2020 00:00:00 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/blog/ios-development/iap/storekit-testing-swift-start/</guid>
      <description>&lt;h1 id=&#34;manually-testing-storekit-code&#34;&gt;Manually Testing StoreKit Code&lt;/h1&gt;
&lt;h2 id=&#34;development-workflow-changes&#34;&gt;Development Workflow Changes&lt;/h2&gt;
&lt;p&gt;The previous workflow for setting up in-app purchases was a bit disjointed.&lt;/p&gt;
&lt;p&gt;You&amp;rsquo;d have to create your app in Xcode&amp;hellip; then jump out to App Store Connect to define your app &lt;em&gt;there&lt;/em&gt;&amp;hellip; set up your digital products &lt;em&gt;there&lt;/em&gt; (in App Store Connect)&amp;hellip; and then jump &lt;em&gt;back&lt;/em&gt; to Xcode to write your StoreKit code.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Now&lt;/strong&gt;, you can get started in Xcode, and &lt;em&gt;stay&lt;/em&gt; in Xcode.  Here&amp;rsquo;s how:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Go to the File menu&lt;/li&gt;
&lt;li&gt;Click New ➡️ File&lt;/li&gt;
&lt;li&gt;Type &amp;ldquo;storekit&amp;rdquo; into the filter&lt;/li&gt;
&lt;li&gt;Click Next to add a StoreKit Configuration File ✅&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img src=&#34;add-storekit-configuration.gif&#34; alt=&#34;Add StoreKit Configuration File&#34;&gt;&lt;/p&gt;
&lt;p&gt;From there, you get to add the digital products you intend to offer in your app, all without leaving Xcode!&lt;/p&gt;
&lt;p&gt;Xcode offers support for configuring all three product types:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Consumable&lt;/li&gt;
&lt;li&gt;Non-Consumable&lt;/li&gt;
&lt;li&gt;Auto-Renewable Subscription&lt;/li&gt;
&lt;/ul&gt;
&lt;blockquote&gt;
&lt;p&gt;‼️Note: It appears that Non-Renewing Subscriptions are no longer encouraged&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src=&#34;set-up-products.png&#34; alt=&#34;Set Up Products&#34;&gt;&lt;/p&gt;
&lt;p&gt;Once you have all of your products configured and your Store view written, you&amp;rsquo;ll want to &lt;em&gt;use&lt;/em&gt; your configuration for testing.&lt;/p&gt;
&lt;p&gt;To do it, you need to edit your app&amp;rsquo;s scheme:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Click your app target in the toolbar&lt;/li&gt;
&lt;li&gt;Click Edit Scheme&amp;hellip;&lt;/li&gt;
&lt;li&gt;Click Options&lt;/li&gt;
&lt;li&gt;Find StoreKit Configuration, and select the configuration you&amp;rsquo;ve added for your project in the dropdown ✅&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img src=&#34;edit-scheme.gif&#34; alt=&#34;Edit Scheme&#34;&gt;&lt;/p&gt;
&lt;h2 id=&#34;making-test-purchases&#34;&gt;Making Test Purchases&lt;/h2&gt;
&lt;p&gt;Now to actually test things!&lt;/p&gt;
&lt;p&gt;To test things locally, you don&amp;rsquo;t need to create a sandbox tester account first (something &lt;em&gt;else&lt;/em&gt; you used to have to do before testing in-app purchases).&lt;/p&gt;
&lt;p&gt;Everything happens locally.&lt;/p&gt;
&lt;p&gt;All of your code that would be communicating with the App Store itself in Production, is &amp;ldquo;taken care of&amp;rdquo; by Xcode.&lt;/p&gt;
&lt;p&gt;So you may be wondering&amp;hellip;&lt;/p&gt;
&lt;p&gt;❓&amp;ldquo;Do my SKPaymentTransactionObserver functions get executed when they&amp;rsquo;re supposed to?&amp;rdquo;&lt;/p&gt;
&lt;p&gt;👉&lt;strong&gt;Yes&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;❓&amp;ldquo;Do I get a receipt for the purchase?&amp;rdquo;&lt;/p&gt;
&lt;p&gt;👉&lt;strong&gt;Yes&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;❓&amp;ldquo;Can I use that receipt to validate successfully?&amp;rdquo;&lt;/p&gt;
&lt;p&gt;👉&lt;strong&gt;Kinda&lt;/strong&gt;. Receipts from the local Xcode environment are not signed with the same signature as the Sandbox and Production environments are, so they cannot be used if you&amp;rsquo;re implementing server-side receipt validation.&lt;/p&gt;
&lt;h2 id=&#34;development-workflow-enhancements&#34;&gt;Development Workflow Enhancements&lt;/h2&gt;
&lt;h3 id=&#34;storekit-transaction-manager&#34;&gt;StoreKit Transaction Manager&lt;/h3&gt;
&lt;p&gt;How often does your implementation work perfectly &lt;strong&gt;the first time&lt;/strong&gt;?&lt;/p&gt;
&lt;p&gt;Rarely?&lt;/p&gt;
&lt;p&gt;Yeah&amp;hellip; I&amp;rsquo;m with you.&lt;/p&gt;
&lt;p&gt;With the new &lt;strong&gt;StoreKit Transaction Manager&lt;/strong&gt;, it&amp;rsquo;s easy to &amp;ldquo;clear things out&amp;rdquo; so that previous test purchases that didn&amp;rsquo;t go so well can be tried again fresh, as if a purchase never happened.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;storekit-transaction-manager.gif&#34; alt=&#34;StoreKit Transaction Manager&#34;&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Note: If you don&amp;rsquo;t see the StoreKit Transaction Manager icon in the debug console, make sure you&amp;rsquo;ve added the In-App Purchasing capability to your app.  Then try again.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3 id=&#34;test-edge-cases&#34;&gt;Test Edge Cases&lt;/h3&gt;
&lt;p&gt;To support testing out what could be considered &amp;ldquo;edge cases&amp;rdquo; in the in-app purchasing experience, Xcode 12 also has the ability to simulate things like:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Time Rate for Auto-Renewable Subscription expiration&lt;/li&gt;
&lt;li&gt;Interrupted purchases&lt;/li&gt;
&lt;li&gt;Failed transactions&lt;/li&gt;
&lt;li&gt;Ask To Buy&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img src=&#34;edge-cases.png&#34; alt=&#34;Edge Cases&#34;&gt;&lt;/p&gt;
&lt;h3 id=&#34;auto-renewable-subscription-testing-enhancements&#34;&gt;Auto-Renewable Subscription Testing Enhancements&lt;/h3&gt;
&lt;p&gt;Testing subscription expiration is important for preserving the revenue you intend to generate from auto-renewable subscriptions.&lt;/p&gt;
&lt;p&gt;The challenge is that you and I don&amp;rsquo;t have time to wait!&lt;/p&gt;
&lt;p&gt;Previously, you could update your product in App Store Connect to have a shorter duration, and the Sandbox environment would interpret, say, a six month subscription to be &amp;ldquo;30 minutes&amp;rdquo; worth of time.&lt;/p&gt;
&lt;p&gt;But that&amp;rsquo;s still a long time to wait between making a subscription purchase, and trying out expiration logic, isn&amp;rsquo;t it?&lt;/p&gt;
&lt;p&gt;Problem. Solved.  Now you can select even shorter durations for testing subscription expiration using the Time Rate menu option in Xcode&amp;rsquo;s editor menu &lt;strong&gt;when you&amp;rsquo;re on the StoreKit configuration page&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;time-rate.png&#34; alt=&#34;Time Rate&#34;&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Remember: To see this editor menu, you have to be in the StoreKit configuration file of your app.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;If a second equals a day, and there&amp;rsquo;s 30ish days in a month, a six month subscription should expire and auto-renew in &lt;strong&gt;three-ish&lt;/strong&gt; minutes now.&lt;/p&gt;
&lt;p&gt;A huge help for impatient types like myself!&lt;/p&gt;
&lt;h1 id=&#34;automated-testing-for-storekit-code&#34;&gt;Automated Testing for StoreKit Code&lt;/h1&gt;
&lt;p&gt;Tired of launching your app and clicking around manually to test things out?&lt;/p&gt;
&lt;p&gt;Not only is that tedious, you and I are bound to either miss something, or test inconsistently, leading to a potentially sub-par end-user experience.&lt;/p&gt;
&lt;p&gt;Apple has given us a new &lt;strong&gt;StoreKitTest framework&lt;/strong&gt; that you can use within XCTest!&lt;/p&gt;
&lt;p&gt;Everything that you can point-and-click on is fully testable with unit and UI tests with zero human interaction.&lt;/p&gt;
&lt;p&gt;To get started using this new framework, it&amp;rsquo;s &lt;code&gt;import StoreKitTest&lt;/code&gt;.&lt;/p&gt;
&lt;h1 id=&#34;is-the-sandbox-environment-still-useful&#34;&gt;Is the Sandbox Environment Still Useful?&lt;/h1&gt;
&lt;p&gt;Super useful.&lt;/p&gt;
&lt;p&gt;Testing things out locally is amazingly powerful for getting you started, but before you deploy your app to the App Store and send it into review, you still have to&amp;hellip;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Set up your products in App Store Connect &amp;hellip; right now the Xcode configuration doesn&amp;rsquo;t get &amp;ldquo;pushed up&amp;rdquo; to App Store Connect, so it&amp;rsquo;s a double-entry world right now.&lt;/li&gt;
&lt;li&gt;Set up a Sandbox tester account in App Store Connect so that you can&amp;hellip;&lt;/li&gt;
&lt;li&gt;Test out your in-app purchasing workflow and receipt validation logic with receipts signed by a &amp;ldquo;real&amp;rdquo; App Store environment (just like your TestFlight testers and your &lt;strong&gt;App Reviewer&lt;/strong&gt; will use)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;And if you&amp;rsquo;re using App Store server notifications to detect changes to auto-renewable subscriptions, you need to test those out as well.&lt;/p&gt;
&lt;p&gt;So for all of these reasons, the Sandbox environment is still there, and is still useful.&lt;/p&gt;
&lt;p&gt;Alas, &lt;em&gt;some&lt;/em&gt; manual testing is still required, but can you imagine some of the possibilities for getting started with implementing in-app purchases, right in Xcode, on day zero of your project?&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Implementing In-App Purchases on iOS</title>
      <link>https://www.andrewcbancroft.com/pluralsight/2020-06-15-implementing-in-app-purchases-on-ios/</link>
      <pubDate>Tue, 09 Jun 2020 10:03:33 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/pluralsight/2020-06-15-implementing-in-app-purchases-on-ios/</guid>
      <description>&lt;p&gt;Monetizing apps is challenging. In this course, Implementing In-App Purchases on iOS, you’ll learn to offer digital products and services directly within your app as in-app purchases.&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;https://bit.ly/implementing-in-app-purchases-ios&#34; target=&#34;_blank&#34;&gt;Implementing In-App Purchases on iOS&lt;/a&gt;&lt;a href=&#34;https://bit.ly/implementing-in-app-purchases-ios&#34; target=&#34;_blank&#34;&gt;&lt;br /&gt; &lt;img src=&#34;https://www.andrewcbancroft.com/images/social-assets/implementing-in-app-purchases-ios-title.png&#34; alt=&#34;Implementing In-App Purchases on iOS&#34; width=&#34;1024&#34; height=&#34;576&#34; class=&#34;alignnone size-large wp-image-13737&#34;/&gt;&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;
&lt;h1 id=&#34;course-outline&#34;&gt;Course Outline&lt;/h1&gt;
&lt;p&gt;First, you’ll explore how to translate business requirements into digital products and configure them in App Store Connect and Xcode.&lt;/p&gt;
&lt;p&gt;Next, you’ll discover how to build and test a store within your app using StoreKit.&lt;/p&gt;
&lt;p&gt;Finally, you’ll learn how to make sure purchases made within your app are authentic, and unlock content that’s been purchased by your users.&lt;/p&gt;
&lt;p&gt;When you’re finished with this course, you’ll have the skills and knowledge needed to implement in-app purchases on iOS!&lt;/p&gt;
&lt;h2 id=&#34;1--preparing-to-implement-in-app-purchases&#34;&gt;1 – Preparing to Implement In-App Purchases&lt;/h2&gt;
&lt;p&gt;Prepare to implement in-app purchases by understanding what’s involved from a big picture standpoint.&lt;/p&gt;
&lt;p&gt;You will learn to configure digital products in App Store Connect and Xcode to kick off your in-app purchase learning adventure.&lt;/p&gt;
&lt;p&gt;Topics in this module include&amp;hellip;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The Perks (And the Work) Ahead of You&lt;/li&gt;
&lt;li&gt;First Things First - Checking off the Prerequisites&lt;/li&gt;
&lt;li&gt;Matching Product Types to Business Requirements&lt;/li&gt;
&lt;li&gt;Setting Up Products in App Store Connect&lt;/li&gt;
&lt;li&gt;Setting Up an Auto-Renewable Subscription&lt;/li&gt;
&lt;li&gt;Configuring an Xcode Project for In-App Purchases&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;2--building-in-app-purchase-functionality-with-storekit&#34;&gt;2 – Building In-App Purchase Functionality with StoreKit&lt;/h2&gt;
&lt;p&gt;The focus of this module is on the StoreKit APIs for working with digital products and App Store Connect.&lt;/p&gt;
&lt;p&gt;You will learn to build and test a fully-working store view that will support in-app purchases.&lt;/p&gt;
&lt;p&gt;Topics in this module include&amp;hellip;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Using StoreKit to Build In-App Purchase Functionality: A Roadmap&lt;/li&gt;
&lt;li&gt;Retrieving a Set of Products for Purchase from the App Store&lt;/li&gt;
&lt;li&gt;Displaying Products in a User Interface&lt;/li&gt;
&lt;li&gt;Implementing a “Buy” Button&lt;/li&gt;
&lt;li&gt;Preparing to Test Purchases by Setting up a Sandbox Tester Account&lt;/li&gt;
&lt;li&gt;Making Test Purchases&lt;/li&gt;
&lt;li&gt;Implementing a “Restore Purchases” Button&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;3--protecting-your-revenue-by-verifying-purchase-authenticity&#34;&gt;3 – Protecting Your Revenue by Verifying Purchase Authenticity&lt;/h2&gt;
&lt;p&gt;After all your hard work to implement in-app purchases, how awful would it be if malicious users &lt;em&gt;stole&lt;/em&gt; the content you&amp;rsquo;ve labored to create?&lt;/p&gt;
&lt;p&gt;In this module, you will learn to implement measures to protect revenue generated through in-app purchases by validating App Store receipts.&lt;/p&gt;
&lt;p&gt;Receipt validation is a notoriously burdensome hassle. But it doesn&amp;rsquo;t have to be!&lt;/p&gt;
&lt;p&gt;I will teach you how to implement server-side receipt validation from end-to-end with &lt;em&gt;no&lt;/em&gt; third-party libraries, &lt;em&gt;no&lt;/em&gt; complicated server setup, and &lt;em&gt;no&lt;/em&gt; platform-specific proprietary tools.&lt;/p&gt;
&lt;p&gt;Here&amp;rsquo;s the roadmap of topics incluced:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Verifying Purchase Authenticity by Validating App Store Receipts&lt;/li&gt;
&lt;li&gt;Weighing Strategies for Receipt Validation&lt;/li&gt;
&lt;li&gt;Charting the Course for Server-side Receipt Validation&lt;/li&gt;
&lt;li&gt;Sending the Receipt from Your App to Your Server&lt;/li&gt;
&lt;li&gt;Writing Server-side Code to Forward the Receipt to the App Store for Validation&lt;/li&gt;
&lt;li&gt;Handling the Response from the App Store on Your Server&lt;/li&gt;
&lt;li&gt;Accounting for the Sandbox Environment on Your Server&lt;/li&gt;
&lt;li&gt;Handling the Response from Your Server in Your App&lt;/li&gt;
&lt;li&gt;The Case of the Missing Receipt: Requesting a Refresh&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;4--unlocking-purchased-content&#34;&gt;4 – Unlocking Purchased Content&lt;/h2&gt;
&lt;p&gt;After a user makes a valid purchase, you&amp;rsquo;ve got to unlock the content they legitimately bought!&lt;/p&gt;
&lt;p&gt;Each product type has its own nuances for how to unlock and provide the associated content though.&lt;/p&gt;
&lt;p&gt;What do you need to think about, and what code do you need to write to make it happen?&lt;/p&gt;
&lt;p&gt;To find out, you will explore these topics:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Analyzing Content-providing Logic by Product Type&lt;/li&gt;
&lt;li&gt;Persisting Purchased Products Securely&lt;/li&gt;
&lt;li&gt;Using the iOS Keychain&lt;/li&gt;
&lt;li&gt;Keeping Track of Consumable Product Inventory&lt;/li&gt;
&lt;li&gt;Managing Non-Renewing Subscriptions&lt;/li&gt;
&lt;li&gt;Synchronizing Purchases Across Devices&lt;/li&gt;
&lt;li&gt;Handling Auto-Renewable Subscriptions&lt;/li&gt;
&lt;li&gt;Saving Non-Consumable Transactions&lt;/li&gt;
&lt;li&gt;Preparing for App Store Review&lt;/li&gt;
&lt;/ul&gt;
&lt;h1 id=&#34;feedback-welcome&#34;&gt;Feedback Welcome!&lt;/h1&gt;
&lt;p&gt;From the beginning I set out to make the course I wish &lt;em&gt;I&lt;/em&gt; had when I started learning to implement in-app purchases for &lt;em&gt;my&lt;/em&gt; apps.  My unwavering goal is to help you filter the noise and learn as efficiently as possible.  My hope is that this course benefits you in your iOS development career!&lt;/p&gt;
&lt;p&gt;I welcome feedback on this course, and on other iOS development courses you might be interested in seeing in the Pluralsight library. Happy learning!&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>iOS Receipt Validation: Understanding and Troubleshooting the Original App Version Field</title>
      <link>https://www.andrewcbancroft.com/blog/ios-development/iap/ios-receipt-validation-understanding-troubleshooting-original-app-version-field/</link>
      <pubDate>Wed, 05 Feb 2020 00:00:00 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/blog/ios-development/iap/ios-receipt-validation-understanding-troubleshooting-original-app-version-field/</guid>
      <description>&lt;h2 id=&#34;setting-the-stage-with-a-use-case-for-checking-the-original_application_version&#34;&gt;Setting the stage with a use case for checking the original_application_version&lt;/h2&gt;
&lt;p&gt;The task appears to be simple.  Suppose that&amp;hellip;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;You have an existing app on the app store that&amp;rsquo;s currently at version 1.2&lt;/li&gt;
&lt;li&gt;You want to shift to the &amp;ldquo;freemium&amp;rdquo; model by implementing in-app purchases starting with version 1.3&lt;/li&gt;
&lt;li&gt;You want to be fair to previous-purchasers of my app (anyone who downloaded the app between version 1.0 and 1.2)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Therefore&amp;hellip;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;You want to check to see if someone originally purchased version 1.2 or below of your app so that you can &amp;ldquo;grandfather them in&amp;rdquo;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;To figure this out, you&amp;rsquo;ll need to &lt;a href=&#34;2017/07/31/finalizing-receipt-validation-in-swift-computing-a-guid-hash/&#34;&gt;implement local receipt validation&lt;/a&gt; or set up an environment to do &lt;a href=&#34;https://developer.apple.com/library/archive/releasenotes/General/ValidateAppStoreReceipt/Chapters/ValidateRemotely.html#//apple_ref/doc/uid/TP40010573-CH104-SW1&#34;&gt;remote receipt validation&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;When you&amp;rsquo;re done, you&amp;rsquo;ll get access to a field that contains an &amp;ldquo;original application version&amp;rdquo;.&lt;/p&gt;
&lt;p&gt;The temptation is to inspect this field, parse it out into major and minor version numbers, and write some conditional logic to unlock (or not) certain features of your app.&lt;/p&gt;
&lt;p&gt;But&amp;hellip;&lt;/p&gt;
&lt;h2 id=&#34;what-does-original_application_version-really-mean&#34;&gt;What does original_application_version really mean?&lt;/h2&gt;
&lt;p&gt;&amp;ldquo;Original application version&amp;rdquo; may not mean what you think it means.&lt;/p&gt;
&lt;p&gt;What &lt;em&gt;does&lt;/em&gt; it mean?&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Original application version&amp;rdquo; refers to the value of &lt;strong&gt;the Build field of your Xcode project&lt;/strong&gt; at the time a user downloaded it from the App Store:
&lt;img src=&#34;Xcode-project-build-field.png&#34; alt=&#34;Xcode Project Build Field 1.2.5&#34;&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Just&lt;/strong&gt; the Build field?&lt;/p&gt;
&lt;p&gt;Yes. &lt;em&gt;Just&lt;/em&gt; the Build field.&lt;/p&gt;
&lt;p&gt;And that&amp;rsquo;s weird, because when you and I think of &amp;ldquo;application versions&amp;rdquo;, we naturally think in terms of the &lt;a href=&#34;https://semver.org/&#34;&gt;semantic version number&lt;/a&gt; of our apps.&lt;/p&gt;
&lt;p&gt;Our brains think &amp;ldquo;1.2.5&amp;rdquo; as &amp;ldquo;{majorVersion}.{minorVersion}.{patchNumber}&amp;rdquo;.&lt;/p&gt;
&lt;p&gt;Many folks use that natural intuition and splice together the Version and Build fields from the Xcode project configuration screen to make what could be considered to be the full semantic version of an app.&lt;/p&gt;
&lt;p&gt;If you follow this logic through and conclude that the Version + Build fields are what appears in the &lt;code&gt;original_application_version&lt;/code&gt; field of the Receipt, you&amp;rsquo;ll be in for a rude awakening like I was.&lt;/p&gt;
&lt;p&gt;Your grandfathering unlocking logic could be buggy if you&amp;rsquo;re thinking &amp;ldquo;Build == patch number&amp;rdquo; and Apple is thinking &amp;ldquo;Build == application version at time of download from App Store&amp;rdquo;.&lt;/p&gt;
&lt;h2 id=&#34;gotchas-when-inspecting-the-original_application_version-field&#34;&gt;Gotchas when inspecting the original_application_version field&lt;/h2&gt;
&lt;h3 id=&#34;sandbox-vs-app-store-receipts&#34;&gt;Sandbox vs App Store receipts&lt;/h3&gt;
&lt;p&gt;To make things even trickier, the value that&amp;rsquo;s present in this &lt;a href=&#34;https://www.andrewcbancroft.com/2017/07/27/receipt-validation-parsing-a-receipt-with-swift/&#34;&gt;parsed receipt&lt;/a&gt; field is unlikely to be the same between the receipt you get from the sandbox environment and the receipt you get from the production environment (the App Store).&lt;/p&gt;
&lt;p&gt;According to &lt;a href=&#34;https://developer.apple.com/library/archive/releasenotes/General/ValidateAppStoreReceipt/Chapters/ReceiptFields.html&#34;&gt;Apple&amp;rsquo;s Receipt Validation Programming Guide&lt;/a&gt;, the &amp;ldquo;original application version&amp;rdquo; field &lt;strong&gt;always contains &amp;ldquo;1.0&amp;rdquo; in the sandbox environment&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;The only way that your sandbox receipt and your App Store receipt &amp;ldquo;original application version&amp;rdquo; fields will match is if you put &amp;ldquo;1.0&amp;rdquo; in the Build field of your Xcode project and deploy to the App Store.&lt;/p&gt;
&lt;h3 id=&#34;version--build-confusion&#34;&gt;Version / Build confusion&lt;/h3&gt;
&lt;p&gt;Furthermore, the fact that the sandbox environment has &amp;ldquo;1.0&amp;rdquo; as its value makes it look like it contains a {majorVersion}.{minorVersion} combo.&lt;/p&gt;
&lt;p&gt;It&amp;rsquo;s deceiving.&lt;/p&gt;
&lt;p&gt;It might &lt;em&gt;seem&lt;/em&gt; like &amp;ldquo;original application version&amp;rdquo; corresponds to the &lt;strong&gt;Version&lt;/strong&gt; field instead of the &lt;strong&gt;Build&lt;/strong&gt; field of your Xcode project configuration.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;But it doesn&amp;rsquo;t.&lt;/em&gt;&lt;/p&gt;
&lt;h2 id=&#34;build-number-cautions&#34;&gt;Build number cautions&lt;/h2&gt;
&lt;p&gt;Coming full circle now.&lt;/p&gt;
&lt;p&gt;In the past, I often used the Build field of my Xcode project to mean the equivalent of a patch number for a given major-minor version of my app.&lt;/p&gt;
&lt;p&gt;Using &amp;ldquo;Build&amp;rdquo; to mean &amp;ldquo;patch number&amp;rdquo; is a less-than-ideal use of the Build field if you plan to do app version checking for &amp;ldquo;grandfathering in&amp;rdquo; like I did.&lt;/p&gt;
&lt;p&gt;Why?&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;1) Apple uses it to mean &amp;ldquo;app version at time of download&amp;rdquo;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;By now you might be realizing that the using your Xcode project&amp;rsquo;s Build field to mean &amp;ldquo;patch number&amp;rdquo; when &lt;em&gt;Apple&lt;/em&gt; uses it to mean &amp;ldquo;app version at time of download&amp;rdquo; presents some discrepancies in meaning, yes?&lt;/p&gt;
&lt;p&gt;When it comes to receipt checking, it only makes sense to come to terms with what Build means &lt;em&gt;to Apple&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;You&amp;rsquo;ll set yourself up for success if you adjust &lt;em&gt;your&lt;/em&gt; ussage to match theirs.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;2) Build numbers aren&amp;rsquo;t unique&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Build numbers can be reused across releases of your app.&lt;/p&gt;
&lt;p&gt;Suppose that you decide you want to use your Xcode project&amp;rsquo;s Version and Build fields to construct a &amp;ldquo;semantic version&amp;rdquo; for your app.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;You use &amp;ldquo;1.0&amp;rdquo; as your Xcode project&amp;rsquo;s Version.&lt;/li&gt;
&lt;li&gt;You implement five rounds of bug fixes and land on &amp;ldquo;5&amp;rdquo; as your Xcode project&amp;rsquo;s Build.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In this way, you encode &amp;ldquo;1.0.5&amp;rdquo; as the semantic version of your app.&lt;/p&gt;
&lt;p&gt;Now suppose that later on&amp;hellip;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;You use &amp;ldquo;1.1&amp;rdquo; as your Xcode project&amp;rsquo;s Version.&lt;/li&gt;
&lt;li&gt;You implement five more rounds bug fixes for version 1.1 and land on &amp;ldquo;5&amp;rdquo; as your Xcode project&amp;rsquo;s Build.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In this way, you encode &amp;ldquo;1.1.5&amp;rdquo; as the &amp;ldquo;semantic version&amp;rdquo; of your app.&lt;/p&gt;
&lt;p&gt;Guess what: The receipts for both app releases will contain &amp;ldquo;5&amp;rdquo; in the &amp;ldquo;original application version&amp;rdquo; field.&lt;/p&gt;
&lt;p&gt;Why?&lt;/p&gt;
&lt;p&gt;Because &amp;ldquo;original application version&amp;rdquo; only contains the value from the Build field, and that&amp;rsquo;s the Build value that landed in the App Store for both releases.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;3) Using sequential &amp;ldquo;patch number&amp;rdquo; integers for the Build doesn&amp;rsquo;t contain enough encoded information to perform &amp;ldquo;grandfathering in&amp;rdquo; logic&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;To set yourself up for &amp;ldquo;grandfathering in&amp;rdquo; logic, you want to encode enough meaning in the Build field to write the most appropriate conditional logic for locking/unlocking your app&amp;rsquo;s purchase-only features.&lt;/p&gt;
&lt;h2 id=&#34;choosing-a-strategic-build-value&#34;&gt;Choosing a strategic Build value&lt;/h2&gt;
&lt;p&gt;What could you do?  Here are a few ideas:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Regardless of what&amp;rsquo;s in the Version field of your Xcode project configuration, you could encode the &lt;strong&gt;full semantic version&lt;/strong&gt; of your app in the Build field.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Doing this will let you parse the &amp;ldquo;original application version&amp;rdquo; value like you&amp;rsquo;d expect and implement the right &lt;code&gt;if-else&lt;/code&gt; statements in your code.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;You could encode more meaningful information in the Build field, such as a &amp;ldquo;date&amp;rdquo; represented as an integer&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Suppose that it&amp;rsquo;s January 1, 2020 and you&amp;rsquo;re preparing builds for an App Store submission.&lt;/p&gt;
&lt;p&gt;You could put &amp;ldquo;20200101.1&amp;rdquo; in the Build field to represent the first build of today&amp;rsquo;s submission.&lt;/p&gt;
&lt;p&gt;Things may not go perfect during testing with the first upload.&lt;/p&gt;
&lt;p&gt;If you need to submit a new build, you could change the Build field&amp;rsquo;s value to &amp;ldquo;20200101.2&amp;rdquo; to represent the second build of today&amp;rsquo;s submission.&lt;/p&gt;
&lt;p&gt;In both cases, you&amp;rsquo;ve encoded a &amp;ldquo;date&amp;rdquo; as an integer inside of the Build field.&lt;/p&gt;
&lt;p&gt;Now suppose that you want to grandfather in everyone who bought your app prior to today (January 1, 2020 from the scenario above).&lt;/p&gt;
&lt;p&gt;Here&amp;rsquo;s the pseudo code narrative of the branching logic you&amp;rsquo;d write:&lt;/p&gt;
&lt;p&gt;&amp;ldquo;If the user&amp;rsquo;s receipt contains a value less than 20200101 in his/her original application version field, consider them an original purchaser, grandfather them in, and unlock the feature.  Otherwise, make them buy the in-app purchase before unlocking.&amp;rdquo;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Bottom line:&lt;/strong&gt; If you can come to terms with the nuances of your app receipt&amp;rsquo;s &amp;ldquo;original application version&amp;rdquo; field, you&amp;rsquo;ll ensure that your app works correctly in production.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Privacy Policy</title>
      <link>https://www.andrewcbancroft.com/privacy/privacy-policy/</link>
      <pubDate>Mon, 27 Jan 2020 00:53:49 -0700</pubDate>
      <guid>https://www.andrewcbancroft.com/privacy/privacy-policy/</guid>
      <description>&lt;h1 id=&#34;privacy-notice&#34;&gt;Privacy Notice&lt;/h1&gt;
&lt;p&gt;This privacy notice discloses the privacy practices for and applies solely to information collected by the following websites and apps (SERVICE):&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;https://www.andrewcbancroft.com&#34;&gt;www.andrewcbancroft.com&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Ten Read Ten Said&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This page is used to inform website visitors regarding our policies with the collection, use, and disclosure of Personal Information if anyone decided to use our Service.&lt;/p&gt;
&lt;p&gt;If you choose to use our Service, then you agree to the collection and use of information in relation with this policy. The Personal Information that we collect are used for providing and improving the Service. We will not use or share your information with anyone except as described in this Privacy Policy.&lt;/p&gt;
&lt;h2 id=&#34;information-collection-use-and-sharing&#34;&gt;Information Collection, Use, and Sharing&lt;/h2&gt;
&lt;p&gt;For a better experience while using our Service, we may require you to provide us with certain personally identifiable information, including but not limited to your name, phone number, and postal address. The information that we collect will be used to contact or identify you.&lt;/p&gt;
&lt;h2 id=&#34;log-data&#34;&gt;Log Data&lt;/h2&gt;
&lt;p&gt;We want to inform you that whenever you visit our Service, we collect information that your browser sends to us that is called Log Data. This Log Data may include information such as your computer&amp;rsquo;s Internet Protocol (“IP”) address, browser version, pages of our Service that you visit, the time and date of your visit, the time spent on those pages, and other statistics.&lt;/p&gt;
&lt;h2 id=&#34;cookies&#34;&gt;Cookies&lt;/h2&gt;
&lt;p&gt;Cookies are files with small amount of data that is commonly used an anonymous unique identifier. These are sent to your browser from the website that you visit and are stored on your computer&amp;rsquo;s hard drive.&lt;/p&gt;
&lt;p&gt;Our website uses these “cookies” to collection information and to improve our Service. You have the option to either accept or refuse these cookies, and know when a cookie is being sent to your computer. If you choose to refuse our cookies, you may not be able to use some portions of our Service.&lt;/p&gt;
&lt;h2 id=&#34;service-providers&#34;&gt;Service Providers&lt;/h2&gt;
&lt;p&gt;We may employ third-party companies and individuals due to the following reasons:&lt;/p&gt;
&lt;p&gt;To facilitate our Service;
To provide the Service on our behalf;
To perform Service-related services; or
To assist us in analyzing how our Service is used.
We want to inform our Service users that these third parties have access to your Personal Information. The reason is to perform the tasks assigned to them on our behalf. However, they are obligated not to disclose or use the information for any other purpose.&lt;/p&gt;
&lt;h2 id=&#34;security&#34;&gt;Security&lt;/h2&gt;
&lt;p&gt;We value your trust in providing us your Personal Information, thus we are striving to use commercially acceptable means of protecting it. But remember that no method of transmission over the internet, or method of electronic storage is 100% secure and reliable, and we cannot guarantee its absolute security.&lt;/p&gt;
&lt;h2 id=&#34;links-to-other-sites&#34;&gt;Links to Other Sites&lt;/h2&gt;
&lt;p&gt;Our Service may contain links to other sites. If you click on a third-party link, you will be directed to that site. Note that these external sites are not operated by us. Therefore, we strongly advise you to review the Privacy Policy of these websites. We have no control over, and assume no responsibility for the content, privacy policies, or practices of any third-party sites or services.&lt;/p&gt;
&lt;h2 id=&#34;childrens-privacy&#34;&gt;Children&amp;rsquo;s Privacy&lt;/h2&gt;
&lt;p&gt;Our Services do not address anyone under the age of 13. We do not knowingly collect personal identifiable information from children under 13. In the case we discover that a child under 13 has provided us with personal information, we immediately delete this from our servers. If you are a parent or guardian and you are aware that your child has provided us with personal information, please contact us so that we will be able to do necessary actions.&lt;/p&gt;
&lt;h2 id=&#34;changes-to-this-privacy-policy&#34;&gt;Changes to This Privacy Policy&lt;/h2&gt;
&lt;p&gt;We may update our Privacy Policy from time to time. Thus, we advise you to review this page periodically for any changes. We will notify you of any changes by posting the new Privacy Policy on this page. These changes are effective immediately, after they are posted on this page.&lt;/p&gt;
&lt;h2 id=&#34;contact-us&#34;&gt;Contact Us&lt;/h2&gt;
&lt;p&gt;If you have any questions or suggestions about our Privacy Policy, do not hesitate to contact us.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>4 Reasons Your iOS App Is Crashing</title>
      <link>https://www.andrewcbancroft.com/blog/ios-development/debugging/reasons-your-ios-app-is-crashing/</link>
      <pubDate>Sat, 25 Jan 2020 00:00:00 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/blog/ios-development/debugging/reasons-your-ios-app-is-crashing/</guid>
      <description>&lt;h2 id=&#34;cpu-cannot-execute-an-instruction&#34;&gt;CPU cannot execute an instruction&lt;/h2&gt;
&lt;p&gt;Sometimes we developers don&amp;rsquo;t play fair with the CPU.&lt;/p&gt;
&lt;p&gt;Suppose that I wrote some code that ended up telling the CPU to divide by zero.&lt;/p&gt;
&lt;p&gt;That&amp;rsquo;s&amp;hellip; impossible&amp;hellip; for you &lt;em&gt;and&lt;/em&gt; the CPU.&lt;/p&gt;
&lt;h2 id=&#34;ios-is-ensuring-its-own-stability&#34;&gt;iOS is ensuring its own stability&lt;/h2&gt;
&lt;p&gt;iOS may be enforcing a fundamental requirement or policy for its own stability.&lt;/p&gt;
&lt;p&gt;Apple isn&amp;rsquo;t going to let developers wreck the overall user experience of iOS.&lt;/p&gt;
&lt;p&gt;Examples:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Your app takes forever to launch&lt;/li&gt;
&lt;li&gt;Your app is using too much memory&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;iOS will opt for killing your app to protect the user’s overall iOS experience.&lt;/p&gt;
&lt;h2 id=&#34;swift-runtime-is-preventing-failure&#34;&gt;Swift runtime is preventing failure&lt;/h2&gt;
&lt;p&gt;There are certain protections within the Swift runtime that may cause your app to crash.&lt;/p&gt;
&lt;p&gt;For example, if you’ve got an array with 5 elements inside (indexed from 0 to 4), and you attempt to access the 6th one by referencing index 5, the Swift runtime will kill your app when it sees this kind of instruction.&lt;/p&gt;
&lt;p&gt;Force-unwrapping optionals using the &lt;code&gt;!&lt;/code&gt; operator is another classic app crasher.&lt;/p&gt;
&lt;h2 id=&#34;another-developer-is-preventing-failure&#34;&gt;Another developer is preventing failure&lt;/h2&gt;
&lt;p&gt;Finally, it may be that the developer of a framework or function you’re using is preventing you from misusing his/her API.&lt;/p&gt;
&lt;p&gt;Some developers will use &lt;code&gt;assert&lt;/code&gt; in a function to test that a condition is met before their function proceeds.&lt;/p&gt;
&lt;p&gt;Guard statements mixed with a &lt;code&gt;fatalError&lt;/code&gt; in the &lt;code&gt;else&lt;/code&gt; block could also be a failure protection strategy.&lt;/p&gt;
&lt;p&gt;In both cases your app would crash when the assert or guard conditions are not met.&lt;/p&gt;
&lt;p&gt;While it would be nicer to see an &lt;code&gt;Error&lt;/code&gt; instead of a crash, knowing that these are posssible &amp;ldquo;under the hood&amp;rdquo; reasons for the crash you&amp;rsquo;re seeing can be helpful.&lt;/p&gt;
&lt;h2 id=&#34;summing-up&#34;&gt;Summing Up&lt;/h2&gt;
&lt;p&gt;It&amp;rsquo;s true: a crash can feel like it comes out of nowhere. Scoping the problem into four &amp;ldquo;big picture&amp;rdquo; reasons for a crash has helped me think rationally and probabilistically about what&amp;rsquo;s going on.&lt;/p&gt;
&lt;p&gt;If you&amp;rsquo;re getting your bearings when it comes to debugging iOS apps, I&amp;rsquo;d love to impact your learning experience. Perhaps my &lt;a href=&#34;http://bit.ly/ios-debugging-fundamentals&#34;&gt;iOS Debugging Fudamentals&lt;/a&gt; course on Pluralsight will guide you to more efficient debugging experiences!&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/ios-debugging-fundamentals&#34; target=&#34;_blank&#34;&gt;iOS Debugging Fundamentals&lt;/a&gt;&lt;a href=&#34;http://bit.ly/ios-debugging-fundamentals&#34; target=&#34;_blank&#34;&gt;&lt;br /&gt; &lt;img src=&#34;https://www.andrewcbancroft.com/images/social-assets/ios-debugging-fundamentals-title.png&#34; alt=&#34;iOS Debugging Fundamentals&#34; width=&#34;1024&#34; height=&#34;576&#34; class=&#34;alignnone size-large wp-image-13737&#34;/&gt;&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;</description>
    </item>
    
    <item>
      <title>4 Things You Should Never Do When Debugging iOS Apps</title>
      <link>https://www.andrewcbancroft.com/blog/ios-development/debugging/4-things-never-do-when-debugging-ios-apps/</link>
      <pubDate>Thu, 16 Jan 2020 00:00:00 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/blog/ios-development/debugging/4-things-never-do-when-debugging-ios-apps/</guid>
      <description>&lt;h2 id=&#34;never-change-code-without-reproducing-the-problem-first&#34;&gt;Never change code without reproducing the problem first&lt;/h2&gt;
&lt;p&gt;Reproducing the problem is a critical first step in debugging.&lt;/p&gt;
&lt;p&gt;Before you start scouring your code&amp;hellip;
Before you start setting breakpoints&amp;hellip;
Before you start using tools or changing things&amp;hellip;&lt;/p&gt;
&lt;p&gt;See if you can &lt;strong&gt;cause the bug to happen again&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;The goal of this exercise is to &lt;strong&gt;confirm that the problem is consistent&lt;/strong&gt;. Reproducing the problem helps you &lt;strong&gt;clarify the conditions that are present when the bug happens.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Do your best to clearly state what’s going on &amp;ndash; I even write it down sometimes to document the problem and make sure I&amp;rsquo;ve got it pinned down.&lt;/p&gt;
&lt;h2 id=&#34;never-bug-hunt-before-hypothesizing&#34;&gt;Never bug hunt before hypothesizing&lt;/h2&gt;
&lt;p&gt;Having a plan sets you up to avoid haphazardly hoping to find the source of the problem you&amp;rsquo;re experiencing.&lt;/p&gt;
&lt;p&gt;Before you set off to track down what&amp;rsquo;s going on, &lt;strong&gt;make an educated guess&lt;/strong&gt; about what you &lt;em&gt;think&lt;/em&gt; might be going on.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Jot down a hypothesis&lt;/strong&gt; or two (or three) and rank them in order of &lt;strong&gt;which feels more likely&lt;/strong&gt;, based on your past debugging experiences.&lt;/p&gt;
&lt;p&gt;Pair your hypothesis with possible Xcode tools to use and spots to look within your codebase to gather evidence to support or discredit the hypothesis.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Then&lt;/em&gt;, once you have a plan, head out into the code on a mission to investigate your hypothesis.&lt;/p&gt;
&lt;p&gt;Take appropriate action based on your findings.&lt;/p&gt;
&lt;h2 id=&#34;never-make-code-changes-without-a-new-branch&#34;&gt;Never make code changes without a new branch&lt;/h2&gt;
&lt;p&gt;When you&amp;rsquo;re taking action though, make sure you&amp;rsquo;ve created a new code branch &lt;strong&gt;first&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Often, your investigation of a best-guess hypothesis about a bug&amp;rsquo;s root cause means you need to change something about your app.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Without a branch, you won&amp;rsquo;t be able to roll back&lt;/strong&gt; to the original state of your code.&lt;/p&gt;
&lt;p&gt;Set your self up to quickly iterate on evidence gathering.&lt;/p&gt;
&lt;p&gt;Make a branch -&amp;gt; attempt a fix -&amp;gt; Did it work? Great! Does the bug persist? Rollback! (rinse and repeat)&lt;/p&gt;
&lt;h2 id=&#34;never-change-more-than-one-thing-at-a-time&#34;&gt;Never change more than one &amp;ldquo;thing&amp;rdquo; at a time&lt;/h2&gt;
&lt;p&gt;Imagine experiencing a bug.&lt;/p&gt;
&lt;p&gt;Now you go in and change a line of code here, toggle an Xcode project setting there, refactor a function, and then re-run your app.&lt;/p&gt;
&lt;p&gt;The bug is fixed!  Buuut&amp;hellip; &lt;strong&gt;which change from your list of changes was the actual fix&lt;/strong&gt;?&lt;/p&gt;
&lt;p&gt;Never change more than one thing at a time.&lt;/p&gt;
&lt;p&gt;Doing so not only makes the actual fix ambiguous, but could also inadvertently cause &lt;em&gt;different&lt;/em&gt; bugs that you didn&amp;rsquo;t anticipate.&lt;/p&gt;
&lt;p&gt;Whatever you do, &lt;strong&gt;only change one thing at a time&lt;/strong&gt; when you&amp;rsquo;re attempting to fix a big.&lt;/p&gt;
&lt;p&gt;Now&amp;hellip;what does &amp;ldquo;one thing&amp;rdquo; mean?&lt;/p&gt;
&lt;p&gt;&amp;ldquo;One thing&amp;rdquo; means &lt;strong&gt;&amp;ldquo;one fix strategy&amp;rdquo;&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;In order to attempt some bug fixes, you may have to change 2 or 3 lines of code.  That&amp;rsquo;s okay.  You&amp;rsquo;re changing them as a &amp;ldquo;unit&amp;rdquo;, of sorts, as part of a single fix strategy.&lt;/p&gt;
&lt;p&gt;Trying more than one fix strategy at a time is what I&amp;rsquo;m cautioning against.&lt;/p&gt;
&lt;h2 id=&#34;summing-up&#34;&gt;Summing Up&lt;/h2&gt;
&lt;p&gt;Debugging is challenging enough as it is. Don&amp;rsquo;t make debugging harder than it needs to be. Avoid these four things and you should be a much happier developer.&lt;/p&gt;
&lt;p&gt;If you&amp;rsquo;re getting your bearings when it comes to debugging iOS apps, I&amp;rsquo;d love to impact your learning experience. Perhaps my &lt;a href=&#34;http://bit.ly/ios-debugging-fundamentals&#34;&gt;iOS Debugging Fudamentals&lt;/a&gt; course on Pluralsight will guide you to more efficient debugging experiences!&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/ios-debugging-fundamentals&#34; target=&#34;_blank&#34;&gt;iOS Debugging Fundamentals&lt;/a&gt;&lt;a href=&#34;http://bit.ly/ios-debugging-fundamentals&#34; target=&#34;_blank&#34;&gt;&lt;br /&gt; &lt;img src=&#34;https://www.andrewcbancroft.com/images/social-assets/ios-debugging-fundamentals-title.png&#34; alt=&#34;iOS Debugging Fundamentals&#34; width=&#34;1024&#34; height=&#34;576&#34; class=&#34;alignnone size-large wp-image-13737&#34;/&gt;&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;</description>
    </item>
    
    <item>
      <title>What Is Debugging (And What Isn&#39;t)?</title>
      <link>https://www.andrewcbancroft.com/blog/ios-development/debugging/what-is-debugging/</link>
      <pubDate>Mon, 30 Dec 2019 03:11:48 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/blog/ios-development/debugging/what-is-debugging/</guid>
      <description>&lt;p&gt;Does it ever help you to narrow the scope of overwhelming topics like &amp;ldquo;debugging&amp;rdquo;?&lt;/p&gt;
&lt;p&gt;It helps &lt;em&gt;me&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;When you&amp;rsquo;re new to something like iOS development, you may find yourself stepping into a world of problems.  Everyone around you is always fixing something it seems!&lt;/p&gt;
&lt;p&gt;All kinds of problems can happen when you’re building an iOS app.&lt;/p&gt;
&lt;p&gt;But are all problems&amp;hellip; &lt;em&gt;debugging&lt;/em&gt; problems?&lt;/p&gt;
&lt;p&gt;TLDR; Answer: Not necessarily.&lt;/p&gt;
&lt;p&gt;My brain appreciates focused definitions.  It lightens the the mental load a bit to &amp;ldquo;bucket&amp;rdquo; problems into categories.&lt;/p&gt;
&lt;p&gt;Which problems don&amp;rsquo;t fit into the &amp;ldquo;debugging problems&amp;rdquo; category?  Which ones do?&lt;/p&gt;
&lt;h2 id=&#34;non-debugging-problems&#34;&gt;Non-Debugging Problems&lt;/h2&gt;
&lt;p&gt;This list of common problem areas definitely take up a developer&amp;rsquo;s time. But rather than lump them all in with &amp;ldquo;debugging&amp;rdquo;, I prefer to treat them as problem sets of their own.&lt;/p&gt;
&lt;h3 id=&#34;build-problems&#34;&gt;Build Problems&lt;/h3&gt;
&lt;p&gt;If your app won’t build, that’s definitely a must-solve problem.  However, buildtime errors are not debugging problems.&lt;/p&gt;
&lt;h3 id=&#34;learning-curves&#34;&gt;Learning Curves&lt;/h3&gt;
&lt;p&gt;When you’re learning a new framework or trying something you’ve never done before, it can take some time to clear the hurdle of the Swift complier as you&amp;rsquo;re coding.  Even still, this isn&amp;rsquo;t debugging.&lt;/p&gt;
&lt;h3 id=&#34;xcode-warnings&#34;&gt;Xcode Warnings&lt;/h3&gt;
&lt;p&gt;If Xcode is issuing warnings, you should probably take heed and resolve them before shipping to the App Store.  But I still wouldn&amp;rsquo;t call this debugging.&lt;/p&gt;
&lt;h3 id=&#34;design-dilemmas&#34;&gt;Design Dilemmas&lt;/h3&gt;
&lt;p&gt;Design can get controversial, but overcoming these often-subjective dilemmas is not debugging.&lt;/p&gt;
&lt;h3 id=&#34;user-experience-debates&#34;&gt;User Experience Debates&lt;/h3&gt;
&lt;p&gt;As with design dilemmas, user experience debates are also trouble spots for many developers.  The debates themselves, however, are not debugging.&lt;/p&gt;
&lt;h3 id=&#34;performance-optimization&#34;&gt;Performance Optimization&lt;/h3&gt;
&lt;p&gt;Sometimes, you get your app built and running, but you’re struggling with performance-related issues.  Even though it works (in a technical sense), it could be faster, more responsive, use less memory, etc.&lt;/p&gt;
&lt;p&gt;Optimization, however, is big enough to be in a category of its own for me.&lt;/p&gt;
&lt;h2 id=&#34;debugging-problems&#34;&gt;Debugging Problems&lt;/h2&gt;
&lt;p&gt;So&amp;hellip; what &lt;em&gt;is&lt;/em&gt; debugging then?&lt;/p&gt;
&lt;p&gt;This is how I define it in &lt;a href=&#34;http://bit.ly/ios-debugging-fundamentals&#34;&gt;iOS Debugging Fundamentals&lt;/a&gt;:&lt;/p&gt;
&lt;p&gt;Debugging is&amp;hellip;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Investigating&lt;/strong&gt; and &lt;strong&gt;fixing flaws&lt;/strong&gt; that you and your users experience &lt;strong&gt;while your app is running&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Thinking about&lt;/strong&gt; and &lt;strong&gt;correcting defects&lt;/strong&gt; that happen at &lt;strong&gt;runtime&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Debugging is a two-part problem solving exercise: It involves thinking skills and fixing skills.  Debugging involves reasoning about the probable causes of flaws and defects in your apps. When you debug, you spend time trying to figure out the root cause of an issue you&amp;rsquo;re noticing.&lt;/p&gt;
&lt;p&gt;Once you find the source of the problem, your job shifts to applying your programming skills and your knowledge of the app&amp;rsquo;s purpose (domain knowledge) to fix the problem.&lt;/p&gt;
&lt;p&gt;But just as I started off this discussion by asking, &amp;ldquo;Are all problems&amp;hellip; &lt;em&gt;debugging&lt;/em&gt; problems?&amp;rdquo;, I&amp;rsquo;m able to answer &amp;ldquo;no&amp;rdquo; by emphasizing &lt;em&gt;when&lt;/em&gt; the problems occur, namely, at runtime.&lt;/p&gt;
&lt;p&gt;That&amp;rsquo;s why build problems, learnig curves, Xcode warnings, design dilemmas, and UX debates aren&amp;rsquo;t considered debugging activites (in my view).&lt;/p&gt;
&lt;p&gt;I grant that performance problems are typically noticed at runtime, &lt;em&gt;but&lt;/em&gt;, I believe this to be a separate activity, primarily because the app &lt;em&gt;works&lt;/em&gt; (even if it could work &lt;em&gt;better&lt;/em&gt;).&lt;/p&gt;
&lt;h2 id=&#34;summing-up&#34;&gt;Summing Up&lt;/h2&gt;
&lt;p&gt;Narrowing the scope of debugging to reasoning about, inspecting, and repairing runtime problems lifts some cognitive weight for me.&lt;/p&gt;
&lt;p&gt;It allows me to enjoy programming a &lt;em&gt;lot&lt;/em&gt; more when I know that I&amp;rsquo;m not actually spending 99% of my time &amp;ldquo;debugging&amp;rdquo;. 😂&lt;/p&gt;
&lt;p&gt;If you&amp;rsquo;re getting your bearings when it comes to debugging iOS apps, I&amp;rsquo;d love to impact your learning experience. Perhaps my &lt;a href=&#34;http://bit.ly/ios-debugging-fundamentals&#34;&gt;iOS Debugging Fudamentals&lt;/a&gt; course on Pluralsight will guide you to more efficient debugging experiences!&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/ios-debugging-fundamentals&#34; target=&#34;_blank&#34;&gt;iOS Debugging Fundamentals&lt;/a&gt;&lt;a href=&#34;http://bit.ly/ios-debugging-fundamentals&#34; target=&#34;_blank&#34;&gt;&lt;br /&gt; &lt;img src=&#34;https://www.andrewcbancroft.com/images/social-assets/ios-debugging-fundamentals-title.png&#34; alt=&#34;iOS Debugging Fundamentals&#34; width=&#34;1024&#34; height=&#34;576&#34; class=&#34;alignnone size-large wp-image-13737&#34;/&gt;&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;</description>
    </item>
    
    <item>
      <title>iOS Debugging Fundamentals</title>
      <link>https://www.andrewcbancroft.com/pluralsight/2019-12-26-ios-debugging-fundamentals/</link>
      <pubDate>Thu, 26 Dec 2019 10:03:33 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/pluralsight/2019-12-26-ios-debugging-fundamentals/</guid>
      <description>&lt;p&gt;Bugs are bound to happen. Getting rid of them is an essential skill if you want to keep your sanity when building apps.&lt;/p&gt;
&lt;p&gt;In this course, you will learn a set of thinking skills to couple with the fundamental tools that Xcode provides you for finding and fixing flaws in your apps.&lt;/p&gt;
&lt;p&gt;🐞1.5 hours
🦟To the point
🐛Engaging storyline
🐜Relevant demos&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/ios-debugging-fundamentals&#34; target=&#34;_blank&#34;&gt;iOS Debugging Fundamentals&lt;/a&gt;&lt;a href=&#34;http://bit.ly/ios-debugging-fundamentals&#34; target=&#34;_blank&#34;&gt;&lt;br /&gt; &lt;img src=&#34;https://www.andrewcbancroft.com/images/social-assets/ios-debugging-fundamentals-title.png&#34; alt=&#34;iOS Debugging Fundamentals&#34; width=&#34;1024&#34; height=&#34;576&#34; class=&#34;alignnone size-large wp-image-13737&#34;/&gt;&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;
&lt;h1 id=&#34;course-outline&#34;&gt;Course Outline&lt;/h1&gt;
&lt;p&gt;First, you will learn to approach debugging situations with a set of principles for strategic problem solving.&lt;/p&gt;
&lt;p&gt;Next, you will discover how to configure breakpoints and inspect view hierarchies effectively.&lt;/p&gt;
&lt;p&gt;Finally, you will explore how to interpret a backtrace, glean valuable information from the Xcode debug area and console, locate changes in your codebase and simulate conditions that happen in the wild.&lt;/p&gt;
&lt;p&gt;When you are finished with this course, you will have the skills and knowledge to strategically and methodically reason about probable causes of defects that you experience at runtime.&lt;/p&gt;
&lt;h2 id=&#34;1--debugging-as-strategic-problem-solving&#34;&gt;1 – Debugging as Strategic Problem Solving&lt;/h2&gt;
&lt;p&gt;Debugging means a lot of things to a lot of people.&lt;/p&gt;
&lt;p&gt;In this module, I will define the scope of debugging for you so that you’re clear on the skills you’ll walk away from this course with.&lt;/p&gt;
&lt;p&gt;You will begin to develop a problem solving mentality and learn a useful mnemonic for R.E.P.A.I.R.-ing broken apps.&lt;/p&gt;
&lt;p&gt;Learn how to categorize bugs into buckets for strategic resolution.&lt;/p&gt;
&lt;p&gt;Explore some of the most common sources of bugs to complete this module.&lt;/p&gt;
&lt;h2 id=&#34;2--correcting-unmet-expectations&#34;&gt;2 – Correcting Unmet Expectations&lt;/h2&gt;
&lt;p&gt;Probabilistic thinking is the key component of a debugging mindset.&lt;/p&gt;
&lt;p&gt;Topics in this module include&amp;hellip;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Pausing an App to Gather Clues With Breakpoints&lt;/li&gt;
&lt;li&gt;Combining Problem Solving with Xcode Tools&lt;/li&gt;
&lt;li&gt;Managing Your Project’s Breakpoints&lt;/li&gt;
&lt;li&gt;Controlling Breakpoints by Defining Trigger Conditions&lt;/li&gt;
&lt;li&gt;Executing Actions When Breakpoints Are Triggered&lt;/li&gt;
&lt;li&gt;Inspecting User Interface Issues&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;3--addressing-app-crashes&#34;&gt;3 – Addressing App Crashes&lt;/h2&gt;
&lt;p&gt;When an app crashes, it’s because of a critical problem that can’t be recovered from.&lt;/p&gt;
&lt;p&gt;In this module you will work on the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Understanding Exceptional Issues&lt;/li&gt;
&lt;li&gt;Inspecting the Crash Scene with the Backtrace and Console Output&lt;/li&gt;
&lt;li&gt;Pausing before Errors and Exceptions Cause Crashes&lt;/li&gt;
&lt;li&gt;Avoiding Errors that Cause Crashes&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;4--alleviating-anomalies&#34;&gt;4 – Alleviating Anomalies&lt;/h2&gt;
&lt;p&gt;Have you ever found yourself saying, “Something weird is going on” or “It used to work - what’s going on now??”&lt;/p&gt;
&lt;p&gt;Have you ever experienced one of those “intermittent” bugs that seem to present themselves rarely or inconsistently and are hard to even reproduce?&lt;/p&gt;
&lt;p&gt;Anomalous bugs can feel like they occur “out of nowhere”&amp;hellip; but do they?&lt;/p&gt;
&lt;p&gt;To find out, you will explore these topics:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Reasoning About Anomalous Bugs&lt;/li&gt;
&lt;li&gt;Inspecting App Changes&lt;/li&gt;
&lt;li&gt;Simulating Real-World Conditions With Xcode&lt;/li&gt;
&lt;/ul&gt;
&lt;h1 id=&#34;feedback-welcome&#34;&gt;Feedback Welcome!&lt;/h1&gt;
&lt;p&gt;My unwavering goal is to help you filter the noise and learn as efficiently as possible.&lt;/p&gt;
&lt;p&gt;I welcome feedback on this course, and on other iOS development courses you might be interested in seeing in the Pluralsight library. Happy learning!&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>How to Use @FetchRequest in SwiftUI</title>
      <link>https://www.andrewcbancroft.com/blog/ios-development/data-persistence/how-to-use-fetchrequest-swiftui/</link>
      <pubDate>Sat, 03 Aug 2019 03:11:48 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/blog/ios-development/data-persistence/how-to-use-fetchrequest-swiftui/</guid>
      <description>&lt;p&gt;Once you&amp;rsquo;ve &lt;a href=&#34;https://www.andrewcbancroft.com/blog/ios-development/data-persistence/passing-nsmanagedobjectcontext-to-swiftui-view/&#34;&gt;passed an NSManagedObjectContext instance to your SwiftUI View&lt;/a&gt;, you&amp;rsquo;ll need to pull data out of your Core Data persistent store with a fetch request.&lt;/p&gt;
&lt;h2 id=&#34;first-things-first&#34;&gt;First Things First&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;The remainder of this how-to assumes that you&amp;rsquo;ve already got a way to initialize the Core Data stack. Ticking the &amp;lsquo;Use Core Data&amp;rsquo; checkbox when you start a new app will place some boilerplate code in &lt;code&gt;AppDelegate&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;@Environment(\.managedobjectcontext)&lt;/code&gt; must &lt;a href=&#34;https://www.andrewcbancroft.com/blog/ios-development/data-persistence/passing-nsmanagedobjectcontext-to-swiftui-view/&#34;&gt;be assigned&lt;/a&gt; before the View is initialized for &lt;code&gt;@FetchRequest&lt;/code&gt; to work.&lt;/li&gt;
&lt;li&gt;Code examples are taken from my &amp;ldquo;Blog Idea List&amp;rdquo; sample project that includes a single Core Data Entity named &lt;code&gt;BlogIdea&lt;/code&gt;:&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/blog/ios-development/data-persistence/using-core-data-with-swiftui-introduction/blog-idea-entity.png&#34; alt=&#34;Blog Idea Entity&#34;&gt;&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;resources&#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-file-code&#34;&gt;&lt;/i&gt; &lt;a href=&#34;https://github.com/andrewcbancroft/BlogIdeaList-SwiftUI&#34;&gt; Blog Idea List SwiftUI Example Xcode Project&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;p&gt;You can refer to my &lt;a href=&#34;https://www.andrewcbancroft.com/blog/ios-development/data-persistence/using-core-data-with-swiftui-introduction/&#34;&gt;introduction to using Core Data with SwiftUI&lt;/a&gt; to review all of the steps in one spot.  It includes an example project with all of the pieces stitched together!&lt;/p&gt;
&lt;h2 id=&#34;breaking-down-the-code&#34;&gt;Breaking Down the Code&lt;/h2&gt;
&lt;p&gt;There are three key components to this:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;A way to configure an &lt;code&gt;NSFetchRequest&lt;/code&gt; &lt;em&gt;with&lt;/em&gt; an &lt;code&gt;NSSortDescriptor&lt;/code&gt; added. Your app will blow up 💥 if you don&amp;rsquo;t have one added to your fetch request.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;@FetchRequest&lt;/code&gt; property wrapper in your SwiftUI view.&lt;/li&gt;
&lt;li&gt;A SwiftUI View that displays the results of the executed fetch request.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The following code snippets provide you an example of how to weave all three components together.&lt;/p&gt;
&lt;p&gt;Comments with ❇️ symbols will explain the details inline.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;BlogIdea.swift&lt;/strong&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;12
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;13
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;14
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;15
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;16
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;17
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;18
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;19
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;20
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;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;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;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;// ❇️ BlogIdea code generation is turned OFF in the xcdatamodeld file&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;BlogIdea&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSManagedObject&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Identifiable&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;cl&#34;&gt;    &lt;span class=&#34;kr&#34;&gt;@NSManaged&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;ideaTitle&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;cl&#34;&gt;    &lt;span class=&#34;kr&#34;&gt;@NSManaged&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;ideaDescription&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;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;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;BlogIdea&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;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// ❇️ The @FetchRequest property wrapper in the ContentView will call this function&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;static&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;allIdeasFetchRequest&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;NSFetchRequest&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;BlogIdea&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&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;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;&amp;lt;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;BlogIdea&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;BlogIdea&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;k&#34;&gt;as&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;&amp;lt;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;BlogIdea&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;        &lt;span class=&#34;c1&#34;&gt;// ❇️ The @FetchRequest property wrapper in the ContentView requires a sort descriptor&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&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;sortDescriptors&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;NSSortDescriptor&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;key&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;ideaTitle&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ascending&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&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;request&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;ContentView.swift&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;12
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;import&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;SwiftUI&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;struct&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;SwiftUIView&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;View&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;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// ❇️ The BlogIdea class has an `allIdeasFetchRequest` static function that can be used here&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;FetchRequest&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;BlogIdea&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;allIdeasFetchRequest&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;())&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;blogIdeas&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;FetchedResults&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;BlogIdea&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;body&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;some&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;View&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;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;List&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;blogIdeas&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;blogIdea&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;cl&#34;&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;n&#34;&gt;blogIdea&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;ideaTitle&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;??&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;&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;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;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;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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Note that you don&amp;rsquo;t actually need to hold a reference to the &lt;code&gt;@Environment&lt;/code&gt;&amp;rsquo;s &lt;code&gt;managedObjectContext&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;@Environment&lt;/code&gt;&amp;rsquo;s &lt;code&gt;managedObjectContext&lt;/code&gt; must be set! But you don&amp;rsquo;t have to &amp;ldquo;call&amp;rdquo; it or use it directly to make the fetch request &amp;ldquo;go&amp;rdquo;.&lt;/p&gt;
&lt;h2 id=&#34;wheres-the-call-to-perform-the-fetch-request&#34;&gt;Where&amp;rsquo;s the Call to Perform the Fetch Request?&lt;/h2&gt;
&lt;p&gt;With most Core Data apps, you&amp;rsquo;re going to see a call to &lt;code&gt;fetch(_:)&lt;/code&gt; somewhere in your code.&lt;/p&gt;
&lt;p&gt;This happens &amp;ldquo;behind the scenes&amp;rdquo; when you use the &lt;code&gt;@FetchRequest&lt;/code&gt; property wrapper in your SwiftUI view. No need to use your managed object context to perform the fetch request manually.&lt;/p&gt;
&lt;p&gt;For a more complete guide to using Core Data with SwiftUI, you can refer to the &lt;a href=&#34;https://www.andrewcbancroft.com/blog/ios-development/data-persistence/using-core-data-with-swiftui-introduction/&#34;&gt;introduction I published&lt;/a&gt;.  It includes an example project with all of the pieces stitched together!&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>How to Pass NSManagedObjectContext to a SwiftUI View</title>
      <link>https://www.andrewcbancroft.com/blog/ios-development/data-persistence/passing-nsmanagedobjectcontext-to-swiftui-view/</link>
      <pubDate>Fri, 02 Aug 2019 03:11:48 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/blog/ios-development/data-persistence/passing-nsmanagedobjectcontext-to-swiftui-view/</guid>
      <description>&lt;p&gt;Step one to &lt;a href=&#34;https://www.andrewcbancroft.com/blog/ios-development/data-persistence/using-core-data-with-swiftui-introduction/&#34;&gt;using Core Data with SwiftUI&lt;/a&gt; is to initialize the Core Data stack and pass an instance of your &lt;code&gt;NSManagedObjectContext&lt;/code&gt; to your view.&lt;/p&gt;
&lt;h2 id=&#34;first-things-first&#34;&gt;First Things First&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;The remainder of this how-to assumes that you&amp;rsquo;ve already got a way to initialize the Core Data stack. Ticking the &amp;lsquo;Use Core Data&amp;rsquo; checkbox when you start a new app will place some boilerplate code in &lt;code&gt;AppDelegate&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Code examples are taken from my &amp;ldquo;Blog Idea List&amp;rdquo; sample project that includes a single Core Data Entity named &lt;code&gt;BlogIdea&lt;/code&gt;:&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/blog/ios-development/data-persistence/using-core-data-with-swiftui-introduction/blog-idea-entity.png&#34; alt=&#34;Blog Idea Entity&#34;&gt;&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;resources&#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-file-code&#34;&gt;&lt;/i&gt; &lt;a href=&#34;https://github.com/andrewcbancroft/BlogIdeaList-SwiftUI&#34;&gt; Blog Idea List SwiftUI Example Xcode Project&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;p&gt;You can refer to my &lt;a href=&#34;https://www.andrewcbancroft.com/blog/ios-development/data-persistence/using-core-data-with-swiftui-introduction/&#34;&gt;introduction to using Core Data with SwiftUI&lt;/a&gt; to review all of the steps in one spot.  It includes an example project with all of the pieces stitched together!&lt;/p&gt;
&lt;h2 id=&#34;breaking-down-the-code&#34;&gt;Breaking Down the Code&lt;/h2&gt;
&lt;p&gt;There are two key components to this:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;the Core Data Stack&lt;/li&gt;
&lt;li&gt;the SwiftUI &lt;code&gt;@Environment&lt;/code&gt;&amp;rsquo;s &lt;code&gt;managedObjectContext&lt;/code&gt; variable.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&amp;ldquo;Passing in&amp;rdquo; your managed object context will probably happen early on in your app&amp;rsquo;s launch sequence.  &lt;code&gt;SceneDelegate&lt;/code&gt; is where I pass mine in with the Blog Idea List app.&lt;/p&gt;
&lt;p&gt;On the receiving end is your SwiftUI View.  An &lt;code&gt;@Environment&lt;/code&gt; property wrapper needs to be set up in order to pull &lt;em&gt;out&lt;/em&gt; the managed object context so you can use it.&lt;/p&gt;
&lt;p&gt;The following code snippets provide you glimpses into both sides of the coin.  Comments with ❇️ symbols will explain the details inline.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;SceneDelegate.swift&lt;/strong&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;12
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;13
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;14
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;15
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;16
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;17
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;18
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;19
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;20
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;21
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;22
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;scene&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;scene&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIScene&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;cl&#34;&gt;            &lt;span class=&#34;n&#34;&gt;willConnectTo&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;session&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UISceneSession&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;cl&#34;&gt;            &lt;span class=&#34;n&#34;&gt;options&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;connectionOptions&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIScene&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;ConnectionOptions&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;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;windowScene&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;scene&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;UIWindowScene&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;cl&#34;&gt;        &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;window&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIWindow&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;windowScene&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;windowScene&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;cl&#34;&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;c1&#34;&gt;// ❇️ Get the managedObjectContext from the persistent container&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;c1&#34;&gt;// ❇️ This assumes you&amp;#39;ve left the Core Data stack creation code within AppDelegate&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;UIApplication&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;shared&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;k&#34;&gt;as&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;!&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;AppDelegate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;).&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;persistentContainer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;viewContext&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;        &lt;span class=&#34;c1&#34;&gt;// ❇️ Pass it to the ContentView through the &lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;c1&#34;&gt;// ❇️ managedObjectContext @Environment variable&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;contentView&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ContentView&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;cl&#34;&gt;                            &lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;environment&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;err&#34;&gt;\&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;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;cl&#34;&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;window&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;rootViewController&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIHostingController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;rootView&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;contentView&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;cl&#34;&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;window&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;window&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;window&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;makeKeyAndVisible&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;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;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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;ContentView.swift&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;import&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;SwiftUI&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;struct&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;SwiftUIView&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;View&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;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// ❇️ Access the @Environment&amp;#39;s managedObjectContext variable&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// ❇️ and keep a reference to use later&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;Environment&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;err&#34;&gt;\&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;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;managedObjectContext&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;body&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;some&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;View&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;cl&#34;&gt;        &lt;span class=&#34;c1&#34;&gt;// ❇️ Use self.managedObjectContext however you need within your SwiftUI view!&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;For a more complete guide to using Core Data with SwiftUI, you can refer to the &lt;a href=&#34;https://www.andrewcbancroft.com/blog/ios-development/data-persistence/using-core-data-with-swiftui-introduction/&#34;&gt;introduction I published&lt;/a&gt;.  It includes an &lt;a href=&#34;https://github.com/andrewcbancroft/BlogIdeaList-SwiftUI&#34;&gt;example project&lt;/a&gt; with all of the pieces stitched together!&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Using Core Data With SwiftUI - An Introduction</title>
      <link>https://www.andrewcbancroft.com/blog/ios-development/data-persistence/using-core-data-with-swiftui-introduction/</link>
      <pubDate>Wed, 31 Jul 2019 04:40:54 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/blog/ios-development/data-persistence/using-core-data-with-swiftui-introduction/</guid>
      <description>&lt;p&gt;How does Apple intend for us to use Core Data with SwiftUI?&lt;/p&gt;
&lt;p&gt;They gave us a path forward with the most recent beta release of iOS 13 (Beta 5 at the time of this writing)!&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;resources&#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-file-code&#34;&gt;&lt;/i&gt; &lt;a href=&#34;https://github.com/andrewcbancroft/BlogIdeaList-SwiftUI&#34;&gt; Blog Idea List SwiftUI Example Xcode Project&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;i class=&#34;fas fa-link&#34;&gt;&lt;/i&gt; &lt;a href=&#34;https://developer.apple.com/videos/play/wwdc2019/226/&#34; rel=&#34;nofollow&#34;&gt;Data Flow Through SwiftUI - WWDC 2019 Session 226&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;h2 id=&#34;first-things-first&#34;&gt;First Things First&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;To work effectively with Core Data in SwiftUI, the &lt;a href=&#34;https://developer.apple.com/videos/play/wwdc2019/226/&#34;&gt;Data Flow Through SwiftUI&lt;/a&gt; WWDC session is a huge help.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;With this talk under your belt, you&amp;rsquo;ll have at least been introduced to key words like &lt;code&gt;@State&lt;/code&gt;, &lt;code&gt;@Binding&lt;/code&gt;, &lt;code&gt;@ObservableObject&lt;/code&gt; (which replaces &lt;code&gt;@BindableObject&lt;/code&gt; as of Beta 5), and &lt;code&gt;@Environment&lt;/code&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;on-the-right-path&#34;&gt;On the Right Path&lt;/h2&gt;
&lt;p&gt;Even prior to Beta 5, the &amp;ldquo;right path&amp;rdquo; for using Core Data with SwiftUI seemed to be through the &lt;code&gt;@Environment&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;In the release notes for iOS Beta 5, Apple seems to be leading us there:&lt;/p&gt;
&lt;blockquote&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;NSManagedObject&lt;/code&gt; now conforms to &lt;code&gt;ObservableObject&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;blockquote&gt;
&lt;ul&gt;
&lt;li&gt;The new &lt;code&gt;@FetchRequest&lt;/code&gt; property wrapper can drive views from the results of a fetch request&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;blockquote&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;managedObjectContext&lt;/code&gt; is now &lt;strong&gt;included in the environment&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;With these three pieces in place, Apple has given us out-of-the-box support for using Core Data with SwiftUI.&lt;/p&gt;
&lt;p&gt;So&amp;hellip;&lt;/p&gt;
&lt;h2 id=&#34;how-do-you-get-started&#34;&gt;How Do You Get Started?&lt;/h2&gt;
&lt;p&gt;I decided to create a basic, single-view app that can&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Persist a list of &lt;code&gt;BlogIdea&lt;/code&gt;s to a Core Data persistent store&lt;/li&gt;
&lt;li&gt;Use the new &lt;code&gt;@FetchRequest&lt;/code&gt; property wrapper to fetch &lt;code&gt;BlogIdea&lt;/code&gt;s&lt;/li&gt;
&lt;li&gt;Use the &lt;code&gt;@Environment&lt;/code&gt;&amp;rsquo;s &lt;code&gt;managedObjectContext&lt;/code&gt; to create, update, and delete &lt;code&gt;BlogIdea&lt;/code&gt;s&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Grab the project &lt;a href=&#34;https://github.com/andrewcbancroft/BlogIdeaList-SwiftUI&#34;&gt;over at GitHub&lt;/a&gt; and explore!&lt;/p&gt;
&lt;p&gt;The sample project that includes a single Core Data Entity named &lt;code&gt;BlogIdea&lt;/code&gt;:&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/blog/ios-development/data-persistence/using-core-data-with-swiftui-introduction/blog-idea-entity.png&#34; alt=&#34;Blog Idea Entity&#34;&gt;&lt;/p&gt;
&lt;p&gt;I kept as much out-of-the-box stuff as I could.&lt;/p&gt;
&lt;h2 id=&#34;how-does-it-work&#34;&gt;How Does It Work?&lt;/h2&gt;
&lt;p&gt;Throughout the project, you&amp;rsquo;ll notice emoji symbols to help draw your eye to the Core Data pieces.&lt;/p&gt;
&lt;p&gt;❇️ Alerts you to Core Data pieces&lt;/p&gt;
&lt;p&gt;ℹ️ Alerts you to general info about what my brain was thinking when I wrote the code&lt;/p&gt;
&lt;h2 id=&#34;code-walkthroughs&#34;&gt;Code Walkthroughs&lt;/h2&gt;
&lt;p&gt;I completely relate if you need to chunk things up and inspect the details of how each piece of the puzzle looks in isolation before you dive into the full app.&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;ve written up two separate how-tos that show you more specific details:&lt;/p&gt;
&lt;div class=&#34;row align-items-center&#34;&gt;
&lt;div class=&#34;col&#34;&gt;
&lt;div class=&#34;card&#34; style=&#34;width: 25rem;&#34;&gt;
  &lt;a href=&#34;https://www.andrewcbancroft.com/blog/ios-development/data-persistence/passing-nsmanagedobjectcontext-to-swiftui-view/&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/images/social-assets/Twitter - How to Pass NSManagedObjectContext to a SwiftUI View.png&#34; class=&#34;card-img-top&#34; alt=&#34;How to Pass NSManagedObjectContext to a SwiftUI View&#34;&gt;&lt;/a&gt;
  &lt;div class=&#34;card-body&#34;&gt;
    &lt;p class=&#34;card-text&#34;&gt;Breaks down how to pass an NSManagedObjectContext instance to your SwiftUI Views.&lt;/p&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&#34;col&#34;&gt;
&lt;div class=&#34;card&#34; style=&#34;width: 25rem;&#34;&gt;
  &lt;a href=&#34;https://www.andrewcbancroft.com/blog/ios-development/data-persistence/how-to-use-fetchrequest-swiftui/&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/images/social-assets/Twitter - How to Use @FetchRequest in SwiftUI.png&#34; class=&#34;card-img-top&#34; alt=&#34;How to Use @FetchRequest in SwiftUI&#34;&gt;&lt;/a&gt;
  &lt;div class=&#34;card-body&#34;&gt;
    &lt;p class=&#34;card-text&#34;&gt;Breaks down how to use the @FetchRequest property wrapper in your SwiftUI Views.&lt;/p&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
</description>
    </item>
    
    <item>
      <title>NSPersistentCloudKitContainer Buggy Behavior List</title>
      <link>https://www.andrewcbancroft.com/blog/ios-development/data-persistence/nspersistentcloudkitcontainer-buggy-behavior-list/</link>
      <pubDate>Thu, 11 Jul 2019 04:40:54 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/blog/ios-development/data-persistence/nspersistentcloudkitcontainer-buggy-behavior-list/</guid>
      <description>&lt;p&gt;&lt;code&gt;NSPersistentCloudKitContainer&lt;/code&gt; is brand new with Xcode 11 and iOS 13.&lt;/p&gt;
&lt;p&gt;It works&amp;hellip; kind of&amp;hellip; sometimes&amp;hellip;&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;m keeping this list of buggy behavior to help keep my head straight.  I&amp;rsquo;ve also filed bug reports on each.&lt;/p&gt;
&lt;p&gt;Hopefully others in the iOS community who are experimenting with &lt;code&gt;NSPersistentCloudKitContainer&lt;/code&gt; can confirm or deny these same oddities.  If they&amp;rsquo;re fixed in a subsequent beta, I&amp;rsquo;ll come back here and note it!&lt;/p&gt;
&lt;h2 id=&#34;which-beta-version-does-this-affect&#34;&gt;Which Beta Version Does This Affect?&lt;/h2&gt;
&lt;p&gt;I&amp;rsquo;ve tested out each of these on iOS 13 Beta 5 and Xcode 11 Beta 5.&lt;/p&gt;
&lt;h2 id=&#34;enabling-cloudkit-doesnt-create-a-default-container&#34;&gt;Enabling CloudKit doesn&amp;rsquo;t create a default container&lt;/h2&gt;
&lt;p&gt;In Xcode 11, adding the iCloud capability and clicking the CloudKit checkbox is not automatically creating a default container for new apps that I&amp;rsquo;m testing&amp;hellip;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Xcode 10&lt;/strong&gt;
&lt;img src=&#34;XC10_CKDefaultContainer.gif&#34; alt=&#34;Xcode 10 - Creates Default Container&#34;&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Xcode 11&lt;/strong&gt;
&lt;img src=&#34;XC11_NoCKDefaultContainer.gif&#34; alt=&#34;Xcode 11 - Does Not Create Default Container&#34;&gt;&lt;/p&gt;
&lt;h2 id=&#34;existing-data-doesnt-sync&#34;&gt;Existing data doesn&amp;rsquo;t sync&lt;/h2&gt;
&lt;p&gt;Suppose that&amp;hellip;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;You have an existing Core Data app&lt;/li&gt;
&lt;li&gt;The app is installed on two devices with the same user signed in to iCloud&lt;/li&gt;
&lt;li&gt;Data has been saved independently on both devices&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Now suppose that you want to take advantage of the automagic syncing offered with &lt;code&gt;NSPersistendCloudKitContainer&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;If you&amp;hellip;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Set up your app to work with &lt;code&gt;NSPersistentCloudKitContainer&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Rebuild your app and install it on both devices&lt;/li&gt;
&lt;li&gt;Launch the app on both devices&amp;hellip;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You might expect the existing data to show up on both devices.  &lt;strong&gt;It doesn&amp;rsquo;t.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;If you add &lt;em&gt;new&lt;/em&gt; data on one device, &lt;em&gt;only the new data&lt;/em&gt; that gets saved &lt;em&gt;after&lt;/em&gt; the &lt;code&gt;NSPersistentCloudKitContainer&lt;/code&gt; conversion gets synced.  Existing data never does.&lt;/p&gt;
&lt;p&gt;🤷🏼‍♂️&lt;/p&gt;
&lt;h2 id=&#34;data-only-syncs-after-you-close-and-re-launch-your-app&#34;&gt;Data only syncs after you close and re-launch your app&lt;/h2&gt;
&lt;p&gt;I really thought I had this figured out.  Beta 5 has proved me wrong.&lt;/p&gt;
&lt;p&gt;In Beta 2, simply &lt;a href=&#34;https://www.andrewcbancroft.com/blog/ios-development/data-persistence/getting-started-with-nspersistentcloudkitcontainer/#reflecting-changes-in-the-ui&#34;&gt;setting &lt;code&gt;automaticallyMergesChangesFromParent&lt;/code&gt; on the container&amp;rsquo;s &lt;code&gt;viewContext&lt;/code&gt; to &lt;code&gt;true&lt;/code&gt;&lt;/a&gt; seemed to work.&lt;/p&gt;
&lt;p&gt;Not with Beta 5.&lt;/p&gt;
&lt;p&gt;You can save new data and wait (and wait&amp;hellip; and wait&amp;hellip;).  It doesn&amp;rsquo;t show up in the UI until after you re-launch the app.&lt;/p&gt;
&lt;p&gt;Here&amp;rsquo;s what I&amp;rsquo;m experiencing:&lt;/p&gt;
&lt;div style=&#34;position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;&#34;&gt;
			&lt;iframe allow=&#34;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share; fullscreen&#34; loading=&#34;eager&#34; referrerpolicy=&#34;strict-origin-when-cross-origin&#34; src=&#34;https://www.youtube.com/embed/DLAwrSCl3Cc?autoplay=0&amp;amp;controls=1&amp;amp;end=0&amp;amp;loop=0&amp;amp;mute=0&amp;amp;start=0&#34; style=&#34;position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;&#34; title=&#34;YouTube video&#34;&gt;&lt;/iframe&gt;
		&lt;/div&gt;

&lt;h2 id=&#34;record-accessor-methods-missing-in-xcode&#34;&gt;Record accessor methods missing in Xcode&lt;/h2&gt;
&lt;p&gt;&amp;ldquo;On &lt;em&gt;my&lt;/em&gt; machine&amp;rdquo;, attempting to use the &lt;code&gt;record(for:)&lt;/code&gt; methods on &lt;code&gt;NSPersistentCloudKitContainer&lt;/code&gt; produce the following error message:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Value of type &amp;lsquo;NSPersistentCloudKitContainer&amp;rsquo; has no member &amp;lsquo;record&amp;rsquo;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src=&#34;no-record-method.png&#34; alt=&#34;Has no member &amp;lsquo;record&amp;rsquo;&#34;&gt;&lt;/p&gt;
&lt;p&gt;I can &amp;ldquo;Jump to definition&amp;rdquo; and the methods appear to be there&amp;hellip;&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;jump-definition.gif&#34; alt=&#34;Jump to Definition&#34;&gt;&lt;/p&gt;
&lt;p&gt;Attempting to use the function in code doesn&amp;rsquo;t work&amp;hellip;&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;no-member.gif&#34; alt=&#34;No Member&#34;&gt;&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Apple&#39;s SwiftUI Essentials Tutorial Series as Playgrounds</title>
      <link>https://www.andrewcbancroft.com/blog/ios-development/ui-work/swiftui-essentials-playgrounds/</link>
      <pubDate>Sat, 06 Jul 2019 03:11:48 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/blog/ios-development/ui-work/swiftui-essentials-playgrounds/</guid>
      <description>&lt;p&gt;&lt;a name=&#34;resources&#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-file-code&#34;&gt;&lt;/i&gt; &lt;a href=&#34;https://github.com/andrewcbancroft/swiftui-essentials-playgrounds&#34;&gt;SwiftUI Essentials Tutorials as Playgrounds&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;i class=&#34;fas fa-link&#34;&gt;&lt;/i&gt; &lt;a href=&#34;https://developer.apple.com/tutorials/swiftui&#34; rel=&#34;nofollow&#34;&gt;Apple&#39;s SwiftUI tutorials&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;h2 id=&#34;why-playgrounds&#34;&gt;Why Playgrounds?&lt;/h2&gt;
&lt;p&gt;I&amp;rsquo;ve been making my way through &lt;a href=&#34;https://developer.apple.com/tutorials/swiftui&#34; rel=&#34;nofollow&#34;&gt;Apple&amp;rsquo;s SwiftUI tutorials&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;If you&amp;rsquo;re anything like me, you learn by tinkering with things. Change the modifiers, reposition &lt;code&gt;Text&lt;/code&gt;, use a different &lt;code&gt;Image&lt;/code&gt;, and see what happens.&lt;/p&gt;
&lt;p&gt;BUT.&lt;/p&gt;
&lt;p&gt;Without macOS Catalina, iterating quickly on these tinker sessions involves starting and stopping a simulator to see the changes.&lt;/p&gt;
&lt;p&gt;#annoying, right?&lt;/p&gt;
&lt;p&gt;&lt;em&gt;However&lt;/em&gt;&amp;hellip; since you can &lt;a href=&#34;https://www.andrewcbancroft.com/blog/ios-development/ui-work/using-swiftui-in-playground/&#34;&gt;use SwiftUI in a Playground&lt;/a&gt;, I decided to take my learning experience out of the sample Xcode projects that Apple provides, and turn them into Playgrounds.&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;ve &lt;a href=&#34;https://github.com/andrewcbancroft/swiftui-essentials-playgrounds&#34;&gt;pushed them to GitHub&lt;/a&gt;, &lt;em&gt;just in case&lt;/em&gt; they&amp;rsquo;d be useful to you.&lt;/p&gt;
&lt;h2 id=&#34;repository-structure&#34;&gt;Repository Structure&lt;/h2&gt;
&lt;p&gt;Each tutorial in the Essentials series has its own completed Playground.&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://www.andrewcbancroft.com/blog/ios-development/ui-work/using-swiftui-in-playground/&#34;&gt;Download or clone&lt;/a&gt; the repository to experiment with the SwiftUI tutorials individually in a Playground environment.&lt;/p&gt;
&lt;p&gt;Run the playground, and &lt;a href=&#34;https://www.andrewcbancroft.com/blog/ios-development/xcode/showing-live-view-in-swift-playground/&#34;&gt;show the live preview&lt;/a&gt; to see it in action.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;run-preview.gif&#34; alt=&#34;Run and Preview&#34;&gt;&lt;/p&gt;
&lt;h3 id=&#34;creating-and-combining-views&#34;&gt;Creating and Combining Views&lt;/h3&gt;
&lt;p&gt;All resources required for generating the SwiftUI preview have been added under the Resources folder.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;creating-combining-views.png&#34; alt=&#34;Creating and Combining Views&#34;&gt;&lt;/p&gt;
&lt;h3 id=&#34;building-lists-and-navigation&#34;&gt;Building Lists and Navigation&lt;/h3&gt;
&lt;p&gt;All resources required for generating the SwiftUI preview have been added under the Resources folder.&lt;/p&gt;
&lt;p&gt;Helper code for loading the JSON data file and generating images in the UI at different sizes has been placed in the &lt;code&gt;Data Helpers.swift&lt;/code&gt; file.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;Model Objects.swift&lt;/code&gt; contains the code for the &lt;code&gt;Landmark&lt;/code&gt; and &lt;code&gt;Coordinates&lt;/code&gt; types.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;building-lists-and-navigation.gif&#34; alt=&#34;Building Lists and Navigation&#34;&gt;&lt;/p&gt;
&lt;h3 id=&#34;handling-user-input&#34;&gt;Handling User Input&lt;/h3&gt;
&lt;p&gt;All resources required for generating the SwiftUI preview have been added under the Resources folder.&lt;/p&gt;
&lt;p&gt;Helper code for loading the JSON data file &lt;strong&gt;including the new isFavorite property&lt;/strong&gt;, and generating images in the UI at different sizes has been placed in the &lt;code&gt;Data Helpers.swift&lt;/code&gt; file.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;Model Objects.swift&lt;/code&gt; contains the code for the &lt;code&gt;Landmark&lt;/code&gt; and &lt;code&gt;Coordinates&lt;/code&gt; types.  It also adds the &lt;code&gt;UserData&lt;/code&gt; bindable object.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;handling-user-input.gif&#34; alt=&#34;Handling User Input&#34;&gt;&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Why is Swift ABI Stability a Big Deal?</title>
      <link>https://www.andrewcbancroft.com/blog/ios-development/swift-language-notes/why-is-abi-stability-a-big-deal/</link>
      <pubDate>Fri, 21 Jun 2019 04:40:54 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/blog/ios-development/swift-language-notes/why-is-abi-stability-a-big-deal/</guid>
      <description>&lt;p&gt;Imagine writing some Swift code.  You want to share it across multiple apps, so you build a framework.  You compile it with the Swift 4.0 compiler.&lt;/p&gt;
&lt;p&gt;Now imagine Apple announces Swift 4.1.  Sweet!  You decide to build a new app and compile it with the Swift 4.1 compiler.&lt;/p&gt;
&lt;p&gt;Ah, but your apps needs some of that &amp;ldquo;shared feature&amp;rdquo; code from your framework.&lt;/p&gt;
&lt;p&gt;Will it work?  I&amp;rsquo;ll give you one guess. (the answer&amp;rsquo;s &amp;ldquo;nope&amp;rdquo;)&lt;/p&gt;
&lt;p&gt;Prior to &amp;ldquo;ABI stability&amp;rdquo; being a feature of Swift, the only guarantee we had that two pieces of compiled code could talk to one another is if the two pieces of code were compiled with the &lt;strong&gt;same compiler&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;A framework could be compiled with the Swift 4.0 compiler. &lt;em&gt;You&lt;/em&gt; can build an app with the Swift 4.1 compiler.  &lt;strong&gt;BUT. The two cannot communicate.&lt;/strong&gt;  Either the framework or your app needed to be &lt;strong&gt;re&lt;/strong&gt;compiled using the same version of the compiler for it to work.&lt;/p&gt;
&lt;p&gt;Now, &amp;ldquo;ABI stability&amp;rdquo; &lt;em&gt;is&lt;/em&gt; a feature of Swift Proper.  A framework could be compiled with the Swift 5.0 compiler. &lt;em&gt;You&lt;/em&gt; can build an app with the Swift 5.1 compiler.  &lt;strong&gt;The two can communicate!&lt;/strong&gt;&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Make a Bash Script Executable</title>
      <link>https://www.andrewcbancroft.com/blog/musings/make-bash-script-executable/</link>
      <pubDate>Fri, 21 Jun 2019 04:40:54 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/blog/musings/make-bash-script-executable/</guid>
      <description>&lt;p&gt;If you constantly run the same set of commands at the command line, why not automate that?&lt;/p&gt;
&lt;p&gt;I found myself typing the same things over and over to deploy this website.  Here&amp;rsquo;s how I encapsulated it into a script that saves me keystrokes at the command line.&lt;/p&gt;
&lt;h3 id=&#34;1-create-a-new-text-file-with-a-sh-extension&#34;&gt;1) Create a new text file with a &lt;code&gt;.sh&lt;/code&gt; extension.&lt;/h3&gt;
&lt;p&gt;I created a new file called &lt;code&gt;deploy.sh&lt;/code&gt; for my website.&lt;/p&gt;
&lt;h3 id=&#34;2-add-binbash-to-the-top-of-it&#34;&gt;2) Add &lt;code&gt;#!/bin/bash&lt;/code&gt; to the top of it.&lt;/h3&gt;
&lt;p&gt;This is necessary for the &amp;ldquo;make it executable&amp;rdquo; part.&lt;/p&gt;
&lt;h3 id=&#34;3-add-lines-that-youd-normally-type-at-the-command-line&#34;&gt;3) Add lines that you&amp;rsquo;d normally type at the command line.&lt;/h3&gt;
&lt;p&gt;As an example, here&amp;rsquo;s the full contents of the file I use to deploy general updates to &lt;a href=&#34;https://www.andrewcbancroft.com&#34;&gt;andrewcbancroft.com&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-bash&#34; data-lang=&#34;bash&#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;cp&#34;&gt;#!/bin/bash
&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;hugo
&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;git add .
&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;git commit -m &lt;span class=&#34;s2&#34;&gt;&amp;#34;Updates&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&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;git push
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id=&#34;4-at-the-command-line-run-chmod-ux-yourscriptfilenamesh&#34;&gt;4) At the command line, run &lt;code&gt;chmod u+x YourScriptFileName.sh&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;I ran &lt;code&gt;chmod u+x deploy.sh&lt;/code&gt; to make mine executable.&lt;/p&gt;
&lt;h3 id=&#34;5-run-it-whenever-you-need&#34;&gt;5) Run it whenever you need!&lt;/h3&gt;
&lt;p&gt;Now, whenever I deploy changes to my website, I run &lt;code&gt;./deploy.sh&lt;/code&gt; and &lt;strong&gt;boom&lt;/strong&gt;.  Done.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>What is an ABI</title>
      <link>https://www.andrewcbancroft.com/blog/ios-development/swift-language-notes/what-is-an-abi/</link>
      <pubDate>Thu, 20 Jun 2019 04:40:54 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/blog/ios-development/swift-language-notes/what-is-an-abi/</guid>
      <description>&lt;p&gt;&amp;ldquo;ABI&amp;rdquo; stands for &amp;ldquo;&lt;strong&gt;A&lt;/strong&gt;pplication &lt;strong&gt;B&lt;/strong&gt;inary &lt;strong&gt;I&lt;/strong&gt;nterface&amp;rdquo;.&lt;/p&gt;
&lt;p&gt;The word &amp;ldquo;interface&amp;rdquo; means the same as it does in other places where you hear the word.&lt;/p&gt;
&lt;p&gt;If a &lt;em&gt;human&lt;/em&gt; wants to interact with a computer, s/he needs a way to engage with it.  A &lt;strong&gt;user&lt;/strong&gt; interface is the point where humans and computers communicate with one another.&lt;/p&gt;
&lt;p&gt;If a &lt;em&gt;computer&lt;/em&gt; wants to interact with a computer, it needs a way to engage as well.  An API&amp;hellip;an &lt;strong&gt;Application Programming&lt;/strong&gt; Interface is the point where computers interact with other computers.  It&amp;rsquo;s also the point where a computer communicates with itself!  Computers are just instruction performers.  Developers plan out and write sets of instructions for computers to perform with code. Since the code doesn&amp;rsquo;t write itself (yet!), APIs are &lt;em&gt;also&lt;/em&gt; the point where developers like you and I stitch together chunks of code.  Every class, struct, property, function&amp;hellip;all of them are some form of API.&lt;/p&gt;
&lt;p&gt;So what&amp;rsquo;s an &lt;strong&gt;Application Binary&lt;/strong&gt; Interface?&lt;/p&gt;
&lt;p&gt;If you stack all of these interfaces, one on top of the other, a &lt;strong&gt;U&lt;/strong&gt;I would be on the top.  Underneath the UI are APIs.  And then&amp;hellip; ABIs.&lt;/p&gt;
&lt;p&gt;ABI&amp;rsquo;s are one level deeper than &lt;strong&gt;AP&lt;/strong&gt;Is. They&amp;rsquo;re one step down from code that developers write.&lt;/p&gt;
&lt;p&gt;Developers write instructions for computers to perform in a language. Say&amp;hellip;Swift!&lt;/p&gt;
&lt;p&gt;Computers perform instructions, but iPhones and Macs don&amp;rsquo;t &amp;ldquo;speak&amp;rdquo; Swift.  They &amp;ldquo;speak&amp;rdquo; &lt;strong&gt;binary&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;For a computer to &lt;em&gt;understand&lt;/em&gt;, much less &lt;em&gt;perform&lt;/em&gt; instructions that developers have written in Swift, those instructions have to be translated and packaged. Translating Swift code and packaging it up is called &lt;em&gt;compiling&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;When you launch an app, iOS begins to perform the instructions that a developer compiled.  A message is received by one piece of compiled code and performed by another piece of compiled code.  The result is sent over to another piece of compiled code.  And so on and so on&amp;hellip;&lt;/p&gt;
&lt;p&gt;If &lt;em&gt;compiled code&lt;/em&gt; wants to interact with other &lt;em&gt;compiled code&lt;/em&gt;, guess what?  It needs a way to do that.  It needs an interface.  A &lt;strong&gt;binary&lt;/strong&gt; interface.&lt;/p&gt;
&lt;p&gt;An ABI is the point where &lt;em&gt;compiled code&lt;/em&gt; interacts with &lt;em&gt;other compiled code&lt;/em&gt; while an app is running.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>How Smart (Or Dumb?) Should NSManagedObjects Be?</title>
      <link>https://www.andrewcbancroft.com/blog/ios-development/data-persistence/how-smart-dumb-should-nsmanagedobjects-be/</link>
      <pubDate>Wed, 19 Jun 2019 04:40:54 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/blog/ios-development/data-persistence/how-smart-dumb-should-nsmanagedobjects-be/</guid>
      <description>&lt;p&gt;Apps aren&amp;rsquo;t &amp;ldquo;dumb&amp;rdquo;.  Apps do things.  &lt;em&gt;Smart&lt;/em&gt; things.  The question is:  Where do the &amp;ldquo;smart parts&amp;rdquo; go?&lt;/p&gt;
&lt;p&gt;So how you smart or how dumb should &lt;code&gt;NSManagedObject&lt;/code&gt; subclasses be?&lt;/p&gt;
&lt;p&gt;What code should you write in one of these? What should you &lt;em&gt;not&lt;/em&gt;?&lt;/p&gt;
&lt;h2 id=&#34;tldr&#34;&gt;TLDR;&lt;/h2&gt;
&lt;p&gt;Keep &lt;code&gt;NSManagedObject&lt;/code&gt;s about Core Data-y things.&lt;/p&gt;
&lt;p&gt;Ask, &amp;ldquo;Does this help my &lt;code&gt;NSManagedObject&lt;/code&gt; subclass work with Core Data-related functionality?&amp;rdquo;&lt;/p&gt;
&lt;p&gt;Let the answer to that question guide your decision about whether to extend your &lt;code&gt;NSManagedObject&lt;/code&gt; subclass with additional features.&lt;/p&gt;
&lt;h2 id=&#34;designed-overrides&#34;&gt;Designed Overrides&lt;/h2&gt;
&lt;p&gt;Apple has designed some spots for you to customize an &lt;code&gt;NSManagedObject&lt;/code&gt;&amp;rsquo;s default behavior.&lt;/p&gt;
&lt;h3 id=&#34;managed-object-life-cycle-events&#34;&gt;Managed Object &amp;ldquo;Life Cycle&amp;rdquo; Events&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;NSManagedObject&lt;/code&gt; comes with several overridable methods that you can implement in order to &amp;ldquo;hook in&amp;rdquo; to the life cycle of the object.  What do I mean?  Check out this sampling of overrideable methods:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;awakeFromFetch()&lt;/code&gt;: Provides an opportunity to add code into the life cycle of the managed object when Core Data is bringing the object from the persistent store into memory.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;awakeFromInsert()&lt;/code&gt;: Provides an opportunity to add code into the life cycle of the managed object when it is initially created.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;willSave()&lt;/code&gt;: Provides an opportunity to add code into the life cycle of the managed object when it is about to be saved.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;didSave()&lt;/code&gt;: Provides an opportunity to add code into the life cycle of the managed object after the managed object’s context completes a save operation.&lt;/p&gt;
&lt;p&gt;These methods can be overwritten to add code that should be strategically executed at these pre-determined-by-Apple points in time.&lt;/p&gt;
&lt;h3 id=&#34;validation-code&#34;&gt;Validation Code&lt;/h3&gt;
&lt;p&gt;You can implement custom validation logic in an &lt;code&gt;NSManagedObject&lt;/code&gt; subclass.  Don&amp;rsquo;t override &lt;code&gt;validateValue(_:forKey:)&lt;/code&gt; though.&lt;/p&gt;
&lt;p&gt;Instead, implement a custom method with a signature that matches the signature of &lt;code&gt;validatePropertyName:error:&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;For example, if your &lt;code&gt;NSManagedObject&lt;/code&gt; subclass has an &lt;code&gt;age&lt;/code&gt; property and you wanted to implement special validation logic, you could do this:&lt;/p&gt;
&lt;p&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;12
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;validateAge&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;value&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;AutoreleasingUnsafeMutablePointer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;lt;&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;o&#34;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;kr&#34;&gt;throws&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;cl&#34;&gt;    &lt;span class=&#34;k&#34;&gt;guard&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;valueNumber&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;value&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;pointee&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;NSNumber&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 class=&#34;k&#34;&gt;return&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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;valueNumber&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;floatValue&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;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&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;errorStr&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSLocalizedString&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;Age must be greater than zero&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;tableName&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;Employee&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;comment&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;validation: zero age error&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;userInfoDict&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;NSLocalizedDescriptionKey&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;errorStr&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#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;n&#34;&gt;domain&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;EMPLOYEE_ERROR_DOMAIN&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;code&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;1123&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;userInfo&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;userInfoDict&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;cl&#34;&gt;    &lt;span class=&#34;k&#34;&gt;throw&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;error&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
You could also override one of these methods to add advanced validation:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;validateForInsert()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;validateForUpdate()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;validateForDelete()&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;key-value-observing&#34;&gt;Key-Value Observing&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;NSManagedObject&lt;/code&gt; subclasses inherit methods related to key-value observing that could be appropriate to override.&lt;/p&gt;
&lt;h3 id=&#34;summarizing-designed-overrides&#34;&gt;Summarizing Designed Overrides&lt;/h3&gt;
&lt;p&gt;Have you noticed a pattern in each of these sections?&lt;/p&gt;
&lt;p&gt;Key word:  &lt;strong&gt;override&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Apple designed &lt;code&gt;NSManagedObject&lt;/code&gt; with intentional, overrideable methods.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Should I implement &lt;code&gt;this code&lt;/code&gt; here?&amp;rdquo;&lt;/p&gt;
&lt;p&gt;Take a look at the method names and let that be a guiding factor in your decision-making.&lt;/p&gt;
&lt;h2 id=&#34;on-fetching-behavior&#34;&gt;On Fetching Behavior&lt;/h2&gt;
&lt;p&gt;Xcode adds a &lt;code&gt;fetchRequest&lt;/code&gt; method when it auto-generates code for you.  So&amp;hellip;I&amp;rsquo;d say it&amp;rsquo;s probably okay to put some &lt;em&gt;basic&lt;/em&gt; fetching behavior with an &lt;code&gt;NSManagedObject&lt;/code&gt; subclass.  Keep it simple though:&lt;/p&gt;
&lt;p&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;  &lt;span class=&#34;p&#34;&gt;@&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;nonobjc&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;func&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;p&#34;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSFetchRequest&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;EntityName&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&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;NSFetchRequest&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;EntityName&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;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;EntityName&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;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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
My personal preference is to put object fetching behavior in a separate Type.&lt;/p&gt;
&lt;p&gt;You might make a &lt;code&gt;DataProvider&lt;/code&gt; Type and let &lt;em&gt;it&lt;/em&gt; handle setting up &lt;code&gt;NSFetchRequest&lt;/code&gt;(s) with appropriate predicates and sort descriptors.&lt;/p&gt;
&lt;p&gt;Avoid putting &lt;em&gt;all&lt;/em&gt; data-providing code in a catch-all &lt;code&gt;DataProvider&lt;/code&gt;.  If you&amp;rsquo;re fetching &lt;code&gt;Note&lt;/code&gt;s, make a &lt;code&gt;NoteProvider&lt;/code&gt;.  If you&amp;rsquo;re fetching &lt;code&gt;Tag&lt;/code&gt;s, make a &lt;code&gt;TagProvider&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&#34;on-convenience-properties--functions&#34;&gt;On Convenience Properties &amp;amp; Functions&lt;/h2&gt;
&lt;p&gt;One piece of convenience code that I extend my &lt;code&gt;NSManagedObject&lt;/code&gt; subclasses with is an &lt;code&gt;entityName&lt;/code&gt; property.&lt;/p&gt;
&lt;p&gt;This helps me avoid sprinkling the name of an Entity in &lt;code&gt;String&lt;/code&gt; form throughout my code.&lt;/p&gt;
&lt;p&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;extension&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;EntityName&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;static&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#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;EntityName&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
When it comes to other types of functionality, my recommendation is to put that code elsewhere.&lt;/p&gt;
&lt;p&gt;Keep &lt;code&gt;NSManagedObject&lt;/code&gt;s about Core Data-y things.&lt;/p&gt;
&lt;p&gt;Ask, &amp;ldquo;Does this help my &lt;code&gt;NSManagedObject&lt;/code&gt; subclass work with Core Data-related functionality?&amp;rdquo;&lt;/p&gt;
&lt;p&gt;Let the answer to that question guide your decision about whether to extend your &lt;code&gt;NSManagedObject&lt;/code&gt; subclass with additional features.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Additional features&amp;rdquo; often translates to &amp;ldquo;additional responsibility&amp;rdquo;. Single responsibility is the name of the game if you can achieve it.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>On Core Data Object Fetching and Display Strategies</title>
      <link>https://www.andrewcbancroft.com/blog/ios-development/data-persistence/on-core-data-object-fetching-and-display-strategies/</link>
      <pubDate>Fri, 14 Jun 2019 04:40:54 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/blog/ios-development/data-persistence/on-core-data-object-fetching-and-display-strategies/</guid>
      <description>&lt;h2 id=&#34;setting-the-stage&#34;&gt;Setting the Stage&lt;/h2&gt;
&lt;p&gt;Suppose one of the &lt;code&gt;NSManagedObject&lt;/code&gt; subclasses from your Core Data data model looked like this:&lt;/p&gt;
&lt;p&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;6
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;BlogIdea&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSManagedObject&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;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kr&#34;&gt;@NSManaged&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;ideaTitle&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;cl&#34;&gt;    &lt;span class=&#34;kr&#34;&gt;@NSManaged&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;ideaDescription&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;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
Suppose that you also have a class that will provide &lt;code&gt;BlogIdeas&lt;/code&gt; to your view controller:&lt;/p&gt;
&lt;p&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;BlogIdeaProvider&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;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// fetches BlogIdea instances to use in your view controller&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;strong&gt;Requirement:&lt;/strong&gt; Say that you need to list all of the &lt;code&gt;ideaTitle&lt;/code&gt;s you have inside of a table view (note that you &lt;em&gt;don&amp;rsquo;t&lt;/em&gt; need to display the &lt;code&gt;ideaDescription&lt;/code&gt; at this time).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Question:&lt;/strong&gt;  Which of the following strategies would you recommend for this requirement?&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Fetch all the &lt;code&gt;BlogIdea&lt;/code&gt; objects, return them, and then use those returned &lt;code&gt;NSManagedObject&lt;/code&gt; instances to display the &lt;code&gt;ideaTitle&lt;/code&gt; in the table view.&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;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;BlogIdeaProvider&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line hl&#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;fetchBlogIdeas&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;BlogIdea&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;// fetch BlogIdea instances&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;c1&#34;&gt;// return the BlogIdea NSManagedObject subclass for the view controller to use&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;ol start=&#34;2&#34;&gt;
&lt;li&gt;Fetch all of the &lt;code&gt;BlogIdea&lt;/code&gt; objects, but make a &lt;code&gt;fetchBlogIdeaTitles&lt;/code&gt; function instead. Just return the &lt;code&gt;ideaTitle&lt;/code&gt;s, say, in an array of strings to display in the table view.&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;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;BlogIdeaProvider&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line hl&#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;fetchBlogIdeaTitles&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;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;3&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;c1&#34;&gt;// fetch BlogIdea instances&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;c1&#34;&gt;// pull out ONLY the ideaTitle, &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;// assemble an array for the titles, &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;// return the array&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;8&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;9&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2 id=&#34;swifting-out-loud&#34;&gt;Swifting Out Loud&lt;/h2&gt;
&lt;p&gt;I&amp;rsquo;ll give my recommendation and then walk through my reasoning.&lt;/p&gt;
&lt;p&gt;In nearly all situations, I would go with option number 1: fetch the &lt;code&gt;NSManagedObject&lt;/code&gt; instances that I need to display, and work with those fully-featured objects, rather than go through the effort to return only the &lt;code&gt;String&lt;/code&gt;s that I need for display.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Why?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;To that, I&amp;rsquo;d ask the question:  &amp;ldquo;What are you planning to do &lt;em&gt;next&lt;/em&gt;, once the &lt;code&gt;ideaTitle&lt;/code&gt;s are displayed in the table view?&lt;/p&gt;
&lt;p&gt;Presumably, you&amp;rsquo;re displaying them &lt;em&gt;so that&lt;/em&gt; a user can tap on a cell and&amp;hellip;do something with them.  And that &amp;ldquo;do something with them&amp;rdquo; part almost always means&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Show &lt;em&gt;more&lt;/em&gt; details (in which case, you need the &lt;code&gt;ideaDescription&lt;/code&gt; property as well now)&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Edit&lt;/em&gt; the object (in which case you get all your saving features only &lt;strong&gt;if&lt;/strong&gt; it&amp;rsquo;s in &lt;code&gt;NSManagedObject&lt;/code&gt; form)&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Delete&lt;/em&gt; the object (again&amp;hellip;needs to be a full &lt;code&gt;NSManagedObject&lt;/code&gt; to easily delete)&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;efficiency-trade-offs&#34;&gt;Efficiency Trade-offs&lt;/h2&gt;
&lt;p&gt;One might be concerned about efficiency &amp;ndash; &amp;ldquo;Why return a full object when you only need one of its properties?&amp;rdquo;&lt;/p&gt;
&lt;p&gt;That&amp;rsquo;s true. You only need one property&amp;hellip;&lt;em&gt;for now&lt;/em&gt;.  Again, I think it goes back to &amp;ldquo;what happens next?&amp;rdquo;&lt;/p&gt;
&lt;h3 id=&#34;fetching-efficiency&#34;&gt;Fetching Efficiency&lt;/h3&gt;
&lt;p&gt;Will you display the &lt;code&gt;ideaTitle&lt;/code&gt; and then have to go fetch &lt;em&gt;again&lt;/em&gt; to get the rest of what you need from your persistent store?  That&amp;rsquo;s almost guaranteed to be less efficient than holding the full &lt;code&gt;NSManagedObject&lt;/code&gt; in memory from the start.&lt;/p&gt;
&lt;h3 id=&#34;memory-efficiency&#34;&gt;Memory Efficiency&lt;/h3&gt;
&lt;p&gt;If you&amp;rsquo;re trying to stay memory-efficient, perhaps another idea is to fetch only a subset of the &lt;code&gt;BlogIdea&lt;/code&gt;s&amp;hellip; You could limit the number of results that come back (after all, only so many can be shown in a table view at a time, anyway, right?).&lt;/p&gt;
&lt;h3 id=&#34;code-efficiency&#34;&gt;Code Efficiency&lt;/h3&gt;
&lt;p&gt;When you say &amp;ldquo;Core Data&amp;rdquo; and &amp;ldquo;table view&amp;rdquo; in the same sentence, it should also trigger the word &lt;code&gt;NSFetchedResultsController&lt;/code&gt;.  This class is a huuuuge help because it was designed specifically for displaying data from &lt;code&gt;NSManagedObject&lt;/code&gt;s in table or collection views.  If you only return an array of &lt;code&gt;String&lt;/code&gt;s, you&amp;rsquo;ll have to write a bunch of boiler plate code yourself to keep your table view in sync with your persistent store, but you get all of that for free with &lt;code&gt;NSFetchedResultsController&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Using &lt;code&gt;NSFetchedResultsController&lt;/code&gt; would change the code a bit.  Instead of returning an array of blog ideas (&lt;code&gt;[BlogIdea]&lt;/code&gt;), you could hold a reference to a &lt;code&gt;NSFetchedResultsController&amp;lt;BlogIdea&amp;gt;&lt;/code&gt; and configure it to fetch:&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;// &lt;/span&gt;&lt;span class=&#34;cs&#34;&gt;MARK:&lt;/span&gt;&lt;span class=&#34;c1&#34;&gt; - Concept&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;// If you give a `Provider` a persistent container, and a fetched results controller delegate to talk back to,&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;// a `Provider` Type can act as a liaison between the view controller and the pieces of Core Data that are needed&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;// to initialize a fetched results controller, perform fetches, and other Core Data related actions on behalf of your&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;// view controller (instead of you putting all this code in the view controller itself)...&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;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;BlogIdeaProvider&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 8&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;private&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;persistentContainer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSPersistentContainer&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;private&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;fetchedResultsControllerDelegate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSFetchedResultsControllerDelegate&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;// &lt;/span&gt;&lt;span class=&#34;cs&#34;&gt;MARK:&lt;/span&gt;&lt;span class=&#34;c1&#34;&gt; - Initializers&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;init&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;with&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;persistentContainer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSPersistentContainer&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;fetchedResultsControllerDelegate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSFetchedResultsControllerDelegate&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&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;persistentContainer&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;persistentContainer&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;fetchedResultsControllerDelegate&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;fetchedResultsControllerDelegate&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;p&#34;&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;c1&#34;&gt;// use this in your view controller to display BlogIdea instances&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line hl&#34;&gt;&lt;span class=&#34;ln&#34;&gt;21&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kr&#34;&gt;lazy&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;fetchedResultsController&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSFetchedResultsController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;BlogIdea&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;p&#34;&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;c1&#34;&gt;// Configure a fetched results controller and perform an initial fetch&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;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;blogIdeasFetchRequest&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;&amp;lt;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;BlogIdea&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;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;BlogIdea&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&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;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;controller&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSFetchedResultsController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;BlogIdea&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;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;blogIdeasFetchRequest&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;managedObjectContext&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;persistentContainer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;viewContext&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;27&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;                                                                &lt;span class=&#34;n&#34;&gt;sectionNameKeyPath&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;28&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;                                                                &lt;span class=&#34;n&#34;&gt;cacheName&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;29&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;30&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;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;fetchedResultsControllerDelegate&lt;/span&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&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;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;33&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;controller&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;performFetch&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 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;35&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;            &lt;span class=&#34;bp&#34;&gt;fatalError&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&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;kc&#34;&gt;#function&lt;/span&gt;&lt;span class=&#34;si&#34;&gt;)&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;: Failed to performFetch: &lt;/span&gt;&lt;span class=&#34;si&#34;&gt;\(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;error&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 class=&#34;p&#34;&gt;)&lt;/span&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;p&#34;&gt;}&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&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;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;controller&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;h2 id=&#34;concluding-thoughts&#34;&gt;Concluding Thoughts&lt;/h2&gt;
&lt;p&gt;I know I keep going back to it, but I think it&amp;rsquo;s what will help make the decision.&lt;/p&gt;
&lt;p&gt;What&amp;rsquo;s the next thing you&amp;rsquo;re expecting to happen after you fetch the objects and display them?&lt;/p&gt;
&lt;p&gt;Let that be your guiding principle for your fetching and displaying strategy.&lt;/p&gt;
&lt;p&gt;With Core Data, it&amp;rsquo;s almost always going to be more convenient to be working with &lt;code&gt;NSManagedObject&lt;/code&gt;s.  Start there, and handle efficiency problems as they arise. 🙌🏻&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Using SwiftUI in a Playground</title>
      <link>https://www.andrewcbancroft.com/blog/ios-development/ui-work/using-swiftui-in-playground/</link>
      <pubDate>Thu, 13 Jun 2019 04:00:18 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/blog/ios-development/ui-work/using-swiftui-in-playground/</guid>
      <description>&lt;p&gt;If you&amp;rsquo;re looking for a quicker way to iterate while you&amp;rsquo;re building SwiftUI views, and you don&amp;rsquo;t have macOS Catalina installed, you might enjoy the Playground experience more than you enjoy pressing &lt;code&gt;command + R&lt;/code&gt; every time you make a change and want to see it in the user interface.&lt;/p&gt;
&lt;p&gt;How do you do it?  Here&amp;rsquo;s a code snippet to copy-paste into a Playground to get you going.&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 hl&#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;PlaygroundSupport&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line hl&#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;SwiftUI&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;c1&#34;&gt;// Make a SwiftUI view&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;struct&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;ContentView&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;View&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;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;body&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;some&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;View&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;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;SwiftUI in a Playground!&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;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;// Make a UIHostingController&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line hl&#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;viewController&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIHostingController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;rootView&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ContentView&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;// Assign it to the playground&amp;#39;s liveView&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line hl&#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;PlaygroundPage&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;current&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;liveView&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;viewController&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;c1&#34;&gt;// RUN!&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;img src=&#34;result.png&#34; alt=&#34;Result&#34;&gt;&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Getting Started With NSPersistentCloudKitContainer</title>
      <link>https://www.andrewcbancroft.com/blog/ios-development/data-persistence/getting-started-with-nspersistentcloudkitcontainer/</link>
      <pubDate>Tue, 11 Jun 2019 04:40:54 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/blog/ios-development/data-persistence/getting-started-with-nspersistentcloudkitcontainer/</guid>
      <description>&lt;h2 id=&#34;first-things-first&#34;&gt;First Things First&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;In order to use &lt;code&gt;NSPersistentCloudKitContainer&lt;/code&gt; in your app, you need to be targeting iOS 13+.&lt;/li&gt;
&lt;li&gt;The CloudKit syncing portion only works on physical devices (not in the simulator).  That being the case, you&amp;rsquo;ll need two or more devices running the app and using the same iCloud account to fully test things out and make sure syncing and data merging behavior works as you&amp;rsquo;d expect it to.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The goal of this walkthrough is to provide you with a fully-working example of performing and synchronizing create, read, update, and delete operations using &lt;code&gt;NSPersistentCloudKitContainer&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;It&amp;rsquo;s the project I wish I had for referencing the basics&amp;hellip;something &lt;strong&gt;beyond&lt;/strong&gt; the out-of-the-box, saving a list of timestamps that comes pre-implemented with a Master-Detail app, but something &lt;strong&gt;less complicated&lt;/strong&gt; than &lt;a href=&#34;https://developer.apple.com/documentation/coredata/synchronizing_a_local_store_to_the_cloud&#34; rel=&#34;nofollow&#34;&gt;Apple&amp;rsquo;s example project&lt;/a&gt; which ends up having several relationships involved in the data model, extra features configured on the persistent container, etc.&lt;/p&gt;
&lt;p&gt;I offer it to you as reference as well &amp;ndash; I hope it helps!&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;resources&#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-file-code&#34;&gt;&lt;/i&gt; &lt;a href=&#34;https://github.com/andrewcbancroft/BlogIdeaList&#34;&gt; Blog Idea List Example Xcode Project&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;i class=&#34;fas fa-link&#34;&gt;&lt;/i&gt; &lt;a href=&#34;https://developer.apple.com/videos/play/wwdc2019/202&#34; rel=&#34;nofollow&#34;&gt;Using Core Data With CloudKit - WWDC 2019 Session 202&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;h2 id=&#34;setup&#34;&gt;Setup&lt;/h2&gt;
&lt;p&gt;The easiest way to get stareted with &lt;code&gt;NSPersistentCloudKitContainer&lt;/code&gt; is to enable Core Data and CloudKit when you start your new project.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;enable-core-data-cloudkit.png&#34; alt=&#34;Enable Core Data and CloudKit&#34;&gt;&lt;/p&gt;
&lt;h3 id=&#34;the-core-data--cloudkit-stack&#34;&gt;The Core Data + CloudKit Stack&lt;/h3&gt;
&lt;p&gt;Xcode provides the Core Data Stack code you need in the usual location: in the AppDelegate.swift file.&lt;/p&gt;
&lt;p&gt;There&amp;rsquo;s one notable difference though.  Instead of initializing a normal &lt;code&gt;NSPersistentContainer&lt;/code&gt;, Xcode uses the new &lt;code&gt;NSPersistentCloudKitContainer&lt;/code&gt; (this is why it&amp;rsquo;s important to check the CloudKit box when you create your app).&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;nspersistentcloudkitcontainer-code.png&#34; alt=&#34;NSPersistentCloudKitContainer&#34;&gt;&lt;/p&gt;
&lt;h3 id=&#34;what-about-existing-apps&#34;&gt;What About Existing Apps?&lt;/h3&gt;
&lt;p&gt;If you&amp;rsquo;ve got an existing app that&amp;rsquo;s using a &amp;ldquo;regular&amp;rdquo; &lt;code&gt;NSPersistentContainer&lt;/code&gt;, you can change it to &lt;code&gt;NSPersistentCloudKitContainer&lt;/code&gt; and continue on from there!&lt;/p&gt;
&lt;p&gt;If you&amp;rsquo;re not using &lt;code&gt;NSPersistentContainer&lt;/code&gt; with your app yet, &lt;a href=&#34;https://www.andrewcbancroft.com/2017/07/10/using-an-existing-sqlite-store-with-nspersistentcontainer/&#34;&gt;my guide on how to migrate&lt;/a&gt; may be of help!&lt;/p&gt;
&lt;h3 id=&#34;adding-capabilities&#34;&gt;Adding Capabilities&lt;/h3&gt;
&lt;p&gt;While Xcode auto-generates the Core Data + CloudKit stack for you, it does &lt;em&gt;not&lt;/em&gt; enable iCloud for you.&lt;/p&gt;
&lt;p&gt;To enable full functionality, you need to go to your &lt;strong&gt;Xcode project settings&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Then click on the &lt;strong&gt;Signing &amp;amp; Capabilities&lt;/strong&gt; section and add the &lt;strong&gt;iCloud&lt;/strong&gt; and &lt;strong&gt;Background Modes&lt;/strong&gt; capabilities.&lt;/p&gt;
&lt;p&gt;Check the &lt;strong&gt;CloudKit checkbox&lt;/strong&gt;, and the &lt;strong&gt;Remote Notifications checkbox&lt;/strong&gt;.&lt;/p&gt;
&lt;h4 id=&#34;add-icloud--cloudkit&#34;&gt;Add iCloud + CloudKit&lt;/h4&gt;
&lt;p&gt;&lt;img src=&#34;add-icloud.gif&#34; alt=&#34;Add iCloud with CloudKit&#34;&gt;&lt;/p&gt;
&lt;h4 id=&#34;add-background-modes--remote-notifications&#34;&gt;Add Background Modes + Remote Notifications&lt;/h4&gt;
&lt;p&gt;&lt;img src=&#34;add-background-modes.gif&#34; alt=&#34;Add Background Modes with Remote Notifications&#34;&gt;&lt;/p&gt;
&lt;h3 id=&#34;what-about-registering-for-remote-notifications&#34;&gt;What About Registering for Remote Notifications?&lt;/h3&gt;
&lt;p&gt;The beauty of &lt;code&gt;NSPersistentCloudKitContainer&lt;/code&gt; is that it handles all the work required to listen for and respond to remote notifications.&lt;/p&gt;
&lt;p&gt;This means that data saved locally on &lt;em&gt;one&lt;/em&gt; device gets pushed up to iCloud synced back down to &lt;em&gt;another&lt;/em&gt; device automatically.&lt;/p&gt;
&lt;h3 id=&#34;what-about-my-data-model-in-icloud&#34;&gt;What About My Data Model in iCloud?&lt;/h3&gt;
&lt;p&gt;When you enable the iCloud capability for your app, Xcode automatically creates a container for your app in the CloudKit Dashboard.&lt;/p&gt;
&lt;p&gt;It does &lt;em&gt;not&lt;/em&gt;, however, create a schema that matches your Core Data model&amp;hellip;yet.  This is a setting you can toggle, but the default behavior is to &lt;a href=&#34;#revisiting-the-icloud-schema&#34;&gt;&amp;ldquo;lazily&amp;rdquo; create the schema&lt;/a&gt; as people create objects and save them to your persistent store.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;empty-schema.gif&#34; alt=&#34;Empty Schema&#34;&gt;&lt;/p&gt;
&lt;h2 id=&#34;walking-through-the-example-project&#34;&gt;Walking Through the Example Project&lt;/h2&gt;
&lt;p&gt;WWDC always gets the blog ideas flowing through my brain, so this little app&amp;rsquo;s theme is &amp;ldquo;saving a list of blog ideas&amp;rdquo;, both locally, and &lt;em&gt;across&lt;/em&gt; my devices using &lt;code&gt;NSPersistentCloudKitContainer&lt;/code&gt;.&lt;/p&gt;
&lt;h3 id=&#34;user-interface&#34;&gt;User Interface&lt;/h3&gt;
&lt;p&gt;The user interfacde allows you to perform the essential operations of a data-driven app:  create, read, update, and delete objects.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;user-interface.png&#34; alt=&#34;User Interface&#34;&gt;&lt;/p&gt;
&lt;h3 id=&#34;data-model&#34;&gt;Data Model&lt;/h3&gt;
&lt;p&gt;The data model for this is basic:  a single &lt;code&gt;BlogIdea&lt;/code&gt; Entity with two &lt;code&gt;String&lt;/code&gt; properties: &lt;code&gt;ideaTitle&lt;/code&gt; and &lt;code&gt;ideaDescription&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;data-model.png&#34; alt=&#34;BlogIdea Data Model&#34;&gt;&lt;/p&gt;
&lt;p&gt;No relationships or extra configuration options&amp;hellip; just one Entity to keep it a basic reference-able project.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;BlogIdea&lt;/code&gt; NSManagedObject subclass is implemented like this:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;7
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;BlogIdea&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSManagedObject&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;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kr&#34;&gt;@NSManaged&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;ideaTitle&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;cl&#34;&gt;    &lt;span class=&#34;kr&#34;&gt;@NSManaged&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;ideaDescription&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;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;static&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;entityName&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;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;BlogIdea&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;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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;h3 id=&#34;view-controllers&#34;&gt;View Controllers&lt;/h3&gt;
&lt;p&gt;There are two view controllers that come with this example:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;MainViewController&lt;/strong&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;12
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;13
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;14
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;15
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;16
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;17
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;18
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;MainViewController&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;cl&#34;&gt;                            &lt;span class=&#34;n&#34;&gt;NSFetchedResultsControllerDelegate&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;cl&#34;&gt;                            &lt;span class=&#34;n&#34;&gt;UITableViewDataSource&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;cl&#34;&gt;                            &lt;span class=&#34;n&#34;&gt;UITableViewDelegate&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;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Responsible for listing out BlogIdeas in a table view&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Holds a reference to an NSManagedObjectContext instance&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// which gets initialized in the SceneDelegate.swift file&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// and passed to this view controller when the scene gets &amp;#34;connected&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Uses NSFetchedResultsController to keep the table view in sync&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// with the Core Data managed object context&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Implements swipe-to-delete with delete confirmation&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Navigates to editor when someone taps on a table view row&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// and passes its NSManagedObjectContext instance along&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;strong&gt;BlogIdeaEditorViewController&lt;/strong&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;8
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;BlogIdeaEditorViewController&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;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Responsible for creating new BlogIdeas&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Holds a reference to an NSManagedObjectContext instance&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// which gets passed along in MainViewController&amp;#39;s prepare for segue method&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Able to edit existing BlogIdeas&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;/p&gt;
&lt;h2 id=&#34;understanding-the-default-sync-behavior&#34;&gt;Understanding the Default Sync Behavior&lt;/h2&gt;
&lt;p&gt;At this point no additional configuration has happened. The Core Data stack is the unmodified Xcode generated code, and the fetched results controller is hooked up to the managed object context for your app, ready to fetch Blog Ideas and help get them into the table view.&lt;/p&gt;
&lt;p&gt;If you open the project, build and run on two devices, and begin adding Blog Ideas, updating them, deleting them, etc. what happens?&lt;/p&gt;
&lt;p&gt;The first observation is that&amp;hellip;things&amp;hellip;don&amp;rsquo;t&amp;hellip;seem to be working&amp;hellip;&lt;/p&gt;
&lt;div style=&#34;position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;&#34;&gt;
			&lt;iframe allow=&#34;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share; fullscreen&#34; loading=&#34;eager&#34; referrerpolicy=&#34;strict-origin-when-cross-origin&#34; src=&#34;https://www.youtube.com/embed/DLAwrSCl3Cc?autoplay=0&amp;amp;controls=1&amp;amp;end=0&amp;amp;loop=0&amp;amp;mute=0&amp;amp;start=0&#34; style=&#34;position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;&#34; title=&#34;YouTube video&#34;&gt;&lt;/iframe&gt;
		&lt;/div&gt;

&lt;p&gt;&amp;hellip;At least not how I thought they would.&lt;/p&gt;
&lt;p&gt;I &lt;em&gt;expected&lt;/em&gt; my Blog List view to automatically update in response to changes made on my other device, buuut&amp;hellip; nope.  Thankfully it&amp;rsquo;s an &amp;ldquo;easy&amp;rdquo; fix.&lt;/p&gt;
&lt;h2 id=&#34;reflecting-changes-in-the-ui&#34;&gt;Reflecting Changes in the UI&lt;/h2&gt;
&lt;p&gt;Wherever you access your app&amp;rsquo;s persistent CloudKit container to grab the &lt;code&gt;viewContext&lt;/code&gt;, you need to set the &lt;code&gt;automaticallyMergesChangesFromParent&lt;/code&gt; property to &lt;code&gt;true&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;I initialize the container in the &lt;code&gt;SceneDelegate&lt;/code&gt;, so check out the code exerpt below to see where that gets set.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;SceneDelegate.swift&lt;/strong&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;hl&#34;&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;12
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;13
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;14
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;scene&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;scene&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIScene&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;willConnectTo&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;session&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UISceneSession&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;n&#34;&gt;connectionOptions&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIScene&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;ConnectionOptions&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;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;k&#34;&gt;guard&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#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;scene&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;UIWindowScene&lt;/span&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 class=&#34;k&#34;&gt;return&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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;navigationController&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;window&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;?.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;rootViewController&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;UINavigationController&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;mainVC&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;navigationController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;viewControllers&lt;/span&gt;&lt;span class=&#34;p&#34;&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 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;MainViewController&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;viewContext&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;UIApplication&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;shared&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;k&#34;&gt;as&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;!&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;AppDelegate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;).&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;persistentContainer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;viewContext&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line hl&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;n&#34;&gt;viewContext&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;automaticallyMergesChangesFromParent&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&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;    &lt;span class=&#34;n&#34;&gt;mainVC&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;viewContext&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;/p&gt;
&lt;p&gt;Making this one-line change will enable the app (which is supported by &lt;code&gt;NSFetchedResultsController&lt;/code&gt;) to update the UI in response to remote data changes&amp;hellip;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;eventually&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;It can be slow (anywhere from 5 to 15 seconds).  But it will eventually update.&lt;/p&gt;
&lt;h2 id=&#34;revisiting-the-icloud-schema&#34;&gt;Revisiting the iCloud Schema&lt;/h2&gt;
&lt;p&gt;Once you save an object, the schema within the iCloud Dashboard will update to reflect the properties you designed in your Core Data Model.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;revisit-schema.png&#34; alt=&#34;Revisiting the Schema&#34;&gt;&lt;/p&gt;
&lt;h2 id=&#34;wheres-my-data&#34;&gt;Where&amp;rsquo;s My Data?&lt;/h2&gt;
&lt;p&gt;If you attempt to query for BlogIdea records in any of the iCloud databases provisioned for your app, you won&amp;rsquo;t find any.&lt;/p&gt;
&lt;p&gt;Why?&lt;/p&gt;
&lt;p&gt;That&amp;rsquo;s because Apple fully manages a hidden Zone for your Core Data + CloudKit data. It was veeeery briefly mentioned in the &lt;a href=&#34;https://developer.apple.com/videos/play/wwdc2019/202&#34; rel=&#34;nofollow&#34;&gt;WWDC 2019 presentation&lt;/a&gt; (right about 10:50 if you&amp;rsquo;re watching).&lt;/p&gt;
&lt;h2 id=&#34;wrapping-up&#34;&gt;Wrapping Up&lt;/h2&gt;
&lt;p&gt;My hope was to provide you with a fully-working example of performing and synchronizing create, read, update, and delete operations using &lt;code&gt;NSPersistentCloudKitContainer&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Check out the &lt;a href=&#34;https://github.com/andrewcbancroft/BlogIdeaList&#34;&gt;GitHub repo&lt;/a&gt;, leave a comment, or &lt;a href=&#34;https://twitter.com/andrewcbancroft&#34;&gt;@ me on Twitter&lt;/a&gt; to continue the conversation from here!&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Accessing the Root View Controller at Launch on iOS 13 with SceneDelegate</title>
      <link>https://www.andrewcbancroft.com/blog/ios-development/ui-work/accessing-root-view-controller-ios13-scenedelegate/</link>
      <pubDate>Fri, 07 Jun 2019 18:20:41 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/blog/ios-development/ui-work/accessing-root-view-controller-ios13-scenedelegate/</guid>
      <description>&lt;p&gt;A new &lt;a href=&#34;https://developer.apple.com/documentation/uikit/uiwindowscenedelegate&#34; rel=&#34;nofollow&#34;&gt;SceneDelegate&lt;/a&gt; was introduced for Storyboard-based apps in iOS 13, and with it came some changes in how you&amp;rsquo;re able to access your app&amp;rsquo;s root view controller at app launch.&lt;/p&gt;
&lt;h2 id=&#34;the-old-way&#34;&gt;The Old Way&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;AppDelegate&lt;/code&gt; Swift file used to be where code landed for accessing your app&amp;rsquo;s &lt;code&gt;rootViewController&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;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;application&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;application&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIApplication&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;didFinishLaunchingWithOptions&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;launchOptions&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;UIApplication&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;LaunchOptionsKey&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;Any&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;2&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;c1&#34;&gt;// Override point for customization after application launch.&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line hl&#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;rootVC&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;window&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;?.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;rootViewController&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;return&lt;/span&gt; &lt;span class=&#34;kc&#34;&gt;true&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2 id=&#34;what-to-do-now&#34;&gt;What To Do Now&lt;/h2&gt;
&lt;p&gt;&lt;em&gt;Now&lt;/em&gt;, you&amp;rsquo;ll be moving that code over to your app&amp;rsquo;s &lt;code&gt;SceneDelegate&lt;/code&gt; Swift file:&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;SceneDelegate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIResponder&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIWindowSceneDelegate&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 class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;window&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIWindow&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&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;scene&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;scene&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIScene&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;willConnectTo&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;session&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UISceneSession&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;n&#34;&gt;connectionOptions&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIScene&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;ConnectionOptions&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;// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.&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;// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.&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;// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).&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;guard&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#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;scene&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;UIWindowScene&lt;/span&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 class=&#34;k&#34;&gt;return&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 hl&#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;rootVC&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;window&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;?.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;rootViewController&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;// ... the rest of SceneDelegate&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>New Delights With Xcode 11</title>
      <link>https://www.andrewcbancroft.com/blog/ios-development/xcode/new-delights-with-xcode-11/</link>
      <pubDate>Fri, 07 Jun 2019 04:21:19 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/blog/ios-development/xcode/new-delights-with-xcode-11/</guid>
      <description>&lt;h2 id=&#34;capabilities-search&#34;&gt;Capabilities Search&lt;/h2&gt;
&lt;p&gt;In your project settings, you can search for capabilities you want to add (instead of scrolling down the list and turning things &amp;ldquo;on&amp;rdquo; or &amp;ldquo;off&amp;rdquo;).&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;capabilities-search.gif&#34; alt=&#34;Capabilities Search&#34;&gt;&lt;/p&gt;
&lt;h2 id=&#34;editor-quick-actions&#34;&gt;Editor Quick Actions&lt;/h2&gt;
&lt;p&gt;Holding the &lt;code&gt;command&lt;/code&gt; key down while mousing over and clicking on various levels of your code presents a quick action pop-out.  Handy!  Xcode 10 had some of these as well, but Xcode 11 adds additional actions in additional areas.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;editor-quick-actions.gif&#34; alt=&#34;Editor Quick Actions&#34;&gt;&lt;/p&gt;
&lt;h2 id=&#34;jump-to-code-from-minimap&#34;&gt;Jump to Code from Minimap&lt;/h2&gt;
&lt;p&gt;The minimap in and of itself is nice, but I found the &amp;ldquo;jump to code&amp;rdquo; features smart and convenient.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;jump-to-code.gif&#34; alt=&#34;Jump to Code&#34;&gt;&lt;/p&gt;
&lt;h2 id=&#34;marks-in-the-minimap&#34;&gt;Marks in the Minimap&lt;/h2&gt;
&lt;p&gt;Have you noticed that &lt;code&gt;// Mark:&lt;/code&gt; produces &amp;ldquo;header&amp;rdquo;-like text in the minimap?&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;marks-minimap.png&#34; alt=&#34;Marks in the Minimap&#34;&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;// Mark:&lt;/code&gt; also produces a thin line break in your code - I think it definitely helps spot divisions in your code and could help find what you want a little quicker.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;marks-linebreak.png&#34; alt=&#34;Marks Line Break&#34;&gt;&lt;/p&gt;
&lt;h2 id=&#34;history-inspector-source-control-history&#34;&gt;History Inspector (source control history)&lt;/h2&gt;
&lt;p&gt;There&amp;rsquo;s a little source control icon in the Inspectors pane that lets you quickly see commit history and perform quick actions.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;show-history-inspector.gif&#34; alt=&#34;Show History Inspector&#34;&gt;&lt;/p&gt;
&lt;h2 id=&#34;tooltip-from-minimap&#34;&gt;Tooltip From Minimap&lt;/h2&gt;
&lt;p&gt;If you move your mouse over the minimap, Xcode will display little tooltips out to the left that correspond to major sections of your code, such as the class/struct declaration, properties, and functions.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;tooltip-from-minimap.gif&#34; alt=&#34;Tooltip From Minimap&#34;&gt;&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Resolving Failed to Load Model Named... with Core Data</title>
      <link>https://www.andrewcbancroft.com/blog/ios-development/data-persistence/resolving-core-data-failed-to-load-model-named/</link>
      <pubDate>Thu, 06 Jun 2019 04:40:54 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/blog/ios-development/data-persistence/resolving-core-data-failed-to-load-model-named/</guid>
      <description>&lt;p&gt;You&amp;rsquo;ve done the work to get your Core Data model ready, but right when you hit run, &lt;em&gt;wham&lt;/em&gt;.  Your debug output log shows:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Failed to load model named &lt;strong&gt;ModelName&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;You you may experience one of these when you attempt to access your persistent container:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;What in the world is going on?&lt;/p&gt;
&lt;h2 id=&#34;double-check-target-membership&#34;&gt;Double-check Target Membership&lt;/h2&gt;
&lt;p&gt;This is particularly important if you happend to have copied and pasted and &lt;code&gt;.xcdatamodeld&lt;/code&gt; file between projects.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;target-membership.png&#34; alt=&#34;Check Target Membership&#34;&gt;&lt;/p&gt;
&lt;h2 id=&#34;double-check-model-name&#34;&gt;Double-check Model Name&lt;/h2&gt;
&lt;p&gt;Your model name needs to match throughout the various places you refer to it.  Things can get out of sync if you ever rename your model.&lt;/p&gt;
&lt;p&gt;Check to make sure that the following all match:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;YourModelName&lt;/strong&gt;.xcdatamodeld&lt;/li&gt;
&lt;li&gt;let container = NSPersistentContainer(name: &amp;ldquo;&lt;strong&gt;YourModelName&lt;/strong&gt;&amp;rdquo;)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img src=&#34;match-model-names.png&#34; alt=&#34;Model Names Must Match&#34;&gt;&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>First Impressions of NSPersistentCloudKitContainer</title>
      <link>https://www.andrewcbancroft.com/blog/ios-development/data-persistence/first-impressions-of-nspersistentcloudkitcontainer/</link>
      <pubDate>Thu, 06 Jun 2019 04:40:54 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/blog/ios-development/data-persistence/first-impressions-of-nspersistentcloudkitcontainer/</guid>
      <description>&lt;p&gt;&lt;a name=&#34;resources&#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-link&#34;&gt;&lt;/i&gt; &lt;a href=&#34;https://developer.apple.com/videos/play/wwdc2019/202&#34; rel=&#34;nofollow&#34;&gt;Using Core Data With CloudKit - WWDC 2019 Session 202&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;i class=&#34;fas fa-file-code&#34;&gt;&lt;/i&gt; &lt;a href=&#34;https://developer.apple.com/documentation/coredata/synchronizing_a_local_store_to_the_cloud&#34; rel=&#34;nofollow&#34;&gt;Sample Project from Apple&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;h2 id=&#34;how-nspersistentcloudkitcontainer-helps&#34;&gt;How NSPersistentCloudKitContainer Helps&lt;/h2&gt;
&lt;p&gt;I suppose the first question everyone should ask is, &amp;ldquo;What&amp;rsquo;s the big deal?  How does &lt;code&gt;NSPersistentCloudKitContainer&lt;/code&gt; even help me?&amp;rdquo;&lt;/p&gt;
&lt;p&gt;I noted at least &lt;strong&gt;three huge wins&lt;/strong&gt; from the &lt;a href=&#34;https://developer.apple.com/videos/play/wwdc2019/202&#34; rel=&#34;nofollow&#34;&gt;WWDC 2019 session&lt;/a&gt;.  Ask yourself&amp;hellip;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&amp;ldquo;Do I want to figure out how to retrieve data from CloudKit and merge it with my local Core Data persistent store on my own?&amp;rdquo; &amp;ndash; No?  Me either. 😃&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;code&gt;NSPersistentCloudKitContainer&lt;/code&gt; handles making a local replica of your app&amp;rsquo;s CloudKit data.&lt;/p&gt;
&lt;ol start=&#34;2&#34;&gt;
&lt;li&gt;&amp;ldquo;Do I want to implement synchronization schedluing and all the error handling that comes with CloudKit?&amp;rdquo; &amp;ndash; I &lt;em&gt;definitely&lt;/em&gt; don&amp;rsquo;t&amp;hellip;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;code&gt;NSPersistentCloudKitContainer&lt;/code&gt; handles scheduling CloudKit operations, and takes your &lt;code&gt;// handle errors&lt;/code&gt; placeholder code (👀) and actually implements it the right way internally. #sweet&lt;/p&gt;
&lt;ol start=&#34;3&#34;&gt;
&lt;li&gt;&amp;ldquo;Do I want to map between &lt;code&gt;NSManagedObjects&lt;/code&gt; and &lt;code&gt;CKRecords&lt;/code&gt; by hand?&amp;rdquo; &amp;ndash; Not if that could be automagic!&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;code&gt;NSPersistentCloudKitContainer&lt;/code&gt; handles transforming your &lt;code&gt;NSManagedObjects&lt;/code&gt; into &lt;code&gt;CKRecords&lt;/code&gt; as well.&lt;/p&gt;
&lt;h2 id=&#34;exciting-times&#34;&gt;Exciting Times&lt;/h2&gt;
&lt;p&gt;Apple did a lot of work to &lt;strong&gt;encapsulate a very common set of code patterns&lt;/strong&gt; that were necessary for implementing an app with Core Data and CloudKit.&lt;/p&gt;
&lt;p&gt;They claim to be able to &lt;strong&gt;save us thousands of lines of code&lt;/strong&gt; (and I believe it!).&lt;/p&gt;
&lt;p&gt;I have an app that uses Core Data locally. I&amp;rsquo;ve been wanting to enable CloudKit on, so that data is synced across my users&amp;rsquo; devices.  Every time I think about what it&amp;rsquo;ll take to get the Core Data pieces talking the on the same wavelength as the CloudKit pieces, I just dread the inevitable headache that would ensue.  So I run away.  😬&lt;/p&gt;
&lt;p&gt;My hope is to see what it&amp;rsquo;s like to enable &lt;code&gt;NSPersistentCloudKitContainer&lt;/code&gt; with that existing app.  I&amp;rsquo;ll be sure to document the journey!&lt;/p&gt;
&lt;h1 id=&#34;dipping-a-toe-in&#34;&gt;Dipping a Toe In&lt;/h1&gt;
&lt;p&gt;Just a couple of tidbits about the setup process for getting started with this new class&amp;hellip;&lt;/p&gt;
&lt;h2 id=&#34;enabling-core-data--cloudkit&#34;&gt;Enabling Core Data + CloudKit&lt;/h2&gt;
&lt;p&gt;While it&amp;rsquo;s not &lt;em&gt;absolutely&lt;/em&gt; required to check these checkboxes, Xcode will provide you with some of the boilerplate code necessary for working with Core Data and CloudKit &lt;strong&gt;together&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;enable-coredata-cloudkit.png&#34; alt=&#34;Check Use Core Data + CloudKit&#34;&gt;&lt;/p&gt;
&lt;h2 id=&#34;starting-point&#34;&gt;Starting Point&lt;/h2&gt;
&lt;p&gt;Xcode generates some code in your AppDelegate.swift file to initialize the Core Data Stack, only this time, instead of a standard &lt;code&gt;NSPersistentContainer&lt;/code&gt;, it initializes the new &lt;code&gt;NSPersistentCloudKitContainer&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 hl&#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;lazy&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;persistentContainer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSPersistentCloudKitContainer&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 hl&#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;container&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSPersistentCloudKitContainer&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;s&#34;&gt;&amp;#34;NSPersistentCloudKitContainer_First_Steps&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;n&#34;&gt;container&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;loadPersistentStores&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;completionHandler&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;storeDescription&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;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; 4&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;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;k&#34;&gt;as&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; 5&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;                &lt;span class=&#34;c1&#34;&gt;// Replace this implementation with code to handle the error appropriately.&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;fatalError&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;Unresolved error &lt;/span&gt;&lt;span class=&#34;si&#34;&gt;\(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;error&lt;/span&gt;&lt;span class=&#34;si&#34;&gt;)&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;, &lt;/span&gt;&lt;span class=&#34;si&#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;userInfo&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 class=&#34;p&#34;&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;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 9&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;container&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;h2 id=&#34;not-ready-yet-more-to-enable&#34;&gt;Not Ready Yet (more to enable)&lt;/h2&gt;
&lt;h4 id=&#34;enable-cloudkit-capability&#34;&gt;Enable CloudKit Capability&lt;/h4&gt;
&lt;p&gt;Since you&amp;rsquo;re working with CloudKit in this scenario, you still need to &lt;em&gt;enable&lt;/em&gt; CloudKit so that your app has the appropriate &lt;strong&gt;entitlements&lt;/strong&gt;, and so that your app&amp;rsquo;s &lt;strong&gt;CloudKit container&lt;/strong&gt; gets created in iCloud.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;enable-icloud.gif&#34; alt=&#34;Enable iCloud&#34;&gt;&lt;/p&gt;
&lt;h4 id=&#34;enable-remote-notifications&#34;&gt;Enable Remote Notifications&lt;/h4&gt;
&lt;p&gt;CloudKit also uses Push Notifications to alert your app to data changes coming in from other devices.  To get these notifications, you need to enable the Remote Notifications capability for your app as well.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;enable-remote-notifications.gif&#34; alt=&#34;Enable Remote Notifications&#34;&gt;&lt;/p&gt;
&lt;h2 id=&#34;next-steps&#34;&gt;Next Steps&lt;/h2&gt;
&lt;p&gt;When it comes to learning what to do next, &lt;a href=&#34;https://developer.apple.com/documentation/coredata/synchronizing_a_local_store_to_the_cloud&#34; rel=&#34;nofollow&#34;&gt;Apple has provided a sample project&lt;/a&gt; that I hope to examine and take apart.&lt;/p&gt;
&lt;p&gt;More to come!&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>SwiftUI is Declarative.  What Does That Mean?</title>
      <link>https://www.andrewcbancroft.com/blog/ios-development/ui-work/swiftui-what-does-declarative-mean/</link>
      <pubDate>Tue, 04 Jun 2019 03:11:48 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/blog/ios-development/ui-work/swiftui-what-does-declarative-mean/</guid>
      <description>&lt;p&gt;&amp;ldquo;Declarative&amp;rdquo; is one of Swift UI&amp;rsquo;s four core principles.&lt;/p&gt;
&lt;p&gt;Aside from taking for granted that declarative is just &amp;ldquo;better&amp;rdquo; than &lt;em&gt;whatever else&lt;/em&gt;, how about a visual to understand the declarative way of thinking?&lt;/p&gt;
&lt;p&gt;Imagine yourself in a restaurant (sit-down or fast food, whichever).&lt;/p&gt;
&lt;p&gt;When you want a burger, which best describes what you do?&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;burger-imperative.png&#34; alt=&#34;Burger Imperative&#34;&gt;&lt;/p&gt;
&lt;p&gt;or&amp;hellip;&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;burger-declarative.png&#34; alt=&#34;Burger Declarative&#34;&gt;&lt;/p&gt;
&lt;p&gt;Saying &lt;strong&gt;what&lt;/strong&gt; you want (aka, being &amp;ldquo;declarative&amp;rdquo;) is what the most probable thing you&amp;rsquo;d do in a restaurant.&lt;/p&gt;
&lt;p&gt;That&amp;rsquo;s exactly the recipe you use when you&amp;rsquo;re working with SwiftUI.&lt;/p&gt;
&lt;h2 id=&#34;in-summary&#34;&gt;In Summary&lt;/h2&gt;
&lt;p&gt;&lt;img src=&#34;burger-declarative-animation.gif&#34; alt=&#34;Burger Declarative Animation&#34;&gt;&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Taking First Steps With SwiftUI</title>
      <link>https://www.andrewcbancroft.com/blog/ios-development/ui-work/taking-first-steps-with-swiftui/</link>
      <pubDate>Mon, 03 Jun 2019 03:11:48 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/blog/ios-development/ui-work/taking-first-steps-with-swiftui/</guid>
      <description>&lt;p&gt;&lt;a name=&#34;resources&#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-book&#34;&gt;&lt;/i&gt; &lt;a href=&#34;https://developer.apple.com/tutorials/swiftui/creating-and-combining-views&#34; rel=&#34;nofollow&#34;&gt;Tutorial Reference&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;h2 id=&#34;enabling-swiftui&#34;&gt;Enabling SwiftUI&lt;/h2&gt;
&lt;p&gt;Opting in to using SwiftUI is as easy as creating a new project with Xcode 11 and clicking the &amp;ldquo;Use SwiftUI&amp;rdquo; checkbox.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;enable-swiftui.png&#34; alt=&#34;Check Use SwiftUI&#34;&gt;&lt;/p&gt;
&lt;h2 id=&#34;starting-point&#34;&gt;Starting Point&lt;/h2&gt;
&lt;p&gt;Xcode will give you two structs to start with:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;One that describes the &lt;code&gt;View&lt;/code&gt;&amp;rsquo;s content and layout&lt;/li&gt;
&lt;li&gt;One that declares a preview for that &lt;code&gt;View&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;View&lt;/strong&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;struct&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;ContentView&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;View&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;body&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;some&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;View&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;// This is where you describe the view&amp;#39;s content, layout, and behavior&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;A view&amp;rsquo;s content, layout, and behavior get described in the &lt;code&gt;body&lt;/code&gt; property.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Preview&lt;/strong&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;struct&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;ContentView_Previews&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;PreviewProvider&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;static&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;previews&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;some&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;View&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;ContentView&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;h2 id=&#34;seeing-what-youre-doing&#34;&gt;Seeing What You&amp;rsquo;re Doing&lt;/h2&gt;
&lt;p&gt;Xcode gives you a canvas so that you can visually see what your SwiftUI code is doing.&lt;/p&gt;
&lt;p&gt;Open it by clicking &lt;code&gt;Editor &amp;gt; Editor and Canvas&lt;/code&gt;&lt;/p&gt;
&lt;h2 id=&#34;wait-wheres-the-canvas&#34;&gt;Wait, Where&amp;rsquo;s the Canvas?&lt;/h2&gt;
&lt;p&gt;If you don&amp;rsquo;t have the option to view the canvas in Xcode, it&amp;rsquo;s because this feature requires you to be running macOS Catalina.&lt;/p&gt;
&lt;p&gt;Don&amp;rsquo;t feel left out of the SwiftUI fun, though!  Just press &lt;code&gt;command&lt;/code&gt; + &lt;code&gt;R&lt;/code&gt; to see everything load up in the iOS 13 Simulator.&lt;/p&gt;
&lt;p&gt;If you&amp;rsquo;re learning, you might opt to use &lt;a href=&#34;https://www.andrewcbancroft.com/blog/ios-development/ui-work/using-swiftui-in-playground/&#34;&gt;SwiftUI in a Playground&lt;/a&gt; for a more convenient workflow!&lt;/p&gt;
&lt;h2 id=&#34;syncing-changes-between-swiftui-code-and-xcodes-canvas&#34;&gt;Syncing Changes Between SwiftUI Code and Xcode&amp;rsquo;s Canvas&lt;/h2&gt;
&lt;p&gt;Changing and saving your SwiftUI view code automatically updates the canvas preview.&lt;/p&gt;
&lt;p&gt;Changing the view using Xcode&amp;rsquo;s view inspector automatically updates your SwiftUI code!&lt;/p&gt;
&lt;p&gt;I love this quote:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Your code is always the source of truth for the view. When you use the inspector to change or remove a modifier, Xcode updates your code immediately to match.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2 id=&#34;combining-views--the-key-is-stacks&#34;&gt;Combining Views:  The Key is Stacks&lt;/h2&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;struct&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;ContentView&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;View&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;body&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;some&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;View&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;// This is where you describe the view&amp;#39;s content, layout, and behavior&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;A view&amp;rsquo;s content, layout, and behavior get described in the &lt;code&gt;body&lt;/code&gt; property.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;body&lt;/code&gt; only returns a single view, though.&lt;/p&gt;
&lt;p&gt;To combine multiple views together, you use a &lt;strong&gt;stack&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Stacks can be vertical (&lt;code&gt;VStack&lt;/code&gt;), horizontal (&lt;code&gt;HStack&lt;/code&gt;), or &amp;ldquo;back-to-front&amp;rdquo; (&lt;code&gt;ZStack&lt;/code&gt;).&lt;/p&gt;
&lt;h2 id=&#34;spacing-things-out&#34;&gt;Spacing Things Out&lt;/h2&gt;
&lt;p&gt;Use a &lt;code&gt;Spacer&lt;/code&gt; to make a containing view expand out to use all of its parent view&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;struct&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;ContentView&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;View&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;body&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;some&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;View&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;VStack&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;alignment&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;leading&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;spacing&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; 4&lt;/span&gt;&lt;span class=&#34;cl&#34;&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;Turtle Rock&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;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;font&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&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;n&#34;&gt;HStack&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;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;Joshua Tree National Park&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 class=&#34;n&#34;&gt;font&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;subheadline&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;n&#34;&gt;Spacer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;// Make the horizontal stack use up all the horizontal space of the parent VStack&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;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;California&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;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;font&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;subheadline&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 class=&#34;p&#34;&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;h2 id=&#34;giving-views-some-breathing-room&#34;&gt;Giving Views Some Breathing Room&lt;/h2&gt;
&lt;p&gt;Use the &lt;code&gt;padding()&lt;/code&gt; modifier to give a view some breathing room.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Displaying and Updating Currency in a UILabel</title>
      <link>https://www.andrewcbancroft.com/blog/ios-development/swift-how-tos/displaying-updating-currency-in-uilabel/</link>
      <pubDate>Sat, 01 Jun 2019 04:31:49 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/blog/ios-development/swift-how-tos/displaying-updating-currency-in-uilabel/</guid>
      <description>&lt;p&gt;&lt;a name=&#34;resources&#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;div class=&#34;resources-download-instructions&#34;&gt;
Right-click -&gt; Save as...
&lt;/div&gt;
&lt;ul class=&#34;resources-content&#34;&gt;
&lt;li&gt;
&lt;i class=&#34;fas fa-file&#34;&gt;&lt;/i&gt; &lt;a href=&#34;https://github.com/andrewcbancroft/andrewcbancroft-blog/tree/master/content/blog/ios-development/swift-how-tos/displaying-updating-currency-in-uilabel.playground&#34; download&gt;Swift Playground&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;h2 id=&#34;setup&#34;&gt;Setup&lt;/h2&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;PlaygroundSupport&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;MyViewController&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&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;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;currencyLabel&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; 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;loadView&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;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;view&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#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; 9&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;view&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;backgroundColor&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;white&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;n&#34;&gt;currencyLabel&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;frame&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;CGRect&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;x&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;150&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;y&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;200&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;width&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;200&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;height&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;20&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;currencyLabel&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;$0.00&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;n&#34;&gt;currencyLabel&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;textColor&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;black&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;n&#34;&gt;view&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;addSubview&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;currencyLabel&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;view&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;view&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;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;viewController&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;MyViewController&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;n&#34;&gt;PlaygroundPage&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;current&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;liveView&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;viewController&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;strong&gt;Live View&lt;/strong&gt;
&lt;img src=&#34;starting-label-value.png&#34; alt=&#34;Starting Label Value&#34;&gt;&lt;/p&gt;
&lt;h2 id=&#34;exract-the-current-currency-value-from-the-label&#34;&gt;Exract the current currency value from the label&lt;/h2&gt;
&lt;p&gt;Chances are, you&amp;rsquo;re displaying whatever currency value you&amp;rsquo;re displaying as a &lt;code&gt;String&lt;/code&gt; in your user interface.
Therefore, you need to extract it out of whatever UI element you&amp;rsquo;re displaying it in, and get it ready to do &amp;ldquo;number stuff&amp;rdquo; with&amp;hellip;&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;currentTotalString&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;viewController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;currencyLabel&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;n&#34;&gt;replacingOccurrences&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;of&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;$&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;with&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;&amp;#34;&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;nb&#34;&gt;String&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;CompareOptions&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;literal&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;range&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;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;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;currentTotal&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSDecimalNumber&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;string&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;currentTotalString&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;0&lt;/p&gt;
&lt;h2 id=&#34;make-a-random-currency-amount-to-add-to-the-current-total&#34;&gt;Make a random currency amount to add to the current total&lt;/h2&gt;
&lt;p&gt;For this example, create a random number to between 0 and 5 to test out adding some amount to the current total.&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;randomRange&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;Range&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;Double&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;uncheckedBounds&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;mf&#34;&gt;0.0&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;&lt;span class=&#34;mf&#34;&gt;5.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;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;randomAmount&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;Double&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;random&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&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;n&#34;&gt;randomRange&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;amountToAdd&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSDecimalNumber&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;floatLiteral&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;randomAmount&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;2.095023084295587 &lt;code&gt;// Output from my Playground run&lt;/code&gt;&lt;/p&gt;
&lt;h2 id=&#34;add-to-the-current-total&#34;&gt;Add to the current total&lt;/h2&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;currentTotal&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;currentTotal&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;adding&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;amountToAdd&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;2.095023084295587 &lt;code&gt;// Output from my Playground run&lt;/code&gt;&lt;/p&gt;
&lt;h2 id=&#34;make-a-currency-formatter-format-as-currency-and-update-the-ui&#34;&gt;Make a currency formatter, format as currency, and update the UI&lt;/h2&gt;
&lt;p&gt;The way to format numbers as currency is with a &lt;a href=&#34;https://developer.apple.com/documentation/foundation/numberformatter&#34;&gt;NumberFormatter&lt;/a&gt;.
I&amp;rsquo;m keeping it simple in this example, but there are various customization points available to you.&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;currencyFormatter&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NumberFormatter&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;currencyFormatter&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;locale&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Locale&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;current&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;currencyFormatter&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;usesGroupingSeparator&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&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;currencyFormatter&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;numberStyle&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NumberFormatter&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;Style&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;currency&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;n&#34;&gt;viewController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;currencyLabel&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;n&#34;&gt;currencyFormatter&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;string&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;from&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;currentTotal&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;strong&gt;Live View&lt;/strong&gt;
&lt;img src=&#34;ending-label-value.png&#34; alt=&#34;Ending Label Value&#34;&gt;&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Load a CSV File With R</title>
      <link>https://www.andrewcbancroft.com/blog/data/data-engineering-r/load-csv-file-with-r-windows/</link>
      <pubDate>Wed, 29 May 2019 00:00:00 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/blog/data/data-engineering-r/load-csv-file-with-r-windows/</guid>
      <description>&lt;h2 id=&#34;prerequisites&#34;&gt;Prerequisites&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;No additional R packages are necessary for this notebook to run&lt;/li&gt;
&lt;li&gt;Cells in this notebook expect the &lt;a href=&#34;https://www.andrewcbancroft.com/datasets/Car%20Sales.csv&#34;&gt;Car Sales.csv&lt;/a&gt; file to be in certain locations; specifics are in the cell itself&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#resources&#34;&gt;Resources&lt;/a&gt; to help you practice are available at the end&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;load-data-from-a-csv-file&#34;&gt;Load Data From a CSV File&lt;/h2&gt;
&lt;h3 id=&#34;file-is-in-the-same-directory-as-your-jupyter-notebook&#34;&gt;File is in the same directory as your Jupyter Notebook&lt;/h3&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-R&#34; data-lang=&#34;R&#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;# Read the CSV file&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;car.sales&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;read.csv&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;Car Sales.csv&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;# Show the first 5 rows&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;nf&#34;&gt;head&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;car.sales&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;m&#34;&gt;5&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;table&gt;
&lt;thead&gt;&lt;tr&gt;&lt;th scope=col&gt;DealershipName&lt;/th&gt;&lt;th scope=col&gt;RedCars&lt;/th&gt;&lt;th scope=col&gt;SilverCars&lt;/th&gt;&lt;th scope=col&gt;BlackCars&lt;/th&gt;&lt;th scope=col&gt;BlueCars&lt;/th&gt;&lt;th scope=col&gt;MonthSold&lt;/th&gt;&lt;th scope=col&gt;YearSold&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;
&lt;tbody&gt;
	&lt;tr&gt;&lt;td&gt;Clyde&#39;s Clunkers&lt;/td&gt;&lt;td&gt;902             &lt;/td&gt;&lt;td&gt;650             &lt;/td&gt;&lt;td&gt;754             &lt;/td&gt;&lt;td&gt;792             &lt;/td&gt;&lt;td&gt;1               &lt;/td&gt;&lt;td&gt;2018            &lt;/td&gt;&lt;/tr&gt;
	&lt;tr&gt;&lt;td&gt;Clyde&#39;s Clunkers&lt;/td&gt;&lt;td&gt;710             &lt;/td&gt;&lt;td&gt;476             &lt;/td&gt;&lt;td&gt;518             &lt;/td&gt;&lt;td&gt;492             &lt;/td&gt;&lt;td&gt;2               &lt;/td&gt;&lt;td&gt;2018            &lt;/td&gt;&lt;/tr&gt;
	&lt;tr&gt;&lt;td&gt;Clyde&#39;s Clunkers&lt;/td&gt;&lt;td&gt;248             &lt;/td&gt;&lt;td&gt;912             &lt;/td&gt;&lt;td&gt;606             &lt;/td&gt;&lt;td&gt;350             &lt;/td&gt;&lt;td&gt;3               &lt;/td&gt;&lt;td&gt;2018            &lt;/td&gt;&lt;/tr&gt;
	&lt;tr&gt;&lt;td&gt;Clyde&#39;s Clunkers&lt;/td&gt;&lt;td&gt;782             &lt;/td&gt;&lt;td&gt;912             &lt;/td&gt;&lt;td&gt;858             &lt;/td&gt;&lt;td&gt;446             &lt;/td&gt;&lt;td&gt;4               &lt;/td&gt;&lt;td&gt;2018            &lt;/td&gt;&lt;/tr&gt;
	&lt;tr&gt;&lt;td&gt;Clyde&#39;s Clunkers&lt;/td&gt;&lt;td&gt;278             &lt;/td&gt;&lt;td&gt;354             &lt;/td&gt;&lt;td&gt;482             &lt;/td&gt;&lt;td&gt;752             &lt;/td&gt;&lt;td&gt;5               &lt;/td&gt;&lt;td&gt;2018            &lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;h3 id=&#34;file-is-in-a-different-directory-than-your-jupyter-notebook&#34;&gt;File is in a different directory than your Jupyter Notebook&lt;/h3&gt;
&lt;p&gt;The example will use your &amp;ldquo;home directory&amp;rdquo; to make this example applicable across operating systems, but you can use any path as long as the file exists there&amp;hellip;&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-R&#34; data-lang=&#34;R&#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;# Make sure to use &amp;#34;\&amp;#34; slashes and not &amp;#34;/&amp;#34; slashes (it&amp;#39;s a Windows thing)&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;# There actually needs to be folders named &amp;#34;Path&amp;#34; and &amp;#34;To&amp;#34; and &amp;#34;CSV&amp;#34; and &amp;#34;File&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;c1&#34;&gt;# in your home directory (the &amp;#34;~&amp;#34; means &amp;#34;home directory&amp;#34;) for this cell to work&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;csv.file.path&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;normalizePath&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;~\\Path\\To\\CSV\\File\\Car Sales.csv&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;winslash&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;\\&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&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;other.path.car.sales&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;read.csv&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;csv.file.path&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;# Show the first 5 rows&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;nf&#34;&gt;head&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;other.path.car.sales&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;m&#34;&gt;5&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;table&gt;
&lt;thead&gt;&lt;tr&gt;&lt;th scope=col&gt;DealershipName&lt;/th&gt;&lt;th scope=col&gt;RedCars&lt;/th&gt;&lt;th scope=col&gt;SilverCars&lt;/th&gt;&lt;th scope=col&gt;BlackCars&lt;/th&gt;&lt;th scope=col&gt;BlueCars&lt;/th&gt;&lt;th scope=col&gt;MonthSold&lt;/th&gt;&lt;th scope=col&gt;YearSold&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;
&lt;tbody&gt;
	&lt;tr&gt;&lt;td&gt;Clyde&#39;s Clunkers&lt;/td&gt;&lt;td&gt;902             &lt;/td&gt;&lt;td&gt;650             &lt;/td&gt;&lt;td&gt;754             &lt;/td&gt;&lt;td&gt;792             &lt;/td&gt;&lt;td&gt;1               &lt;/td&gt;&lt;td&gt;2018            &lt;/td&gt;&lt;/tr&gt;
	&lt;tr&gt;&lt;td&gt;Clyde&#39;s Clunkers&lt;/td&gt;&lt;td&gt;710             &lt;/td&gt;&lt;td&gt;476             &lt;/td&gt;&lt;td&gt;518             &lt;/td&gt;&lt;td&gt;492             &lt;/td&gt;&lt;td&gt;2               &lt;/td&gt;&lt;td&gt;2018            &lt;/td&gt;&lt;/tr&gt;
	&lt;tr&gt;&lt;td&gt;Clyde&#39;s Clunkers&lt;/td&gt;&lt;td&gt;248             &lt;/td&gt;&lt;td&gt;912             &lt;/td&gt;&lt;td&gt;606             &lt;/td&gt;&lt;td&gt;350             &lt;/td&gt;&lt;td&gt;3               &lt;/td&gt;&lt;td&gt;2018            &lt;/td&gt;&lt;/tr&gt;
	&lt;tr&gt;&lt;td&gt;Clyde&#39;s Clunkers&lt;/td&gt;&lt;td&gt;782             &lt;/td&gt;&lt;td&gt;912             &lt;/td&gt;&lt;td&gt;858             &lt;/td&gt;&lt;td&gt;446             &lt;/td&gt;&lt;td&gt;4               &lt;/td&gt;&lt;td&gt;2018            &lt;/td&gt;&lt;/tr&gt;
	&lt;tr&gt;&lt;td&gt;Clyde&#39;s Clunkers&lt;/td&gt;&lt;td&gt;278             &lt;/td&gt;&lt;td&gt;354             &lt;/td&gt;&lt;td&gt;482             &lt;/td&gt;&lt;td&gt;752             &lt;/td&gt;&lt;td&gt;5               &lt;/td&gt;&lt;td&gt;2018            &lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;h3 id=&#34;from-a-url&#34;&gt;From a URL&lt;/h3&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-R&#34; data-lang=&#34;R&#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;# Note the URL Encoding with &amp;#34;%20&amp;#34; for spaces&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;url.to.csv.file&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;/datasets/Car%20Sales.csv&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;c1&#34;&gt;# Read the CSV file&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;url.car.sales&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;read.&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;url_to_csv_file&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;# Show the first 5 rows&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;nf&#34;&gt;head&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;url.car.sales&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;m&#34;&gt;5&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;pre&gt;&lt;code&gt;Error in read.table(file = file, header = header, sep = sep, quote = quote, : object &#39;url_to_csv_file&#39; not found
Traceback:


1. read.csv2(url_to_csv_file)

2. read.table(file = file, header = header, sep = sep, quote = quote, 
 .     dec = dec, fill = fill, comment.char = comment.char, ...)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;a name=&#34;resources&#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-file-csv&#34;&gt;&lt;/i&gt; &lt;a href=&#34;https://www.andrewcbancroft.com/datasets/Car%20Sales.csv&#34;&gt;Car Sales.csv&lt;/a&gt;
    &lt;/li&gt;
    &lt;li&gt;
        &lt;i class=&#34;fas fa-book&#34;&gt;&lt;/i&gt; &lt;a href=&#34;https://raw.githubusercontent.com/andrewcbancroft/datadaylife-blog/master/content/notes/load-csv-file-with-python-pandas.ipynb&#34;&gt;load-csv-file-with-python-pandas.ipynb&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;
</description>
    </item>
    
    <item>
      <title>Showing the Live View in a Swift Playground</title>
      <link>https://www.andrewcbancroft.com/blog/ios-development/xcode/showing-live-view-in-swift-playground/</link>
      <pubDate>Tue, 28 May 2019 05:06:19 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/blog/ios-development/xcode/showing-live-view-in-swift-playground/</guid>
      <description>&lt;h2 id=&#34;wheres-the-live-view&#34;&gt;Where&amp;rsquo;s the Live View?&lt;/h2&gt;
&lt;p&gt;So you&amp;rsquo;ve created a new Single View playground from the template picker&amp;hellip;&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;playground-template-selection.png&#34; alt=&#34;Playground Template Selection&#34;&gt;&lt;/p&gt;
&lt;p&gt;You run the playground right out of the gate to see what you&amp;rsquo;ve got&amp;hellip;&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;run-playground.gif&#34; alt=&#34;Run Playground&#34;&gt;&lt;/p&gt;
&lt;p&gt;But&amp;hellip; Where&amp;rsquo;s the Live View?&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;wheres-the-live-view.png&#34; alt=&#34;Where&amp;rsquo;s the Live View?&#34;&gt;&lt;/p&gt;
&lt;h2 id=&#34;showing-the-live-view&#34;&gt;Showing the Live View&lt;/h2&gt;
&lt;p&gt;To get the live view to show up, you need to &lt;strong&gt;open the Assitant Editor&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;You can do this by clicking &lt;code&gt;View&lt;/code&gt; - &lt;code&gt;Assistant Editor&lt;/code&gt; - &lt;code&gt;Show Assistant Editor&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Or you can press &lt;code&gt;option&lt;/code&gt; + &lt;code&gt;command&lt;/code&gt; + &lt;code&gt;return&lt;/code&gt; to show it.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;show-assistant-editor.png&#34; alt=&#34;Show the Assistant Editor&#34;&gt;&lt;/p&gt;
&lt;p&gt;And we&amp;rsquo;re off to play!&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;live-view-shows.png&#34; alt=&#34;Live View Shows&#34;&gt;&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>What is the Difference Between axis=0 and axis=1 When Working with Pandas Dataframes?</title>
      <link>https://www.andrewcbancroft.com/blog/data/data-engineering-python/difference-between-axis-0-axis-1-python-pandas/</link>
      <pubDate>Wed, 22 May 2019 00:00:00 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/blog/data/data-engineering-python/difference-between-axis-0-axis-1-python-pandas/</guid>
      <description>&lt;p&gt;Sometimes, functions ask you to specify an &lt;code&gt;axis&lt;/code&gt;.  The documentation can often feel vague and/or technical.&lt;/p&gt;
&lt;p&gt;For instance, here&amp;rsquo;s a quote from the &lt;code&gt;apply&lt;/code&gt; function&amp;rsquo;s documentation:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;axis : {0 or ‘index’, 1 or ‘columns’}, default 0
Axis along which the function is applied:
0 or ‘index’: apply function to each column.
1 or ‘columns’: apply function to each row.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Uuuum&amp;hellip; right.&lt;/p&gt;
&lt;p&gt;So what&amp;rsquo;s the difference?  Here&amp;rsquo;s an example&amp;hellip;&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;resources&#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;div class=&#34;resources-download-instructions&#34;&gt;
    Right-click -&gt; Save as...
  &lt;/div&gt;
  &lt;ul class=&#34;resources-content&#34;&gt;
    &lt;li&gt;
        &lt;i class=&#34;fas fa-file-csv&#34;&gt;&lt;/i&gt; &lt;a href=&#34;https://www.andrewcbancroft.com/datasets/Car%20Sales.csv&#34; download&gt;Car Sales.csv&lt;/a&gt;
    &lt;/li&gt;
    &lt;li&gt;
        &lt;i class=&#34;fas fa-book&#34;&gt;&lt;/i&gt; &lt;a href=&#34;https://raw.githubusercontent.com/andrewcbancroft/datadaylife-blog/master/content/notes/data-engineering-python/difference-between-axis-0-axis-1-python-pandas.ipynb&#34; download&gt;difference-between-axis-0-axis-1-python-pandas.ipynb&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;
&lt;h2 id=&#34;load-a-csv-file-to-play-with&#34;&gt;Load a CSV file to play with&lt;/h2&gt;
&lt;h3 id=&#34;prerequisites-if-you-want-to-practice&#34;&gt;Prerequisites (if you want to practice)&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Install the &lt;a href=&#34;https://pandas.pydata.org/&#34;&gt;Pandas&lt;/a&gt; library for your Python environment&lt;/li&gt;
&lt;li&gt;Cells in this notebook expect the &lt;a href=&#34;https://www.andrewcbancroft.com/datasets/Car%20Sales.csv&#34;&gt;Car Sales.csv&lt;/a&gt; file to be in the same directory as your notebook&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#resources&#34;&gt;Resources&lt;/a&gt; to help you practice&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;first-things-first&#34;&gt;First Things First&lt;/h3&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#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;kn&#34;&gt;import&lt;/span&gt; &lt;span class=&#34;nn&#34;&gt;pandas&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;as&lt;/span&gt; &lt;span class=&#34;nn&#34;&gt;pd&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#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;# Read the CSV file&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;# This assumes &amp;#34;Car Sales.csv&amp;#34; is in the same directory as your notebook&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;car_sales_data&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;pd&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;read_csv&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&amp;#34;Car Sales.csv&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&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;# Show the first 5 rows&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;first_five&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;car_sales_data&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;head&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;5&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;display&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;first_five&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;div&gt;
&lt;style scoped&gt;
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }
&lt;pre&gt;&lt;code&gt;.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;/style&gt;&lt;/p&gt;
&lt;table border=&#34;1&#34; class=&#34;dataframe&#34;&gt;
  &lt;thead&gt;
    &lt;tr style=&#34;text-align: right;&#34;&gt;
      &lt;th&gt;&lt;/th&gt;
      &lt;th&gt;DealershipName&lt;/th&gt;
      &lt;th&gt;RedCars&lt;/th&gt;
      &lt;th&gt;SilverCars&lt;/th&gt;
      &lt;th&gt;BlackCars&lt;/th&gt;
      &lt;th&gt;BlueCars&lt;/th&gt;
      &lt;th&gt;MonthSold&lt;/th&gt;
      &lt;th&gt;YearSold&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;th&gt;0&lt;/th&gt;
      &lt;td&gt;Clyde&#39;s Clunkers&lt;/td&gt;
      &lt;td&gt;902.0&lt;/td&gt;
      &lt;td&gt;650.0&lt;/td&gt;
      &lt;td&gt;754.0&lt;/td&gt;
      &lt;td&gt;792.0&lt;/td&gt;
      &lt;td&gt;1.0&lt;/td&gt;
      &lt;td&gt;2018.0&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;th&gt;1&lt;/th&gt;
      &lt;td&gt;Clyde&#39;s Clunkers&lt;/td&gt;
      &lt;td&gt;710.0&lt;/td&gt;
      &lt;td&gt;476.0&lt;/td&gt;
      &lt;td&gt;518.0&lt;/td&gt;
      &lt;td&gt;492.0&lt;/td&gt;
      &lt;td&gt;2.0&lt;/td&gt;
      &lt;td&gt;2018.0&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;th&gt;2&lt;/th&gt;
      &lt;td&gt;Clyde&#39;s Clunkers&lt;/td&gt;
      &lt;td&gt;248.0&lt;/td&gt;
      &lt;td&gt;912.0&lt;/td&gt;
      &lt;td&gt;606.0&lt;/td&gt;
      &lt;td&gt;350.0&lt;/td&gt;
      &lt;td&gt;3.0&lt;/td&gt;
      &lt;td&gt;2018.0&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;th&gt;3&lt;/th&gt;
      &lt;td&gt;Clyde&#39;s Clunkers&lt;/td&gt;
      &lt;td&gt;782.0&lt;/td&gt;
      &lt;td&gt;912.0&lt;/td&gt;
      &lt;td&gt;858.0&lt;/td&gt;
      &lt;td&gt;446.0&lt;/td&gt;
      &lt;td&gt;4.0&lt;/td&gt;
      &lt;td&gt;2018.0&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;th&gt;4&lt;/th&gt;
      &lt;td&gt;Clyde&#39;s Clunkers&lt;/td&gt;
      &lt;td&gt;278.0&lt;/td&gt;
      &lt;td&gt;354.0&lt;/td&gt;
      &lt;td&gt;482.0&lt;/td&gt;
      &lt;td&gt;752.0&lt;/td&gt;
      &lt;td&gt;5.0&lt;/td&gt;
      &lt;td&gt;2018.0&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;p&gt;The car sales data looks like it contains one row that summarizes the &lt;strong&gt;total sales of each color&lt;/strong&gt; of car for a &lt;strong&gt;given dealership&lt;/strong&gt;, for &lt;strong&gt;each month&lt;/strong&gt; of the &lt;strong&gt;year&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;To state the &amp;ldquo;grain&amp;rdquo; of the data frame another way, the data frame contains &lt;strong&gt;one row per dealership, month, year&lt;/strong&gt; combo and reports the total number of cars sold by color.&lt;/p&gt;
&lt;h3 id=&#34;choose-your-scenario&#34;&gt;Choose Your Scenario&lt;/h3&gt;
&lt;p&gt;Suppose that two people come to you and ask separate questions about &lt;strong&gt;average car sales&lt;/strong&gt;.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Lucy asks, &amp;ldquo;Can you calculate the average number of cars sold for each color?&amp;rdquo;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Zack asks, &amp;ldquo;Can you calculate the average number of cars sold (regardless of color) for each dealership in each month &amp;amp; year?&amp;rdquo; (so basically the average of red, silver, black, and blue cars for each row)&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id=&#34;start-with-lucy&#34;&gt;Start with Lucy.&lt;/h4&gt;
&lt;p&gt;Think about what you&amp;rsquo;d do to answer Lucy&amp;rsquo;s question by hand, manually, if you didn&amp;rsquo;t have Pandas to do the work for you. Here&amp;rsquo;s what I&amp;rsquo;d do:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Start with the RedCars column.&lt;/li&gt;
&lt;li&gt;Add up 902, 710, 248, 782, 278, and so on.&lt;/li&gt;
&lt;li&gt;Divide that sum by the total number of values.  Boom.  RedCars average.&lt;/li&gt;
&lt;li&gt;Rinse and reapeat steps 1-3 for SilverCars, BlackCars, and BlueCars.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;strong&gt;This is an axis=0 scenario&lt;/strong&gt; in Pandas.&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-python&#34; data-lang=&#34;python&#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;first_five&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[[&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&amp;#39;RedCars&amp;#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;s1&#34;&gt;&amp;#39;SilverCars&amp;#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;s1&#34;&gt;&amp;#39;BlackCars&amp;#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;s1&#34;&gt;&amp;#39;BlueCars&amp;#39;&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;n&#34;&gt;mean&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;axis&lt;/span&gt;&lt;span class=&#34;o&#34;&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;pre&gt;&lt;code&gt;RedCars       584.0
SilverCars    660.8
BlackCars     643.6
BlueCars      566.4
dtype: float64
&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&#34;what-about-zack&#34;&gt;What about Zack?&lt;/h4&gt;
&lt;p&gt;What would you do to answer his question by hand, without Pandas? How&amp;rsquo;s this&amp;hellip;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Start with the first row of data (Row 0), since his question matches the &amp;ldquo;grain&amp;rdquo; of the data frame&amp;hellip; one row per dealership per month &amp;amp; year.&lt;/li&gt;
&lt;li&gt;Add up the RedCars, SilverCars, BlackCars, and BlueCars values for Row 0 and divide by 4.  So (902 + 650 + 754 + 792)/4&lt;/li&gt;
&lt;li&gt;Rinse and repeat steps 1 &amp;amp; 2 for every row in the data frame.  Boom.  Average cars sold by dealer/month/year.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;strong&gt;This is an axis=1 scenario&lt;/strong&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-python&#34; data-lang=&#34;python&#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;first_five&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[[&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&amp;#39;RedCars&amp;#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;s1&#34;&gt;&amp;#39;SilverCars&amp;#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;s1&#34;&gt;&amp;#39;BlackCars&amp;#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;s1&#34;&gt;&amp;#39;BlueCars&amp;#39;&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;n&#34;&gt;mean&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;axis&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;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;pre&gt;&lt;code&gt;0    774.5
1    549.0
2    529.0
3    749.5
4    466.5
dtype: float64
&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&#34;summarizing-the-findings&#34;&gt;Summarizing the Findings&lt;/h3&gt;
&lt;p&gt;Specifying an &lt;code&gt;axis&lt;/code&gt; to a function in Pandas is helping answer one of the following questions:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Should I (Pandas) &lt;strong&gt;start with a column&lt;/strong&gt; and make this function do its job &lt;em&gt;downward&lt;/em&gt; on all the &amp;ldquo;cells&amp;rdquo; for that column, and then continue doing the same thing &lt;strong&gt;for all the rest of the columns&lt;/strong&gt; in the data frame? (&lt;code&gt;axis=0&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;em&gt;or&lt;/em&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Should I (Pandas) &lt;strong&gt;start with the first row&lt;/strong&gt; of data in the data frame and make this function do its job &lt;em&gt;horizontally&lt;/em&gt; on all of the &amp;ldquo;cells&amp;rdquo; for that row, and then continue doing the same thing &lt;strong&gt;for all the rest of the rows&lt;/strong&gt; in the data frame? (&lt;code&gt;axis=1&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;
</description>
    </item>
    
    <item>
      <title>Load an Excel File With Python and Pandas</title>
      <link>https://www.andrewcbancroft.com/blog/data/data-engineering-python/load-excel-file-with-python-pandas/</link>
      <pubDate>Wed, 22 May 2019 00:00:00 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/blog/data/data-engineering-python/load-excel-file-with-python-pandas/</guid>
      <description>&lt;p&gt;&lt;a name=&#34;resources&#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;div class=&#34;resources-download-instructions&#34;&gt;
    Right-click -&gt; Save as...
  &lt;/div&gt;
  &lt;ul class=&#34;resources-content&#34;&gt;
    &lt;li&gt;
        &lt;i class=&#34;fas fa-file-excel&#34;&gt;&lt;/i&gt; &lt;a href=&#34;https://www.andrewcbancroft.com/datasets/Car%20Sales.xlsx&#34;&gt;Car Sales.xlsx&lt;/a&gt;
    &lt;/li&gt;
    &lt;li&gt;
        &lt;i class=&#34;fas fa-book&#34;&gt;&lt;/i&gt; &lt;a href=&#34;https://raw.githubusercontent.com/andrewcbancroft/datadaylife-blog/master/content/notes/data-engineering-python/load-excel-file-with-python-pandas.ipynb&#34;&gt;load-excel-file-with-python-pandas.ipynb&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;
&lt;h2 id=&#34;prerequisites&#34;&gt;Prerequisites&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Install the &lt;a href=&#34;https://pandas.pydata.org/&#34;&gt;Pandas&lt;/a&gt; library for your Python environment&lt;/li&gt;
&lt;li&gt;Cells in this notebook expect the &lt;a href=&#34;https://www.andrewcbancroft.com/datasets/Car%20Sales.xlsx&#34;&gt;Car Sales.xlsx&lt;/a&gt; file to be in certain locations; specifics are in the cell itself&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#resources&#34;&gt;Resources&lt;/a&gt; to help you practice&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;first-things-first&#34;&gt;First Things First&lt;/h2&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#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;kn&#34;&gt;import&lt;/span&gt; &lt;span class=&#34;nn&#34;&gt;pandas&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;as&lt;/span&gt; &lt;span class=&#34;nn&#34;&gt;pd&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&#34;load-data-from-excel&#34;&gt;Load Data From Excel&lt;/h2&gt;
&lt;h3 id=&#34;file-is-in-the-same-directory-as-your-jupyter-notebook&#34;&gt;File is in the same directory as your Jupyter Notebook&lt;/h3&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#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;# Read the Excel file&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;car_sales_data&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;pd&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;read_excel&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&amp;#34;Car Sales.xlsx&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;# Show the first 5 rows&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;car_sales_data&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;head&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;5&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;div&gt;
&lt;style scoped&gt;
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }
&lt;pre&gt;&lt;code&gt;.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;/style&gt;&lt;/p&gt;
&lt;table border=&#34;1&#34; class=&#34;dataframe&#34;&gt;
  &lt;thead&gt;
    &lt;tr style=&#34;text-align: right;&#34;&gt;
      &lt;th&gt;&lt;/th&gt;
      &lt;th&gt;DealershipName&lt;/th&gt;
      &lt;th&gt;RedCars&lt;/th&gt;
      &lt;th&gt;SilverCars&lt;/th&gt;
      &lt;th&gt;BlackCars&lt;/th&gt;
      &lt;th&gt;BlueCars&lt;/th&gt;
      &lt;th&gt;MonthSold&lt;/th&gt;
      &lt;th&gt;YearSold&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;th&gt;0&lt;/th&gt;
      &lt;td&gt;Clyde&#39;s Clunkers&lt;/td&gt;
      &lt;td&gt;902&lt;/td&gt;
      &lt;td&gt;650&lt;/td&gt;
      &lt;td&gt;754&lt;/td&gt;
      &lt;td&gt;792&lt;/td&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;2018&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;th&gt;1&lt;/th&gt;
      &lt;td&gt;Clyde&#39;s Clunkers&lt;/td&gt;
      &lt;td&gt;710&lt;/td&gt;
      &lt;td&gt;476&lt;/td&gt;
      &lt;td&gt;518&lt;/td&gt;
      &lt;td&gt;492&lt;/td&gt;
      &lt;td&gt;2&lt;/td&gt;
      &lt;td&gt;2018&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;th&gt;2&lt;/th&gt;
      &lt;td&gt;Clyde&#39;s Clunkers&lt;/td&gt;
      &lt;td&gt;248&lt;/td&gt;
      &lt;td&gt;912&lt;/td&gt;
      &lt;td&gt;606&lt;/td&gt;
      &lt;td&gt;350&lt;/td&gt;
      &lt;td&gt;3&lt;/td&gt;
      &lt;td&gt;2018&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;th&gt;3&lt;/th&gt;
      &lt;td&gt;Clyde&#39;s Clunkers&lt;/td&gt;
      &lt;td&gt;782&lt;/td&gt;
      &lt;td&gt;912&lt;/td&gt;
      &lt;td&gt;858&lt;/td&gt;
      &lt;td&gt;446&lt;/td&gt;
      &lt;td&gt;4&lt;/td&gt;
      &lt;td&gt;2018&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;th&gt;4&lt;/th&gt;
      &lt;td&gt;Clyde&#39;s Clunkers&lt;/td&gt;
      &lt;td&gt;278&lt;/td&gt;
      &lt;td&gt;354&lt;/td&gt;
      &lt;td&gt;482&lt;/td&gt;
      &lt;td&gt;752&lt;/td&gt;
      &lt;td&gt;5&lt;/td&gt;
      &lt;td&gt;2018&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;h3 id=&#34;file-is-in-a-different-directory-than-your-jupyter-notebook&#34;&gt;File is in a different directory than your Jupyter Notebook&lt;/h3&gt;
&lt;p&gt;The example will use your &amp;ldquo;home directory&amp;rdquo; to make this example applicable across operating systems, but you can use any path as long as the file exists there&amp;hellip;&lt;/p&gt;
&lt;h5 id=&#34;find-the-home-directory-using-python&#34;&gt;Find the home directory using Python&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-python&#34; data-lang=&#34;python&#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;kn&#34;&gt;from&lt;/span&gt; &lt;span class=&#34;nn&#34;&gt;os.path&lt;/span&gt; &lt;span class=&#34;kn&#34;&gt;import&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;expanduser&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;as&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ospath&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;n&#34;&gt;user_home_directory&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ospath&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&amp;#34;~&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;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#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;# Make sure to use &amp;#34;/&amp;#34; slashes and not &amp;#34;\&amp;#34; slashes&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;# There actually needs to be folders named &amp;#34;Path&amp;#34; and &amp;#34;To&amp;#34; and &amp;#34;Excel&amp;#34; and &amp;#34;File&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;c1&#34;&gt;# in your home directory (the &amp;#34;~&amp;#34; means &amp;#34;home directory&amp;#34;) for this cell to work&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;excel_file_path&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;user_home_directory&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;+&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;/Path/To/Excel/File/Car Sales.xlsx&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&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;other_path_car_sales_data&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;pd&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;read_excel&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;excel_file_path&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;# Show the first 5 rows&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;other_path_car_sales_data&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;head&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;5&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;div&gt;
&lt;style scoped&gt;
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }
&lt;pre&gt;&lt;code&gt;.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;/style&gt;&lt;/p&gt;
&lt;table border=&#34;1&#34; class=&#34;dataframe&#34;&gt;
  &lt;thead&gt;
    &lt;tr style=&#34;text-align: right;&#34;&gt;
      &lt;th&gt;&lt;/th&gt;
      &lt;th&gt;DealershipName&lt;/th&gt;
      &lt;th&gt;RedCars&lt;/th&gt;
      &lt;th&gt;SilverCars&lt;/th&gt;
      &lt;th&gt;BlackCars&lt;/th&gt;
      &lt;th&gt;BlueCars&lt;/th&gt;
      &lt;th&gt;MonthSold&lt;/th&gt;
      &lt;th&gt;YearSold&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;th&gt;0&lt;/th&gt;
      &lt;td&gt;Clyde&#39;s Clunkers&lt;/td&gt;
      &lt;td&gt;902&lt;/td&gt;
      &lt;td&gt;650&lt;/td&gt;
      &lt;td&gt;754&lt;/td&gt;
      &lt;td&gt;792&lt;/td&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;2018&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;th&gt;1&lt;/th&gt;
      &lt;td&gt;Clyde&#39;s Clunkers&lt;/td&gt;
      &lt;td&gt;710&lt;/td&gt;
      &lt;td&gt;476&lt;/td&gt;
      &lt;td&gt;518&lt;/td&gt;
      &lt;td&gt;492&lt;/td&gt;
      &lt;td&gt;2&lt;/td&gt;
      &lt;td&gt;2018&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;th&gt;2&lt;/th&gt;
      &lt;td&gt;Clyde&#39;s Clunkers&lt;/td&gt;
      &lt;td&gt;248&lt;/td&gt;
      &lt;td&gt;912&lt;/td&gt;
      &lt;td&gt;606&lt;/td&gt;
      &lt;td&gt;350&lt;/td&gt;
      &lt;td&gt;3&lt;/td&gt;
      &lt;td&gt;2018&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;th&gt;3&lt;/th&gt;
      &lt;td&gt;Clyde&#39;s Clunkers&lt;/td&gt;
      &lt;td&gt;782&lt;/td&gt;
      &lt;td&gt;912&lt;/td&gt;
      &lt;td&gt;858&lt;/td&gt;
      &lt;td&gt;446&lt;/td&gt;
      &lt;td&gt;4&lt;/td&gt;
      &lt;td&gt;2018&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;th&gt;4&lt;/th&gt;
      &lt;td&gt;Clyde&#39;s Clunkers&lt;/td&gt;
      &lt;td&gt;278&lt;/td&gt;
      &lt;td&gt;354&lt;/td&gt;
      &lt;td&gt;482&lt;/td&gt;
      &lt;td&gt;752&lt;/td&gt;
      &lt;td&gt;5&lt;/td&gt;
      &lt;td&gt;2018&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;h3 id=&#34;from-a-url&#34;&gt;From a URL&lt;/h3&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#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;# Note the URL Encoding with &amp;#34;%20&amp;#34; for spaces&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;url_to_excel_file&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;/datasets/Car%20Sales.xlsx&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;c1&#34;&gt;# Read the Excel file&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;url_car_sales_data&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;pd&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;read_excel&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;url_to_excel_file&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;# Show the first 5 rows&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;url_car_sales_data&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;head&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;5&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;div&gt;
&lt;style scoped&gt;
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }
&lt;pre&gt;&lt;code&gt;.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;/style&gt;&lt;/p&gt;
&lt;table border=&#34;1&#34; class=&#34;dataframe&#34;&gt;
  &lt;thead&gt;
    &lt;tr style=&#34;text-align: right;&#34;&gt;
      &lt;th&gt;&lt;/th&gt;
      &lt;th&gt;DealershipName&lt;/th&gt;
      &lt;th&gt;RedCars&lt;/th&gt;
      &lt;th&gt;SilverCars&lt;/th&gt;
      &lt;th&gt;BlackCars&lt;/th&gt;
      &lt;th&gt;BlueCars&lt;/th&gt;
      &lt;th&gt;MonthSold&lt;/th&gt;
      &lt;th&gt;YearSold&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;th&gt;0&lt;/th&gt;
      &lt;td&gt;Clyde&#39;s Clunkers&lt;/td&gt;
      &lt;td&gt;902&lt;/td&gt;
      &lt;td&gt;650&lt;/td&gt;
      &lt;td&gt;754&lt;/td&gt;
      &lt;td&gt;792&lt;/td&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;2018&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;th&gt;1&lt;/th&gt;
      &lt;td&gt;Clyde&#39;s Clunkers&lt;/td&gt;
      &lt;td&gt;710&lt;/td&gt;
      &lt;td&gt;476&lt;/td&gt;
      &lt;td&gt;518&lt;/td&gt;
      &lt;td&gt;492&lt;/td&gt;
      &lt;td&gt;2&lt;/td&gt;
      &lt;td&gt;2018&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;th&gt;2&lt;/th&gt;
      &lt;td&gt;Clyde&#39;s Clunkers&lt;/td&gt;
      &lt;td&gt;248&lt;/td&gt;
      &lt;td&gt;912&lt;/td&gt;
      &lt;td&gt;606&lt;/td&gt;
      &lt;td&gt;350&lt;/td&gt;
      &lt;td&gt;3&lt;/td&gt;
      &lt;td&gt;2018&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;th&gt;3&lt;/th&gt;
      &lt;td&gt;Clyde&#39;s Clunkers&lt;/td&gt;
      &lt;td&gt;782&lt;/td&gt;
      &lt;td&gt;912&lt;/td&gt;
      &lt;td&gt;858&lt;/td&gt;
      &lt;td&gt;446&lt;/td&gt;
      &lt;td&gt;4&lt;/td&gt;
      &lt;td&gt;2018&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;th&gt;4&lt;/th&gt;
      &lt;td&gt;Clyde&#39;s Clunkers&lt;/td&gt;
      &lt;td&gt;278&lt;/td&gt;
      &lt;td&gt;354&lt;/td&gt;
      &lt;td&gt;482&lt;/td&gt;
      &lt;td&gt;752&lt;/td&gt;
      &lt;td&gt;5&lt;/td&gt;
      &lt;td&gt;2018&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
</description>
    </item>
    
    <item>
      <title>Load a CSV File With Python and Pandas</title>
      <link>https://www.andrewcbancroft.com/blog/data/data-engineering-python/load-csv-file-with-python-pandas/</link>
      <pubDate>Wed, 22 May 2019 00:00:00 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/blog/data/data-engineering-python/load-csv-file-with-python-pandas/</guid>
      <description>&lt;p&gt;&lt;a name=&#34;resources&#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;div class=&#34;resources-download-instructions&#34;&gt;
    Right-click -&gt; Save as...
  &lt;/div&gt;
  &lt;ul class=&#34;resources-content&#34;&gt;
    &lt;li&gt;
        &lt;i class=&#34;fas fa-file-csv&#34;&gt;&lt;/i&gt; &lt;a href=&#34;https://www.andrewcbancroft.com/datasets/Car%20Sales.csv&#34;&gt;Car Sales.csv&lt;/a&gt;
    &lt;/li&gt;
    &lt;li&gt;
        &lt;i class=&#34;fas fa-book&#34;&gt;&lt;/i&gt; &lt;a href=&#34;https://raw.githubusercontent.com/andrewcbancroft/datadaylife-blog/master/content/notes/data-engineering-python/load-csv-file-with-python-pandas.ipynb&#34;&gt;load-csv-file-with-python-pandas.ipynb&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;
&lt;h2 id=&#34;prerequisites&#34;&gt;Prerequisites&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Install the &lt;a href=&#34;https://pandas.pydata.org/&#34;&gt;Pandas&lt;/a&gt; library for your Python environment&lt;/li&gt;
&lt;li&gt;Cells in this notebook expect the &lt;a href=&#34;https://www.andrewcbancroft.com/datasets/Car%20Sales.csv&#34;&gt;Car Sales.csv&lt;/a&gt; file to be in certain locations; specifics are in the cell itself&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#resources&#34;&gt;Resources&lt;/a&gt; to help you practice&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;first-things-first&#34;&gt;First Things First&lt;/h2&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#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;kn&#34;&gt;import&lt;/span&gt; &lt;span class=&#34;nn&#34;&gt;pandas&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;as&lt;/span&gt; &lt;span class=&#34;nn&#34;&gt;pd&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&#34;load-data-from-a-csv-file&#34;&gt;Load Data From a CSV File&lt;/h2&gt;
&lt;h3 id=&#34;file-is-in-the-same-directory-as-your-jupyter-notebook&#34;&gt;File is in the same directory as your Jupyter Notebook&lt;/h3&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#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;# Read the CSV file&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;car_sales_data&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;pd&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;read_csv&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&amp;#34;Car Sales.csv&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;# Show the first 5 rows&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;car_sales_data&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;head&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;5&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;div&gt;
&lt;style scoped&gt;
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }
&lt;pre&gt;&lt;code&gt;.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;/style&gt;&lt;/p&gt;
&lt;table border=&#34;1&#34; class=&#34;dataframe&#34;&gt;
  &lt;thead&gt;
    &lt;tr style=&#34;text-align: right;&#34;&gt;
      &lt;th&gt;&lt;/th&gt;
      &lt;th&gt;DealershipName&lt;/th&gt;
      &lt;th&gt;RedCars&lt;/th&gt;
      &lt;th&gt;SilverCars&lt;/th&gt;
      &lt;th&gt;BlackCars&lt;/th&gt;
      &lt;th&gt;BlueCars&lt;/th&gt;
      &lt;th&gt;MonthSold&lt;/th&gt;
      &lt;th&gt;YearSold&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;th&gt;0&lt;/th&gt;
      &lt;td&gt;Clyde&#39;s Clunkers&lt;/td&gt;
      &lt;td&gt;902.0&lt;/td&gt;
      &lt;td&gt;650.0&lt;/td&gt;
      &lt;td&gt;754.0&lt;/td&gt;
      &lt;td&gt;792.0&lt;/td&gt;
      &lt;td&gt;1.0&lt;/td&gt;
      &lt;td&gt;2018.0&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;th&gt;1&lt;/th&gt;
      &lt;td&gt;Clyde&#39;s Clunkers&lt;/td&gt;
      &lt;td&gt;710.0&lt;/td&gt;
      &lt;td&gt;476.0&lt;/td&gt;
      &lt;td&gt;518.0&lt;/td&gt;
      &lt;td&gt;492.0&lt;/td&gt;
      &lt;td&gt;2.0&lt;/td&gt;
      &lt;td&gt;2018.0&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;th&gt;2&lt;/th&gt;
      &lt;td&gt;Clyde&#39;s Clunkers&lt;/td&gt;
      &lt;td&gt;248.0&lt;/td&gt;
      &lt;td&gt;912.0&lt;/td&gt;
      &lt;td&gt;606.0&lt;/td&gt;
      &lt;td&gt;350.0&lt;/td&gt;
      &lt;td&gt;3.0&lt;/td&gt;
      &lt;td&gt;2018.0&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;th&gt;3&lt;/th&gt;
      &lt;td&gt;Clyde&#39;s Clunkers&lt;/td&gt;
      &lt;td&gt;782.0&lt;/td&gt;
      &lt;td&gt;912.0&lt;/td&gt;
      &lt;td&gt;858.0&lt;/td&gt;
      &lt;td&gt;446.0&lt;/td&gt;
      &lt;td&gt;4.0&lt;/td&gt;
      &lt;td&gt;2018.0&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;th&gt;4&lt;/th&gt;
      &lt;td&gt;Clyde&#39;s Clunkers&lt;/td&gt;
      &lt;td&gt;278.0&lt;/td&gt;
      &lt;td&gt;354.0&lt;/td&gt;
      &lt;td&gt;482.0&lt;/td&gt;
      &lt;td&gt;752.0&lt;/td&gt;
      &lt;td&gt;5.0&lt;/td&gt;
      &lt;td&gt;2018.0&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;h3 id=&#34;file-is-in-a-different-directory-than-your-jupyter-notebook&#34;&gt;File is in a different directory than your Jupyter Notebook&lt;/h3&gt;
&lt;p&gt;The example will use your &amp;ldquo;home directory&amp;rdquo; to make this example applicable across operating systems, but you can use any path as long as the file exists there&amp;hellip;&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-python&#34; data-lang=&#34;python&#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;kn&#34;&gt;from&lt;/span&gt; &lt;span class=&#34;nn&#34;&gt;os.path&lt;/span&gt; &lt;span class=&#34;kn&#34;&gt;import&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;expanduser&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;as&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ospath&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;n&#34;&gt;user_home_directory&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ospath&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&amp;#34;~&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;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#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;# Make sure to use &amp;#34;/&amp;#34; slashes and not &amp;#34;\&amp;#34; slashes&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;# There actually needs to be folders named &amp;#34;Path&amp;#34; and &amp;#34;To&amp;#34; and &amp;#34;CSV&amp;#34; and &amp;#34;File&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;c1&#34;&gt;# in your home directory (the &amp;#34;~&amp;#34; means &amp;#34;home directory&amp;#34;) for this cell to work&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;csv_file_path&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;user_home_directory&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;+&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;/Path/To/CSV/File/Car Sales.csv&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&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;other_path_car_sales_data&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;pd&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;read_csv&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;csv_file_path&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;# Show the first 5 rows&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;other_path_car_sales_data&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;head&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;5&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;div&gt;
&lt;style scoped&gt;
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }
&lt;pre&gt;&lt;code&gt;.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;/style&gt;&lt;/p&gt;
&lt;table border=&#34;1&#34; class=&#34;dataframe&#34;&gt;
  &lt;thead&gt;
    &lt;tr style=&#34;text-align: right;&#34;&gt;
      &lt;th&gt;&lt;/th&gt;
      &lt;th&gt;DealershipName&lt;/th&gt;
      &lt;th&gt;RedCars&lt;/th&gt;
      &lt;th&gt;SilverCars&lt;/th&gt;
      &lt;th&gt;BlackCars&lt;/th&gt;
      &lt;th&gt;BlueCars&lt;/th&gt;
      &lt;th&gt;MonthSold&lt;/th&gt;
      &lt;th&gt;YearSold&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;th&gt;0&lt;/th&gt;
      &lt;td&gt;Clyde&#39;s Clunkers&lt;/td&gt;
      &lt;td&gt;902.0&lt;/td&gt;
      &lt;td&gt;650.0&lt;/td&gt;
      &lt;td&gt;754.0&lt;/td&gt;
      &lt;td&gt;792.0&lt;/td&gt;
      &lt;td&gt;1.0&lt;/td&gt;
      &lt;td&gt;2018.0&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;th&gt;1&lt;/th&gt;
      &lt;td&gt;Clyde&#39;s Clunkers&lt;/td&gt;
      &lt;td&gt;710.0&lt;/td&gt;
      &lt;td&gt;476.0&lt;/td&gt;
      &lt;td&gt;518.0&lt;/td&gt;
      &lt;td&gt;492.0&lt;/td&gt;
      &lt;td&gt;2.0&lt;/td&gt;
      &lt;td&gt;2018.0&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;th&gt;2&lt;/th&gt;
      &lt;td&gt;Clyde&#39;s Clunkers&lt;/td&gt;
      &lt;td&gt;248.0&lt;/td&gt;
      &lt;td&gt;912.0&lt;/td&gt;
      &lt;td&gt;606.0&lt;/td&gt;
      &lt;td&gt;350.0&lt;/td&gt;
      &lt;td&gt;3.0&lt;/td&gt;
      &lt;td&gt;2018.0&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;th&gt;3&lt;/th&gt;
      &lt;td&gt;Clyde&#39;s Clunkers&lt;/td&gt;
      &lt;td&gt;782.0&lt;/td&gt;
      &lt;td&gt;912.0&lt;/td&gt;
      &lt;td&gt;858.0&lt;/td&gt;
      &lt;td&gt;446.0&lt;/td&gt;
      &lt;td&gt;4.0&lt;/td&gt;
      &lt;td&gt;2018.0&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;th&gt;4&lt;/th&gt;
      &lt;td&gt;Clyde&#39;s Clunkers&lt;/td&gt;
      &lt;td&gt;278.0&lt;/td&gt;
      &lt;td&gt;354.0&lt;/td&gt;
      &lt;td&gt;482.0&lt;/td&gt;
      &lt;td&gt;752.0&lt;/td&gt;
      &lt;td&gt;5.0&lt;/td&gt;
      &lt;td&gt;2018.0&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;h3 id=&#34;from-a-url&#34;&gt;From a URL&lt;/h3&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#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;# Note the URL Encoding with &amp;#34;%20&amp;#34; for spaces&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;url_to_csv_file&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;/datasets/Car%20Sales.csv&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;c1&#34;&gt;# Read the CSV file&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;url_car_sales_data&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;pd&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;read_csv&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;url_to_csv_file&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;# Show the first 5 rows&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;url_car_sales_data&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;head&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;5&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;div&gt;
&lt;style scoped&gt;
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }
&lt;pre&gt;&lt;code&gt;.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;/style&gt;&lt;/p&gt;
&lt;table border=&#34;1&#34; class=&#34;dataframe&#34;&gt;
  &lt;thead&gt;
    &lt;tr style=&#34;text-align: right;&#34;&gt;
      &lt;th&gt;&lt;/th&gt;
      &lt;th&gt;DealershipName&lt;/th&gt;
      &lt;th&gt;RedCars&lt;/th&gt;
      &lt;th&gt;SilverCars&lt;/th&gt;
      &lt;th&gt;BlackCars&lt;/th&gt;
      &lt;th&gt;BlueCars&lt;/th&gt;
      &lt;th&gt;MonthSold&lt;/th&gt;
      &lt;th&gt;YearSold&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;th&gt;0&lt;/th&gt;
      &lt;td&gt;Clyde&#39;s Clunkers&lt;/td&gt;
      &lt;td&gt;902.0&lt;/td&gt;
      &lt;td&gt;650.0&lt;/td&gt;
      &lt;td&gt;754.0&lt;/td&gt;
      &lt;td&gt;792.0&lt;/td&gt;
      &lt;td&gt;1.0&lt;/td&gt;
      &lt;td&gt;2018.0&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;th&gt;1&lt;/th&gt;
      &lt;td&gt;Clyde&#39;s Clunkers&lt;/td&gt;
      &lt;td&gt;710.0&lt;/td&gt;
      &lt;td&gt;476.0&lt;/td&gt;
      &lt;td&gt;518.0&lt;/td&gt;
      &lt;td&gt;492.0&lt;/td&gt;
      &lt;td&gt;2.0&lt;/td&gt;
      &lt;td&gt;2018.0&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;th&gt;2&lt;/th&gt;
      &lt;td&gt;Clyde&#39;s Clunkers&lt;/td&gt;
      &lt;td&gt;248.0&lt;/td&gt;
      &lt;td&gt;912.0&lt;/td&gt;
      &lt;td&gt;606.0&lt;/td&gt;
      &lt;td&gt;350.0&lt;/td&gt;
      &lt;td&gt;3.0&lt;/td&gt;
      &lt;td&gt;2018.0&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;th&gt;3&lt;/th&gt;
      &lt;td&gt;Clyde&#39;s Clunkers&lt;/td&gt;
      &lt;td&gt;782.0&lt;/td&gt;
      &lt;td&gt;912.0&lt;/td&gt;
      &lt;td&gt;858.0&lt;/td&gt;
      &lt;td&gt;446.0&lt;/td&gt;
      &lt;td&gt;4.0&lt;/td&gt;
      &lt;td&gt;2018.0&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;th&gt;4&lt;/th&gt;
      &lt;td&gt;Clyde&#39;s Clunkers&lt;/td&gt;
      &lt;td&gt;278.0&lt;/td&gt;
      &lt;td&gt;354.0&lt;/td&gt;
      &lt;td&gt;482.0&lt;/td&gt;
      &lt;td&gt;752.0&lt;/td&gt;
      &lt;td&gt;5.0&lt;/td&gt;
      &lt;td&gt;2018.0&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
</description>
    </item>
    
    <item>
      <title>Cheat Sheet - Jupyter Notebook Keyboard Shortcuts</title>
      <link>https://www.andrewcbancroft.com/blog/data/jupyter-notebooks/cheat-sheet-jupyter-notebook-keyboard-shortcuts/</link>
      <pubDate>Tue, 21 May 2019 00:00:00 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/blog/data/jupyter-notebooks/cheat-sheet-jupyter-notebook-keyboard-shortcuts/</guid>
      <description>&lt;h2 id=&#34;command-mode-blue&#34;&gt;Command Mode (Blue)&lt;/h2&gt;
&lt;p&gt;&lt;img src=&#34;command-mode.png&#34; alt=&#34;Command Mode&#34;&gt;&lt;/p&gt;
&lt;div class=&#34;row&#34;&gt;
&lt;div class=&#34;col-sm-4&#34;&gt;
&lt;h4&gt;Shortcut&lt;/h4&gt;
&lt;/div&gt;
&lt;div class=&#34;col-sm-8&#34;&gt;
&lt;h4&gt;What Happens&lt;/h4&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&#34;mt-3&#34;&gt;
&lt;div class=&#34;row&#34;&gt;
&lt;div class=&#34;col-sm-4&#34;&gt;
&lt;code&gt;h&lt;/code&gt;
&lt;/div&gt;
&lt;div class=&#34;col-sm-8&#34;&gt;
Shows a pop-up window listing out all keyboard shortcuts
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&#34;mt-3&#34;&gt;
&lt;div class=&#34;row&#34;&gt;
&lt;div class=&#34;col-sm-4&#34;&gt;
&lt;code&gt;cmd&lt;/code&gt; + &lt;code&gt;shift&lt;/code&gt; + &lt;code&gt;p&lt;/code&gt; (Mac)&lt;br /&gt;
&lt;code&gt;ctrl&lt;/code&gt; + &lt;code&gt;shift&lt;/code&gt; + &lt;code&gt;p&lt;/code&gt; (Win)
&lt;/div&gt;
&lt;div class=&#34;col-sm-8&#34;&gt;
Shows the Command Palette
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&#34;mt-3&#34;&gt;
&lt;div class=&#34;row&#34;&gt;
&lt;div class=&#34;col-sm-4&#34;&gt;
&lt;code&gt;enter&lt;/code&gt;
&lt;/div&gt;
&lt;div class=&#34;col-sm-8&#34;&gt;
Goes into Edit Mode (green) for the currently selected cell
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&#34;mt-3&#34;&gt;
&lt;div class=&#34;row&#34;&gt;
&lt;div class=&#34;col-sm-4&#34;&gt;
&lt;code&gt;ctrl&lt;/code&gt; + &lt;code&gt;enter&lt;/code&gt;
&lt;/div&gt;
&lt;div class=&#34;col-sm-8&#34;&gt;
Runs the selected cell
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&#34;mt-3&#34;&gt;
&lt;div class=&#34;row&#34;&gt;
&lt;div class=&#34;col-sm-4&#34;&gt;
&lt;code&gt;shift&lt;/code&gt; + &lt;code&gt;enter&lt;/code&gt;
&lt;/div&gt;
&lt;div class=&#34;col-sm-8&#34;&gt;
Runs the selected cell; Selects the cell below
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&#34;mt-3&#34;&gt;
&lt;div class=&#34;row&#34;&gt;
&lt;div class=&#34;col-sm-4&#34;&gt;
&lt;code&gt;option&lt;/code&gt; + &lt;code&gt;shift&lt;/code&gt; + &lt;code&gt;enter&lt;/code&gt; (Mac)
&lt;code&gt;alt&lt;/code&gt; + &lt;code&gt;enter&lt;/code&gt; (Win)
&lt;/div&gt;
&lt;div class=&#34;col-sm-8&#34;&gt;
Runs the selected cell; Adds a new cell below; Enters the new cell in Edit Mode
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&#34;mt-3&#34;&gt;
&lt;div class=&#34;row&#34;&gt;
&lt;div class=&#34;col-sm-4&#34;&gt;
&lt;code&gt;A&lt;/code&gt;
&lt;/div&gt;
&lt;div class=&#34;col-sm-8&#34;&gt;
Inserts a new cell &lt;em&gt;above&lt;/em&gt; the currently-selected cell
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&#34;mt-3&#34;&gt;
&lt;div class=&#34;row&#34;&gt;
&lt;div class=&#34;col-sm-4&#34;&gt;
&lt;code&gt;B&lt;/code&gt;
&lt;/div&gt;
&lt;div class=&#34;col-sm-8&#34;&gt;
Inserts a new cell &lt;em&gt;below&lt;/em&gt; the currently-selected cell
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&#34;mt-3&#34;&gt;
&lt;div class=&#34;row&#34;&gt;
&lt;div class=&#34;col-sm-4&#34;&gt;
&lt;code&gt;C&lt;/code&gt;
&lt;/div&gt;
&lt;div class=&#34;col-sm-8&#34;&gt;
Copies the currently-selected cell(s)
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&#34;mt-3&#34;&gt;
&lt;div class=&#34;row&#34;&gt;
&lt;div class=&#34;col-sm-4&#34;&gt;
&lt;code&gt;V&lt;/code&gt;
&lt;/div&gt;
&lt;div class=&#34;col-sm-8&#34;&gt;
Pastes a cell(s) that have been &lt;code&gt;C&lt;/code&gt; Copied as new cell(s) &lt;em&gt;below&lt;/em&gt; the currently-selected cell
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&#34;mt-3&#34;&gt;
&lt;div class=&#34;row&#34;&gt;
&lt;div class=&#34;col-sm-4&#34;&gt;
&lt;code&gt;shift&lt;/code&gt;&lt;code&gt;V&lt;/code&gt;
&lt;/div&gt;
&lt;div class=&#34;col-sm-8&#34;&gt;
Pastes a cell(s) that have been &lt;code&gt;C&lt;/code&gt; Copied as new cell(s) &lt;em&gt;above&lt;/em&gt; the currently-selected cell
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&#34;mt-3&#34;&gt;
&lt;div class=&#34;row&#34;&gt;
&lt;div class=&#34;col-sm-4&#34;&gt;
&lt;code&gt;D&lt;/code&gt; &lt;code&gt;D&lt;/code&gt; (the &#39;D&#39; key twice)
&lt;/div&gt;
&lt;div class=&#34;col-sm-8&#34;&gt;
Deletes the currently selected cell(s)
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&#34;mt-3&#34;&gt;
&lt;div class=&#34;row&#34;&gt;
&lt;div class=&#34;col-sm-4&#34;&gt;
&lt;code&gt;Shift&lt;/code&gt; + Click Cell
&lt;/div&gt;
&lt;div class=&#34;col-sm-8&#34;&gt;
Highlights the range of cells between the one that was selected, and the one that you clicked...just like &lt;code&gt;Shift&lt;/code&gt;-Clicking a series of files
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&#34;mt-3&#34;&gt;
&lt;div class=&#34;row&#34;&gt;
&lt;div class=&#34;col-sm-4&#34;&gt;
&lt;code&gt;Shift&lt;/code&gt; + &lt;code&gt;M&lt;/code&gt;
&lt;/div&gt;
&lt;div class=&#34;col-sm-8&#34;&gt;
Merges any highlighted cells that had been &lt;code&gt;Shift&lt;/code&gt;-Clicked;&lt;br /&gt;If only one cel is selected, it merges the selected cell with the cell directly below
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&#34;mt-3&#34;&gt;
&lt;div class=&#34;row&#34;&gt;
&lt;div class=&#34;col-sm-4&#34;&gt;
&lt;code&gt;M&lt;/code&gt;
&lt;/div&gt;
&lt;div class=&#34;col-sm-8&#34;&gt;
Changes the cell&#39;s type to &lt;code&gt;Markdown&lt;/code&gt;; If the cell is run, it will be executed as Markdown and the output will be formatted and shown; Helpful for providing documentation in the notebook
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&#34;mt-3&#34;&gt;
&lt;div class=&#34;row&#34;&gt;
&lt;div class=&#34;col-sm-4&#34;&gt;
&lt;code&gt;Y&lt;/code&gt;
&lt;/div&gt;
&lt;div class=&#34;col-sm-8&#34;&gt;
Changes the cell&#39;s type to &lt;code&gt;Code&lt;/code&gt;; If the cell is run, it will be executed as code and the output displayed below the cell
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;h2 id=&#34;edit-mode-green&#34;&gt;Edit Mode (Green)&lt;/h2&gt;
&lt;p&gt;&lt;img src=&#34;edit-mode.png&#34; alt=&#34;Edit Mode&#34;&gt;&lt;/p&gt;
&lt;div class=&#34;row&#34;&gt;
&lt;div class=&#34;col-sm-4&#34;&gt;
&lt;h4&gt;Shortcut&lt;/h4&gt;
&lt;/div&gt;
&lt;div class=&#34;col-sm-8&#34;&gt;
&lt;h4&gt;What Happens&lt;/h4&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&#34;mt-3&#34;&gt;
&lt;div class=&#34;row&#34;&gt;
&lt;div class=&#34;col-sm-4&#34;&gt;
&lt;code&gt;esc&lt;/code&gt;
&lt;/div&gt;
&lt;div class=&#34;col-sm-8&#34;&gt;
Goes back to Command Mode (Blue)
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&#34;mt-3&#34;&gt;
&lt;div class=&#34;row&#34;&gt;
&lt;div class=&#34;col-sm-4&#34;&gt;
&lt;code&gt;cmd&lt;/code&gt; + &lt;code&gt;shift&lt;/code&gt; + &lt;code&gt;p&lt;/code&gt; (Mac)&lt;br /&gt;
&lt;code&gt;ctrl&lt;/code&gt; + &lt;code&gt;shift&lt;/code&gt; + &lt;code&gt;p&lt;/code&gt; (Win)
&lt;/div&gt;
&lt;div class=&#34;col-sm-8&#34;&gt;
Shows the Command Palette
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&#34;mt-3&#34;&gt;
&lt;div class=&#34;row&#34;&gt;
&lt;div class=&#34;col-sm-4&#34;&gt;
&lt;code&gt;ctrl&lt;/code&gt; + &lt;code&gt;enter&lt;/code&gt;
&lt;/div&gt;
&lt;div class=&#34;col-sm-8&#34;&gt;
Runs the selected cell
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&#34;mt-3&#34;&gt;
&lt;div class=&#34;row&#34;&gt;
&lt;div class=&#34;col-sm-4&#34;&gt;
&lt;code&gt;shift&lt;/code&gt; + &lt;code&gt;enter&lt;/code&gt;
&lt;/div&gt;
&lt;div class=&#34;col-sm-8&#34;&gt;
Runs the selected cell; Selects the cell below
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&#34;mt-3&#34;&gt;
&lt;div class=&#34;row&#34;&gt;
&lt;div class=&#34;col-sm-4&#34;&gt;
&lt;code&gt;option&lt;/code&gt; + &lt;code&gt;shift&lt;/code&gt; + &lt;code&gt;enter&lt;/code&gt; (Mac)
&lt;code&gt;alt&lt;/code&gt; + &lt;code&gt;enter&lt;/code&gt; (Win)
&lt;/div&gt;
&lt;div class=&#34;col-sm-8&#34;&gt;
Runs the selected cell; Adds a new cell below; Enters the new cell in Edit Mode
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&#34;mt-3&#34;&gt;
&lt;div class=&#34;row&#34;&gt;
&lt;div class=&#34;col-sm-4&#34;&gt;
&lt;code&gt;cmd&lt;/code&gt; + &lt;code&gt;click&lt;/code&gt; (Mac)&lt;br /&gt;
&lt;code&gt;ctrl&lt;/code&gt; + &lt;code&gt;click&lt;/code&gt; (Win)
&lt;/div&gt;
&lt;div class=&#34;col-sm-8&#34;&gt;
Creates multiple cursors for multi-cursor editing
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&#34;mt-3&#34;&gt;
&lt;div class=&#34;row&#34;&gt;
&lt;div class=&#34;col-sm-4&#34;&gt;
&lt;code&gt;cmd&lt;/code&gt; + &lt;code&gt;/&lt;/code&gt; (Mac)&lt;br /&gt;
&lt;code&gt;ctrl&lt;/code&gt; + &lt;code&gt;/&lt;/code&gt; (Win)
&lt;/div&gt;
&lt;div class=&#34;col-sm-8&#34;&gt;
Toggles whether or not the line(s) of code are commented out or not
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&#34;mt-3&#34;&gt;
&lt;div class=&#34;row&#34;&gt;
&lt;div class=&#34;col-sm-4&#34;&gt;
&lt;code&gt;tab&lt;/code&gt;
&lt;/div&gt;
&lt;div class=&#34;col-sm-8&#34;&gt;
Engages code completion if you&#39;re typing code; Indents the line of code if you&#39;re at the beginning of a line
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&#34;mt-3&#34;&gt;
&lt;div class=&#34;row&#34;&gt;
&lt;div class=&#34;col-sm-4&#34;&gt;
&lt;code&gt;shift&lt;/code&gt; + &lt;code&gt;tab&lt;/code&gt;
&lt;/div&gt;
&lt;div class=&#34;col-sm-8&#34;&gt;
Displays a tooltip if you&#39;re typing code; helpful for referencing function parameters
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&#34;mt-3&#34;&gt;
&lt;div class=&#34;row&#34;&gt;
&lt;div class=&#34;col-sm-4&#34;&gt;
&lt;code&gt;ctrl&lt;/code&gt; + &lt;code&gt;shift&lt;/code&gt; + &lt;code&gt;-&lt;/code&gt;
&lt;/div&gt;
&lt;div class=&#34;col-sm-8&#34;&gt;
Splits the cell into two cells where your cursor is located
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&#34;mt-3&#34;&gt;
</description>
    </item>
    
    <item>
      <title>Create a SQL Server Credential for Accessing Azure Blob Storage</title>
      <link>https://www.andrewcbancroft.com/blog/data/sql-server/create-sql-server-credential-access-azure-blob-storage/</link>
      <pubDate>Mon, 20 May 2019 00:00:00 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/blog/data/sql-server/create-sql-server-credential-access-azure-blob-storage/</guid>
      <description>&lt;h2 id=&#34;prerequisites&#34;&gt;Prerequisites:&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;SqlServer&lt;/code&gt; PowerShell module must be installed.
&lt;ul&gt;
&lt;li&gt;Run &lt;code&gt;Install-Module -Name SqlServer&lt;/code&gt; if you need to install it&amp;hellip;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h5 id=&#34;create-credentialps1&#34;&gt;create-credential.ps1&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-powershell&#34; data-lang=&#34;powershell&#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;nb&#34;&gt;import-module&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;SqlServer&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;nv&#34;&gt;$storageAccount&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;&amp;lt;StorageAccountName&amp;gt;&amp;#34;&lt;/span&gt;  &lt;span class=&#34;c&#34;&gt;#replace this with your Azure storage account name&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;nv&#34;&gt;$storageKey&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;&amp;lt;StorageAccountKey&amp;gt;&amp;#34;&lt;/span&gt;  &lt;span class=&#34;c&#34;&gt;#replace this with one of your Azure storage account keys&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;nv&#34;&gt;$secureString&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;convertto-securestring&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;$storageKey&lt;/span&gt;  &lt;span class=&#34;n&#34;&gt;-asplaintext&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;-force&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;nv&#34;&gt;$credentialName&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;BlobStorageCredential&amp;#34;&lt;/span&gt; &lt;span class=&#34;c&#34;&gt;#give the credential a name - you&amp;#39;ll reference it again later (in other scripts, for example)&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;nb&#34;&gt;cd &lt;/span&gt;&lt;span class=&#34;n&#34;&gt;SQLServer&lt;/span&gt;&lt;span class=&#34;err&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;\&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;SQL&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;\&lt;/span&gt;&lt;span class=&#34;nv&#34;&gt;$env:COMPUTERNAME&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;nv&#34;&gt;$instances&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;Get-ChildItem&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;nv&#34;&gt;$instances&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;|&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;new-sqlcredential&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;-Name&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;$credentialName&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;-Identity&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;$storageAccount&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;-Secret&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;$secureString&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
</description>
    </item>
    
    <item>
      <title>A Mnemonic for Remembering Type I vs Type II Errors</title>
      <link>https://www.andrewcbancroft.com/blog/data/statistics/mnemonic-for-remembering-type-i-type-ii-errors/</link>
      <pubDate>Tue, 07 May 2019 00:00:00 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/blog/data/statistics/mnemonic-for-remembering-type-i-type-ii-errors/</guid>
      <description>&lt;p&gt;Step away from inferential statistics for a while and the interpretation of Type I and Type II errors in hypothesis testing could slip away.&lt;/p&gt;
&lt;p&gt;How about a mnemonic to help solidify the concepts?&lt;/p&gt;
&lt;p&gt;To frame the mnemonic, consider the following scenario:&lt;/p&gt;
&lt;p&gt;A man has been charged with murder and you&amp;rsquo;re on the jury.  Witnesses have brought forth testimony.  Evidence has been submitted.  You&amp;rsquo;ve &lt;em&gt;just&lt;/em&gt; handed in your verdict.&lt;/p&gt;
&lt;p&gt;Now for the mnemonic:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;If he&amp;rsquo;s going to jail but shouldn&amp;rsquo;t be&lt;br&gt;you&amp;rsquo;ve committed an error&lt;br&gt;of the 1st degree (Type I)&lt;br&gt;&lt;br&gt;on the &lt;strong&gt;other&lt;/strong&gt; hand&lt;br&gt;&lt;br&gt;If you set him free&lt;br&gt;when he&amp;rsquo;sactually guilty&lt;br&gt;your evidence was too few&lt;br&gt;and you&amp;rsquo;ve committed a Type II.&lt;/p&gt;
&lt;/blockquote&gt;
</description>
    </item>
    
    <item>
      <title>iOS Data Persistence: The Big Picture</title>
      <link>https://www.andrewcbancroft.com/2019/04/30/ios-data-persistence-the-big-picture/</link>
      <pubDate>Tue, 30 Apr 2019 10:03:33 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2019/04/30/ios-data-persistence-the-big-picture/</guid>
      <description>&lt;p&gt;We&amp;rsquo;ve all been there. Do a search on the topic of &amp;ldquo;saving data in iOS” and you&amp;rsquo;ll get &lt;em&gt;dozens&lt;/em&gt; of search results mentioning at least six different frameworks and platforms, and a month’s worth of reading.&lt;/p&gt;
&lt;p&gt;Wouldn&amp;rsquo;t it be nice to distill the possibilities down into &lt;em&gt;just what you need&lt;/em&gt; in order to start down a path for persisting data in your app?&lt;/p&gt;
&lt;p&gt;&amp;ldquo;&lt;a href=&#34;http://bit.ly/ios-data-persistence-bp&#34;&gt;iOS Data Persistence: The Big Picture&lt;/a&gt;” will be worth the hour of your time.&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/ios-data-persistence-bp&#34; target=&#34;_blank&#34;&gt;iOS Data Persistence: The Big Picture&lt;/a&gt;&lt;a href=&#34;http://bit.ly/ios-data-persistence-bp&#34; target=&#34;_blank&#34;&gt;&lt;br /&gt; &lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2019/04/ios-data-persistence-big-picture.001-1024x576.png&#34; alt=&#34;iOS Data Persistence: The Big Picture&#34; width=&#34;1024&#34; height=&#34;576&#34; class=&#34;alignnone size-large wp-image-13737&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2019/04/ios-data-persistence-big-picture.001-1024x576.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2019/04/ios-data-persistence-big-picture.001-300x169.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2019/04/ios-data-persistence-big-picture.001-768x432.png 768w, https://www.andrewcbancroft.com/wp-content/uploads/2019/04/ios-data-persistence-big-picture.001.png 1280w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;
&lt;h1 id=&#34;course-outline&#34;&gt;Course Outline&lt;/h1&gt;
&lt;p&gt;During this one hour course, you will learn the &lt;em&gt;reasons&lt;/em&gt; for saving data to help ground you as you begin to traverse the data persistence landscape.&lt;/p&gt;
&lt;p&gt;You will discover how to use technologies like &lt;strong&gt;User Defaults&lt;/strong&gt; and &lt;strong&gt;Property Lists&lt;/strong&gt;, and I will guide you on how to work with Swift’s &lt;strong&gt;Encodable&lt;/strong&gt; and &lt;strong&gt;Decodable&lt;/strong&gt; protocols.&lt;/p&gt;
&lt;p&gt;You will understand what you’re getting into if you choose to work with &lt;strong&gt;SQLite&lt;/strong&gt;, &lt;strong&gt;Core Data&lt;/strong&gt;, or &lt;strong&gt;Realm Database&lt;/strong&gt; for local data persistence.&lt;/p&gt;
&lt;p&gt;You will know how it feels to reach for a remote data storage option like &lt;strong&gt;CloudKit&lt;/strong&gt;, &lt;strong&gt;Realm Platform&lt;/strong&gt;, or &lt;strong&gt;Firebase&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Finally, you will see a clear picture of what it’s like to design a &lt;strong&gt;document-based application&lt;/strong&gt; and integrate your app’s data with the &lt;strong&gt;iOS Files app&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;When you’re finished with this course, you will have the skills and knowledge you need to make an informed decision about putting a reliable data storage plan for your iOS app into action.&lt;/p&gt;
&lt;h2 id=&#34;1--establishing-the-data-persistence-landscape&#34;&gt;1 – Establishing the Data Persistence Landscape&lt;/h2&gt;
&lt;p&gt;During the course, you will have the opportunity to experience a variety of data persistence scenarios and technologies.&lt;/p&gt;
&lt;p&gt;You’ll get general feel for setup requirements and a taste of what it’s like to work with each solution in code.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;To start you off on the right footing, this module will establish the data persistence possibilities and focus in on the &lt;em&gt;reasons&lt;/em&gt; for saving data so that you&amp;rsquo;re persisting with a purpose.&lt;/li&gt;
&lt;li&gt;You will be introduced to a player profile matrix to help you analyze your data persistence options from a number of angles.&lt;/li&gt;
&lt;li&gt;Finally, you will get the feel for working with the Swift &lt;code&gt;Encodable&lt;/code&gt; and &lt;code&gt;Decodable&lt;/code&gt; protocols.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;2--saving-settings-and-configuration&#34;&gt;2 – Saving Settings and Configuration&lt;/h2&gt;
&lt;p&gt;Users are picky, aren’t they? Everybody has preferences though, so it’s no wonder that you as a developer are often tasked with letting your users to customize their experience within your app.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;You will learn how to manage user preferences with the &lt;code&gt;UserDefaults&lt;/code&gt; system that&amp;rsquo;s built in to iOS&lt;/li&gt;
&lt;li&gt;Next, you will explore synchronizing settings with iCloud Key-Value Store&lt;/li&gt;
&lt;li&gt;Finally, you will understand how to ship configuration for your app with Property Lists that are included in your application bundle&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;4--saving-objects-and-object-graphs&#34;&gt;4 – Saving Objects and Object Graphs&lt;/h2&gt;
&lt;p&gt;What if you only wanted to load a subset of the data into memory instead of being locked in to an all-or-nothing situation with &lt;code&gt;UserDefaults&lt;/code&gt; or Property Lists?&lt;/p&gt;
&lt;p&gt;What if you needed to save a more complex data model that includes relationships?&lt;/p&gt;
&lt;p&gt;For that, you’ll need to know about saving and sharing objects and object graphs…&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Six Technologies/Frameworks&lt;/strong&gt; will be featured in this module:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;SQLite&lt;/li&gt;
&lt;li&gt;Core Data&lt;/li&gt;
&lt;li&gt;Realm Database&lt;/li&gt;
&lt;li&gt;CloudKit&lt;/li&gt;
&lt;li&gt;Realm Platform&lt;/li&gt;
&lt;li&gt;Firebase&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The goal is to help you answer &lt;strong&gt;two questions&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;“What am I getting myself into if I pick one persistence technology over another?”&lt;/li&gt;
&lt;li&gt;“How does it feel to work with this persistence technology in code?”&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You will answer these questions by…&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Scanning the arena of persistence options and explore what sets these technologies apart from &lt;code&gt;UserDefaults&lt;/code&gt; and Property Lists.&lt;/li&gt;
&lt;li&gt;Surveying the setup process to see what it takes to get up and running with each technology in the list&lt;/li&gt;
&lt;li&gt;Exploring the data modeling process to wrap your head around what the data structure design process is like for each technology in the list&lt;/li&gt;
&lt;li&gt;Getting the feel for working with data so that you understand what it&amp;rsquo;s like to create objects, update them, delete them, query and filter them, and watch for data changes so that you can update your user interface appropriately&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;5--saving-data-as-documents-and-files&#34;&gt;5 – Saving Data as Documents and Files&lt;/h2&gt;
&lt;p&gt;What if you need to encapsulate chunks of content together and save it all as a packaged up file…as a single unit with a custom file extension?&lt;/p&gt;
&lt;p&gt;For that, you need to know about saving data as documents and files.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;You will explore the use-case for building a &amp;ldquo;document-based application”&lt;/li&gt;
&lt;li&gt;Next you will walk through designing a document as a subclass of &lt;code&gt;UIDocument&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;You will learn to create and load documents from a Document Browser&lt;/li&gt;
&lt;li&gt;Customizing your app&amp;rsquo;s file extension is a key step that you will work through&lt;/li&gt;
&lt;li&gt;You will spend some time understanding how to interact with document data in code as &lt;em&gt;users&lt;/em&gt; interact with the user interface&lt;/li&gt;
&lt;li&gt;Finally, you will answer the question, “How do I get my app’s files to show up in the iOS Files app?”&lt;/li&gt;
&lt;/ul&gt;
&lt;h1 id=&#34;feedback-welcome&#34;&gt;Feedback Welcome!&lt;/h1&gt;
&lt;p&gt;My goal is to help you filter the noise… to help you distill an overwhelming amount of information down into the big picture possibilities so that you’re on a trajectory for being able to make an informed decision about putting a reliable data storage plan for your iOS app into action.&lt;/p&gt;
&lt;p&gt;I welcome feedback on this course, and on other iOS development courses you might be interested in seeing in the Pluralsight library. Happy learning!&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>What is the tilde (~) symbol in R?</title>
      <link>https://www.andrewcbancroft.com/blog/data/r/what-is-tilde-operator-in-r/</link>
      <pubDate>Tue, 30 Apr 2019 00:00:00 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/blog/data/r/what-is-tilde-operator-in-r/</guid>
      <description>&lt;p&gt;What is the tilde (&lt;code&gt;~&lt;/code&gt;) &amp;ldquo;symbol&amp;rdquo; in R?&lt;/p&gt;
&lt;p&gt;If you were to put words to what it &lt;em&gt;is&lt;/em&gt;, how would you explain it or interpret it?&lt;/p&gt;
&lt;p&gt;What does it mean (and therefore, what does it &lt;em&gt;do&lt;/em&gt;)?&lt;/p&gt;
&lt;p&gt;Here, I document a few of the unpackings that I&amp;rsquo;ve run across when it comes to &lt;code&gt;~&lt;/code&gt; in R.&lt;/p&gt;
&lt;h2 id=&#34;from-the-docs--y-on-x&#34;&gt;From the Docs:  &amp;ldquo;y on x&amp;rdquo;&lt;/h2&gt;
&lt;p&gt;In the &lt;a href=&#34;https://cran.r-project.org/doc/manuals/r-release/R-intro.html#Statistical-models-in-R&#34;&gt;R-project.org documentation&lt;/a&gt;, tilde &lt;code&gt;~&lt;/code&gt; starts getting mentioned in chapter 11 when the discussion of &amp;ldquo;statistical models&amp;rdquo; begins.&lt;/p&gt;
&lt;p&gt;So right away, one could glean that the &lt;code&gt;~&lt;/code&gt; is used when trying to express a statistical model.&lt;/p&gt;
&lt;p&gt;When [this variable] changes, [that one] responds.&lt;/p&gt;
&lt;p&gt;When I nudge [this number], [that number] goes up (or down or stays the same).&lt;/p&gt;
&lt;p&gt;Or at least we &lt;em&gt;think&lt;/em&gt; so&amp;hellip; The goal of a statistical model is to estimate an output (y) from one or more inputs (x&amp;rsquo;s).  One thing we&amp;rsquo;d like to do is &amp;ldquo;model&amp;rdquo; the relationship of the variables using the data we&amp;rsquo;ve got, and use that model in the future on &lt;em&gt;new&lt;/em&gt; data.&lt;/p&gt;
&lt;p&gt;The way one &lt;em&gt;expresses&lt;/em&gt; that modeled relationship in R is with &lt;code&gt;~&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;y ~ x&lt;/code&gt; for example might express a &amp;ldquo;linear regression model of y on x&amp;rdquo;, according to the docs.&lt;/p&gt;
&lt;p&gt;Interesting phrase&amp;hellip; &amp;ldquo;y on x&amp;rdquo;.&lt;/p&gt;
&lt;p&gt;In other words, what we&amp;rsquo;re saying is that &amp;ldquo;y responds to changes in x&amp;rdquo;&amp;hellip; &amp;ldquo;values that come out (y&amp;rsquo;s) depend on the various values that come in through x&amp;rsquo;s&amp;rdquo;.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;~&lt;/code&gt; is a &amp;ldquo;definition&amp;rdquo; operator.&lt;/p&gt;
&lt;p&gt;What does it define? A formula for a statistical model.  It encapsulates all the inputs that are required to produce an estimated output of some sort.&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://stackoverflow.com/questions/14976331/use-of-tilde-in-r-programming-language&#34;&gt;https://stackoverflow.com/questions/14976331/use-of-tilde-in-r-programming-language&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://stackoverflow.com/questions/8055508/in-r-formulas-why-do-i-have-to-use-the-i-function-on-power-terms-like-y-i/8055683#8055683&#34;&gt;https://stackoverflow.com/questions/8055508/in-r-formulas-why-do-i-have-to-use-the-i-function-on-power-terms-like-y-i/8055683#8055683&lt;/a&gt;&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Resolving No Events Found When Sampling Data with Azure Stream Analytics</title>
      <link>https://www.andrewcbancroft.com/blog/data/azure/azure-stream-analytics-resolving-sample-no-events-found/</link>
      <pubDate>Fri, 04 Jan 2019 00:00:00 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/blog/data/azure/azure-stream-analytics-resolving-sample-no-events-found/</guid>
      <description>&lt;p&gt;Data is making it to the Event Hub&amp;hellip;at least you&amp;rsquo;re 99% sure it is, because successful requests are being made by your app/device and received by the Hub.&lt;/p&gt;
&lt;p&gt;The trouble is, when you attempt to sample data with Azure Stream Analytics, you come up empty with a message like&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;No events found for &lt;code&gt;HubName&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;or&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Diagnostics: While sampling data, no data was received from &lt;code&gt;#&lt;/code&gt; partitions.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;In my particular case, everything was fine with both the Event Hub and the Stream Analytics Job configurations. My issue was &lt;strong&gt;how my client app was encoding things as JSON&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;I was using Node to simulate a device sending data to the Event Hub.&lt;/p&gt;
&lt;p&gt;I had a standard import of the required library objects, and the creation of an &lt;code&gt;EventHubClient&lt;/code&gt; 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-js&#34; data-lang=&#34;js&#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;const&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;EventHubClient&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;EventPosition&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;nx&#34;&gt;require&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&amp;#39;@azure/event-hubs&amp;#39;&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;const&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;client&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;EventHubClient&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;createFromConnectionString&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;process&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;env&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&amp;#34;EVENTHUB_CONNECTION_STRING&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;],&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;process&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;env&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&amp;#34;EVENTHUB_NAME&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;But my &lt;code&gt;main&lt;/code&gt; function contained a flaw. It was sending valid JSON, but not in the &amp;ldquo;shape&amp;rdquo; that Azure Stream Analytics expected it.  It resulted in a JSON parsing error.&lt;/p&gt;
&lt;p&gt;What was the flaw?  Compare the following snippets inline:&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-js&#34; data-lang=&#34;js&#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;function&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;main&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;kr&#34;&gt;const&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;incorrectDataFormat&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;DeviceID&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;SomeDeviceID&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;LocationID&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;SomeLocationID&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;Measurement&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;SomeMeasurement&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;kr&#34;&gt;const&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;CORRECTDataFormat&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;body&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;DeviceID&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;SomeDeviceID&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;LocationID&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;SomeLocationID&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;Measurement&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;SomeMeasurement&amp;#34;&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&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;nx&#34;&gt;client&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;send&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;incorrectDataFormat&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;);&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;// won&amp;#39;t get picked up by Stream Analytics
&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;nx&#34;&gt;client&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;send&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;CORRECTDataFormat&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;);&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;// WILL get picked up by Stream Analytics
&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;p&gt;What&amp;rsquo;s the difference?&lt;/p&gt;
&lt;p&gt;For receiving events from Azure Stream Analytics, you&amp;rsquo;ve got to send Events to an EventHub where the &lt;strong&gt;body&lt;/strong&gt; is a JSON object/array&amp;hellip;which means you&amp;rsquo;ve got to have a &lt;code&gt;body&lt;/code&gt; property with a &lt;em&gt;value&lt;/em&gt;&amp;hellip;an object, or an array&amp;hellip; that contains the data you want to send.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;const CORRECTDataFormat = { body: { DeviceID: &amp;quot;SomeDeviceID&amp;quot;, LocationID: &amp;quot;SomeLocationID&amp;quot;, Measurement: &amp;quot;SomeMeasurement&amp;quot; } };&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;If you&amp;rsquo;re running into issues with sampling Event Hub data using Stream Analytics, it might be worth the effort to double-check that the structure of your JSON object is in line with the expectation described above. 🙌🏻&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Deciphering U-SQL&#39;s Error Messages</title>
      <link>https://www.andrewcbancroft.com/blog/data/azure/deciphering-usql-error-messages/</link>
      <pubDate>Thu, 03 Jan 2019 00:00:00 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/blog/data/azure/deciphering-usql-error-messages/</guid>
      <description>&lt;p&gt;It&amp;rsquo;s inevitable - you&amp;rsquo;re going to encounter job failures when working with Azure Data Lake Analytics U-SQL jobs.&lt;/p&gt;
&lt;h1 id=&#34;error-sources&#34;&gt;Error Sources&lt;/h1&gt;
&lt;p&gt;There are at least three root causes for a U-SQL job failure:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;A problem with your &lt;strong&gt;script&lt;/strong&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;A problem with the &lt;strong&gt;data&lt;/strong&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;A problem with your extractor&amp;rsquo;s &amp;ldquo;&lt;strong&gt;shematizing&lt;/strong&gt;&amp;rdquo; of your data&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h1 id=&#34;deciphering-error-messages&#34;&gt;Deciphering Error Messages&lt;/h1&gt;
&lt;p&gt;Whenever an error is reported, it can be helpful to slow down and read through the feedback that the Azure Data Lake Analytics service gave you.&lt;/p&gt;
&lt;p&gt;Here&amp;rsquo;s my general approach to deciphering error messages produced by the Azure Data Lake Analytics service:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Read the &amp;ldquo;Message&amp;rdquo; portion of the error to get a general feel for what the service didn&amp;rsquo;t like.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Decide where the root cause was:  The script?  The data?  The extrator&amp;rsquo;s schematizing of your data?&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Read the &amp;ldquo;Detail&amp;rdquo; section and &lt;strong&gt;look for the position of the &lt;code&gt;###&lt;/code&gt; symbols&lt;/strong&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;This is your biggest clue about where the job service encountered a problem that it didn&amp;rsquo;t know how to recover from.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>U-SQL Pickiness - Resolving Common Syntax Errors</title>
      <link>https://www.andrewcbancroft.com/blog/data/azure/u-sql-pickiness-resolving-common-syntax-errors/</link>
      <pubDate>Wed, 02 Jan 2019 00:00:00 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/blog/data/azure/u-sql-pickiness-resolving-common-syntax-errors/</guid>
      <description>&lt;p&gt;Newbies to U-SQL from T-SQL will soon run face-first into the wall that I ran into.&lt;/p&gt;
&lt;p&gt;Where T-SQL isn&amp;rsquo;t picky about stuff like &lt;strong&gt;capitalization&lt;/strong&gt; and &lt;strong&gt;terminating semicolons&lt;/strong&gt;, U-SQL definitely is.&lt;/p&gt;
&lt;p&gt;If you happen to fail at capitalizing one of the U-SQL keywords like &lt;code&gt;SELECT&lt;/code&gt; or &lt;code&gt;FROM&lt;/code&gt;, or if you miss a &lt;code&gt;;&lt;/code&gt; somewhere, you&amp;rsquo;ll get a syntax error:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;syntax error. Expected one of: &amp;lsquo;.&amp;rsquo; ALL ANTISEMIJOIN ANY AS BEGIN BROADCASTLEFT BROADCASTRIGHT CROSS DISTINCT EXCEPT FULL FULLCROSS GROUP HASH HAVING INDEXLOOKUP INNER INTERSECT JOIN LEFT LOOP MERGE ON OPTION ORDER OUTER OUTER UNION PAIR PIVOT PRESORT PRODUCE READONLY REQUIRED RIGHT SAMPLE SEMIJOIN SERIAL TO UNIFORM UNION UNIVERSE UNPIVOT USING WHERE WITH &amp;lsquo;;&amp;rsquo; &amp;lsquo;(&amp;rsquo; &amp;lsquo;)&amp;rsquo; &amp;lsquo;,&amp;rsquo;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;If you fail to capitalize &lt;code&gt;AS&lt;/code&gt;, you&amp;rsquo;ll get this fun one:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;SELECT expression is missing an AS alias, did you mean to capitalize [as]?&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;strong&gt;Takeaway:&lt;/strong&gt;  Make sure your &amp;ldquo;more casual&amp;rdquo; writing of T-SQL doesn&amp;rsquo;t carry over to U-SQL.  If you&amp;rsquo;re getting interesting syntax errors, double-check your script to ensure you&amp;rsquo;ve got correct capitalization and semicolons after each statement.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>CloudKit Fundamentals</title>
      <link>https://www.andrewcbancroft.com/2018/09/24/cloudkit-fundamentals/</link>
      <pubDate>Mon, 24 Sep 2018 17:58:49 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2018/09/24/cloudkit-fundamentals/</guid>
      <description>&lt;p&gt;At the core of developing a seamless and collaborative data sharing experience for Apple’s platforms is a thorough knowledge of CloudKit.&lt;/p&gt;
&lt;p&gt;In this course, you’ll learn to leverage Apple’s flagship framework to achieve your app’s remote data storage and data sharing requirements.&lt;/p&gt;
&lt;p&gt;First, you’ll learn how to configure CloudKit in your Xcode project and how to save, query, update, and delete data that your users store in iCloud.&lt;/p&gt;
&lt;p&gt;Next, you’ll explore how to synchronize data across a user’s devices.&lt;/p&gt;
&lt;p&gt;Finally, you’ll discover how to handle CloudKit errors, test your app on physical devices, and deploy to production.&lt;/p&gt;
&lt;p&gt;When you’re finished with this course, you’ll have a foundational knowledge of CloudKit that will help you provide a fluid, interactive data sharing experience.&lt;/p&gt;
&lt;h1 id=&#34;course-trailer&#34;&gt;Course Trailer&lt;/h1&gt;
&lt;p&gt;You can check out a trailer of the course over at Pluralsight completely free.&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/cloudkit-fundamentals&#34; target=&#34;_blank&#34;&gt;CloudKit Fundamentals&lt;/a&gt;&lt;br /&gt; &lt;a href=&#34;http://bit.ly/cloudkit-fundamentals&#34; target=&#34;_blank&#34;&gt;&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2018/09/cloudkit-fundamentals-title-screen.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2018/09/cloudkit-fundamentals-title-screen-1024x576.png&#34; alt=&#34;CloudKit Fundamentals&#34; width=&#34;1024&#34; height=&#34;576&#34; class=&#34;alignnone size-large wp-image-13708&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2018/09/cloudkit-fundamentals-title-screen-1024x576.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2018/09/cloudkit-fundamentals-title-screen-300x169.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2018/09/cloudkit-fundamentals-title-screen-768x432.png 768w, https://www.andrewcbancroft.com/wp-content/uploads/2018/09/cloudkit-fundamentals-title-screen.png 1560w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt; &lt;/div&gt; 
&lt;h1&gt;
  Course Outline
&lt;/h1&gt;
&lt;h2&gt;
  1 – Setting up CloudKit
&lt;/h2&gt;
&lt;p&gt;
  Module 1 is the introduction to the course with discussion of the motivating factors for using CloudKit, course prerequisites, storyline overview, and a demo of the end product of the course.
&lt;/p&gt;
&lt;p&gt;
  The demo app for this course is a fun one! I put you in the scenario where we team up to build a hazard reporting app for our company’s Safety Department.
&lt;/p&gt;
&lt;p&gt;
  The final product will come complete with the ability to save, query, update, and delete text, images, and location information into iCloud using the CloudKit framework.
&lt;/p&gt;
&lt;h2&gt;
  2 – Modeling and Saving Data
&lt;/h2&gt;
&lt;p&gt;
  Working with CloudKit begins with an understanding of the framework Types that are used to model and save data.
&lt;/p&gt;
&lt;p&gt;
  You&#39;ll learn about &lt;code&gt;CKRecords&lt;/code&gt; and &lt;code&gt;CKAssets&lt;/code&gt;, and save your first bits of data up to iCloud in this module.
&lt;/p&gt;
&lt;h2&gt;
  3 – Querying, Displaying, Updating, and Deleting Data
&lt;/h2&gt;
&lt;p&gt;
  Once data has been saved into an iCloud database, the next logical step is to query it and display it within the app.
&lt;/p&gt;
&lt;p&gt;
  I will teach about the &lt;code&gt;CKQuery&lt;/code&gt; class for querying the CloudKit database for records and/or assets, so that data can be retrieved and displayed in the UI.
&lt;/p&gt;
&lt;p&gt;
  And of course, once data is retrieved and displayed, it&#39;s fair game for updating and deleting.
&lt;/p&gt;
&lt;h2&gt;
  4 – Synchronizing Data Across Devices
&lt;/h2&gt;
&lt;p&gt;
  The whole point of CloudKit is to make sure that data is accessible from more than one device.
&lt;/p&gt;
&lt;p&gt;
  One of the primary features still glaringly missing from our Hazard Reporter app at this point in the course is the fact that it doesn’t automatically update when data changes in the CloudKit database.
&lt;/p&gt;
&lt;p&gt;
  In this module, I&#39;ll teach you about how CloudKit uses subscriptions and push notifications to alert other users’ devices to changes that have occurred in the CloudKit database, so that appropriate action can be taken in code to respond to those changes.
&lt;/p&gt;
&lt;p&gt;
  I&#39;ll also simulate hazard reports coming in from another user by adding data through the CloudKit Dashboard and watching the screen update with the new data in the iOS app.
&lt;/p&gt;
&lt;h2&gt;
  5 – Modeling Relationships Between Record Types
&lt;/h2&gt;
&lt;p&gt;
  Modeling relationships between Record Types is important for many scenarios. In this module I will teach how to work with &lt;code&gt;CKReferences&lt;/code&gt; in order to represent hierarchical relationships or to represent ownership between model objects.
&lt;/p&gt;
&lt;h2&gt;
  6 – Planning for Production
&lt;/h2&gt;
&lt;p&gt;
  Apple engineers repeatedly warn that CloudKit error handling isn’t the difference between a good app and a bad app… it’s the difference between a functional app and a non-functional app. To that end, I cover some of the most common CloudKit errors and how to handle them.
&lt;/p&gt;
&lt;p&gt;
  And alas, once a version of the app is ready enough to put into users’ hands, it’s crucial to test it and then put it into Production. I will discuss creating an Ad-Hoc deployment of the app so that it can be installed on other users’ devices via iTunes. I will also discuss deploying the CloudKit database schema to Production in preparation for deployment to the App Store.
&lt;/p&gt;
&lt;h1&gt;
  Course Resources
&lt;/h1&gt;
&lt;p&gt;
  Demos of technology in action are &lt;em&gt;super&lt;/em&gt; important to me. That being the case, I tried to pack as much demo content into the course as possible. You&#39;ll get to see the full array of dev-time experiences, not just the final &#34;it works perfectly&amp;#8221; outcomes.
&lt;/p&gt;
&lt;p&gt;
  The course also comes complete with working Xcode projects that gradually build you up to the final version of the app, one step at a time through each module.
&lt;/p&gt;
&lt;h1&gt;
  Feedback Welcome!
&lt;/h1&gt;
&lt;p&gt;
  I welcome feedback on this course, and on other iOS development courses you might be interested in seeing in the Pluralsight library. Happy learning!
&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Do Core Data Entities Need Primary Keys?</title>
      <link>https://www.andrewcbancroft.com/2018/08/10/core-data-entities-need-primary-keys/</link>
      <pubDate>Fri, 10 Aug 2018 22:31:47 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2018/08/10/core-data-entities-need-primary-keys/</guid>
      <description>&lt;p&gt;In this video I pop the hood on Core Data&amp;rsquo;s SQLite persistent store to discover the details about whether or not you need to add a property representing a &amp;ldquo;primary key” for your Core Data Entities. Take a look and be sure to subscribe for to stay connected to future screen casts!&lt;/p&gt;
&lt;div style=&#34;position:relative;height:0;padding-bottom:56.25%&#34;&gt;
&lt;/div&gt;
&lt;h1 id=&#34;transcript&#34;&gt;Transcript&lt;/h1&gt;
&lt;p&gt;I had a question asked to me recently to the effect of… “Do I need to create a Primary Key property when I model my Core Data Entities?”&lt;/p&gt;
&lt;p&gt;I’m going to tackle answering this one with an Xcode sample project I built a while back and put out on GitHub…&lt;/p&gt;
&lt;p&gt;If you go to &lt;a href=&#34;https://github.com/andrewcbancroft/Zootastic&#34;&gt;https://github.com/andrewcbancroft/Zootastic&lt;/a&gt;, you’ll be able to clone the repository and follow along with me if you’d like.&lt;/p&gt;
&lt;p&gt;This repo is Zoo-themed … don’t judge -haha My kids love animals so I couldn’t help myself…&lt;/p&gt;
&lt;p&gt;The Core Data model for this project looks like this. I’m going to focus in on just &lt;em&gt;one&lt;/em&gt; of those entities in this video…namely, the Zoo entity.&lt;/p&gt;
&lt;p&gt;With that context in mind, let’s think about primary keys… Do you need them when you’re modeling Entities in a Core Data project?&lt;/p&gt;
&lt;p&gt;Let’s consider what primary keys are &lt;em&gt;for&lt;/em&gt;, first of all. Essentially what you’re after when you specify a Primary Key in a relational database is a guarantee of the uniqueness of a row in a table…&lt;/p&gt;
&lt;p&gt;You want to make sure that a given representation…a given instance…one row of an Entity can be uniquely identified, and that’s typically done by adding an ID column with a sequential, unrepeated integer value called a Primary Key.&lt;/p&gt;
&lt;p&gt;That way even though two rows may have similar information, such as these two zoos with the same name, you can always represent the City Zoo in Denver Colorado with the unique ID of 1, and the City Zoo in Oklahoma City, OK with the ID of 3.&lt;/p&gt;
&lt;p&gt;Suppose our data model has this Zoo entity with two properties: Location and Name…&lt;/p&gt;
&lt;p&gt;The question at hand is… Do you add this ZooID property? Or not?&lt;/p&gt;
&lt;p&gt;By far the most common use-case with apps using Core Data is to use the NSSQLiteStoreType as the underlying persistent store.&lt;/p&gt;
&lt;p&gt;What this means is that Core Data acts as an abstraction layer over the underlying persistent store, which happens to be a SQLite relational database under the hood.&lt;/p&gt;
&lt;p&gt;When you create Entities with Attributes in your core data model, those turn into Tables with Columns in the SQLite database.&lt;/p&gt;
&lt;p&gt;What I want to do right now is show you what happens when your data model is initialized in an app using Core Data with a SQLite persistent store, and what you’re going to discover is that Core Data’s got your back when it comes to Primary Keys.&lt;/p&gt;
&lt;p&gt;Watch this…&lt;/p&gt;
&lt;p&gt;I’m going to edit the scheme of the Zootastic app to show you how to get a little bit of extra debug information related to the SQL queries that are being run under the hood as Core Data interacts with your persistent store.&lt;/p&gt;
&lt;p&gt;Under Run, you can add an argument that’s passed when the app is launched to get this extra debug information. Use the -com.apple.CoreData.SQLDebug switch and pass 1 as the argument.&lt;/p&gt;
&lt;p&gt;Running the app loads a ton of text into the debug console’s output, but what I’m interested in showing you is right up here at the top.&lt;/p&gt;
&lt;p&gt;Do you see this right here? Core Data sends a CREATE TABLE statement to the SQLite database engine, and within the columns it creates is this Z_PK integer column as a… Primary Key.&lt;/p&gt;
&lt;p&gt;But I haven’t listed a PK property in my Entity!&lt;/p&gt;
&lt;p&gt;Seeing the fact that even though I don’t put in a special primary key property, Core Data still creates that column in the SQLite database, I think you can answer the question on your own.&lt;/p&gt;
&lt;p&gt;My answer would be, “No… you don’t need to specify a special ID property to serve as your entity’s Primary Key because Core Data knows this internally and creates one automatically.”&lt;/p&gt;
&lt;p&gt;Now just in case you’re skeptical and don’t believe that the column is actually IN the database, I want to show you explicitly.&lt;/p&gt;
&lt;p&gt;At the top of this debug output, do you see this? it’s a path to the sqlite database file that the app is using. I’m going to copy it and jump over to a terminal session.&lt;/p&gt;
&lt;p&gt;cd to that directory and list its contents.&lt;/p&gt;
&lt;p&gt;Zootastic.sqlite is what I want to peek into, so I’ll run sqlite3 with that file specified.&lt;/p&gt;
&lt;p&gt;If you run .table, sqlite will list out all the tables in this database file.&lt;/p&gt;
&lt;p&gt;Running .schema on ZZOO shows you that yes indeed the primary key column is present in the database table that holds the Zoos for the app.&lt;/p&gt;
&lt;p&gt;Selecting all columns from the ZZOO table shows that it’s populated with integer values as you’d expect.&lt;/p&gt;
&lt;p&gt;Bottom line? You don’t need to explicitly create an ID column to serve as a primary key. Core Data takes care of that for you.&lt;/p&gt;
&lt;p&gt;If you’re learning Core Data, you might enjoy my Core Data Fundamentals with Swift course on Pluralsight. I cover the essentials to get you up and running with Core Data in your apps developed for Apple’s platforms.&lt;/p&gt;
&lt;p&gt;Thank you for watching!&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>The Case for CloudKit</title>
      <link>https://www.andrewcbancroft.com/2018/08/06/case-for-cloudkit/</link>
      <pubDate>Mon, 06 Aug 2018 11:58:32 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2018/08/06/case-for-cloudkit/</guid>
      <description>&lt;p&gt;When it comes to remote backend services for mobile apps, you’ve got options. What&amp;rsquo;s the case for CloudKit? Where does Apple&amp;rsquo;s remote data storage and sharing solution fit into the Apple Developer&amp;rsquo;s toolbox?&lt;/p&gt;
&lt;p&gt;I recently put out a survey on Twitter, just to get a feel for what folks are using.&lt;/p&gt;
&lt;blockquote class=&#34;twitter-tweet&#34;&gt;&lt;p lang=&#34;en&#34; dir=&#34;ltr&#34;&gt;&lt;a href=&#34;https://twitter.com/hashtag/iosdev?src=hash&amp;amp;ref_src=twsrc%5Etfw&#34;&gt;#iosdev&lt;/a&gt; folks:  When your app needs a backend (ie, a remote server / remote database), what do you use?&lt;br&gt;&lt;br&gt;I’m also interested in the “why” - reply with a tweet if you care to share! Thanks! 🙌🏻&lt;/p&gt;&amp;mdash; Andrew Bancroft (@andrewcbancroft) &lt;a href=&#34;https://twitter.com/andrewcbancroft/status/1020265988561022976?ref_src=twsrc%5Etfw&#34;&gt;July 20, 2018&lt;/a&gt;&lt;/blockquote&gt;
&lt;script async src=&#34;https://platform.twitter.com/widgets.js&#34; charset=&#34;utf-8&#34;&gt;&lt;/script&gt;
&lt;p&gt;The clear winer was Firebase, followed by folks implementing their own backend server architecture. Dead last? A tie between Realm and CloudKit.&lt;/p&gt;
&lt;p&gt;”Hmm&amp;hellip;”, I thought, “What’s up? Why no love for CloudKit?&amp;quot;&lt;/p&gt;
&lt;p&gt;Of the options on my own survey, I’ve had experience building apps using three of the four: CloudKit, Firebase, and “rolling my own backend” with an app built for my employer.&lt;/p&gt;
&lt;p&gt;So I asked myself, “When would &lt;em&gt;you&lt;/em&gt; use CloudKit, Andrew?”&lt;/p&gt;
&lt;p&gt;Here’s how I tend break it down. The more of these that apply to you and your app development experience, the stronger the case for using CloudKit (in my opinion):&lt;/p&gt;
&lt;p&gt;&lt;a class=&#34;jump-target&#34; name=&#34;apple-platforms-only&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;apple-platforms-only-cloudkit&#34;&gt;Apple Platforms Only? CloudKit.&lt;/h2&gt;
&lt;p&gt;If you&amp;rsquo;re developing &lt;em&gt;solely&lt;/em&gt; for Apple&amp;rsquo;s platforms, CloudKit is the natural choice.&lt;/p&gt;
&lt;p&gt;One of the other CloudKit alternatives mentioned in the survey might have the upper hand if you&amp;rsquo;re developing for Android as well, but if you&amp;rsquo;re sticking to the Apple ecosystem, it makes sense to use their remote data storage and sharing service (ie, CloudKit).&lt;/p&gt;
&lt;p&gt;&lt;a class=&#34;jump-target&#34; name=&#34;cloudkit-js&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;expand-beyond-apple-platforms-with-cloudkit-js&#34;&gt;Expand Beyond Apple Platforms with CloudKit JS&lt;/h2&gt;
&lt;p&gt;Even if you&amp;rsquo;re &lt;em&gt;not&lt;/em&gt; developing solely for Apple&amp;rsquo;s platforms, CloudKit might still be a viable choice for you!&lt;/p&gt;
&lt;p&gt;Apple designed &lt;a href=&#34;https://developer.apple.com/documentation/cloudkitjs&#34;&gt;CloudKit JS&lt;/a&gt; to create companion web apps to your iOS and macOS apps. You must have an existing CloudKit app and enable web services to use CloudKit JS, but once you&amp;rsquo;ve got an Apple app configured, you can leverage CloudKit in the web version of your app if you have one.&lt;/p&gt;
&lt;p&gt;As I engaged with folks on Twitter about this, I learned that some were even leveraging CloudKit in an Android app using CloudKit JS as a bridge into the Apple ecosystem.&lt;/p&gt;
&lt;p&gt;This opens up the audience for your app tremendously!&lt;/p&gt;
&lt;p&gt;&lt;a class=&#34;jump-target&#34; name=&#34;authentication&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;built-in-authentication-cloudkit&#34;&gt;Built-in Authentication: CloudKit.&lt;/h2&gt;
&lt;p&gt;If you&amp;rsquo;re tired of creating sign up and login screens, look no further than CloudKit.&lt;/p&gt;
&lt;p&gt;This is especially true if you&amp;rsquo;re only worrying with developing for Apple&amp;rsquo;s Platforms.&lt;/p&gt;
&lt;p&gt;Users signed in with an Apple ID are automatically authenticated when your app uses CloudKit.&lt;/p&gt;
&lt;p&gt;Kiss the sign up/login screens goodbye!&lt;/p&gt;
&lt;p&gt;&lt;a class=&#34;jump-target&#34; name=&#34;free-longer&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;cloudkit-is-free-for-longer&#34;&gt;CloudKit is Free for Longer&lt;/h2&gt;
&lt;p&gt;All of the backend as a service providers offer a free tier or a free trial, and while I don&amp;rsquo;t have personal experience with these services&amp;rsquo; payment threshholds, it&amp;rsquo;s my observation that CloudKit&amp;rsquo;s free tier might &amp;ldquo;last longer&amp;rdquo;. What do I mean by that?&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Data of any kind stored in a user&amp;rsquo;s &lt;em&gt;private&lt;/em&gt; database doesn&amp;rsquo;t count against your app&amp;rsquo;s storage alotment… it counts against the &lt;em&gt;user&amp;rsquo;s&lt;/em&gt; iCloud storage plan. This extends the mileage of your &lt;em&gt;app&amp;rsquo;s&lt;/em&gt; storage alotment.&lt;/li&gt;
&lt;li&gt;CloudKit distinguishes between storage for &lt;em&gt;non-binary data&lt;/em&gt; and storage for &lt;em&gt;media&lt;/em&gt; (binary data like images, videos, audio, etc). You &lt;a href=&#34;https://developer.apple.com/icloud/cloudkit/&#34;&gt;start off with&lt;/a&gt; 100 MB of non-binary data storage, but &lt;strong&gt;10 GB&lt;/strong&gt; of image/video/audio/etc. storage. In contrast…
* Firebase &lt;a href=&#34;https://firebase.google.com/pricing/&#34;&gt;starts to charge you&lt;/a&gt; $25/mo after &lt;strong&gt;5 GB&lt;/strong&gt; of media storage
* Realm Platform &lt;a href=&#34;https://realm.io/pricing&#34;&gt;offers a 30 day free trial&lt;/a&gt;, but is $30/month after that, and acts primarily as a remote data store for non-binary data.&lt;/li&gt;
&lt;li&gt;As the number of users for your app grows, your storage and transfer limits scale with your app&amp;rsquo;s user base. I don&amp;rsquo;t have personal experience here, but I wonder… by the time your app grows to 100,000 users (the tipping point for Apple to scale your app&amp;rsquo;s alotments up), how probable is it that you&amp;rsquo;d already be paying to use Firebase?&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;a class=&#34;jump-target&#34; name=&#34;own-backend&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;rolling-your-own-backend-consider-cloudkit&#34;&gt;Rolling Your Own Backend? Consider CloudKit.&lt;/h2&gt;
&lt;p&gt;Folks already invested in Realm or Firebase are already leveraging a backend as a service platform. It&amp;rsquo;s those that are maintaining backend server infrastructure &lt;em&gt;themselves&lt;/em&gt; that I might suggest consider CloudKit.&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;ve been in the shoes of those who have to roll their own backend server and maintain those VMs, databases, web servers… the works.&lt;/p&gt;
&lt;p&gt;Many times this is because there&amp;rsquo;s a need to connect a mobile app to an organization&amp;rsquo;s on-premise/public cloud ecosystem, and for that, I can totally empathize.&lt;/p&gt;
&lt;p&gt;CloudKit offers a reliable alternative that organizations developing for Apple&amp;rsquo;s platforms should consider. It&amp;rsquo;s a huge weight off the shoulders to abstract the backend and let Apple maintain those servers and storage components. Data stored in the &lt;strong&gt;public&lt;/strong&gt; cloud database &lt;em&gt;is&lt;/em&gt; accessible programmatically, so your organization &lt;em&gt;can&lt;/em&gt; get at data stored there for incorporation into its internal ecosystem.&lt;/p&gt;
&lt;p&gt;&lt;a class=&#34;jump-target&#34; name=&#34;first-party-only&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;company-requirement-1st-party-frameworks-and-services-only-cloudkit&#34;&gt;Company Requirement: 1st Party Frameworks and Services Only? CloudKit.&lt;/h2&gt;
&lt;p&gt;This requirement is most likely to be seen within companies and organizations that have an aversion to bringing in a lot of 3rd party dependencies.&lt;/p&gt;
&lt;p&gt;Those of us who have an employer or who work for clients that have a say in what our app development toolkit can consist of may need to consider options within the Apple ecosystem.&lt;/p&gt;
&lt;p&gt;CloudKit is a dependable choice for risk-averse companies.&lt;/p&gt;
&lt;p&gt;Speaking of risk aversion…&lt;/p&gt;
&lt;p&gt;&lt;a class=&#34;jump-target&#34; name=&#34;third-party-risk-aversion&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;personal-3rd-party-risk-aversion8230-cloudkit&#34;&gt;Personal 3rd Party Risk Aversion… CloudKit.&lt;/h2&gt;
&lt;p&gt;This one hits close to home for me, because I&amp;rsquo;ve experienced the pain of having a 3rd party service I depended on for &lt;em&gt;multiple apps&lt;/em&gt; be discontinued. *Cough* &lt;strong&gt;&lt;a href=&#34;https://blog.parseplatform.org/announcements/moving-on/&#34;&gt;Parse&lt;/a&gt;&lt;/strong&gt; *Cough*&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;m usually okay with 3rd party libraries, but 3rd party &lt;em&gt;services&lt;/em&gt; are another story altogether for me.&lt;/p&gt;
&lt;p&gt;When you&amp;rsquo;re using a library that ends up fading away, it may take a little reworking to get your code to compile with an alternative.&lt;/p&gt;
&lt;p&gt;But when you&amp;rsquo;ve got data that your app depends on living on a remote server hosted by a 3rd party, and that service goes away… Oy! Talk about the pain, frustration, and hassle of having to do a &lt;em&gt;migration&lt;/em&gt; &lt;strong&gt;on top of&lt;/strong&gt; rewriting code.&lt;/p&gt;
&lt;p&gt;&lt;a class=&#34;jump-target&#34; name=&#34;only-need-data-store&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;only-need-a-remote-data-store-keep-it-simple-cloudkit&#34;&gt;Only Need a Remote Data Store? Keep it Simple. CloudKit.&lt;/h2&gt;
&lt;p&gt;There is a &lt;em&gt;ton&lt;/em&gt; that can be done on-device these days. If all you&amp;rsquo;re in the market for is a remote data store to facilitate data sharing across devices or with other users and have no need of remote cloud functions or analytics or remote machine learning services, etc., you might opt to keep it simple and leverage CloudKit.&lt;/p&gt;
&lt;p&gt;&lt;a class=&#34;jump-target&#34; name=&#34;apple-uses&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;depend-on-what-apple-depends-on-cloudkit&#34;&gt;Depend On What Apple Depends On: CloudKit.&lt;/h2&gt;
&lt;p&gt;What does Apple depend on for their own apps&amp;rsquo; remote data storage and sharing needs?&lt;/p&gt;
&lt;p&gt;Answer: CloudKit.&lt;/p&gt;
&lt;p&gt;Clearly, Apple is in a unique situation: Apple neither needs nor cares to develop any of its apps for Android. Naturally, they&amp;rsquo;d depend on the very framework they created. Otherwise, we could argue, &amp;ldquo;What a bunch of hypocrites!&amp;rdquo;, right?&lt;/p&gt;
&lt;p&gt;CloudKit serves the needs of apps like Photos, iCloud Drive, Notes, News, and more.&lt;/p&gt;
&lt;p&gt;It&amp;rsquo;s almost as if Apple developed CloudKit for Apple, and decided, &amp;ldquo;Hey, let&amp;rsquo;s open this up to our developers and let them tap in to what works awesome for us!&amp;rdquo;&lt;/p&gt;
&lt;p&gt;I like that. I like the security of knowing that Apple depends on the framework they&amp;rsquo;re delivering to us.&lt;/p&gt;
&lt;p&gt;&lt;a class=&#34;jump-target&#34; name=&#34;tradeoffs&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;tradeoffs&#34;&gt;Tradeoffs&lt;/h2&gt;
&lt;p&gt;In fairness, I would point out a few trade-offs if you were to go with CloudKit.&lt;/p&gt;
&lt;p&gt;&lt;a class=&#34;jump-target&#34; name=&#34;no-android-support&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;no-native-android-support&#34;&gt;No Native Android support&lt;/h3&gt;
&lt;p&gt;This tradeoff is qualified with the words &amp;ldquo;no &lt;em&gt;native&lt;/em&gt; Android support&amp;rdquo; for a reason. CloudKit JS may actually open the door for folks developing cross platform apps, though it may take some hacking.&lt;/p&gt;
&lt;p&gt;Expect some challenges on the Android side of things, but don&amp;rsquo;t completely count CloudKit out.&lt;/p&gt;
&lt;p&gt;&lt;a class=&#34;jump-target&#34; name=&#34;data-storage-only&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;data-storage-and-sharing-only-with-cloudkit&#34;&gt;Data storage and sharing &lt;em&gt;only&lt;/em&gt; with CloudKit&lt;/h3&gt;
&lt;p&gt;With CloudKit, you get a fully-functional remote data storage and sharing service. If you&amp;rsquo;re wanting to leverage some advanced functionality like &amp;ldquo;cloud functions&amp;rdquo;, analytics for your app, non-Apple ID authentication, remote machine learning services, and the like, you won&amp;rsquo;t find those features in CloudKit.&lt;/p&gt;
&lt;p&gt;This doesn&amp;rsquo;t mean that Apple leaves you nanging when it comes to authentication, analytics, machine learning, etc.&lt;/p&gt;
&lt;p&gt;Don&amp;rsquo;t forget that you&amp;rsquo;ll have built-in Apple ID authentication as I mentioned above, and analytics are provided through iTunes Connect.&lt;/p&gt;
&lt;p&gt;On-device machine learning with CoreML might also alleviate the need to seek remote ML services.&lt;/p&gt;
&lt;p&gt;If, after considering Apple&amp;rsquo;s component solutions, you&amp;rsquo;re looking for alternatives, Firebase offers all of the above as sort of an all-in-one package.&lt;/p&gt;
&lt;p&gt;As I mentioned above though… if all you really &lt;em&gt;need&lt;/em&gt; is a remote data store for cross-device data syncing, don&amp;rsquo;t over-complicate things. CloudKit can still serve you well.&lt;/p&gt;
&lt;p&gt;Another alternative is to use a public cloud provider like Azure or AWS for &amp;ldquo;cloud functions&amp;rdquo; or remote machine learning. If you prefer to component-ize these kinds of services, this is a viable alternative to Firebase, or Apple&amp;rsquo;s own solutions to these kinds of problems.&lt;/p&gt;
&lt;p&gt;&lt;a class=&#34;jump-target&#34; name=&#34;feedback&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;what-do-you-use-why&#34;&gt;What do you use? Why?&lt;/h2&gt;
&lt;p&gt;I&amp;rsquo;m a pragmatist. I don&amp;rsquo;t always use CloudKit. But I do like CloudKit when I can use it.&lt;/p&gt;
&lt;p&gt;What do &lt;em&gt;you&lt;/em&gt; use? Why? What&amp;rsquo;s your experience with backend as a service providers? What else would you consider in the case for CloudKit? What other tradeoffs do you recognize?&lt;/p&gt;
&lt;p&gt;&lt;a class=&#34;jump-target&#34; name=&#34;share&#34;&gt;&lt;/a&gt;&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Create an HDInsight Cluster in Azure with PowerShell</title>
      <link>https://www.andrewcbancroft.com/blog/data/azure/create-hdinsight-cluster-azure-powershell/</link>
      <pubDate>Fri, 27 Jul 2018 00:00:00 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/blog/data/azure/create-hdinsight-cluster-azure-powershell/</guid>
      <description>&lt;p&gt;Being able to programmatically create an HDInsight cluster, use it, and subsequently tear it down so that you don&amp;rsquo;t incur cost is one use-case for maximizing efficient use of Azure&amp;rsquo;s cloud platform.&lt;/p&gt;
&lt;p&gt;Here, I want to break the use-case down into chunks:&lt;/p&gt;
&lt;h2 id=&#34;log-in-to-azure-powershell&#34;&gt;Log in to Azure (PowerShell)&lt;/h2&gt;
&lt;p&gt;The first step is to log in to your Azure account:&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-powershell&#34; data-lang=&#34;powershell&#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;nb&#34;&gt;Login-AzureRmAccount&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&#34;create-a-resource-group&#34;&gt;Create a Resource Group&lt;/h2&gt;
&lt;p&gt;Resource Groups in Azure make it extremely convenient to package all of the pieces of your temporary, on-the-fly-use, HDInsight Hadoop cluster.&lt;/p&gt;
&lt;p&gt;The idea is to create the storage you need, along with the Hadoop cluster infrastructure you need, and stick them together inside of a resource group. Why? So that you can tear it all down once you&amp;rsquo;re done in a single step by removing the resource group itself. Removing a resource group is how you can delete both the storage and the HDInsight Hadoop cluster all at once.&lt;/p&gt;
&lt;p&gt;If you happened to want to keep the storage around, no worries.  HDInsight is the piece that can get expensive while it&amp;rsquo;s online.  You can always resort to removing the HDInsight cluster &lt;em&gt;only&lt;/em&gt;, if you&amp;rsquo;re wanting to keep the storage, but save on costs while the Hadoop cluster isn&amp;rsquo;t being utilized.&lt;/p&gt;
&lt;p&gt;Om either case, the question remains:  What do you need to know in order to create a resouce group?&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;name&lt;/strong&gt; of the resource group (you get to make this up)&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;location&lt;/strong&gt; of the resource group (this is one of the Azure regions)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Once you&amp;rsquo;ve got these pieces, you can start a PowerShell script:&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-powershell&#34; data-lang=&#34;powershell&#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;c&#34;&gt;###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; 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;c&#34;&gt;# Log in to Azure&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;c&#34;&gt;##New Stuff##&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;nv&#34;&gt;$resourceGroupName&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;nameOfResourceGroup&amp;#34;&lt;/span&gt; &lt;span class=&#34;c&#34;&gt;# Choose a name that represents what the group contains&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;nv&#34;&gt;$location&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;South Central US&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&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;c&#34;&gt;# If you&amp;#39;re not sure what the valid location names are, uncomment and run the line below.&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;c&#34;&gt;# Get-AzureRmLocation | Format-Table # Use the DisplayName value in $location above.&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;nb&#34;&gt;New-AzureRmResourceGroup&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;-Name&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;$resourceGroupName&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;-Location&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;$location&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&#34;create-storage&#34;&gt;Create Storage&lt;/h2&gt;
&lt;p&gt;Hadoop clusters need a storage area that they can use to store files and do processing.  For temporary clusters use on-the-fly, I like to create a new storage account in the newly-created resource group.  Alternatively, you can use an existing storage account.&lt;/p&gt;
&lt;p&gt;What do you need in order to create and/or use storage in PowerShell?&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A &lt;strong&gt;storage account&lt;/strong&gt;.  You can use an existing one, or create a new one just for this temporary-use cluster.  What do you need in order to create a new storage account?
&lt;ul&gt;
&lt;li&gt;A &lt;strong&gt;name&lt;/strong&gt; for the storage account&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;resource group&lt;/strong&gt; where it&amp;rsquo;ll &amp;ldquo;live&amp;rdquo;&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;type&lt;/strong&gt; (Premium_LRS, Standard_GRS, Standard_LRS, Standard_RAGRS, or Standard_ZRS)&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;location&lt;/strong&gt;.  You can reuse the &lt;code&gt;$location&lt;/code&gt; variable you used to create a new resource group&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;storage account key&lt;/strong&gt; to allow you to securely write to the storage area programmatically&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;storage context&lt;/strong&gt;, which is basically a formal PowerShell &amp;ldquo;object&amp;rdquo; that made up of the storage account name and the storage account key&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;container&lt;/strong&gt; within that storage context that should be used to store and process files in (you can make up a new &lt;strong&gt;container name&lt;/strong&gt; and create one, or use an existing container in an existing storage account&amp;hellip;)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Continuing with the PowerShell script now:&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-powershell&#34; data-lang=&#34;powershell&#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;c&#34;&gt;###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; 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;c&#34;&gt;# Log in to Azure&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;c&#34;&gt;# Create Resource Group&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;c&#34;&gt;###New Stuff###&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;nv&#34;&gt;$storageAccountName&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;&lt;/span&gt;&lt;span class=&#34;nv&#34;&gt;$resourceGroupName&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&amp;#34;&lt;/span&gt; &lt;span class=&#34;c&#34;&gt;# You can choose any name you want - I name mine the same as my resource group for convenience&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;nv&#34;&gt;$containerName&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;hdp&lt;/span&gt;&lt;span class=&#34;nv&#34;&gt;$resourceGroupName&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&amp;#34;&lt;/span&gt; &lt;span class=&#34;c&#34;&gt;# I like to add &amp;#34;hdp&amp;#34; to the beginning of the resource group name&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;nb&#34;&gt;Write-Host&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;Creating storage account...&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;12&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;nb&#34;&gt;New-AzureRmStorageAccount&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;-Name&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;$storageAccountName&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;-ResourceGroupName&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;$resourceGroupName&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;-Type&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Standard_GRS&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;`&lt;/span&gt; &lt;span class=&#34;c&#34;&gt;# Options are Premium_LRS, Standard_GRS, Standard_LRS, Standard_RAGRS, and Standard_ZRS&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;-Location&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;$location&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;nb&#34;&gt;Write-Host&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;Creating container...&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;nv&#34;&gt;$storageAccountKey&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;nb&#34;&gt;Get-AzureRmStorageAccountKey&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;-ResourceGroupName&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;$resourceGroupName&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;-Name&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;$storageAccountName&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;).&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;Value&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;mf&#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;20&lt;/span&gt;&lt;span class=&#34;cl&#34;&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;nb&#34;&gt;New-AzureStorageContext&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;-StorageAccountName&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;$storageAccountName&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;-StorageAccountKey&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;$storageAccountKey&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;nb&#34;&gt;New-AzureStorageContainer&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;-Name&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;$containerName&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;-Context&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;$context&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&#34;create-the-hadoop-cluster&#34;&gt;Create the Hadoop Cluster&lt;/h2&gt;
&lt;p&gt;Final piece!  What do you need to know in order to create an HDInsight Hadoop cluster with PowerShell?&lt;/p&gt;
&lt;h3 id=&#34;cluster-specific-details&#34;&gt;Cluster-specific Details&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;A &lt;strong&gt;name&lt;/strong&gt; for the HDInsight cluster&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;type&lt;/strong&gt; of cluster (Hadoop, Storm, Spark, Kafka, etc.)&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;version&lt;/strong&gt; of Hadoop (or Storm, Spark, Kafka, etc.)&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;number of nodes&lt;/strong&gt; you want the cluster to have&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;operating system&lt;/strong&gt; (Windows or Linux)&lt;/li&gt;
&lt;li&gt;A set of &lt;strong&gt;credentials&lt;/strong&gt; that are used to log in to the HDInsight cluster
&lt;ul&gt;
&lt;li&gt;One set for HTTP&lt;/li&gt;
&lt;li&gt;One set for SSH&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;resource-logistic-details&#34;&gt;Resource-logistic Details&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;resource group&lt;/strong&gt; that the cluster will be a part of (you created this earlier)&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;location&lt;/strong&gt; (You can reuse the &lt;code&gt;$location&lt;/code&gt; variable you used to create the new resource group earlier)&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;default storage account name&lt;/strong&gt; (you created this earlier)&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;default storage account key&lt;/strong&gt; (you obtained this earlier)&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;default storage container&lt;/strong&gt; (you made this earlier)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Here&amp;rsquo;s an update to the PowerShell script:&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-powershell&#34; data-lang=&#34;powershell&#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;c&#34;&gt;###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; 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;c&#34;&gt;# Log in to Azure&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;c&#34;&gt;# Create Resource Group&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;c&#34;&gt;# Create Storage&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;c&#34;&gt;###New Stuff###&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;nb&#34;&gt;Write-Host&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;Creating HDInsight cluster...&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;11&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;nv&#34;&gt;$httpCredential&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;New-Object&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;System&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;py&#34;&gt;Management&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;py&#34;&gt;Automation&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;py&#34;&gt;PSCredential&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nv&#34;&gt;$httpUserName&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;$password&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;nv&#34;&gt;$sshCredential&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;New-Object&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;System&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;py&#34;&gt;Management&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;py&#34;&gt;Automation&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;py&#34;&gt;PSCredential&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nv&#34;&gt;$sshUserName&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;$password&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;nb&#34;&gt;New-AzureRmHDInsightCluster&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;-ClusterName&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;$clusterName&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;-ClusterType&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Hadoop&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;n&#34;&gt;-Version&lt;/span&gt; &lt;span class=&#34;mf&#34;&gt;3.6&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;n&#34;&gt;-ClusterSizeInNodes&lt;/span&gt; &lt;span class=&#34;mf&#34;&gt;2&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;`&lt;/span&gt; &lt;span class=&#34;c&#34;&gt;# You may need more... 2 is good for practicing&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;-OSType&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Linux&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;-HttpCredential&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;$httpCredential&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;n&#34;&gt;-SshCredential&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;$sshCredential&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;-ResourceGroupName&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;$resourceGroupName&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;-Location&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;$location&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;-DefaultStorageAccountName&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;&lt;/span&gt;&lt;span class=&#34;nv&#34;&gt;$storageAccountName&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;.blob.core.windows.net&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;-DefaultStorageAccountKey&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;$storageAccountKey&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;-DefaultStorageContainer&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;$containerName&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;nb&#34;&gt;Write-Host&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;Finished!&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&#34;final-script&#34;&gt;Final Script&lt;/h2&gt;
&lt;p&gt;Before I call it &amp;ldquo;good&amp;rdquo;, I like to bring my shared variables up to the top of the script so they&amp;rsquo;re not sprinkled throughout.  Here&amp;rsquo;s a final script that I use as a template for creating new HDInsight Hadoop clusters in Azure:&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-powershell&#34; data-lang=&#34;powershell&#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;nb&#34;&gt;Login-AzureRmAccount&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;nv&#34;&gt;$resourceGroupName&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;nameOfResourceGroup&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;nv&#34;&gt;$location&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;South Central US&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;nv&#34;&gt;$storageAccountName&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;&lt;/span&gt;&lt;span class=&#34;nv&#34;&gt;$resourceGroupName&lt;/span&gt;&lt;span class=&#34;s2&#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; 6&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;nv&#34;&gt;$containerName&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;hdp&lt;/span&gt;&lt;span class=&#34;nv&#34;&gt;$resourceGroupName&lt;/span&gt;&lt;span class=&#34;s2&#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;nv&#34;&gt;$clusterName&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;$containerName&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;nv&#34;&gt;$clusterNodes&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;mf&#34;&gt;2&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;nv&#34;&gt;$httpUserName&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;HDUser&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;10&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;nv&#34;&gt;$sshUserName&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;SSHHuser&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;11&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;nv&#34;&gt;$password&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;ConvertTo-SecureString&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;MyPa&lt;/span&gt;&lt;span class=&#34;se&#34;&gt;`$`$&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;w0rD&amp;#34;&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;-AsPlainText&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;-Force&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;nb&#34;&gt;Write-Host&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;Creating resource group...&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;14&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;nb&#34;&gt;New-AzureRmResourceGroup&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;-Name&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;$resourceGroupName&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;-Location&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;$location&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;nb&#34;&gt;Write-Host&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;Creating storage account...&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;17&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;nb&#34;&gt;New-AzureRmStorageAccount&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;-Name&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;$storageAccountName&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;n&#34;&gt;-ResourceGroupName&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;$resourceGroupName&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;-Type&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Standard_GRS&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;n&#34;&gt;-Location&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;$location&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&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;nb&#34;&gt;Write-Host&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;Creating container...&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;24&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;nv&#34;&gt;$storageAccountKey&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;nb&#34;&gt;Get-AzureRmStorageAccountKey&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;-ResourceGroupName&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;$resourceGroupName&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;-Name&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;$storageAccountName&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;).&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;Value&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;mf&#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;25&lt;/span&gt;&lt;span class=&#34;cl&#34;&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;nb&#34;&gt;New-AzureStorageContext&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;-StorageAccountName&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;$storageAccountName&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;-StorageAccountKey&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;$storageAccountKey&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;nb&#34;&gt;New-AzureStorageContainer&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;-Name&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;$containerName&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;-Context&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;$context&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;nb&#34;&gt;Write-Host&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;Creating HDInsight Hadoop cluster...&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;nv&#34;&gt;$httpCredential&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;New-Object&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;System&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;py&#34;&gt;Management&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;py&#34;&gt;Automation&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;py&#34;&gt;PSCredential&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nv&#34;&gt;$httpUserName&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;$password&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;30&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;nv&#34;&gt;$sshCredential&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;New-Object&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;System&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;py&#34;&gt;Management&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;py&#34;&gt;Automation&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;py&#34;&gt;PSCredential&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nv&#34;&gt;$sshUserName&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;$password&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;31&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;32&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;nb&#34;&gt;New-AzureRmHDInsightCluster&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;-ClusterName&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;$clusterName&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;n&#34;&gt;-ClusterType&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Hadoop&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;35&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;  &lt;span class=&#34;n&#34;&gt;-Version&lt;/span&gt; &lt;span class=&#34;mf&#34;&gt;3.6&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;36&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;  &lt;span class=&#34;n&#34;&gt;-ClusterSizeInNodes&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;$clusterNodes&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;`&lt;/span&gt; &lt;span class=&#34;c&#34;&gt;# You may need more... 2 is good for practicing&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;n&#34;&gt;-OSType&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Linux&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;-HttpCredential&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;$httpCredential&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;n&#34;&gt;-SshCredential&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;$sshCredential&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;n&#34;&gt;-ResourceGroupName&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;$resourceGroupName&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;41&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;  &lt;span class=&#34;n&#34;&gt;-Location&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;$location&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;42&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;  &lt;span class=&#34;n&#34;&gt;-DefaultStorageAccountName&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;&lt;/span&gt;&lt;span class=&#34;nv&#34;&gt;$storageAccountName&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;.blob.core.windows.net&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;43&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;  &lt;span class=&#34;n&#34;&gt;-DefaultStorageAccountKey&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;$storageAccountKey&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;44&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;  &lt;span class=&#34;n&#34;&gt;-DefaultStorageContainer&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;$containerName&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;45&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;46&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;nb&#34;&gt;Write-Host&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;Finished!&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</description>
    </item>
    
    <item>
      <title>Swift Cheat Sheet for iPhone Location Services Access and Usage</title>
      <link>https://www.andrewcbancroft.com/2018/07/16/swift-cheat-sheet-iphone-location-access-usage/</link>
      <pubDate>Tue, 17 Jul 2018 00:43:03 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2018/07/16/swift-cheat-sheet-iphone-location-access-usage/</guid>
      <description>&lt;p&gt;This is a cheat sheet of the code and workflow for iPhone location access and usage, from requesting permission to using the location of the user&amp;rsquo;s device.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;framework-import&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;framework-import&#34;&gt;Framework Import&lt;/h1&gt;
&lt;p&gt;&lt;code&gt;import CoreLocation&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;location-usage-description&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;set-location-usage-description-in-infoplist&#34;&gt;Set Location Usage Description in Info.plist&lt;/h1&gt;
&lt;p&gt;&lt;strong&gt;This is a required first step&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;If you &lt;strong&gt;don&amp;rsquo;t&lt;/strong&gt; set this, your app simply won&amp;rsquo;t present the user with the system alert to request access to the location (and, of course, you won&amp;rsquo;t be getting any location data for your app either).&lt;/p&gt;
&lt;p&gt;When you request permission to use the device&amp;rsquo;s location, a short message will appear in the default iOS system dialog. You customize this message by adding one of the following keys to your Info.plist file:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Privacy – Location Always and When In Use Usage Description&lt;/li&gt;
&lt;li&gt;Privacy – Location Always Usage Description&lt;/li&gt;
&lt;li&gt;Privacy – Location When In Use Usage Description&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For the &lt;strong&gt;value&lt;/strong&gt; of this plist property, type a short string describing what you&amp;rsquo;re using the location for.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;location-manager-delegate&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;initialize-cllocationmanager-and-implement-cllocationmanagerdelegate&#34;&gt;Initialize CLLocationManager and Implement CLLocationManagerDelegate&lt;/h1&gt;
&lt;p&gt;&lt;code&gt;CLLocationManager&lt;/code&gt; is the name of the class that gives you a way to request location services permission. It is also responsible for starting the location tracking process.&lt;/p&gt;
&lt;p&gt;It needs a corresponding &lt;code&gt;CLLocationManagerDelegate&lt;/code&gt; to be assigned. The location manager instance you initialize will alert you to location services authorization changes (ie, if the user turns off location services permissions for your app) &lt;em&gt;through&lt;/em&gt; the one of the methods that your delegate class implements.&lt;/p&gt;
&lt;p&gt;The location manager instance will also send you the user&amp;rsquo;s current location through one of the methods your delegate class implements.&lt;/p&gt;
&lt;p&gt;Here is an example of initializing the &lt;code&gt;CLLocationManager&lt;/code&gt; instance, and setting its delegate.&lt;/p&gt;
&lt;p&gt;There&amp;rsquo;s also a &lt;code&gt;currentLocation&lt;/code&gt; variable that will get continually updated…&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;NameOfViewController&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;CLLocationManagerDelegate&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 2&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;private&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;locationManager&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;CLLocationManager&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;// This will get updated via the location manager delegate&amp;#39;s didUpdateLocations method&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;currentLocation&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;CLLocation&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&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&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;// Do any additional setup after loading the view.&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;n&#34;&gt;locationManager&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&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;kc&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;startLocationServices&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;// This function is implemented below...&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Here is an example of how to implement the two &lt;code&gt;CLLocationManagerDelegate&lt;/code&gt; methods:&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;// Monitor location services authorization changes&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;locationManager&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;manager&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;CLLocationManager&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;didChangeAuthorization&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;status&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;CLAuthorizationStatus&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;switch&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;status&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;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;notDetermined&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;break&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;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;authorizedWhenInUse&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;authorizedAlways&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;if&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;CLLocationManager&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;locationServicesEnabled&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 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;locationManager&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;startUpdatingLocation&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 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;restricted&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;denied&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;alertLocationAccessNeeded&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&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;// Get the device&amp;#39;s current location and assign the latest CLLocation value to your tracking variable&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;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;locationManager&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;manager&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;CLLocationManager&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;n&#34;&gt;didUpdateLocations&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;locations&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;CLLocation&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;currentLocation&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;locations&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;bp&#34;&gt;last&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;start-location-services&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;start-location-services-check-location-authorization-status-request-permission&#34;&gt;Start Location Services, Check Location Authorization Status, Request Permission&lt;/h1&gt;
&lt;p&gt;When you first start location services in your app, you will want to immediately check your app&amp;rsquo;s current authorization status for location services, just in case the user has gone in and disabled location services permission for your app since the last time he/she used it.&lt;/p&gt;
&lt;p&gt;If it&amp;rsquo;s the first time the user has launched your app, the &lt;code&gt;.notDetermined&lt;/code&gt; case will get hit. This is where you request permission for the first time.&lt;/p&gt;
&lt;p&gt;If the user grants permission, you can use the &lt;code&gt;CLLocationManager&lt;/code&gt; instance that your class is using to start updating the user&amp;rsquo;s location.&lt;/p&gt;
&lt;p&gt;If, however, the user denies permission, you can alert them to the fact that your app needs access to his/her location for [&lt;a href=&#34;#location-usage-description&#34;&gt;insert some good reason here&lt;/a&gt;].&lt;/p&gt;
&lt;p&gt;Here&amp;rsquo;s a code snippet showing this in action:&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;startLocationServices&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;locationManager&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;desiredAccuracy&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;kCLLocationAccuracyBest&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;locationAuthorizationStatus&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;CLLocationManager&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;authorizationStatus&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;k&#34;&gt;switch&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;locationAuthorizationStatus&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;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;notDetermined&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;locationManager&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;requestWhenInUseAuthorization&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;// This is where you request permission to use location services&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;case&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;authorizedWhenInUse&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;authorizedAlways&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;if&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;CLLocationManager&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;locationServicesEnabled&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;kc&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;locationManager&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;startUpdatingLocation&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;k&#34;&gt;case&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;restricted&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;denied&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;alertLocationAccessNeeded&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 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;strong&gt;Note&lt;/strong&gt;: You can test for the &lt;code&gt;.notDetermined&lt;/code&gt; case by deleting the app on the device, if it&amp;rsquo;s already been installed or run on a device from the debugger.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;alert-location-access-needed&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;alert-location-services-access-needed&#34;&gt;Alert Location Services Access Needed&lt;/h1&gt;
&lt;p&gt;If location services access has been denied or restricted, you can alert the user and direct them to the Settings app to make the appropriate permissions adjustment:&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;alertLocationAccessNeeded&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;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;settingsAppURL&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;n&#34;&gt;string&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIApplicationOpenSettingsURLString&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; 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;alert&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIAlertController&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;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;Need Location Access&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;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;Location access is required for including the location of the hazard.&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;n&#34;&gt;preferredStyle&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIAlertControllerStyle&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;alert&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;n&#34;&gt;alert&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;addAction&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;UIAlertAction&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;Cancel&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;style&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;k&#34;&gt;default&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;handler&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;11&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;n&#34;&gt;alert&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;addAction&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;UIAlertAction&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;Allow Location Access&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;n&#34;&gt;style&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;cancel&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;handler&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;alert&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;n&#34;&gt;UIApplication&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;shared&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;open&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;settingsAppURL&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;options&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;16&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;                                                                &lt;span class=&#34;n&#34;&gt;completionHandler&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&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;n&#34;&gt;present&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;alert&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;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;strong&gt;Note&lt;/strong&gt;: You can test for this case (&lt;code&gt;.restricted&lt;/code&gt; and &lt;code&gt;.denied&lt;/code&gt;) by going to the Settings app and turning off location services access for your app, if it&amp;rsquo;s been previously granted.&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>Swift Cheat Sheet for iPhone Camera Access and Usage</title>
      <link>https://www.andrewcbancroft.com/2018/02/24/swift-cheat-sheet-for-iphone-camera-access-usage/</link>
      <pubDate>Sat, 24 Feb 2018 12:23:02 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2018/02/24/swift-cheat-sheet-for-iphone-camera-access-usage/</guid>
      <description>&lt;p&gt;This is a cheat sheet of the code and workflow for iPhone camera access and usage, from requesting permission to using the photo taken with a user&amp;rsquo;s device.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;framework-import&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;framework-import&#34;&gt;Framework Import&lt;/h1&gt;
&lt;p&gt;&lt;code&gt;import AVFoundation&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;camera-usage-description&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;set-camera-usage-description-in-infoplist&#34;&gt;Set Camera Usage Description in Info.plist&lt;/h1&gt;
&lt;p&gt;When you request permission to use the device&amp;rsquo;s camera, a short message will appear in the default iOS system dialog. You customize this message by adding the &lt;code&gt;Privacy - Camera Usage Description&lt;/code&gt; key to your Info.plist file.&lt;/p&gt;
&lt;p&gt;For the &lt;strong&gt;value&lt;/strong&gt; of this plist property, type a short string describing what you&amp;rsquo;re using the camera for.&lt;/p&gt;
&lt;p&gt;If you &lt;strong&gt;don&amp;rsquo;t&lt;/strong&gt; set this, your app will crash when you request access to the camera.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;check-authorization-status&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;check-and-respond-to-camera-authorization-status&#34;&gt;Check and Respond to Camera Authorization Status&lt;/h1&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;cameraAuthorizationStatus&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;AVCaptureDevice&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;authorizationStatus&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;k&#34;&gt;for&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;video&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 class=&#34;k&#34;&gt;switch&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;cameraAuthorizationStatus&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;notDetermined&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;requestCameraPermission&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;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;authorized&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;presentCamera&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;restricted&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;denied&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;alertCameraAccessNeeded&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;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;request-permission&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;request-camera-permission&#34;&gt;Request Camera Permission&lt;/h1&gt;
&lt;p&gt;If the user has never responded to a request to access his/her camera, you need to prompt with the iOS system alert to request permission:&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;requestCameraPermission&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;AVCaptureDevice&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;requestAccess&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;k&#34;&gt;for&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;video&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;completionHandler&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;accessGranted&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;guard&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;accessGranted&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;k&#34;&gt;else&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;return&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;kc&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;presentCamera&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;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;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: You can test for this case by deleting the app on the device, if it&amp;rsquo;s already been installed or run on a device from the debugger.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;present-camera&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;present-camera&#34;&gt;Present Camera&lt;/h1&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;presentCamera&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;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;photoPicker&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIImagePickerController&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;photoPicker&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;sourceType&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;camera&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;photoPicker&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;k&#34;&gt;as&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;?&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIImagePickerControllerDelegate&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UINavigationControllerDelegate&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;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;photoPicker&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;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;p&gt;&lt;a name=&#34;alert-camera-access-needed&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;alert-camera-access-needed&#34;&gt;Alert Camera Access Needed&lt;/h1&gt;
&lt;p&gt;If camera access has been denied or restricted, you can alert the user and direct them to the Settings app to make the appropriate permissions adjustment:&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;alertCameraAccessNeeded&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;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;settingsAppURL&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;n&#34;&gt;string&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIApplicationOpenSettingsURLString&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; 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;alert&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIAlertController&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;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;Need Camera Access&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;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;Camera access is required to make full use of this app.&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;n&#34;&gt;preferredStyle&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIAlertControllerStyle&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;alert&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;n&#34;&gt;alert&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;addAction&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;UIAlertAction&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;Cancel&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;style&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;k&#34;&gt;default&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;handler&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;11&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;n&#34;&gt;alert&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;addAction&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;UIAlertAction&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;Allow Camera&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;style&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;cancel&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;handler&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;alert&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;12&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;UIApplication&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;shared&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;open&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;settingsAppURL&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;p&#34;&gt;[:],&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;completionHandler&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;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;n&#34;&gt;present&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;alert&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;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;&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: You can test for this case by going to the Settings app and turning off camera access for your app, if it&amp;rsquo;s been previously granted.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;use-captured-image&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;use-the-captured-image&#34;&gt;Use the Captured Image&lt;/h1&gt;
&lt;p&gt;To use the image that the camera captured, you need to set up your view controller to adhere to and implement couple of delegate protocols:&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;NameOfViewController&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;UINavigationControllerDelegate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIImagePickerControllerDelegate&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;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;delegate&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;uiimagepickercontrollerdelegate&#34;&gt;UIImagePickerControllerDelegate&lt;/h2&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;imagePickerController&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;picker&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIImagePickerController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;didFinishPickingMediaWithInfo&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;info&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;nb&#34;&gt;String&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;Any&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;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;photo&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;info&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;UIImagePickerControllerOriginalImage&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;UIImage&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;// do something with the photo... set to UIImageView, save it, etc.&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;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;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;/code&gt;&lt;/pre&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>Winning with Dimensional Models in the Age of Big Data</title>
      <link>https://www.andrewcbancroft.com/blog/data/dimensional-modeling/winning-with-dimensional-models-in-age-of-big-data/</link>
      <pubDate>Wed, 21 Feb 2018 00:00:00 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/blog/data/dimensional-modeling/winning-with-dimensional-models-in-age-of-big-data/</guid>
      <description>&lt;p&gt;It&amp;rsquo;s a fair question: Is &lt;a href=&#34;https://www.andrewcbancroft.com/blog/data/dimensional-modeling/what-is-a-dimensional-model/&#34;&gt;dimensional modeling&lt;/a&gt; still relevant in the age of big data?&lt;/p&gt;
&lt;p&gt;Read enough about &amp;ldquo;big data&amp;rdquo;, and you may start to get the impression that modeling techniques are old-fashioned or irrelevant.&lt;/p&gt;
&lt;p&gt;Data professionals in IT departments are used to wrangling data - it&amp;rsquo;s understood that there&amp;rsquo;s no &amp;ldquo;easy button&amp;rdquo; when it comes to integrating data and geting meaning out of it.  So we muster the energy and we do what it takes to get the data in order for &lt;a href=&#34;https://www.andrewcbancroft.com/blog/data/general-data-concepts/what-types-of-data-analysis-are-there/&#34;&gt;whatever analysis&lt;/a&gt; the business is after.&lt;/p&gt;
&lt;p&gt;But guess what:  &lt;strong&gt;Business users don&amp;rsquo;t want to wrangle data.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Not even savvy business users want to go through the trouble of writing map-reduce jobs or doing the work necessary to produce predictive models.&lt;/p&gt;
&lt;p&gt;What do business users want? &lt;strong&gt;Self-service&lt;/strong&gt; analysis capability in a context they understand.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;They don&amp;rsquo;t want to write code or queries&lt;/li&gt;
&lt;li&gt;They don&amp;rsquo;t want to be left integrating data sets themselves&lt;/li&gt;
&lt;li&gt;They don&amp;rsquo;t want to understand infrastructure&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;They want the information that can help them make a business decision without lots of hoops to jump through (including work orders for reports to IT).&lt;/p&gt;
&lt;p&gt;That&amp;rsquo;s why I believe there are at least 5 reasons that dimensional models can be a huge win for the business and for IT.&lt;/p&gt;
&lt;h2 id=&#34;1-data-in-context-is-the-most-powerful-model-for-robust-analysis&#34;&gt;1. Data in context is the most powerful model for robust analysis&lt;/h2&gt;
&lt;p&gt;A dimensional model is a representation of data that portrays its measurements &lt;strong&gt;in context&lt;/strong&gt;&amp;hellip; that is, it takes the numbers and surrounds them with highly descriptive characteristics about the event that generated the measurements.&lt;/p&gt;
&lt;p&gt;Dimensional models highlight the characteristics, qualities, features, and facets&amp;hellip;the who, what, when, where, how&amp;hellip;of the data collection situation.&lt;/p&gt;
&lt;p&gt;Therefore, a dimensional model is ideal for understanding the patterns and information that the data contains in a way that is &lt;strong&gt;widely approachable&lt;/strong&gt; by analysts of all kinds.&lt;/p&gt;
&lt;h2 id=&#34;2-data-cleansing-will-always-have-to-be-done&#34;&gt;2. Data cleansing will always have to be done&lt;/h2&gt;
&lt;p&gt;There&amp;rsquo;s no escaping data cleansing and transformation.&lt;/p&gt;
&lt;p&gt;In &lt;a href=&#34;https://www.andrewcbancroft.com/blog/data/machine-learning/stages-to-machine-learning/&#34;&gt;&amp;ldquo;Stages to Machine Learning&amp;rdquo;&lt;/a&gt;, I discuss some always-required steps to getting from A to Z in machine learning endeavors.  Two of those:  data cleansing, and data transformation.&lt;/p&gt;
&lt;p&gt;If data professionals don&amp;rsquo;t do this, end-users must.&lt;/p&gt;
&lt;p&gt;No matter what, someone has to put in the effort to ensure quality, consistent data that is standardized for analysis.&lt;/p&gt;
&lt;p&gt;So if you&amp;rsquo;re going to go through the trouble &lt;em&gt;anyways&lt;/em&gt;, it makes sense to put in the effort to model the data so that business users can focus on what matters most: the business.&lt;/p&gt;
&lt;h2 id=&#34;3-its-just-a-matter-of-time-until-you-need-a-schema&#34;&gt;3. It&amp;rsquo;s just a matter of time until you need a schema&lt;/h2&gt;
&lt;p&gt;You&amp;rsquo;re not escaping schemas if you&amp;rsquo;re delivering the ability for end-users to interact with the business&amp;rsquo; data sets with relative ease.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Data lakes&amp;rdquo; offer the opportunity to reverse the order in which schemas are required for a particular analysis, shifting it from an up-front necessity to a &amp;ldquo;schema on read&amp;rdquo; / &amp;ldquo;just-in-time schema&amp;rdquo; approach.  But &amp;ldquo;schema on read&amp;rdquo; isn&amp;rsquo;t the same as &amp;ldquo;no schema&amp;rdquo;.&lt;/p&gt;
&lt;p&gt;So again, whether it&amp;rsquo;s schema on write, as you do in traditional Extract-Transform-Load processes, or if it&amp;rsquo;s schema on read, as the advent of a data lake provides, there&amp;rsquo;s likely going to be a schema in your future.&lt;/p&gt;
&lt;p&gt;It makes sense, then, to design a schema (a model, if you will) that end-users can readily understand and work with using a business intelligence reporting and visualization tool of their choice.&lt;/p&gt;
&lt;h2 id=&#34;4-not-all-data-problems-are-big-data-problems&#34;&gt;4. Not all data problems are &amp;ldquo;big data&amp;rdquo; problems&lt;/h2&gt;
&lt;p&gt;Before your team runs off to implment a Hadoop cluster, ask, &amp;ldquo;Do we really have &amp;lsquo;big data&amp;rsquo;??&amp;rdquo;&lt;/p&gt;
&lt;p&gt;If your data set can fit in memory on a single computer, your data is not &amp;ldquo;big data&amp;rdquo;.  If your data is coming in at manageable intervals&amp;hellip;ie, if you&amp;rsquo;re able to keep up, you don&amp;rsquo;t have the velocity problem that big data presents.&lt;/p&gt;
&lt;p&gt;If you don&amp;rsquo;t have a big data problem, don&amp;rsquo;t invent one for yourself!&lt;/p&gt;
&lt;p&gt;Somewhere along the way, your business users just want to perform some basic descriptive analytics.  Go there.  Provide that value.&lt;/p&gt;
&lt;h2 id=&#34;5-dimensional-models-are-low-hanging-fruit&#34;&gt;5. Dimensional models are low-hanging fruit&lt;/h2&gt;
&lt;p&gt;In terms of investment, dimensional models are relatively low-hanging fruit to implement.&lt;/p&gt;
&lt;p&gt;Additionally, they provide real, tangible value to a business.&lt;/p&gt;
&lt;p&gt;If you&amp;rsquo;re looking for a relatively &amp;ldquo;easy&amp;rdquo; win, building dimensional models that reflect the processes of the business in an integrated manner is a good way to get off the ground.&lt;/p&gt;
&lt;p&gt;When the time comes, you&amp;rsquo;ll have done the data cleansing and transformation work to provide standard data sets that can be used for more advanced forms of analysis.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Predictive Analytics vs Machine Learning - What&#39;s the Difference?</title>
      <link>https://www.andrewcbancroft.com/blog/data/machine-learning/predictive-analytics-vs-machine-learning-whats-the-difference/</link>
      <pubDate>Mon, 05 Feb 2018 00:00:00 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/blog/data/machine-learning/predictive-analytics-vs-machine-learning-whats-the-difference/</guid>
      <description>&lt;p&gt;There&amp;rsquo;s a strong temptation to equate &amp;ldquo;predictive analytics&amp;rdquo; and &amp;ldquo;machine learning&amp;rdquo;. It&amp;rsquo;s not uncommon to throw the terms around interchangeably, which signals that the two are strongly related.&lt;/p&gt;
&lt;p&gt;BUT. Is there a &lt;em&gt;difference&lt;/em&gt; that sometimes gets lost when we substitute one term for the other? If there &lt;em&gt;is&lt;/em&gt; a difference, is it important to distinguish?&lt;/p&gt;
&lt;h2 id=&#34;predictive-analytics-a-method-for-analysis&#34;&gt;Predictive Analytics: A Method for Analysis&lt;/h2&gt;
&lt;p&gt;In &lt;a href=&#34;https://www.andrewcbancroft.com/blog/data/general-data-concepts/data-analysis-vs-data-analytics-examining-suffix/&#34;&gt;Breaking Down Data Analysis vs Data Analytics: A Look at the Suffix&lt;/a&gt;, I reasoned that analytics essentially &amp;ldquo;denotes the body of facts… the knowledge about… the principles for: analysis. Analytics is about methodology. It’s about the approach to performing analysis. It’s the &lt;em&gt;science&lt;/em&gt; of analysis.&amp;rdquo;&lt;/p&gt;
&lt;p&gt;In &lt;a href=&#34;https://www.andrewcbancroft.com/blog/data/general-data-concepts/what-types-of-data-analysis-are-there/&#34;&gt;What Types of Data Analysis Are There?&lt;/a&gt;, I noted that predictive &lt;em&gt;analysis&lt;/em&gt; is what you&amp;rsquo;re doing when you analyze data in a way that can produce predictions of measurements for specific &amp;ldquo;units&amp;rdquo; within a population (as opposed to inferential analysis where the measurement predictions are made for the population as a whole).&lt;/p&gt;
&lt;p&gt;Combining the two concepts of &amp;ldquo;analytics&amp;rdquo; and &amp;ldquo;predictive analysis&amp;rdquo; ought to lend us to a natural definition for &amp;ldquo;predictive analytics&amp;rdquo;.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Predictive analytics refers to the principles and methods for making measurement predictions for specific &amp;ldquo;units&amp;rdquo; within a population.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Predictive analytics is concerned with the principles and methods for, say&amp;hellip;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Taking an individual customer from the population of a company&amp;rsquo;s customer base, analyzing data about him/her, and predicting the liklihood that he/she will purchase [this product] if we run [this marketing campaign] targeted at them&lt;/li&gt;
&lt;li&gt;Taking a single house from the population of homes in a city or neighborhood, and predicting the selling price&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Predictive analytics encapsulates the principles and methods for computing estimations for these measurements (or any number of others that &lt;a href=&#34;https://www.andrewcbancroft.com/blog/data/general-data-concepts/patterns-and-key-words-of-predictive-analytics-questions/&#34;&gt;fit the pattern of questions&lt;/a&gt; for predictive analytics to provide the answer for).&lt;/p&gt;
&lt;p&gt;The thing about &amp;ldquo;predictive analytics&amp;rdquo; as a term, is that it&amp;rsquo;s pretty abstract.  Notably missing in these &amp;ldquo;principles and methods&amp;rdquo; is the &lt;strong&gt;how to do it&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;That&lt;/em&gt; is what &amp;ldquo;machine learning&amp;rdquo; is.&lt;/p&gt;
&lt;h2 id=&#34;machine-learning--a-way-to-do-predictive-analytics&#34;&gt;Machine Learning:  A Way to do Predictive Analytics&lt;/h2&gt;
&lt;p&gt;Machine learning is a way to &lt;em&gt;do&lt;/em&gt; predictive analytics.&lt;/p&gt;
&lt;p&gt;It is one way to take this idea of predicting measurements for an &amp;ldquo;individual unit&amp;rdquo; within a population, and turn it into a reality.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;One&lt;/em&gt; way?  So&amp;hellip; there are &lt;em&gt;other ways&lt;/em&gt; to do predictive analytics??&lt;/p&gt;
&lt;p&gt;I can think of at least one&amp;hellip;&lt;/p&gt;
&lt;h2 id=&#34;before-there-were-machines-to-learn&#34;&gt;Before There Were Machines to Learn&lt;/h2&gt;
&lt;p&gt;Before there were machines, there were&amp;hellip; &lt;em&gt;people&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;People (statisticians in particular) have been doing predictive analysis for decades, if not centuries. It may have taken longer and been more tedius and error-prone, but steps that are taken to figure out &amp;ldquo;the answer&amp;rdquo; in machine learning have been do-able by humans for a long time&amp;hellip; they are not &amp;ldquo;new&amp;rdquo;.&lt;/p&gt;
&lt;p&gt;Sooooo&amp;hellip; why &amp;ldquo;machine learning&amp;rdquo;?&lt;/p&gt;
&lt;p&gt;In short, it&amp;rsquo;s a &lt;em&gt;scale&lt;/em&gt; problem with modern data sets. The data sets of the 21st century are much larger than ever before in history.&lt;/p&gt;
&lt;p&gt;While it&amp;rsquo;s &lt;em&gt;possible&lt;/em&gt; for people to compute the outputs that a machine learning algorithm can produce, it&amp;rsquo;s &lt;em&gt;unnecessary&lt;/em&gt;.  Due to the scale of data in today&amp;rsquo;s world, it&amp;rsquo;s also insanely impractical for many data sets.&lt;/p&gt;
&lt;h2 id=&#34;automating-the-humans-steps&#34;&gt;Automating the Human&amp;rsquo;s Steps&lt;/h2&gt;
&lt;p&gt;If you handed me a small data set and said, &amp;ldquo;Manually find a way to produce a predicted house price for a home in [this neighborhood]&amp;rdquo;, I could probably swing it.&lt;/p&gt;
&lt;p&gt;If you handed me another similar data set and asked me to do the same thing, I could probably do it again, assuming I remember all the steps.  On the second round, I might even be brilliant enough to &lt;em&gt;write the steps down&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;If you came to me again (and again, and again), I could follow the steps one by one to produce predictions.  I&amp;rsquo;d just be really tired, bored, and annoyed.&lt;/p&gt;
&lt;p&gt;Naturally, you might ask, &amp;ldquo;Why not &lt;em&gt;automate the steps&lt;/em&gt;?&amp;rdquo;&lt;/p&gt;
&lt;p&gt;That&amp;rsquo;s the idea behind machine learning!&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Machine learning&amp;rdquo; simply describes what computers do when they execute &lt;em&gt;programs&lt;/em&gt; to produce predictions.&lt;/p&gt;
&lt;p&gt;Computer scientists take the specific steps that need to be followed in order to produce a prediction (an &lt;em&gt;algorithm&lt;/em&gt;, if you will) and &lt;em&gt;write code&lt;/em&gt; to let a computer (a &lt;em&gt;machine&lt;/em&gt;) do those steps and produce the prediction.&lt;/p&gt;
&lt;p&gt;Same results.  Without goof-ups.  Faster.&lt;/p&gt;
&lt;p&gt;Happier Andrew.&lt;/p&gt;
&lt;p&gt;That is machine learning&amp;hellip; it involves the encoding of the steps that need to be taken in order to produce measurement predictions of all kinds in such a way that a computer can &lt;em&gt;execute&lt;/em&gt; those steps and produce the result we want.&lt;/p&gt;
&lt;h2 id=&#34;the-bottom-line&#34;&gt;The Bottom-Line&lt;/h2&gt;
&lt;p&gt;Predictive analytics and machine learning are not &amp;ldquo;the same&amp;rdquo;.&lt;/p&gt;
&lt;p&gt;Predictive analytics is about [the way(s)], in general, to make estimations about specific people (or specific animals or widgets or [whatever] the &amp;ldquo;unit&amp;rdquo; of interest is from your population of &amp;ldquo;units&amp;rdquo;).&lt;/p&gt;
&lt;p&gt;Machine learning &lt;em&gt;is&lt;/em&gt; one of those ways to make those predictions a reality.&lt;/p&gt;
&lt;p&gt;The means to do something&amp;hellip;the methods and strategies and ways we approach accomplishing a goal&amp;hellip;are not the same thing as the goal itself. They&amp;rsquo;re two distinct things, aren&amp;rsquo;t they?&lt;/p&gt;
&lt;p&gt;If I say, &amp;ldquo;My goal is to save $1000 by Christmas.&amp;rdquo;, I could go about achieving that goal in a dozen ways.  I might begin a saving strategy that involves setting back about $83 / month, the goal and the strategy are two separate things.  I might take on a side project to earn extra money, or sell stuff on Craigslist. In either case, the strategy &lt;em&gt;accomplishes&lt;/em&gt; the goal.&lt;/p&gt;
&lt;p&gt;Predictive analytics is the goal.  Machine learning is a strategy used to &lt;em&gt;accomplish&lt;/em&gt; the goal.&lt;/p&gt;
&lt;h2 id=&#34;theyre-different--so-what&#34;&gt;They&amp;rsquo;re Different &amp;ndash; So What?&lt;/h2&gt;
&lt;p&gt;So who cares if they&amp;rsquo;re different?  Is it even an important distinction to make?  What&amp;rsquo;s the harm in swapping in &amp;ldquo;machine learning&amp;rdquo; for &amp;ldquo;predictive analytics&amp;rdquo;, or vice-versa?&lt;/p&gt;
&lt;p&gt;One could make the argument that it only matters if you&amp;rsquo;re OCD about terms.  My take on it is that &amp;ldquo;data science&amp;rdquo; is already this nebulous, vague, muddy-water field, so any effort we can make to be precise about our terms can help clarify what we mean is helpful.&lt;/p&gt;
&lt;p&gt;Is it &lt;em&gt;harmful&lt;/em&gt; to equate predictive analytics and machine learning? I suppose that depends on your view of how important it is to be clear and precise so as not to cause confusion.&lt;/p&gt;
&lt;p&gt;Having to Google &amp;ldquo;Predictive analytics vs machine learning&amp;rdquo; is one clue that the water is already muddy enough.  Life and death may not be at stake, but I think it&amp;rsquo;s &lt;em&gt;helpful&lt;/em&gt; to develop the right mental model for all of these pieces to fit together in the world of data science.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Work in Progress?</title>
      <link>https://www.andrewcbancroft.com/work-in-progress/</link>
      <pubDate>Thu, 01 Feb 2018 00:00:00 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/work-in-progress/</guid>
      <description>&lt;p&gt;Nothing I publish is perfect.&lt;/p&gt;
&lt;p&gt;Everything I publish is a &amp;ldquo;work in progress&amp;rdquo; to some degree or another.&lt;/p&gt;
&lt;p&gt;I once wrote about &lt;a href=&#34;https://www.andrewcbancroft.com/2014/11/20/agile-blogging/&#34;&gt;Agile Blogging&lt;/a&gt;.  My thought was, &amp;ldquo;What if I could incrementally build up content, rather than have to sit down and write until &lt;em&gt;all&lt;/em&gt; my thoughts were &lt;em&gt;fully&lt;/em&gt; fleshed out and &amp;lsquo;perfect&amp;rsquo;?  What would that look like?&amp;rdquo;&lt;/p&gt;
&lt;p&gt;I took some cues from the &lt;a href=&#34;https://en.wikipedia.org/wiki/Agile_software_development&#34;&gt;Agile software development methodology&lt;/a&gt;, and decided that I&amp;rsquo;d value continual enhancement and gradual build-up of my content, over and above long time commitments and perfection.&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;d focus on rapid delivery of useful information, even if it&amp;rsquo;s only useful to &lt;em&gt;me&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;d make iterative improvements to what I publish, rather than waiting until it was &amp;ldquo;completely done&amp;rdquo; and &amp;ldquo;without flaw&amp;rdquo; (because when would it ever be, anyway??).&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;Sometimes what I pubish is rough.  &lt;em&gt;Really&lt;/em&gt; rough.  Like&amp;hellip; only an outline of the headers, or half-baked in terms of complete thoughts.&lt;/p&gt;
&lt;p&gt;Lest you wonder &amp;ldquo;What&amp;rsquo;s up with this guy??&amp;rdquo;, I&amp;rsquo;ve got this page to give you a heads-up on what&amp;rsquo;s going on.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;When I&amp;rsquo;m actively in the process of improving a particular article&lt;/strong&gt;, you may notice the &amp;ldquo;work in progess&amp;rdquo; notice at the top. It&amp;rsquo;ll land you right here so you know, &amp;ldquo;Oh&amp;hellip; okay&amp;hellip; this isn&amp;rsquo;t &amp;lsquo;finished&amp;rsquo; yet&amp;rdquo;.&lt;/p&gt;
&lt;p&gt;On the flip side, just because I remove the &amp;ldquo;work in progress&amp;rdquo; notice from an article, it doesn&amp;rsquo;t mean my I think my work is perfect.  It just means I&amp;rsquo;ve moved on, and am not &lt;em&gt;actively&lt;/em&gt; working on it.&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;m always open to suggestions for improvement, and &lt;a href=&#34;https://github.com/andrewcbancroft/andrewcbancroft-blog&#34;&gt;since this site is hosted on GitHub&lt;/a&gt;, you&amp;rsquo;re welcome to send pull requests (I&amp;rsquo;d love that)! Fix my spelling; give a better illustration; report a bug; set me straight (kindly, please 😀).&lt;/p&gt;
&lt;p&gt;Now, back to your regularly scheduled reading. 🙌🏻&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Fix SSIS Flat File Destination Error: Cannot open the datafile</title>
      <link>https://www.andrewcbancroft.com/blog/data/sql-server/ssis-flat-file-destination-cannot-open-the-datafile/</link>
      <pubDate>Thu, 01 Feb 2018 00:00:00 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/blog/data/sql-server/ssis-flat-file-destination-cannot-open-the-datafile/</guid>
      <description>&lt;p&gt;This SSIS package execution error can occur for a few reasons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Path to the file does not exist.&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;So for example, if you&amp;rsquo;re writing the file to C:\Some\Folder\somefile.txt, C:\Some\Folder needs to exist &lt;strong&gt;first&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Fix&lt;/strong&gt;:  Create the folder that the package will write to prior to executing it.&lt;/p&gt;
&lt;p&gt;Alternatively, you can add a File System Task with a &lt;code&gt;Create Directory&lt;/code&gt; operation prior to the Flat File Destination component.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;The account executing the package does not have permissions&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Check to make sure whichever account is executing the package has sufficient file system permissions to write the file to disk.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Fix&lt;/strong&gt;:  Grant the appropriate write permissions to the user account that is executing the package. On a server, this would be the SQL Server Agent account that&amp;rsquo;s executing the package.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The file you&amp;rsquo;re writing to is being used by another process&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If some other process is using the file you&amp;rsquo;re writing to, it will block SSIS&amp;rsquo; attempt to write to it simultaneously.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Fix&lt;/strong&gt;:  &lt;strong&gt;In general, make sure that the file is not in use by some other application or process.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Be careful copying and pasting SSIS tasks and components inside of a package &amp;ndash; If you &lt;em&gt;do&lt;/em&gt;, make absolutely sure you&amp;rsquo;ve adjusted each configuration appropriately.  I&amp;rsquo;ve run into this &amp;ldquo;file is being used by another process&amp;rdquo; situation because I copied and pasted an Execute Package task, and forgot to update the config &amp;ndash; attempting ot execute the same package in parrallel can cause issues, apparently!  haha&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Breaking Down Data Analysis vs Data Analytics: A Look at the Suffix</title>
      <link>https://www.andrewcbancroft.com/blog/data/general-data-concepts/data-analysis-vs-data-analytics-examining-suffix/</link>
      <pubDate>Wed, 31 Jan 2018 00:00:00 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/blog/data/general-data-concepts/data-analysis-vs-data-analytics-examining-suffix/</guid>
      <description>&lt;p&gt;&amp;ldquo;Analysis&amp;rdquo; and &amp;ldquo;analytics&amp;rdquo; are extremely similar words&amp;hellip; I mean, just the word-&lt;em&gt;ending&lt;/em&gt; is different:  &amp;ldquo;-sis&amp;rdquo; vs &amp;ldquo;-tics&amp;rdquo;&lt;/p&gt;
&lt;p&gt;Since the end of the words is where the difference lies (otherwise, there wouldn&amp;rsquo;t be two of them, right?), why not go find out what each suffix might indicate to get some clues as to what the difference between the words are?&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;m going to start with &amp;ldquo;analy&lt;strong&gt;sis&lt;/strong&gt;&amp;rdquo;.&lt;/p&gt;
&lt;p&gt;Then I&amp;rsquo;m going to move to &amp;ldquo;analy&lt;strong&gt;tic&lt;/strong&gt;&amp;rdquo;.&lt;/p&gt;
&lt;p&gt;The final stop before I make a summary conclusion will be the word &amp;ldquo;analyt&lt;strong&gt;ics&lt;/strong&gt;&amp;rdquo;.&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s go!&lt;/p&gt;
&lt;hr&gt;
&lt;h3 id=&#34;-sis&#34;&gt;&amp;ldquo;-sis&amp;rdquo;&lt;/h3&gt;
&lt;p&gt;Here&amp;rsquo;s something cool &lt;a href=&#34;http://www.dictionary.com/browse/-sis&#34;&gt;from dictionary.com on the &amp;ldquo;-sis&amp;rdquo; suffix&lt;/a&gt;:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&amp;ldquo;-sis&amp;rdquo; is a suffix that forms abstract nouns of action, process, state, condition, etc. from verbs&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;My interpretation: &amp;ldquo;-sis&amp;rdquo; noun-ifies verbs.  It takes the essential action of a verb and turns it into a concept&amp;hellip; a concept of action, process, state, or condition.&lt;/p&gt;
&lt;p&gt;Come to think of it, &amp;ldquo;analysis&amp;rdquo; is sort of a verb-y noun, isn&amp;rsquo;t it?  &amp;ldquo;We&amp;rsquo;re going to &amp;lsquo;&lt;em&gt;do&lt;/em&gt; data analysis&amp;rsquo;&amp;hellip;&amp;rdquo;, we say.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Do&amp;rdquo; is the verb.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Data&amp;rdquo; is an adjective that tells &amp;ldquo;what kind&amp;rdquo;.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Analysis&amp;rdquo; is the &lt;em&gt;noun&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;Even as a noun though, there&amp;rsquo;s a sense of action involved with data analysis.  It&amp;rsquo;s just captured grammatically in this intangible, conceptual sense of a noun.&lt;/p&gt;
&lt;p&gt;That&amp;rsquo;s because the verb that &amp;ldquo;analysis&amp;rdquo; noun-ifies is &amp;ldquo;analyze&amp;rdquo;, which means &amp;ldquo;to separate into constituent parts or elements&amp;rdquo;.&lt;/p&gt;
&lt;h4 id=&#34;from-the-definition&#34;&gt;From the Definition&lt;/h4&gt;
&lt;p&gt;In dictioary terms, the word &lt;strong&gt;analysis&lt;/strong&gt; encapsulates the idea of breaking something down.&lt;/p&gt;
&lt;p&gt;It&amp;rsquo;s the opposite of &lt;strong&gt;synthesis&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Synthesis is the process of composing separate things together into a new, combined [thing].&lt;/p&gt;
&lt;p&gt;Analysis is the process of taking [something] and &lt;em&gt;de&lt;/em&gt;composing it into its component parts.&lt;/p&gt;
&lt;p&gt;Analysis uses partitioning and dividing and restructuring and rearranging to examine and determine information.&lt;/p&gt;
&lt;p&gt;Applied to data, then, analysis is the process of taking a data set apart, inspecting individual segments at a time, looking for the parts in the whole so that we can understand the information it contains.&lt;/p&gt;
&lt;h3 id=&#34;-tic&#34;&gt;&amp;ldquo;-tic&amp;rdquo;&lt;/h3&gt;
&lt;p&gt;Here&amp;rsquo;s where it gets slightly more interesting. Change &amp;ldquo;analytics&amp;rdquo; to &amp;ldquo;analytic&amp;rdquo; for me real quick&amp;hellip;&lt;/p&gt;
&lt;p&gt;Again, &lt;a href=&#34;http://www.dictionary.com/browse/-tic&#34;&gt;from dictioary.com on the &amp;ldquo;-tic&amp;rdquo; suffix&lt;/a&gt;:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;a suffix forming &lt;em&gt;adjectives&lt;/em&gt; from other parts of speech, &lt;em&gt;especially&lt;/em&gt; in the formation of adjectives from nouns with stems in &amp;ldquo;-sis&amp;rdquo;; equivalent in meaning to &amp;ldquo;-ic&amp;rdquo;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Here&amp;rsquo;s the gist:  If you&amp;rsquo;ve got a &amp;ldquo;-sis&amp;rdquo; noun (like &amp;ldquo;analy&lt;strong&gt;sis&lt;/strong&gt;&amp;rdquo;), you can make a descriptor word out if it by switching the word ending to &amp;ldquo;-tic&amp;rdquo;.  And for all intents and purposes, &amp;ldquo;-tic&amp;rdquo; and &amp;ldquo;-ic&amp;rdquo; do exactly the same thing.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Analysis&lt;/strong&gt;, the &lt;em&gt;noun&lt;/em&gt;, becomes &lt;strong&gt;analytic&lt;/strong&gt;, an adjective.&lt;/p&gt;
&lt;h4 id=&#34;from-the-definition-1&#34;&gt;From the Definition&lt;/h4&gt;
&lt;p&gt;&amp;ldquo;Analytic&amp;rdquo; just means that whatever we&amp;rsquo;re describing &lt;em&gt;has to do with analysis&lt;/em&gt; (breaking something down), as opposed to synthesis (building something up).&lt;/p&gt;
&lt;p&gt;While &amp;ldquo;analytic&amp;rdquo; as a definition isn&amp;rsquo;t extremely interesting (in my opinion), it does act as the bridge in my mind between the &amp;ldquo;-sis&amp;rdquo; suffix and our destiation suffix:  &amp;ldquo;-ics&amp;rdquo;.&lt;/p&gt;
&lt;p&gt;So let&amp;rsquo;s cross the bridge and see where we land&amp;hellip;&lt;/p&gt;
&lt;h3 id=&#34;-ics&#34;&gt;&amp;ldquo;-ics&amp;rdquo;&lt;/h3&gt;
&lt;p&gt;Switch &amp;ldquo;Anlytic&amp;rdquo; (the adjective) back to &amp;ldquo;Analyt&lt;strong&gt;ics&lt;/strong&gt;&amp;rdquo;.&lt;/p&gt;
&lt;p&gt;Here, my friends, is where things get cool (if you&amp;rsquo;re a word nerd).&lt;/p&gt;
&lt;p&gt;&amp;ldquo;-ics&amp;rdquo; is&amp;hellip;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;a suffix of nouns that denote a body of facts, knowledge, principles, etc., &lt;em&gt;usually corresponding to adjectives ending in -ic&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Here we go:  Suppose you&amp;rsquo;ve got an adjective that has the ending &amp;ldquo;-ic&amp;rdquo; (analytic&amp;hellip;which as we saw, basically just means &amp;ldquo;having something to do with analysis&amp;rdquo;).&lt;/p&gt;
&lt;p&gt;If you go from &amp;ldquo;-ic&amp;rdquo; to &amp;ldquo;-ic&lt;strong&gt;s&lt;/strong&gt;&amp;rdquo;, what happens?  You just noun-ified the adjective.&lt;/p&gt;
&lt;p&gt;And &lt;em&gt;with&lt;/em&gt; that noun-ification, you&amp;rsquo;ve brought new connotations to the word.  Suddenly there&amp;rsquo;s a sense of encapsulating a body of facts, knowledge, principles, and so on.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Analytics&lt;/strong&gt; then, denotes the body of facts, knowledge, principles, etc. of &lt;em&gt;something pertaining to&lt;/em&gt;&amp;hellip; &lt;strong&gt;analysis&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;boom&lt;/strong&gt;&lt;/p&gt;
&lt;h3 id=&#34;what-weve-uncovered&#34;&gt;What We&amp;rsquo;ve Uncovered&lt;/h3&gt;
&lt;p&gt;If I had summarize what we&amp;rsquo;ve uncovered, it&amp;rsquo;d be this:  Analytics is the &lt;strong&gt;science of analysis&lt;/strong&gt;.  Analytics encompases the corpus of understanding we have about analysis&amp;hellip;it&amp;rsquo;s the techniques and processes and principles for breaking things down.&lt;/p&gt;
&lt;p&gt;Even though analysis and analytics are both nouns, they aren&amp;rsquo;t &amp;ldquo;the same thing&amp;rdquo;.  The &lt;em&gt;concept&lt;/em&gt; of breaking something down (analysis) is different than the &lt;em&gt;science&lt;/em&gt; or &lt;em&gt;method&lt;/em&gt; for doing that breaking down (analytics).&lt;/p&gt;
&lt;p&gt;Analysis is the end-goal (okay, understanding, interpreting, making inferences and predictions and decisions might be the &lt;em&gt;end&lt;/em&gt;-end goal, but still&amp;hellip;).  Analytics is the way we get there.&lt;/p&gt;
&lt;p&gt;In conclusion,&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Analysis&lt;/strong&gt; is a noun that encompasses the concept and idea of breaking something down.  Data analysis is the concept of taking a data set and reforming it in such a way as to understand it&amp;hellip; to see patterns and form conclusions from it.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Analytic&lt;/strong&gt;, as an adjective, simply means that some &lt;em&gt;other&lt;/em&gt; noun has to do with analysis. We use the term, &amp;ldquo;an analytic &lt;em&gt;approach&lt;/em&gt;&amp;rdquo;, for example&amp;hellip; meaning that &lt;em&gt;the approach&lt;/em&gt; we&amp;rsquo;re taking has to do with breaking something down, decomposing it, shifting it around, etc.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Analytics&lt;/strong&gt; is a noun that shifts the word &amp;ldquo;analytic&amp;rdquo; into a word that denotes the body of facts&amp;hellip; the knowledge about&amp;hellip; the principles for:  &lt;em&gt;analysis&lt;/em&gt;.  Analytics is about &lt;em&gt;methodology&lt;/em&gt;.  It&amp;rsquo;s about the approach to performing analysis.  It&amp;rsquo;s the science of analysis.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
</description>
    </item>
    
    <item>
      <title>Swift 4 Upgrade Error: ‘init(colorLiteralRed:green:blue:alpha:)’ is unavailable</title>
      <link>https://www.andrewcbancroft.com/2018/01/24/swift-4-upgrade-uicolor-init-unavailable/</link>
      <pubDate>Wed, 24 Jan 2018 12:35:04 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2018/01/24/swift-4-upgrade-uicolor-init-unavailable/</guid>
      <description>&lt;p&gt;I upgraded a project to Swift 4 today and hit a compiler error:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;‘init(colorLiteralRed:green:blue:alpha:)&amp;rsquo; is unavailable: This initializer is only meant to be used by color literals.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;The fix is pretty simple: &lt;strong&gt;Don&amp;rsquo;t use that initializer&lt;/strong&gt;! (thank you, Captain Obvious!)&lt;/p&gt;
&lt;p&gt;The correct initializer to use for specifying a red, green, blue, and alpha to get a &lt;code&gt;UIColor&lt;/code&gt; instance in Swift 4 is:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;UIColor(red:green:blue:alpha:)&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;So essentially, just replace &lt;code&gt;colorLiteralRed:&lt;/code&gt;, and replace it with just &lt;code&gt;red:&lt;/code&gt;, and the compiler error will go away. Here&amp;rsquo;s an example of how to call it:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;let color = UIColor(red: 204/255, green: 204/255, blue: 204/255, alpha: 1.0)&lt;/code&gt;&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Where is the SQL Server 2017 Reporting Services License Key??</title>
      <link>https://www.andrewcbancroft.com/blog/data/sql-server/where-is-sql-server-2017-reporting-services-product-key/</link>
      <pubDate>Fri, 19 Jan 2018 00:00:00 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/blog/data/sql-server/where-is-sql-server-2017-reporting-services-product-key/</guid>
      <description>&lt;p&gt;SQL Server 2017 Reporting Services is no longer bundled with the main SQL Server installer, so if you&amp;rsquo;re looking to add the Reporting Services 2017 feature, you need to run the &lt;a href=&#34;https://www.microsoft.com/en-us/download/details.aspx?id=55252&#34;&gt;stand-alone installer&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;When you run said installer, you&amp;rsquo;ll be presented with a dialog asking for a product key.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;ssrs2017-product-key.png&#34; alt=&#34;Prompt for product key&#34;&gt;&lt;/p&gt;
&lt;p&gt;But uhhhh&amp;hellip; Where is it?&lt;/p&gt;
&lt;p&gt;Naturally, we went to our Volume Licensing Portal, but Reporting Services 2017 isn&amp;rsquo;t listed.&lt;/p&gt;
&lt;p&gt;Additionally, we looked for a SQL Server 2017 key, but the portal indicated that our copy of SQL Server &amp;ldquo;didn&amp;rsquo;t require&amp;rdquo; a product key.&lt;/p&gt;
&lt;p&gt;Why?  Because it&amp;rsquo;s &lt;em&gt;embedded&lt;/em&gt; into the SQL Server 2017 installer itself.&lt;/p&gt;
&lt;p&gt;Bingo.&lt;/p&gt;
&lt;h2 id=&#34;re-run-the-sql-server-2017-installer&#34;&gt;Re-run the SQL Server 2017 Installer&lt;/h2&gt;
&lt;p&gt;If you re-run the SQL Server 2017 installer, and just Next Next Next to the &lt;strong&gt;Product Key&lt;/strong&gt; screen, you can copy and paste the key from there.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;sql-server-2017-product-key.png&#34; alt=&#34;Prompt for product key&#34;&gt;&lt;/p&gt;
&lt;p&gt;Simple.  Hopefully this saves you 30 minutes running around to different portals trying to find a key that&amp;rsquo;s &amp;ldquo;hidden&amp;rdquo; right in front of you. 👍&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>What Types of Data Analysis Are There?</title>
      <link>https://www.andrewcbancroft.com/blog/data/general-data-concepts/what-types-of-data-analysis-are-there/</link>
      <pubDate>Thu, 18 Jan 2018 00:00:00 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/blog/data/general-data-concepts/what-types-of-data-analysis-are-there/</guid>
      <description>&lt;h2 id=&#34;questions---data---analysis---answers&#34;&gt;Questions -&amp;gt; Data -&amp;gt; Analysis -&amp;gt; Answers&lt;/h2&gt;
&lt;p&gt;This flow is what it&amp;rsquo;s all about, isn&amp;rsquo;t it?  At the end of the day, you want &lt;em&gt;answers&lt;/em&gt; so that you can take informed action.&lt;/p&gt;
&lt;p&gt;But before you can get &lt;strong&gt;answers&lt;/strong&gt;, you need to do a certain kind of &lt;strong&gt;analysis&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;But before you do &lt;em&gt;analysis&lt;/em&gt;, you need &lt;strong&gt;data&lt;/strong&gt; to perform the analysis on.&lt;/p&gt;
&lt;p&gt;But before you bother with &lt;em&gt;data&lt;/em&gt;, you typically have one or more &lt;strong&gt;questions&lt;/strong&gt; in mind.&lt;/p&gt;
&lt;p&gt;You&amp;rsquo;re curious about the world, your industry, your process. You make observations and you wonder if doing [something] will impact or &lt;em&gt;change&lt;/em&gt; [something else] in a way that would lead to [some goal].&lt;/p&gt;
&lt;p&gt;In order to satisfy your curiosity, you collect &lt;a href=&#34;https://www.andrewcbancroft.com/blog/data/general-data-concepts/what-is-data-like-im-five/&#34;&gt;data&lt;/a&gt; in the form of &lt;a href=&#34;https://www.andrewcbancroft.com/blog/data/general-data-concepts/what-is-data-like-im-five/#quantitative-numerical&#34;&gt;quantitative&lt;/a&gt; and &lt;a href=&#34;https://www.andrewcbancroft.com/blog/data/general-data-concepts/what-is-data-like-im-five/#qualitative-categorical&#34;&gt;qualitative&lt;/a&gt; measurements.&lt;/p&gt;
&lt;p&gt;The data fuels the possibility for analysis, so that hopefully, you walk away with a more complete picture than you had before (ie, you have answers).&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;I recently stumbled upon an article in Science Magazine from March 2015 titled, &amp;ldquo;&lt;a href=&#34;https://www.d.umn.edu/~kgilbert/ened5560-1/The%20Research%20Question-2015-Leek-1314-5.pdf&#34;&gt;What is the Question?&lt;/a&gt;&amp;rdquo;. It&amp;rsquo;s great. It&amp;rsquo;s a short but insightful article that succinctly summarizes the kinds of analysis that can be done on data.&lt;/p&gt;
&lt;p&gt;A &lt;a href=&#34;https://www.coursera.org/learn/managing-data-analysis/lecture/8hYa6/six-types-of-questions&#34;&gt;video summary&lt;/a&gt; is also available at Coursera.&lt;/p&gt;
&lt;h2 id=&#34;how-many-kinds-of-data-analysis-are-there&#34;&gt;How many kinds of data analysis are there?&lt;/h2&gt;
&lt;p&gt;There are at least six ways to categorize a particular kind of analysis that you can do on data:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Descriptive Analysis&lt;/li&gt;
&lt;li&gt;Exploratory Analysis&lt;/li&gt;
&lt;li&gt;Inferential Analysis&lt;/li&gt;
&lt;li&gt;Predictive Analysis&lt;/li&gt;
&lt;li&gt;Causal Analysis&lt;/li&gt;
&lt;li&gt;Mechanistic Analysis&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;img src=&#34;data-analysis-kinds.png&#34; alt=&#34;Kinds of Data Analysis&#34;&gt;&lt;/p&gt;
&lt;h2 id=&#34;how-do-i-know-what-kind-of-analysis-ive-done&#34;&gt;How do I know what kind of analysis I&amp;rsquo;ve done?&lt;/h2&gt;
&lt;p&gt;The authors of the referenced article asked the following questions to determine 1) if you&amp;rsquo;ve done data analysis at all, and 2) if you have, what kind:&lt;/p&gt;
&lt;h3 id=&#34;did-you-summarize-the-data&#34;&gt;&amp;ldquo;Did you summarize the data?&amp;rdquo;&lt;/h3&gt;
&lt;p&gt;&amp;ldquo;Did you summarize the data?&amp;rdquo;, they ask?  If not, then &lt;strong&gt;you didn&amp;rsquo;t do data analysis&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;BUT, if you &lt;em&gt;did&lt;/em&gt; summarize the data, then&amp;hellip;&lt;/p&gt;
&lt;h3 id=&#34;did-you-report-the-summaries-without-interpretation&#34;&gt;&amp;ldquo;Did you report the summaries &lt;em&gt;without interpretation&lt;/em&gt;?&amp;rdquo;&lt;/h3&gt;
&lt;p&gt;If you just gave &amp;rsquo;em the numbers and stopped there, you did &lt;strong&gt;descriptive analysis&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;BUT if you went further and &lt;em&gt;did&lt;/em&gt; interpret the results of those summaries, then&amp;hellip;&lt;/p&gt;
&lt;h3 id=&#34;did-you-quanitify-whether-your-discoveries-are-likely-to-hold-in-a-new-sample&#34;&gt;&amp;ldquo;Did you quanitify whether your discoveries are likely to hold in a &lt;em&gt;new sample&lt;/em&gt;?&amp;rdquo;&lt;/h3&gt;
&lt;p&gt;Assuming you collected data for a sample (a subset of a population), did you compute anything that would give you any insight into whether or not your discoveries about the data are likely to remain true if you went out and randomly sampled again&amp;hellip; and again&amp;hellip; and perhaps again?  If you &lt;em&gt;didn&amp;rsquo;t&lt;/em&gt;, meaning you were satisfied with making interpretations only about your single sample, you did &lt;strong&gt;exploratory analysis&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;BUT, if you &lt;em&gt;did&lt;/em&gt; calculate some statistics that would allow you to go beyond making interpretations about the data set you have in hand, then&amp;hellip;&lt;/p&gt;
&lt;h3 id=&#34;are-you-trying-to-figure-out-how-changing-the-average-of-one-measurement-affects-another&#34;&gt;&amp;ldquo;Are you trying to figure out how changing the average of one measurement affects another?&amp;rdquo;&lt;/h3&gt;
&lt;p&gt;In other words, are you looking for some kind of cause-effect relationship?&lt;/p&gt;
&lt;p&gt;If you&amp;rsquo;re satisfied with making predictions in general, without needing to know the underlying &lt;em&gt;cause&lt;/em&gt; of the relationships found in your analysis, then we&amp;rsquo;re finally at the stage where we can ask one more question:&lt;/p&gt;
&lt;p&gt;&amp;ldquo;&lt;strong&gt;Are you trying to predict measurement(s) for the whole population, or for specific individuals within a population?&lt;/strong&gt;&amp;rdquo;
If you&amp;rsquo;re making measurement predictions for an entire population, you&amp;rsquo;re doing &lt;strong&gt;inferential data analysis&lt;/strong&gt;.  That&amp;rsquo;s what &lt;a href=&#34;https://en.wikipedia.org/wiki/Statistical_inference&#34;&gt;inferential statistics&lt;/a&gt; is all about &amp;ndash; making estimations about populations based on samples.&lt;/p&gt;
&lt;p&gt;If you&amp;rsquo;re making measurement predictions for specific &amp;ldquo;units&amp;rdquo; / &lt;em&gt;individuals&lt;/em&gt;, you&amp;rsquo;re doing &lt;strong&gt;predictive analysis&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Now then, if you &lt;em&gt;did&lt;/em&gt; want to go beyond general predictions and dig down into the level of causation, then&amp;hellip;&lt;/p&gt;
&lt;h3 id=&#34;is-the-effect-you-are-looking-for-an-average-effect-or-a-deterministic-effect&#34;&gt;&amp;ldquo;Is the effect you are looking for an average effect or a deterministic effect?&amp;rdquo;&lt;/h3&gt;
&lt;p&gt;In other words, when you &lt;em&gt;change&lt;/em&gt; one measurement, does it &amp;ldquo;always and exclusively lead to a specific, deterministic behavior in another?&amp;rdquo;  If so, then you&amp;rsquo;re doing &lt;strong&gt;mechanistic data analysis&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;If you&amp;rsquo;re trying to find out what happens to a measurement &lt;em&gt;on average&lt;/em&gt; when another measurement changes, then you&amp;rsquo;re doing &lt;strong&gt;causal analysis&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id=&#34;summarizing-the-kinds-of-data-analysis&#34;&gt;Summarizing the Kinds of Data Analysis&lt;/h2&gt;
&lt;p&gt;&lt;img src=&#34;data-analysis-summary.png&#34; alt=&#34;Kinds of Data Analysis&#34;&gt;&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Search Database Object Definitions for Text</title>
      <link>https://www.andrewcbancroft.com/blog/data/sql-server/search-object-definitions-for-text/</link>
      <pubDate>Tue, 09 Jan 2018 00:00:00 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/blog/data/sql-server/search-object-definitions-for-text/</guid>
      <description>&lt;p&gt;If you need to find some text within a database object definition, this script can be helpful.&lt;/p&gt;
&lt;p&gt;It should allow you to find text within the definitions of the following types of database objects:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Functions (scalar and table-valued)&lt;/li&gt;
&lt;li&gt;Stored Procedures&lt;/li&gt;
&lt;li&gt;Triggers&lt;/li&gt;
&lt;li&gt;Views&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-sql&#34; data-lang=&#34;sql&#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;DECLARE&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;o&#34;&gt;@&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;SearchString&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;VARCHAR&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;255&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;&lt;span class=&#34;w&#34;&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;SET&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;o&#34;&gt;@&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;SearchString&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&amp;#39;TextToFind&amp;#39;&lt;/span&gt;&lt;span class=&#34;w&#34;&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;w&#34;&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;SELECT&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;k&#34;&gt;DISTINCT&lt;/span&gt;&lt;span class=&#34;w&#34;&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;w&#34;&gt;	&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;o&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;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;k&#34;&gt;AS&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;n&#34;&gt;ObjectName&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;&lt;span class=&#34;w&#34;&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;w&#34;&gt;	&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;o&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;type_desc&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;k&#34;&gt;as&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;n&#34;&gt;TypeDescription&lt;/span&gt;&lt;span class=&#34;w&#34;&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;FROM&lt;/span&gt;&lt;span class=&#34;w&#34;&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;w&#34;&gt;	&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;sys&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;sql_modules&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;n&#34;&gt;m&lt;/span&gt;&lt;span class=&#34;w&#34;&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;w&#34;&gt;	&lt;/span&gt;&lt;span class=&#34;k&#34;&gt;INNER&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;k&#34;&gt;JOIN&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;n&#34;&gt;sys&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;objects&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;n&#34;&gt;o&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;k&#34;&gt;ON&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;n&#34;&gt;m&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;object_id&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;n&#34;&gt;o&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;object_id&lt;/span&gt;&lt;span class=&#34;w&#34;&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;WHERE&lt;/span&gt;&lt;span class=&#34;w&#34;&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;w&#34;&gt;	&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;m&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.[&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;definition&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;k&#34;&gt;LIKE&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&amp;#39;%&amp;#39;&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;o&#34;&gt;+&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;o&#34;&gt;@&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;SearchString&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;o&#34;&gt;+&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&amp;#39;%&amp;#39;&lt;/span&gt;&lt;span class=&#34;w&#34;&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;k&#34;&gt;ORDER&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;k&#34;&gt;BY&lt;/span&gt;&lt;span class=&#34;w&#34;&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;w&#34;&gt;	&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;2&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;An alternative script that I&amp;rsquo;ve used for a long time is 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-sql&#34; data-lang=&#34;sql&#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;DECLARE&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;o&#34;&gt;@&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;SearchString&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;VARCHAR&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;255&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;&lt;span class=&#34;w&#34;&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;SET&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;o&#34;&gt;@&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;SearchString&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&amp;#39;TextToFind&amp;#39;&lt;/span&gt;&lt;span class=&#34;w&#34;&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;w&#34;&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;SELECT&lt;/span&gt;&lt;span class=&#34;w&#34;&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;w&#34;&gt;	&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;OBJECT_NAME&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;id&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;),&lt;/span&gt;&lt;span class=&#34;w&#34;&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;w&#34;&gt;	&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;*&lt;/span&gt;&lt;span class=&#34;w&#34;&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;FROM&lt;/span&gt;&lt;span class=&#34;w&#34;&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;w&#34;&gt;	&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;SYSCOMMENTS&lt;/span&gt;&lt;span class=&#34;w&#34;&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;WHERE&lt;/span&gt;&lt;span class=&#34;w&#34;&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;w&#34;&gt;	&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;text&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;k&#34;&gt;LIKE&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&amp;#39;%&amp;#39;&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;o&#34;&gt;+&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;o&#34;&gt;@&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;SearchString&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;o&#34;&gt;+&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&amp;#39;%&amp;#39;&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Source: &lt;a href=&#34;https://stackoverflow.com/questions/674623/how-to-find-a-text-inside-sql-server-procedures-triggers&#34;&gt;Stack Overflow&lt;/a&gt;&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Kill All Sessions</title>
      <link>https://www.andrewcbancroft.com/blog/data/sql-server/kill-all-sessions/</link>
      <pubDate>Tue, 09 Jan 2018 00:00:00 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/blog/data/sql-server/kill-all-sessions/</guid>
      <description>&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-sql&#34; data-lang=&#34;sql&#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;USE&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;n&#34;&gt;master&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt;&lt;span class=&#34;w&#34;&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;GO&lt;/span&gt;&lt;span class=&#34;w&#34;&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;w&#34;&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;ALTER&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;k&#34;&gt;DATABASE&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;DatabaseName&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt;&lt;span class=&#34;w&#34;&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;SET&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;n&#34;&gt;SINGLE_USER&lt;/span&gt;&lt;span class=&#34;w&#34;&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;WITH&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;k&#34;&gt;ROLLBACK&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;k&#34;&gt;IMMEDIATE&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt;&lt;span class=&#34;w&#34;&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;ALTER&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;k&#34;&gt;DATABASE&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;DatabaseName&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt;&lt;span class=&#34;w&#34;&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;SET&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;n&#34;&gt;MULTI_USER&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt;&lt;span class=&#34;w&#34;&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;GO&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</description>
    </item>
    
    <item>
      <title>Get Out of SINGLE_USER Mode</title>
      <link>https://www.andrewcbancroft.com/blog/data/sql-server/get-out-of-single-user-mode/</link>
      <pubDate>Tue, 09 Jan 2018 00:00:00 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/blog/data/sql-server/get-out-of-single-user-mode/</guid>
      <description>&lt;p&gt;If you find yourself stuck in &lt;code&gt;SINGLE_USER&lt;/code&gt; mode in a SQL Server database, do the following:&lt;/p&gt;
&lt;h2 id=&#34;1---find-spids-connected-to-your-database&#34;&gt;1 - Find SPIDs connected to your database&lt;/h2&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-sql&#34; data-lang=&#34;sql&#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;USE&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;n&#34;&gt;master&lt;/span&gt;&lt;span class=&#34;w&#34;&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;GO&lt;/span&gt;&lt;span class=&#34;w&#34;&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;EXEC&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;n&#34;&gt;sp_who&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Note any SPIDs connected to your database, and then&amp;hellip;&lt;/p&gt;
&lt;h2 id=&#34;2---kill-connections&#34;&gt;2 - Kill connections&lt;/h2&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-sql&#34; data-lang=&#34;sql&#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;KILL&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;spid&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;c1&#34;&gt;-- do this for each spid returned by exec sp_who
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&#34;3---set-back-to-multi_user&#34;&gt;3 - Set back to MULTI_USER&lt;/h2&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-sql&#34; data-lang=&#34;sql&#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;USE&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;n&#34;&gt;Master&lt;/span&gt;&lt;span class=&#34;w&#34;&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;ALTER&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;k&#34;&gt;DATABASE&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;n&#34;&gt;DatabaseName&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;k&#34;&gt;SET&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;n&#34;&gt;MULTI_USER&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&#34;plan-b-if-that-doesnt-work&#34;&gt;Plan B (If that doesn&amp;rsquo;t work)&lt;/h2&gt;
&lt;p&gt;If that doesn&amp;rsquo;t work, the following query can be used to find the connection(s):&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-sql&#34; data-lang=&#34;sql&#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;SELECT&lt;/span&gt;&lt;span class=&#34;w&#34;&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;w&#34;&gt;	&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;request_session_id&lt;/span&gt;&lt;span class=&#34;w&#34;&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;FROM&lt;/span&gt;&lt;span class=&#34;w&#34;&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;w&#34;&gt;	&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;sys&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;dm_tran_locks&lt;/span&gt;&lt;span class=&#34;w&#34;&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;WHERE&lt;/span&gt;&lt;span class=&#34;w&#34;&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;w&#34;&gt;	&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;resource_database_id&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;n&#34;&gt;DB_ID&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&amp;#39;DatabaseName&amp;#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Note any spid&amp;rsquo;s that are returned, and then run&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-sql&#34; data-lang=&#34;sql&#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;KILL&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;spid&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;c1&#34;&gt;-- do this for each spid returned by the previous query
&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;w&#34;&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;w&#34;&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;USE&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;n&#34;&gt;Master&lt;/span&gt;&lt;span class=&#34;w&#34;&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;ALTER&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;k&#34;&gt;DATABASE&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;n&#34;&gt;YourDatabase&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;k&#34;&gt;SET&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;n&#34;&gt;MULTI_USER&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Source:  &lt;a href=&#34;https://stackoverflow.com/questions/24608702/sql-server-2008-r2-stuck-in-single-user-mode&#34;&gt;Stack Overflow&lt;/a&gt;&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>What is a Dimensional Model?</title>
      <link>https://www.andrewcbancroft.com/blog/data/dimensional-modeling/what-is-a-dimensional-model/</link>
      <pubDate>Tue, 26 Dec 2017 00:00:00 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/blog/data/dimensional-modeling/what-is-a-dimensional-model/</guid>
      <description>&lt;h2 id=&#34;measurements-and-descriptions&#34;&gt;Measurements and Descriptions&lt;/h2&gt;
&lt;p&gt;There are endless ways to measure and describe the world around us.&lt;/p&gt;
&lt;p&gt;When you boil &lt;a href=&#34;https://www.andrewcbancroft.com/blog/data/general-data-concepts/what-is-data-like-im-five/&#34;&gt;data&lt;/a&gt; down to the essentials, what you get are&amp;hellip;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Measurement(s)&lt;/li&gt;
&lt;li&gt;Descriptions of the measurement(s)&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;It doesn&amp;rsquo;t matter what industry you&amp;rsquo;re in. It doesn&amp;rsquo;t matter the process that captured the data you&amp;rsquo;re looking at. At the end of the day, you&amp;rsquo;ve got &lt;em&gt;&lt;a href=&#34;https://www.andrewcbancroft.com/blog/data/general-data-concepts/what-is-data-like-im-five/#quantitative-numerical&#34;&gt;numbers&lt;/a&gt; and &lt;a href=&#34;https://www.andrewcbancroft.com/blog/data/general-data-concepts/what-is-data-like-im-five/#qualitative-categorical&#34;&gt;classifications&lt;/a&gt;&lt;/em&gt; as measurements, and you&amp;rsquo;ve got the &lt;em&gt;context&lt;/em&gt; describing the &lt;em&gt;who, what, when, where, why, and how&lt;/em&gt; of those measurements.&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id=&#34;dimensional-model---breaking-it-down&#34;&gt;&amp;ldquo;Dimensional Model&amp;rdquo; - Breaking it Down&lt;/h2&gt;
&lt;p&gt;Let&amp;rsquo;s unpack the term &amp;ldquo;dimensional model&amp;rdquo; to see if we can make heads of tails of what the words mean when they&amp;rsquo;re put together in this term.&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;ll start with the noun (model) and then look at the adjective (dimensional).&lt;/p&gt;
&lt;h3 id=&#34;model&#34;&gt;Model&lt;/h3&gt;
&lt;p&gt;What&amp;rsquo;s a &lt;em&gt;model&lt;/em&gt;?&lt;/p&gt;
&lt;p&gt;The first images that come to mind for me involve cars, trains, and rocket ships.  When you think about the hobby of model-making, what you have is an activity where you take [something] (like a car, train, or rocket) and create [something else] (the model) that &lt;em&gt;represents&lt;/em&gt; the original.&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s keep that word:  &lt;strong&gt;represent&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Another image that comes to mind is a &lt;em&gt;person&lt;/em&gt; who is a model. You think about the man or woman walking down the runway, showing off what a particular designer&amp;rsquo;s outfit should look like on an &amp;ldquo;ideal&amp;rdquo; person.&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s keep that word too:  &lt;strong&gt;ideal&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The world is complicated. Sometimes, in order to shut out the noise, we create something that portrays the &lt;em&gt;general concept&lt;/em&gt; of something that would otherwise be difficult to understand.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;General concept&lt;/strong&gt; is the last word we&amp;rsquo;ll keep for now.&lt;/p&gt;
&lt;p&gt;So what do we have?&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;A model is a &lt;strong&gt;representation&lt;/strong&gt; of data that portrays its &lt;strong&gt;general concept(s)&lt;/strong&gt;, in a way that is &lt;strong&gt;ideal&lt;/strong&gt; for understanding the patterns and information that the data contains.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;a name=&#34;kind-of-model&#34;&gt;&lt;/a&gt;
That last part, &amp;ldquo;ideal for understanding the patterns and information that the data contains&amp;rdquo; leaves the door open to the possibility that there might be different &lt;em&gt;kinds&lt;/em&gt; of models that are more or less ideal for different analyses.&lt;/p&gt;
&lt;p&gt;The first half remains true to all models.  Models are representations of data.  And models boil things down to the general concept(s) that exist within the data. The goal for models is to help humans see, understand, and interpret the patterns and information within data.&lt;/p&gt;
&lt;h3 id=&#34;dimensional&#34;&gt;Dimensional&lt;/h3&gt;
&lt;p&gt;What does the word &amp;ldquo;dimension&amp;rdquo; being to mind for you?&lt;/p&gt;
&lt;p&gt;I immediately thought of shapes (2 &lt;em&gt;dimensional&lt;/em&gt; or 3 &lt;em&gt;dimensional&lt;/em&gt; shapes).&lt;/p&gt;
&lt;p&gt;In a 2D shape, the dimensions are length and width, aren&amp;rsquo;t they?  A square or rectangle has sides that are [x] units long by [y] units wide.  3D shapes add a dimension:  depth.&lt;/p&gt;
&lt;p&gt;In the case of shapes, a dimension is [the thing] that answers the question &amp;ldquo;how long?&amp;rdquo; or &amp;ldquo;how wide?&amp;rdquo; or &amp;ldquo;how deep?&amp;rdquo;.&lt;/p&gt;
&lt;p&gt;Outside the world of geometry, the word &amp;ldquo;dimension&amp;rdquo; carries the idea of the &lt;strong&gt;characteristics&lt;/strong&gt; or &lt;strong&gt;qualities&lt;/strong&gt; of something.&lt;/p&gt;
&lt;p&gt;If I say, &amp;ldquo;When I look at the diamond &lt;strong&gt;from this angle&lt;/strong&gt;, it explodes with brilliance!&amp;rdquo;, I&amp;rsquo;m substituting &amp;ldquo;from this angle&amp;rdquo; for a way to describe the diamond&amp;rsquo;s &lt;strong&gt;facets&lt;/strong&gt;&amp;hellip; its &lt;strong&gt;dimensionality&lt;/strong&gt;, if you will.&lt;/p&gt;
&lt;p&gt;If I say, &amp;ldquo;Wow, when you add jalapeños to the salsa, it adds a whole new &lt;strong&gt;dimension&lt;/strong&gt; of flavor!&amp;rdquo;, you see how the word dimension gets at an essential &lt;strong&gt;feature&lt;/strong&gt; of the salsa&amp;rsquo;s flavor.&lt;/p&gt;
&lt;p&gt;Coffee from different regions of the world have distinct flavor &lt;strong&gt;profile&lt;/strong&gt;s - different &lt;em&gt;dimensions&lt;/em&gt; to their taste.&lt;/p&gt;
&lt;p&gt;The same goes with wines and cheeses and teas and on and on with just about anything you can think of.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;The word dimensional tells us that the &lt;strong&gt;characteristics&lt;/strong&gt;, &lt;strong&gt;qualities&lt;/strong&gt;, &lt;strong&gt;features&lt;/strong&gt;, and &lt;strong&gt;facets&lt;/strong&gt; of something are what&amp;rsquo;s important.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3 id=&#34;dimensions-of-what&#34;&gt;Dimensions of What??&lt;/h3&gt;
&lt;p&gt;So we&amp;rsquo;ve got a representation of data (a model).
We&amp;rsquo;ve got characteristics&amp;hellip;qualities&amp;hellip;features&amp;hellip;facets&amp;hellip; (dimensions).&lt;/p&gt;
&lt;p&gt;The question now becomes&amp;hellip; dimensions of&amp;hellip; &lt;em&gt;what&lt;/em&gt;?? The answer gives us the distinguishing characteristic of dimensional models that makes them ideal for understanding the patterns and information that the data contains.&lt;/p&gt;
&lt;h3 id=&#34;the-defining-characteristic-of-a-dimensional-model&#34;&gt;The Defining Characteristic of a Dimensional Model&lt;/h3&gt;
&lt;p&gt;The defining characteristic of a dimensional model is that it captures and highlights the characteristics, qualities, features, and facets of &lt;em&gt;the event that generated the measurements&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;Dimensional models make the numbers visible, true&amp;hellip; but they also make a point to bring &lt;em&gt;meaning&lt;/em&gt; to the numbers by making the who, what, when, where, why, and how &lt;em&gt;behind the generation of those numbers&lt;/em&gt; into the forefront.&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://www.andrewcbancroft.com/blog/data/general-data-concepts/what-is-data-like-im-five/#qualitative-categorical&#34;&gt;Qualitative data&lt;/a&gt;, in and of itself, is descriptive by nature. It&amp;rsquo;s the qualitative data &lt;em&gt;about the situation that caused a measurement to be recorded&lt;/em&gt; that gives dimensional models their &amp;ldquo;ideal&amp;rdquo; edge when it comes to understanding the patterns and information that the data contains.&lt;/p&gt;
&lt;p&gt;There are a few things that have to happen for that to be possible:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;You&amp;rsquo;ve got to &lt;strong&gt;define a &amp;ldquo;measurement event&amp;rdquo;&lt;/strong&gt; &amp;ndash; the action or process that causes you to capture and save data in the first place&lt;/li&gt;
&lt;li&gt;You&amp;rsquo;ve got to &lt;strong&gt;detect when that event has occurred&lt;/strong&gt;, either physically or in some automated way (like with a sensor)&lt;/li&gt;
&lt;li&gt;You&amp;rsquo;ve got to capture and save the measurements that matter, &lt;strong&gt;and&lt;/strong&gt; the 5 W&amp;rsquo;s + How + any other qualitative information that you think are important to describing that event&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&#34;putting-it-all-together--dimensional--model&#34;&gt;Putting it all Together:  &amp;ldquo;Dimensional&amp;rdquo; + &amp;ldquo;Model&amp;rdquo;&lt;/h3&gt;
&lt;p&gt;&amp;ldquo;Model&amp;rdquo; is the noun; &amp;ldquo;dimensional&amp;rdquo; is the adjective.  When you put them together, you might get something like this:&lt;/p&gt;
&lt;p&gt;A dimensional model is a &lt;strong&gt;representation of data&lt;/strong&gt; that&amp;hellip;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Portrays its measurements&lt;/strong&gt; in a way that highlights the &lt;strong&gt;characteristics&lt;/strong&gt;, &lt;strong&gt;qualities&lt;/strong&gt;, &lt;strong&gt;features&lt;/strong&gt;, and &lt;strong&gt;facets&lt;/strong&gt; of the event that generated the measurements (the who, what, when, where, how, of the data collection situation)&lt;/li&gt;
&lt;li&gt;Therefore, a dimensional model is &lt;strong&gt;ideal for understanding the patterns and information that the data contains&lt;/strong&gt; in a way that is widely approachable by analysts of all kinds&lt;/li&gt;
&lt;/ul&gt;
</description>
    </item>
    
    <item>
      <title>Stages to Machine Learning</title>
      <link>https://www.andrewcbancroft.com/blog/data/machine-learning/stages-to-machine-learning/</link>
      <pubDate>Sun, 17 Dec 2017 00:00:00 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/blog/data/machine-learning/stages-to-machine-learning/</guid>
      <description>&lt;p&gt;&lt;img src=&#34;one-does-not-simply-machine-learn.png&#34; alt=&#34;&#34;&gt;&lt;/p&gt;
&lt;p&gt;If one does not simply&amp;hellip; &amp;ldquo;machine learn&amp;rdquo;&amp;hellip; what &lt;em&gt;does&lt;/em&gt; one do to cover all the bases and make sure that the process is complete and value-producing?&lt;/p&gt;
&lt;p&gt;Here 10 high level stages to check off as you take on a machine learning task.&lt;/p&gt;
&lt;h2 id=&#34;business-metric&#34;&gt;Business Metric&lt;/h2&gt;
&lt;p&gt;Before you do &lt;em&gt;anything&lt;/em&gt; with machine learning, you absolutely &lt;em&gt;must&lt;/em&gt; have a good undertanding of &lt;strong&gt;which business metric we&amp;rsquo;re hoping to improve&lt;/strong&gt; as a result of employing machine learning.&lt;/p&gt;
&lt;p&gt;Otherwise, how will you know if it &amp;ldquo;worked&amp;rdquo;?&lt;/p&gt;
&lt;p&gt;Talk with management.  Find out their goals.  Hear the story and understand what they&amp;rsquo;re wanting to see improvement in, &lt;strong&gt;in terms of the business&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Try not to get a high level answer like, &amp;ldquo;We want to see an increase in sales.&amp;rdquo; Okay.  Well.  Don&amp;rsquo;t we all?&lt;/p&gt;
&lt;p&gt;The more specific and defined management can get with their business metric of choice, the better.  &amp;ldquo;We want to see a 10% increase in sales for [some product] among customers to whom we advertise directly regarding an exclusive discount on [that product].&amp;rdquo;  It&amp;rsquo;s much more specific, measurable, targeted, and well-defined than the first &amp;ldquo;goal&amp;rdquo;, isn&amp;rsquo;t it?  With this statement, we could work toward building a machine learning model to classify customers who are more likely to respond positively to our advertising.  At the end, we can look back to the customers we predicted on, and see if a difference 1) exists and 2) can reasonably be attributed to the work of our machine learning model.&lt;/p&gt;
&lt;h2 id=&#34;blend&#34;&gt;Blend&lt;/h2&gt;
&lt;p&gt;&lt;em&gt;More data&lt;/em&gt;.  That always seems to be the answer to our predictive woes, doesn&amp;rsquo;t it?&lt;/p&gt;
&lt;p&gt;Two data sets augmenting one another lend more knowledge for machine learning algorithms to latch on to and figure out patterns within.&lt;/p&gt;
&lt;p&gt;You could use the word &amp;ldquo;integrate&amp;rdquo; here if you want.  The point is that you&amp;rsquo;re gathering data from multiple sources to help give the most complete picture of what you&amp;rsquo;re analyzing and predicting.&lt;/p&gt;
&lt;h2 id=&#34;explore&#34;&gt;Explore&lt;/h2&gt;
&lt;p&gt;To make sense of the data you&amp;rsquo;ve got, especially if you didn&amp;rsquo;t generate it in the first place, you need an exploration process built in to your machine learning pipeline.&lt;/p&gt;
&lt;p&gt;Profile your data &amp;ndash; find out the good parts and the bad (dirty) parts, such as missing values, inconsistent labels, etc.&lt;/p&gt;
&lt;p&gt;Compute summary statistics.&lt;/p&gt;
&lt;p&gt;Visualize your data set by plotting the variables against one another to gain intuition about how the variables relate.&lt;/p&gt;
&lt;p&gt;Use multiple kinds of visualizations to get a good feel for the &amp;ldquo;shape&amp;rdquo; of your data.&lt;/p&gt;
&lt;h2 id=&#34;clean&#34;&gt;Clean&lt;/h2&gt;
&lt;p&gt;Data is messy (pretty much always).&lt;/p&gt;
&lt;p&gt;Somewhere in your machine learning pipeline, you&amp;rsquo;ve got to have a cleaning step where you fill in missing values, correct misspellings, etc.&lt;/p&gt;
&lt;p&gt;Do the work to clean your data so that the predictions generated by machine learning are optimal.&lt;/p&gt;
&lt;h2 id=&#34;transform&#34;&gt;Transform&lt;/h2&gt;
&lt;p&gt;Data is in constant need of transformation, especially when it comes from multiple sources.&lt;/p&gt;
&lt;p&gt;In order to get things consistent and clean, you often need to transform the values of your variables so that they&amp;rsquo;re in conformity with one another.&lt;/p&gt;
&lt;p&gt;For example, suppose that two data sets contain a column named &amp;ldquo;State&amp;rdquo;.  In the first, the values look like this:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;OK&lt;/li&gt;
&lt;li&gt;NY&lt;/li&gt;
&lt;li&gt;FL&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In the second, the values look like this:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Oklahoma&lt;/li&gt;
&lt;li&gt;New York&lt;/li&gt;
&lt;li&gt;Florida&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For conformity and consistency&amp;rsquo;s sake, you need to pick a format (either the abbreviation, or the name) and transform the &lt;em&gt;other&lt;/em&gt; into that standardized format.&lt;/p&gt;
&lt;p&gt;Another example:  Suppose that two data sets contain weight measurements.  In the first, the units of measure are in pounds, and in the second, the units are in kilograms.  Again, a transformation is needed: Pick the standard unit you want to measure things in across your data sets, and transform the one(s) that aren&amp;rsquo;t in conformity to that standard.&lt;/p&gt;
&lt;h2 id=&#34;engineer-features&#34;&gt;Engineer Features&lt;/h2&gt;
&lt;p&gt;Akin to transformation, there are times when additional features (predictor variables, columns) can be created, or &lt;em&gt;engineered&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;At times, you&amp;rsquo;ll use the existing features in the data set to make a calculation &amp;ndash; the calculation can be added to the overall data set as a new feature for the machine learning algorithm(s) you choose.&lt;/p&gt;
&lt;p&gt;Other times, you may bring in a supplemental data set, blend it, explore it, clean it, transform it, and then use its variables as new engineered features.&lt;/p&gt;
&lt;h2 id=&#34;select-features&#34;&gt;Select Features&lt;/h2&gt;
&lt;p&gt;Not all features are created equal.&lt;/p&gt;
&lt;p&gt;Meaningless features should be dropped before moving on to the final stages of machine learning.  &amp;ldquo;Meaningless&amp;rdquo; is relative, but in general, anything that doesn&amp;rsquo;t seem to help with informing or influencing the outcome to your predictive problem should be left out.&lt;/p&gt;
&lt;p&gt;You can (and should) experiment with this, of course.  But at some point, you&amp;rsquo;ve got to &lt;strong&gt;pick the features that matter&lt;/strong&gt;.&lt;/p&gt;
&lt;h2 id=&#34;train-model&#34;&gt;Train Model&lt;/h2&gt;
&lt;p&gt;To &amp;ldquo;machine learn&amp;rdquo;, one needs to select a machine learning algorithm that is reasonable for the predictive problem at hand.&lt;/p&gt;
&lt;p&gt;Once a machine learning algorithm is chosen, the selected features of the cleaned, transformed data sets are used to train a model.&lt;/p&gt;
&lt;p&gt;When this stage is done, you have a model (usually a mathematical equation or function that can take inputs, follow the patterns discovered as the algorithm trained on the data, and produce ann output in the end).&lt;/p&gt;
&lt;h2 id=&#34;evaluate&#34;&gt;Evaluate&lt;/h2&gt;
&lt;p&gt;How well does the trained model perform?  To know, you need to evaluate the outputs.&lt;/p&gt;
&lt;p&gt;Your metric for evaluation varies depending on the predictive problem you&amp;rsquo;ve got on your hands.  You may be interested in maximizing
&lt;strong&gt;Classification&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;https://en.wikipedia.org/wiki/Accuracy_and_precision&#34;&gt;Classification Accuracy&lt;/a&gt;?&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://www.kaggle.com/wiki/LogLoss&#34;&gt;Logarithmic Loss&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://en.wikipedia.org/wiki/Receiver_operating_characteristic&#34;&gt;Area under ROC Curve&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Regression&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;https://en.wikipedia.org/wiki/Mean_absolute_error&#34;&gt;Mean Absolute Error&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://en.wikipedia.org/wiki/Mean_squared_error&#34;&gt;Mean Squared Error&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://en.wikipedia.org/wiki/Coefficient_of_determination&#34;&gt;R-Squared (R^2)&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;deploy&#34;&gt;Deploy&lt;/h2&gt;
&lt;p&gt;A machine learning model doesn&amp;rsquo;t do much good until it&amp;rsquo;s deployed, does it?&lt;/p&gt;
&lt;p&gt;Get it out there, and let it start creating value by proiding actionable insight.&lt;/p&gt;
&lt;h2 id=&#34;verify&#34;&gt;Verify&lt;/h2&gt;
&lt;p&gt;It starts and ends with your business&amp;hellip;your organizational metrics that you or your company&amp;rsquo;s leadership are trying to improve.&lt;/p&gt;
&lt;p&gt;Once the model is producing predictions and leaders are taking action on those predictions, you need to check back to that &lt;strong&gt;original business metric&lt;/strong&gt; that you defined in stage zero, to see if the model is working or not.&lt;/p&gt;
&lt;p&gt;If it is, great!
If it&amp;rsquo;s not, shut it down!&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;In both cases&lt;/strong&gt;, the final stage is:&lt;br&gt;
&lt;em&gt;Rinse and repeat.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Do it all again, either for the purpose of improving your existing model, or building an entirely new one.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Patterns and Key Words of Predictive Analytics Questions</title>
      <link>https://www.andrewcbancroft.com/blog/data/general-data-concepts/patterns-and-key-words-of-predictive-analytics-questions/</link>
      <pubDate>Sun, 17 Dec 2017 00:00:00 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/blog/data/general-data-concepts/patterns-and-key-words-of-predictive-analytics-questions/</guid>
      <description>&lt;p&gt;Not all questions are &amp;ldquo;predictive analytics questions&amp;rdquo;.  That is to say, not every question you receive as a data science practitioner requires predictive analytics to answer.&lt;/p&gt;
&lt;p&gt;So how do you know if someone is asking a question that&amp;rsquo;s a good fit for using predictive analytics to provide the answer?&lt;/p&gt;
&lt;h1 id=&#34;patterns-and-key-words&#34;&gt;Patterns and Key Words&lt;/h1&gt;
&lt;p&gt;From a recent read through &lt;a href=&#34;https://www.amazon.com/Predictive-Analytics-Power-Predict-Click-ebook/dp/B019HR9X4U/ref=sr_1_1?ie=UTF8&amp;amp;qid=1513540111&amp;amp;sr=8-1&amp;amp;keywords=predictive+analytics&#34;&gt;Predictive Analytics&lt;/a&gt;, I jotted down a few patterns that emerged as each application of predictive analytics was presented.  Apendix B in the book is an excellent summary of twenty predictive analytics applications across different industries.&lt;/p&gt;
&lt;p&gt;First, let&amp;rsquo;s take a look at a few examples of predictive questions.  Then let&amp;rsquo;s see if we can find the pattern or identify some key words.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&amp;ldquo;Which customers will respond to marketing contact?&amp;rdquo;&lt;/li&gt;
&lt;li&gt;&amp;ldquo;Which ad is a customer most likely to click?&amp;rdquo;&lt;/li&gt;
&lt;li&gt;&amp;ldquo;Which employees will quit?&amp;rdquo;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;When you hear people asking questions like, &amp;ldquo;Which [customer/ad/employees] will [respond/click/quit]&amp;rdquo;, it should trigger &amp;ldquo;predictive analytics&amp;rdquo; in your mind as a reasonable way to approach getting the answer to the question.&lt;/p&gt;
&lt;h2 id=&#34;key-words-to-listen-for-are&#34;&gt;Key words to listen for are:&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Which [noun]&lt;/strong&gt; &amp;ndash; The questioner is wanting to narrow things down to a particular set&amp;hellip; &amp;ldquo;nouns of interest&amp;rdquo;, if you will.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;em&gt;Will&lt;/em&gt;&lt;/strong&gt; &amp;ndash; There&amp;rsquo;s a future-focus to the question.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Likely&lt;/strong&gt; &amp;ndash; When probability enters the vocabulary of the question, there&amp;rsquo;s a chance that predictive analytics can help with the answer.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;[Take action]&lt;/strong&gt; &amp;ndash; Fill in [take action] with just about any verb of importance.  When you hear someone asking whether someone/something will respond, or click, or quit, or [you name it], predictive analytics could be a tool that helps shed some light on the answer.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Here are a few more:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&amp;ldquo;Is this e-mail spam or not?&amp;rdquo;&lt;/li&gt;
&lt;li&gt;&amp;ldquo;Will the stock go up or down?&amp;rdquo;&lt;/li&gt;
&lt;li&gt;&amp;ldquo;Will the player win or lose?&amp;rdquo;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;key-words-to-listen-for-are-1&#34;&gt;Key words to listen for are:&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&amp;quot;___ or ___&amp;quot;&lt;/strong&gt; &amp;ndash; in each of the questions, the answer is in one of a few &lt;em&gt;categories&lt;/em&gt;:  [is/is not], [up/down], [win/lose].  There need not be only two categories. The point is that any time the questioner is attempting ot figure out whether [something] fits in one category or another, predictive analytics could lend a hand in sorting things out.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;[final outcome]&lt;/strong&gt; &amp;ndash; Many times, questioners are wanting to know the probable final outcome of some event that hasn&amp;rsquo;t occurred yet.  When that&amp;rsquo;s the case, let predictive analytics be a tool you use for giving insight into the answer.&lt;/li&gt;
&lt;/ul&gt;
</description>
    </item>
    
    <item>
      <title>What is Data? (Like I&#39;m 5)</title>
      <link>https://www.andrewcbancroft.com/blog/data/general-data-concepts/what-is-data-like-im-five/</link>
      <pubDate>Sat, 16 Dec 2017 00:00:00 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/blog/data/general-data-concepts/what-is-data-like-im-five/</guid>
      <description>&lt;p&gt;“Daddy, what is your job? What do you &lt;em&gt;do&lt;/em&gt; there??”, asks my 5 year old in a curious tone as he tries to figure out why I went to &lt;em&gt;work&lt;/em&gt; today instead of staying home to play with &lt;em&gt;him&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;“Weeeell&amp;hellip;” (not a confident start to my response, I know&amp;hellip;).&lt;/p&gt;
&lt;p&gt;“Iiiiii&amp;hellip;help people with &lt;em&gt;data&lt;/em&gt;.” 😬&lt;/p&gt;
&lt;p&gt;“&lt;em&gt;Data&lt;/em&gt;?  What’s &lt;em&gt;data&lt;/em&gt;??”&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;[Think, Andrew! Think! &amp;hellip; Iiit&amp;rsquo;s the&amp;hellip; recorded facts (numbers and such) that we use to interperet reality&amp;hellip; no&amp;hellip; sratch that.  It&amp;rsquo;s the observations we made about a process or event that we bothered to write in our notebook or store in our computer&amp;hellip; man, this is &lt;em&gt;hard&lt;/em&gt;&amp;hellip;]&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Ugh. 😞&lt;/p&gt;
&lt;p&gt;“Hey!  Wanna go ride bikes??”&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;Yeah, I know.  Lame.&lt;/p&gt;
&lt;p&gt;But it got me thinking.  How &lt;em&gt;could&lt;/em&gt; I have explained data to my 5 year old?&lt;/p&gt;
&lt;p&gt;Better yet, could that explanation be valuable to someone &lt;em&gt;older&lt;/em&gt; than a 5 year old?&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id=&#34;data&#34;&gt;Data&lt;/h2&gt;
&lt;p&gt;Data.  Where do I start?&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s take a walk&amp;hellip;&lt;/p&gt;
&lt;p&gt;Imagine with me that we&amp;rsquo;re on a bug hunt (c&amp;rsquo;mon, you&amp;rsquo;re 5, remember?). We&amp;rsquo;ve just unearthed a spectacular rock.  What&amp;rsquo;s underneath?  &lt;em&gt;A mother lode of bugs&lt;/em&gt;, of course!&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Quick!&amp;rdquo;, I say, &amp;ldquo;Let&amp;rsquo;s get out our handy dandy &lt;em&gt;notebook&lt;/em&gt;!&amp;rdquo; (as I channel &lt;a href=&#34;https://en.wikipedia.org/wiki/Blue%27s_Clues&#34;&gt;Blues Clues&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;In the notebook, we write down a bunch of things:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;names&lt;/strong&gt; of each kind of bug we saw&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;colors&lt;/strong&gt; of each one&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;How fast&lt;/strong&gt; (slow, medium, speedy) each one moved&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;How many&lt;/strong&gt; we counted&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;We snag a few bugs to take back to &amp;ldquo;the lab&amp;rdquo; (the dining room, of course) to inspect more deelpy.&lt;/p&gt;
&lt;p&gt;On our &amp;ldquo;lab bench&amp;rdquo; (the dining room table, of course), we pick a beetle, and try to hold him down so he can&amp;rsquo;t move.  That way we can take a few more measurements for our notebook:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;How heavy&lt;/strong&gt; it is&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Number of&lt;/strong&gt; legs&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;How long&lt;/strong&gt; the beetle is&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;How wide&lt;/strong&gt; it is&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;We bust out the magnifying glass and observe a bit more:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;It has &lt;em&gt;wings&lt;/em&gt; (physical trait)&lt;/li&gt;
&lt;li&gt;It has &lt;em&gt;antennas&lt;/em&gt; (another physical trait)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&amp;ldquo;All right, beetle back in the bug box!  Next specimen!&amp;rdquo;&lt;/p&gt;
&lt;p&gt;This one&amp;rsquo;s a rolly polly. Same routine:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;How heavy&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Number of&lt;/strong&gt; legs (asking a rolly polly to be still so we can count is unrealistic, so we took a picture and counted what we saw in the photo)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;How long&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;How wide&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Again with the magnifying glass:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;No wings on this one&lt;/li&gt;
&lt;li&gt;Has antennas though!&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;p&gt;Here&amp;rsquo;s the point of this riveting adventure:  Everything we wrote down in the notebook? &lt;strong&gt;Data&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;We looked at something we saw in the world and jotted down what we noticed.&lt;/p&gt;
&lt;p&gt;We put in to &lt;em&gt;words&lt;/em&gt; or measured in &lt;em&gt;numbers&lt;/em&gt; what we noticed about&amp;hellip;bugs, in this case.&lt;/p&gt;
&lt;p&gt;Names (kinds, species), color, slow/medium/fast.  Has wings (or not), Has antennas (or not).&lt;/p&gt;
&lt;p&gt;How many bugs total, how heavy, how many legs on each, how long, how wide.&lt;/p&gt;
&lt;p&gt;Each word and each number that we bothered to write down is &lt;strong&gt;data&lt;/strong&gt;.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;Data is &lt;em&gt;everywhere&lt;/em&gt;&amp;hellip;at least, the &lt;em&gt;potential&lt;/em&gt; for there to be data is everywhere.&lt;/p&gt;
&lt;p&gt;I suppose it&amp;rsquo;s not actually data until it&amp;rsquo;s &amp;ldquo;captured&amp;rdquo; and saved so that we can look at it again later.&lt;/p&gt;
&lt;p&gt;Anytime something happens in the world and we succeed at saving a &lt;em&gt;description&lt;/em&gt; that event, &lt;strong&gt;what we saved is data&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;I think we&amp;rsquo;re making some progress!&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id=&#34;two-kinds-of-data&#34;&gt;Two Kinds of Data&lt;/h2&gt;
&lt;p&gt;I wonder if you noticed the way I grouped a few of the observations we made about the bugs.&lt;/p&gt;
&lt;p&gt;Sometimes we used &lt;em&gt;words&lt;/em&gt; to describe what we saw: &amp;ldquo;Black&amp;rdquo;, &amp;ldquo;Red&amp;rdquo;, &amp;ldquo;Has Antennas&amp;rdquo;, &amp;ldquo;Slow&amp;rdquo;.&lt;/p&gt;
&lt;p&gt;Other times we used &lt;em&gt;numbers&lt;/em&gt;: &amp;ldquo;1&amp;rdquo; (gram), &amp;ldquo;29.2&amp;rdquo; (millimeters), &amp;ldquo;100&amp;rdquo; (legs)&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s use some words split up the two kinds:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Quantitative (Numerical)&lt;/li&gt;
&lt;li&gt;Qualitative (Categorical)&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;quantitative-numerical&#34;&gt;Quantitative (Numerical)&lt;/h3&gt;
&lt;p&gt;&lt;a name=&#34;quantitative-data&#34;&gt;&lt;/a&gt;Quantitative/numerical data describe things we see about the world in terms of &lt;strong&gt;numbers&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Numerical&amp;rdquo; and &amp;ldquo;number&amp;rdquo; go together pretty well. It&amp;rsquo;s that &amp;ldquo;Q&amp;rdquo; word, &amp;ldquo;quantitative&amp;rdquo;, that gives people trouble sometimes.&lt;/p&gt;
&lt;p&gt;If you listen for the word &amp;ldquo;&lt;strong&gt;quant&lt;/strong&gt;ity&amp;rdquo; in the word &amp;ldquo;&lt;strong&gt;quant&lt;/strong&gt;itative&amp;rdquo;, that could give you the hint for putting the term together with numbers.&lt;/p&gt;
&lt;p&gt;In general, quantitative/numerical data describes &amp;ldquo;how much-ness&amp;rdquo; or &amp;ldquo;how many-ness&amp;rdquo; of something we notice about the world.&lt;/p&gt;
&lt;p&gt;If you can measure it, count it, weigh it&amp;hellip; if you can &lt;strong&gt;add up&lt;/strong&gt; the things you wrote down, or &lt;strong&gt;do other &amp;ldquo;math-y&amp;rdquo; things&lt;/strong&gt; to the data, then you&amp;rsquo;ve got quantitative/numerical data.&lt;/p&gt;
&lt;h3 id=&#34;qualitative-categorical&#34;&gt;Qualitative (Categorical)&lt;/h3&gt;
&lt;p&gt;&lt;a name=&#34;qualitative-data&#34;&gt;&lt;/a&gt;Qualitative and categorical mean the same thing, but it might not be clear &lt;em&gt;what&lt;/em&gt; they mean right away.&lt;/p&gt;
&lt;p&gt;One way to look at it is that it&amp;rsquo;s &lt;em&gt;anything that&amp;rsquo;s not quantitative/numerical&lt;/em&gt;, since there are only two overall kinds of data.  That&amp;rsquo;s not so helpful for understanding the term though.&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s listen to the words again&amp;hellip;&lt;/p&gt;
&lt;p&gt;Do you hear &amp;ldquo;&lt;strong&gt;qual&lt;/strong&gt;ity&amp;rdquo; in &amp;ldquo;&lt;strong&gt;qual&lt;/strong&gt;itative&amp;rdquo; or &amp;ldquo;&lt;strong&gt;categor&lt;/strong&gt;y&amp;rdquo; in &amp;ldquo;&lt;strong&gt;categor&lt;/strong&gt;ical&amp;rdquo;&amp;quot;?&lt;/p&gt;
&lt;p&gt;When we describe the qualities of something, what we&amp;rsquo;re noticing are its features. The bugs can be red or black or gray. They can be fast or slow. They can have wings (or not)&amp;hellip; have antennas (or not).&lt;/p&gt;
&lt;p&gt;It&amp;rsquo;s as if you can split them up into groups, or &lt;em&gt;categories&lt;/em&gt;, based on how similar they are to one another&amp;hellip;. winged bugs in this box&amp;hellip; black winged ones in this compartment, and red winged ones in this other. Slow black winged bugs in this sub-compartment; fast black winged bugs in this other.  It&amp;rsquo;s easy to group qualitative/categorical data like this.&lt;/p&gt;
&lt;p&gt;That&amp;rsquo;s one way to think about what makes qualitative/categorical data qualitative (or categorical).&lt;/p&gt;
&lt;p&gt;Another way involves math:
We all know what to do with 1 + 1, right?  We &lt;strong&gt;add it up&lt;/strong&gt;, and get &lt;strong&gt;2&lt;/strong&gt; as the answer.&lt;/p&gt;
&lt;p&gt;Here&amp;rsquo;s one for you though: What about &amp;ldquo;black&amp;rdquo; + &amp;ldquo;red&amp;rdquo;?  Now, I mean&amp;hellip; if you said &amp;ldquo;dark red&amp;rdquo;, I can see what you did there, but we typically don&amp;rsquo;t think about doing math with words, do we?&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Rolly Polly&amp;rdquo; + &amp;ldquo;Beetle&amp;rdquo; doesn&amp;rsquo;t make much sense, does it&amp;hellip;&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Has antennas&amp;rdquo; divided by &amp;ldquo;slow&amp;rdquo; equals &amp;hellip;well&amp;hellip; nonsense, right?&lt;/p&gt;
&lt;h3 id=&#34;rule-of-thumb--quantitative-or-qualitative--numerical-or-categorical&#34;&gt;Rule of Thumb:  Quantitative? or Qualitative? &amp;hellip; Numerical? or Categorical?&lt;/h3&gt;
&lt;p&gt;So here&amp;rsquo;s the rule of thumb:  If you can do math with the data, it&amp;rsquo;s &lt;strong&gt;quant&lt;/strong&gt;itative/numerical, rather than &lt;strong&gt;qual&lt;/strong&gt;itative/categorical.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Word&lt;/em&gt;-y data is the kind of data that ends up being &lt;strong&gt;quali&lt;/strong&gt;tative/categorical in nature.&lt;/p&gt;
&lt;h3 id=&#34;numbers-can-be-qualitativecategorical&#34;&gt;Numbers Can Be Qualitative/categorical&lt;/h3&gt;
&lt;p&gt;Notice what I said in that &amp;ldquo;rule of thumb&amp;rdquo; statement:  If you can &lt;strong&gt;do math&lt;/strong&gt; with the data, it&amp;rsquo;s quantitative/numerical.&lt;/p&gt;
&lt;p&gt;I did &lt;em&gt;not&lt;/em&gt; say, &amp;ldquo;if the data is a number, it&amp;rsquo;s quantitative/numerical&amp;rdquo;.&lt;/p&gt;
&lt;p&gt;Why?&lt;/p&gt;
&lt;p&gt;Because there are examples of data that happen to be numbers, but you&amp;rsquo;d never do math with them. Here are some examples:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Special labels (like zip codes, which refer to a place on a map, and happen to be numbers, but dom&amp;rsquo;t make sense to say, &amp;ldquo;Hey, can you ad zip code 33617 and zip code 73505?&amp;rdquo;)&lt;/li&gt;
&lt;li&gt;Rankings (numbers used to label something like who came in 1st, 2nd, or 3rd in a race.  It doesn&amp;rsquo;t make sense to say &amp;ldquo;What&amp;rsquo;s 1st place divided by 3rd place?&amp;rdquo;, for example)&lt;/li&gt;
&lt;li&gt;Sequences (numbers used to tell which order things are in, like assigning numbers to steps in a process, or assigning numbers to spots where someone should stand in line.)&lt;/li&gt;
&lt;li&gt;Identifiers (numbers that are used once and only once to label something or someone&amp;hellip;maybe something like a driver&amp;rsquo;s license number.)&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;in-review&#34;&gt;In Review&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;We&amp;rsquo;ve looked at data in simple terms, and defined it as the &lt;em&gt;words&lt;/em&gt; or &lt;em&gt;numbers&lt;/em&gt; that we notice or measure about the world around us and just so happen to bother to save, either on paper, or digitally in a computer (preferably the latter, because then we can do cool things later on with it &lt;em&gt;much&lt;/em&gt; more easily).&lt;/li&gt;
&lt;li&gt;There are two types of data:  Quantitative/Numerical and Qualitative/Categorical&lt;/li&gt;
&lt;li&gt;Deciding what kind a particular piece of data is doesn&amp;rsquo;t have to be hard.  The general rule of thumb is: If you can do math with the data, it&amp;rsquo;s quantitative/numerical.  Otherwise it&amp;rsquo;s qualitative/categorical.&lt;/li&gt;
&lt;/ul&gt;
</description>
    </item>
    
    <item>
      <title>Local Receipt Validation for iOS in Swift From Start to Finish</title>
      <link>https://www.andrewcbancroft.com/2017/08/01/local-receipt-validation-swift-start-finish/</link>
      <pubDate>Tue, 01 Aug 2017 17:55:09 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2017/08/01/local-receipt-validation-swift-start-finish/</guid>
      <description>&lt;p&gt;Local receipt validation in Swift doesn&amp;rsquo;t seem to be widely written about. I&amp;rsquo;ve been able to find snippets of code scattered across the Internet, but nothing that brought everything together in one spot so that I could wrap my head around the whole process.&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;ve been working on some code for a while, and this post is my attempt to bring it all together from start to finish.&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;ve written seven guides that will take you from preparing to test receipt validation, all the way through to working with the result of the receipt validation process to enable/disable features of your app.&lt;/p&gt;
&lt;p&gt;Additionally, I&amp;rsquo;ve put all of my code into a new GitHub repository for you to use and learn from!&lt;/p&gt;
&lt;p&gt;Just want the code? Here you go!&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;fab fa-github fa-lg&#34;&gt;&lt;/i&gt; &lt;a href=&#34;https://github.com/andrewcbancroft/SwiftyLocalReceiptValidator&#34;&gt;Swifty Local Receipt Validator&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;
&lt;h2 id=&#34;seven-guides-taking-you-from-start-to-finish&#34;&gt;Seven guides taking you from start to finish&lt;/h2&gt;
&lt;p&gt;If you&amp;rsquo;re interested in learning about each step along the way, here are the seven guides I&amp;rsquo;ve written on the topic of local receipt validation in Swift:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;https://www.andrewcbancroft.com/2015/10/05/preparing-to-test-receipt-validation-for-ios/#build-run-on-device&#34;&gt;Preparing to Test Receipt Validation for iOS&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://www.andrewcbancroft.com/2015/09/21/openssl-for-ios-swift-the-easy-way/&#34;&gt;OpenSSL for iOS &amp;amp; Swift the Easy Way&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://www.andrewcbancroft.com/2015/10/13/loading-a-receipt-for-validation-with-swift/&#34;&gt;Loading a Receipt for Validation with Swift&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://www.andrewcbancroft.com/2016/06/09/extracting-a-pkcs7-container-for-receipt-validation-with-swift/&#34;&gt;Extracting a PKCS7 Container for Receipt Validation with Swift&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://www.andrewcbancroft.com/2017/07/16/receipt-validation-verifying-a-receipt-signature-in-swift/&#34;&gt;Receipt Validation – Verifying a Receipt Signature in Swift&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://www.andrewcbancroft.com/2017/07/27/receipt-validation-parsing-a-receipt-with-swift/&#34;&gt;Receipt Validation – Parse and Decode a Receipt with Swift&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://www.andrewcbancroft.com/2017/07/31/finalizing-receipt-validation-in-swift-computing-a-guid-hash/&#34;&gt;Finalizing Receipt Validation in Swift – Computing a GUID Hash&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;disclaimer&#34;&gt;Disclaimer&lt;/h2&gt;
&lt;p&gt;Preventing software piracy is hard. The code presented in these guides and shared in the Git repository is not meant to protect you against unauthorized usage of your app or its features.&lt;/p&gt;
&lt;p&gt;The guides and code are meant to be used for learning purposes, and &lt;em&gt;perhaps&lt;/em&gt; as a starting point for implementing local receipt validation on your own. If you use this code as-is in your app, you do it at your own risk.&lt;/p&gt;
&lt;p&gt;You must take additional efforts to obfuscate the code presented here to thwart an attacker&amp;rsquo;s attempt at circumventing the receipt validation logic contained within this repository.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Finalizing Receipt Validation in Swift – Computing a GUID Hash</title>
      <link>https://www.andrewcbancroft.com/2017/07/31/finalizing-receipt-validation-in-swift-computing-a-guid-hash/</link>
      <pubDate>Mon, 31 Jul 2017 12:36:26 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2017/07/31/finalizing-receipt-validation-in-swift-computing-a-guid-hash/</guid>
      <description>&lt;p&gt;The aim of this guide is to help you finalize the receipt validation process by computing the GUID hash for your app, and comparing it to the hash that&amp;rsquo;s stored within your receipt itself.&lt;/p&gt;
&lt;p&gt;This is a continuation of my receipt validation series. I&amp;rsquo;m assuming that…&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;You&amp;rsquo;ve &lt;a href=&#34;https://www.andrewcbancroft.com/2015/10/05/preparing-to-test-receipt-validation-for-ios/&#34;&gt;prepared to test receipt validation&lt;/a&gt; by setting up your app in iTunes Connect.&lt;/li&gt;
&lt;li&gt;You&amp;rsquo;ve brought in a cryptography library like OpenSSL to be able to work with the PKCS #7 container that acts as the &amp;ldquo;envelope” for the receipt. Perhaps you&amp;rsquo;ve even done it &lt;a href=&#34;https://www.andrewcbancroft.com/2015/09/21/openssl-for-ios-swift-the-easy-way/&#34;&gt;the &amp;ldquo;easy way” with CocoaPods&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;You&amp;rsquo;ve &lt;a href=&#34;https://www.andrewcbancroft.com/2015/10/13/loading-a-receipt-for-validation-with-swift/&#34;&gt;located and loaded&lt;/a&gt; the receipt for validation.&lt;/li&gt;
&lt;li&gt;You&amp;rsquo;ve &lt;a href=&#34;https://www.andrewcbancroft.com/2016/06/09/extracting-a-pkcs7-container-for-receipt-validation-with-swift/&#34;&gt;extracted the PKCS #7 container&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;You&amp;rsquo;ve &lt;a href=&#34;https://www.andrewcbancroft.com/2017/07/16/receipt-validation-verifying-a-receipt-signature-in-swift/&#34;&gt;verified the signature on the receipt&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;You&amp;rsquo;ve &lt;a href=&#34;https://www.andrewcbancroft.com/2017/07/27/receipt-validation-parsing-a-receipt-with-swift/&#34;&gt;parsed and decoded the receipt payload&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;After finishing this guide, you&amp;rsquo;ll simply need to use the parsed receipt data to perform any app-specific enabling/disabling of features based on the data within a valid receipt. If the receipt is &lt;em&gt;invalid&lt;/em&gt;, you&amp;rsquo;ll need to handle that as well. But all of the relatively difficult work of working with the Open SSL crypto library will be DONE after this guide.&lt;/p&gt;
&lt;p&gt;Just want the code? Here you go!&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;fab fa-github fa-lg&#34;&gt;&lt;/i&gt; &lt;a href=&#34;https://github.com/andrewcbancroft/SwiftyLocalReceiptValidator&#34;&gt;Swifty Local Receipt Validator&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;
&lt;p&gt;Ready? Let&amp;rsquo;s do this thing!&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;validating-with-receipt-validator&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;validating-the-device-guid-hash-with-receiptvalidator&#34;&gt;Validating the device GUID hash with ReceiptValidator&lt;/h2&gt;
&lt;p&gt;For this final step, I&amp;rsquo;ve imagined a single additional function within the &lt;a href=&#34;https://www.andrewcbancroft.com/2015/10/13/loading-a-receipt-for-validation-with-swift/#receipt-validator-implementation&#34;&gt;&lt;code&gt;ReceiptValidator&lt;/code&gt; struct&lt;/a&gt; called &lt;code&gt;validateHash(receipt:)&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;It&amp;rsquo;ll take in a &lt;code&gt;ParsedReceipt&lt;/code&gt; (&lt;a href=&#34;https://www.andrewcbancroft.com/2017/07/27/receipt-validation-parsing-a-receipt-with-swift/#final-goal&#34;&gt;review what it looks like&lt;/a&gt;), and will to the computation and comparison necessary to verify that the computed hash matches the hash stored in the receipt.&lt;/p&gt;
&lt;p&gt;A &lt;code&gt;ReceiptValidationError&lt;/code&gt; will be thrown if things go wrong within this function. No &lt;code&gt;ReceiptValidationError&lt;/code&gt; being thrown indicates a successful hash computation and comparison.&lt;/p&gt;
&lt;p&gt;Here&amp;rsquo;s the skeleton of the function:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;7
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;n&#34;&gt;fileprivate&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;validateHash&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;receipt&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ParsedReceipt&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;kr&#34;&gt;throws&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;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Make sure that the ParsedReceipt instances has non-nil values needed for hash comparison&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Compute the hash for your app &amp;amp; device&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Compare the computed hash with the receipt&amp;#39;s hash&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;a name=&#34;signaling-validation-failure&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;signaling-hash-validation-failure&#34;&gt;Signaling hash validation failure&lt;/h2&gt;
&lt;p&gt;Before I get into the implementation of the &lt;code&gt;validateHash(receipt:)&lt;/code&gt; function, let&amp;rsquo;s define one more &lt;code&gt;ReceiptValidationError&lt;/code&gt; case to describe a bad hash comparison outcome:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;enum&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;ReceiptValidationError&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&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;couldNotFindReceipt&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;emptyReceiptContents&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;receiptNotSigned&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;appleRootCertificateNotFound&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;receiptSignatureInvalid&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;malformedReceipt&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;malformedInAppPurchaseReceipt&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;incorrectHash&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;a name=&#34;implementing-validate-hash&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;implementing-validatehash-function&#34;&gt;Implementing validateHash function&lt;/h2&gt;
&lt;p&gt;I&amp;rsquo;ll put the code in front of you, and then do my best to explain my thought process.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;12
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;13
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;14
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;15
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;16
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;17
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;18
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;19
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;20
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;21
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;22
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;23
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;24
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;25
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;26
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;27
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;28
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;29
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;30
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;31
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;32
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;n&#34;&gt;fileprivate&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;validateHash&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;receipt&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ParsedReceipt&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;kr&#34;&gt;throws&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;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Make sure that the ParsedReceipt instances has non-nil values needed for hash comparison&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;k&#34;&gt;guard&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;receiptOpaqueValueData&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;receipt&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;opaqueValue&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 class=&#34;k&#34;&gt;throw&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ReceiptValidationError&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;incorrectHash&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;cl&#34;&gt;    &lt;span class=&#34;k&#34;&gt;guard&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;receiptBundleIdData&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;receipt&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;bundleIdData&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 class=&#34;k&#34;&gt;throw&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ReceiptValidationError&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;incorrectHash&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;cl&#34;&gt;    &lt;span class=&#34;k&#34;&gt;guard&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;receiptHashData&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;receipt&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;sha1Hash&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 class=&#34;k&#34;&gt;throw&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ReceiptValidationError&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;incorrectHash&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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;deviceIdentifier&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIDevice&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;current&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;identifierForVendor&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;?.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;uuid&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;rawDeviceIdentifierPointer&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;bp&#34;&gt;withUnsafePointer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;to&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;deviceIdentifier&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;cl&#34;&gt;        &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;unsafeDeviceIdentifierPointer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;UnsafePointer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;uuid_t&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;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;UnsafeRawPointer&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;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UnsafeRawPointer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;unsafeDeviceIdentifierPointer&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;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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;deviceIdentifierData&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSData&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;bytes&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;rawDeviceIdentifierPointer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;length&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;16&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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Compute the hash for your app &amp;amp; device&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Set up the hasing context&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;computedHash&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;Array&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;UInt8&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;repeating&lt;/span&gt;&lt;span class=&#34;p&#34;&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 class=&#34;bp&#34;&gt;count&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;20&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;sha1Context&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;SHA_CTX&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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;n&#34;&gt;SHA1_Init&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;sha1Context&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;cl&#34;&gt;    &lt;span class=&#34;n&#34;&gt;SHA1_Update&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;sha1Context&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;deviceIdentifierData&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;bytes&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;deviceIdentifierData&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;length&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;cl&#34;&gt;    &lt;span class=&#34;n&#34;&gt;SHA1_Update&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;sha1Context&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;receiptOpaqueValueData&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;bytes&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;receiptOpaqueValueData&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;length&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;cl&#34;&gt;    &lt;span class=&#34;n&#34;&gt;SHA1_Update&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;sha1Context&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;receiptBundleIdData&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;bytes&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;receiptBundleIdData&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;length&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;cl&#34;&gt;    &lt;span class=&#34;n&#34;&gt;SHA1_Final&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;computedHash&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;sha1Context&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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;computedHashData&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSData&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;bytes&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;computedHash&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;length&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;20&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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Compare the computed hash with the receipt&amp;#39;s hash&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;k&#34;&gt;guard&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;computedHashData&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;isEqual&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;to&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;receiptHashData&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;as&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Data&lt;/span&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 class=&#34;k&#34;&gt;throw&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ReceiptValidationError&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;incorrectHash&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;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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;a name=&#34;walkthrough&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;walking-through-validatehash&#34;&gt;Walking through validateHash&lt;/h4&gt;
&lt;p&gt;First up, I&amp;rsquo;ve placed three guard statements:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;k&#34;&gt;guard&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;receiptOpaqueValueData&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;receipt&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;opaqueValue&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 class=&#34;k&#34;&gt;throw&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ReceiptValidationError&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;incorrectHash&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;cl&#34;&gt;&lt;span class=&#34;k&#34;&gt;guard&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;receiptBundleIdData&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;receipt&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;bundleIdData&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 class=&#34;k&#34;&gt;throw&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ReceiptValidationError&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;incorrectHash&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;cl&#34;&gt;&lt;span class=&#34;k&#34;&gt;guard&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;receiptHashData&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;receipt&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;sha1Hash&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 class=&#34;k&#34;&gt;throw&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ReceiptValidationError&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;incorrectHash&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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;These help guarantee that the rest of the function will have all of the required pieces of data that it needs to fully compute a hash and compare it with what&amp;rsquo;s in the receipt.&lt;/p&gt;
&lt;p&gt;The hash for your app is computed with the following three pieces of information:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The app purchaser&amp;rsquo;s device identifier&lt;/li&gt;
&lt;li&gt;A piece of &amp;ldquo;opaque data” found within the receipt&lt;/li&gt;
&lt;li&gt;Your app&amp;rsquo;s bundle identifier found within the receipt&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The app purchaser&amp;rsquo;s device identifier is represented as a &lt;code&gt;uuid_t&lt;/code&gt; from the Foundation library. However, to use it with the Open SSL library, we&amp;rsquo;ll need to be working with an &lt;code&gt;NSData&lt;/code&gt; instance. The following code goes from the &lt;code&gt;uuid_t&lt;/code&gt; instance, to a raw pointer, to an &lt;code&gt;NSData&lt;/code&gt; instance:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;8
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;deviceIdentifier&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIDevice&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;current&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;identifierForVendor&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;?.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;uuid&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;rawDeviceIdentifierPointer&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;bp&#34;&gt;withUnsafePointer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;to&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;deviceIdentifier&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;cl&#34;&gt;    &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;unsafeDeviceIdentifierPointer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;UnsafePointer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;uuid_t&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;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;UnsafeRawPointer&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;cl&#34;&gt;    &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UnsafeRawPointer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;unsafeDeviceIdentifierPointer&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;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;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;deviceIdentifierData&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSData&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;bytes&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;rawDeviceIdentifierPointer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;length&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;16&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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The last chunk of code within the function actually computes the hash data.&lt;/p&gt;
&lt;p&gt;The hash is a SHA1 hash, so what we do here is initialize a SHA1 context, and then update it with all of the ingredients (device identifier, opaque data, and bundle identifier). The hash is finalized, and converted to an instance of &lt;code&gt;NSData&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The final guard takes the &lt;code&gt;computedHashData&lt;/code&gt; instance, and compares it to the receipt&amp;rsquo;s hash data. If it&amp;rsquo;s identical, validation passes! Otherwise, &lt;code&gt;ReceiptValidationError.incorrectHash&lt;/code&gt; is thrown.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;final-receipt-validator&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;final-receiptvalidator&#34;&gt;Final ReceiptValidator&lt;/h2&gt;
&lt;p&gt;Let&amp;rsquo;s put it all together into the final &lt;code&gt;ReceiptValidator&lt;/code&gt; struct. Additions are highlighted below:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;12
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;13
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;14
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;15
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;16
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;17
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;18
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;19
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;20
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;21
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;22
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;23
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;24
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;25
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;26
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;27
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;28
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;29
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;30
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;31
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;32
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;33
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;34
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;35
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;36
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;37
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;38
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;39
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;40
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;41
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;42
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;43
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;44
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;45
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;46
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;47
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;48
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;49
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;50
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;51
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;52
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;53
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;54
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;55
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;56
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;struct&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;ReceiptValidator&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;receiptLoader&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ReceiptLoader&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;receiptExtractor&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ReceiptExtractor&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;receiptSignatureValidator&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ReceiptSignatureValidator&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;receiptParser&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ReceiptParser&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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;validateReceipt&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;ReceiptValidationResult&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;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;cl&#34;&gt;            &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;receiptData&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;try&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;receiptLoader&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;loadReceipt&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;cl&#34;&gt;            &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;receiptContainer&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;try&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;receiptExtractor&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;extractPKCS7Container&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;receiptData&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;cl&#34;&gt;            
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;receiptSignatureValidator&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;checkSignaturePresence&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;receiptContainer&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;cl&#34;&gt;            &lt;span class=&#34;k&#34;&gt;try&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;receiptSignatureValidator&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;checkSignatureAuthenticity&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;receiptContainer&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;cl&#34;&gt;            
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;parsedReceipt&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;try&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;receiptParser&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;parse&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;receiptContainer&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;cl&#34;&gt;            &lt;span class=&#34;k&#34;&gt;try&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;validateHash&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;receipt&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;parsedReceipt&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;cl&#34;&gt;            
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;success&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;parsedReceipt&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;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;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;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;k&#34;&gt;as&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;!&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ReceiptValidationError&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;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;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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;n&#34;&gt;fileprivate&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;validateHash&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;receipt&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ParsedReceipt&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;kr&#34;&gt;throws&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;cl&#34;&gt;        &lt;span class=&#34;c1&#34;&gt;// Make sure that the ParsedReceipt instances has non-nil values needed for hash comparison&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;guard&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;receiptOpaqueValueData&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;receipt&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;opaqueValue&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 class=&#34;k&#34;&gt;throw&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ReceiptValidationError&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;incorrectHash&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;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;guard&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;receiptBundleIdData&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;receipt&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;bundleIdData&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 class=&#34;k&#34;&gt;throw&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ReceiptValidationError&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;incorrectHash&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;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;guard&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;receiptHashData&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;receipt&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;sha1Hash&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 class=&#34;k&#34;&gt;throw&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ReceiptValidationError&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;incorrectHash&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;cl&#34;&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;deviceIdentifier&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIDevice&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;current&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;identifierForVendor&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;?.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;uuid&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;        &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;rawDeviceIdentifierPointer&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;bp&#34;&gt;withUnsafePointer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;to&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;deviceIdentifier&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;cl&#34;&gt;            &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;unsafeDeviceIdentifierPointer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;UnsafePointer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;uuid_t&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;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;UnsafeRawPointer&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;cl&#34;&gt;            &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UnsafeRawPointer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;unsafeDeviceIdentifierPointer&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;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;cl&#34;&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;deviceIdentifierData&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSData&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;bytes&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;rawDeviceIdentifierPointer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;length&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;16&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;cl&#34;&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;c1&#34;&gt;// Compute the hash for your app &amp;amp; device&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;        &lt;span class=&#34;c1&#34;&gt;// Set up the hasing context&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;computedHash&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;Array&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;UInt8&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;repeating&lt;/span&gt;&lt;span class=&#34;p&#34;&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 class=&#34;bp&#34;&gt;count&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;20&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;cl&#34;&gt;        &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;sha1Context&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;SHA_CTX&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;cl&#34;&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;SHA1_Init&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;sha1Context&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;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;SHA1_Update&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;sha1Context&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;deviceIdentifierData&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;bytes&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;deviceIdentifierData&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;length&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;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;SHA1_Update&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;sha1Context&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;receiptOpaqueValueData&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;bytes&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;receiptOpaqueValueData&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;length&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;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;SHA1_Update&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;sha1Context&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;receiptBundleIdData&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;bytes&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;receiptBundleIdData&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;length&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;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;SHA1_Final&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;computedHash&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;sha1Context&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;cl&#34;&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;computedHashData&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSData&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;bytes&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;computedHash&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;length&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;20&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;cl&#34;&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;c1&#34;&gt;// Compare the computed hash with the receipt&amp;#39;s hash&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;guard&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;computedHashData&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;isEqual&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;to&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;receiptHashData&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;as&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Data&lt;/span&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 class=&#34;k&#34;&gt;throw&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ReceiptValidationError&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;incorrectHash&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;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;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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;a name=&#34;what-to-do-from-here&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;what-to-do-from-here&#34;&gt;What to do from here&lt;/h2&gt;
&lt;p&gt;So what now?&lt;/p&gt;
&lt;p&gt;Well, at this stage, if no errors have been thrown, you&amp;rsquo;ve got a valid receipt.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;validateReceipt&lt;/code&gt; function returns an instance of &lt;code&gt;ParsedReceipt&lt;/code&gt; so that you can inspect individual Swift-Typed values from the receipt payload to determine what features you should enable or disable, depending on what your needs are.&lt;/p&gt;
&lt;p&gt;If a &lt;code&gt;ReceiptValidationError&lt;/code&gt; is thrown at any point along the way, you&amp;rsquo;ll need to handle them.&lt;br&gt;
Here are a few ideas:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Implement a grace period, just in case the receipt validation failure occurred for a reason that the user can&amp;rsquo;t control (e.g. maybe we couldn&amp;rsquo;t locate the receipt, and requesting a new one failed because Apple was having issues…)&lt;/li&gt;
&lt;li&gt;Disable a feature in your app because receipt validation failed too many times&lt;/li&gt;
&lt;li&gt;Maybe you just need to use the data within the &lt;code&gt;ParsedReceipt&lt;/code&gt; because you&amp;rsquo;re changing the way you monetize your app. Now, instead of making users pay $0.99 for the app, you&amp;rsquo;re going to give it away for free, but let people buy an in-app purchase to enable &amp;ldquo;pro” features, or remove ads…whatever. In this case, you may check the &lt;code&gt;ParsedReceipt&lt;/code&gt; to see the original version of the app that your user downloaded. Maybe you want to require users who download your app after version 2.0 to buy an in-app purchase for &lt;em&gt;Feature X&lt;/em&gt;, but you want to give it to everyone who already &lt;em&gt;has&lt;/em&gt; the app since they may have already paid $0.99 for it, and it&amp;rsquo;d make them feel ripped off if they had to buy the in-app purchase.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;How you handle the parsed receipt data or a receipt validation error is really customizable and specific to your particular app.&lt;/p&gt;
&lt;p&gt;The bottom line is that from this point on, you no longer need Open SSL or any additional cryptic, low-level, unsafe pointer-type stuff to finish things out.&lt;/p&gt;
&lt;p&gt;I hope this series has been helpful in setting you up to validate receipts locally on a user&amp;rsquo;s device!&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/10/05/preparing-to-test-receipt-validation-for-ios/&#34; title=&#34;Preparing to Test Receipt Validation for iOS&#34;&gt;Preparing to Test Receipt Validation for iOS&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/10/13/loading-a-receipt-for-validation-with-swift/&#34; title=&#34;Loading a Receipt for Validation with Swift&#34;&gt;Loading a Receipt for Validation with 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/2015/09/21/openssl-for-ios-swift-the-easy-way/&#34; title=&#34;OpenSSL for iOS &amp;#038; Swift the Easy Way&#34;&gt;OpenSSL for iOS &amp; Swift the Easy Way&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/06/09/extracting-a-pkcs7-container-for-receipt-validation-with-swift/&#34; Extracting a PKCS7 Container for Receipt Validation with Swift=&#34;title&#34;&gt;Extracting a PKCS7 Container for Receipt Validation with 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/2017/07/16/receipt-validation-verifying-a-receipt-signature-in-swift/&#34; Receipt Validation – Verifying a Receipt Signature in Swift=&#34;title&#34;&gt;Receipt Validation – Verifying a Receipt Signature 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/2017/07/27/receipt-validation-parsing-a-receipt-with-swift/&#34; Receipt Validation – Parse and Decode a Receipt with Swift=&#34;title&#34;&gt;Receipt Validation – Parse and Decode a Receipt 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>Receipt Validation –  Parse and Decode a Receipt with Swift</title>
      <link>https://www.andrewcbancroft.com/2017/07/27/receipt-validation-parsing-a-receipt-with-swift/</link>
      <pubDate>Fri, 28 Jul 2017 03:41:33 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2017/07/27/receipt-validation-parsing-a-receipt-with-swift/</guid>
      <description>&lt;p&gt;The aim of this guide is to help you parse a receipt and decode it so that you have readable pieces of metadata to inspect and finalize all of the receipt validation steps.&lt;/p&gt;
&lt;p&gt;This is a continuation of my receipt validation series. I&amp;rsquo;m assuming that…&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;You&amp;rsquo;ve &lt;a href=&#34;https://www.andrewcbancroft.com/2015/10/05/preparing-to-test-receipt-validation-for-ios/&#34;&gt;prepared to test receipt validation&lt;/a&gt; by setting up your app in iTunes Connect.&lt;/li&gt;
&lt;li&gt;You&amp;rsquo;ve brought in a cryptography library like OpenSSL to be able to work with the PKCS #7 container that acts as the &amp;ldquo;envelope&amp;rdquo; for the receipt. Perhaps you&amp;rsquo;ve even done it &lt;a href=&#34;https://www.andrewcbancroft.com/2015/09/21/openssl-for-ios-swift-the-easy-way/&#34;&gt;the &amp;ldquo;easy way&amp;rdquo; with CocoaPods&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;You&amp;rsquo;ve &lt;a href=&#34;https://www.andrewcbancroft.com/2015/10/13/loading-a-receipt-for-validation-with-swift/&#34;&gt;located and loaded&lt;/a&gt; the receipt for validation.&lt;/li&gt;
&lt;li&gt;You&amp;rsquo;ve &lt;a href=&#34;https://www.andrewcbancroft.com/2016/06/09/extracting-a-pkcs7-container-for-receipt-validation-with-swift/&#34;&gt;extracted the PKCS #7 container&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;You&amp;rsquo;ve &lt;a href=&#34;https://www.andrewcbancroft.com/2017/07/16/receipt-validation-verifying-a-receipt-signature-in-swift/&#34;&gt;verified the signature on the receipt&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;After finishing this guide, you&amp;rsquo;ll still have to &lt;a href=&#34;https://www.andrewcbancroft.com/2017/07/31/finalizing-receipt-validation-in-swift-computing-a-guid-hash/&#34;&gt;compute the GUID hash of your app&lt;/a&gt; to compare with the hash that&amp;rsquo;s found within the receipt. You&amp;rsquo;ll also have to inspect the receipt data to perform any app-specific verification steps. But in order to do either, you&amp;rsquo;ll need the parsed receipt metadata.&lt;/p&gt;
&lt;p&gt;Just want the code? Here you go!&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;fab fa-github fa-lg&#34;&gt;&lt;/i&gt; &lt;a href=&#34;https://github.com/andrewcbancroft/SwiftyLocalReceiptValidator&#34;&gt;Swifty Local Receipt Validator&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;
&lt;p&gt;Want to understand the final &lt;code&gt;ReceiptParser&lt;/code&gt;? Let&amp;rsquo;s get to it!&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;final-goal&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;the-final-goal-a-parsed-receipt&#34;&gt;The final goal: A parsed receipt&lt;/h2&gt;
&lt;p&gt;The final goal of this guide is a parsed receipt.&lt;/p&gt;
&lt;p&gt;What do you say we start things off by defining what one looks like?&lt;/p&gt;
&lt;p&gt;At the end of the day, what we&amp;rsquo;d like back from the parsing process is a simple struct that contains the various pieces of metadata that are found within the &lt;a href=&#34;https://www.andrewcbancroft.com/2016/06/09/extracting-a-pkcs7-container-for-receipt-validation-with-swift/&#34;&gt;extracted the PKCS #7 container&lt;/a&gt;. Things like…&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;the app&amp;rsquo;s bundle identifier,&lt;/li&gt;
&lt;li&gt;the original app version that was purchased,&lt;/li&gt;
&lt;li&gt;a collection of all the in app purchase receipts,&lt;/li&gt;
&lt;li&gt;etc.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;How does the following look?&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;12
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;13
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;14
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;15
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;16
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;17
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;18
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;19
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;20
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;21
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;22
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;23
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;struct&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;ParsedReceipt&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;bundleIdentifier&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;bundleIdData&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSData&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;appVersion&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;opaqueValue&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSData&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;sha1Hash&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSData&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;inAppPurchaseReceipts&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;ParsedInAppPurchaseReceipt&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;originalAppVersion&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;receiptCreationDate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Date&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;expirationDate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Date&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;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;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;struct&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;ParsedInAppPurchaseReceipt&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;quantity&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&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;productIdentifier&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;transactionIdentifier&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;originalTransactionIdentifier&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;purchaseDate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Date&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;originalPurchaseDate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Date&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;subscriptionExpirationDate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Date&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;cancellationDate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Date&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;webOrderLineItemId&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&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;You may be wondering, &amp;ldquo;How&amp;rsquo;d he know what values are encoded within the extracted receipt payload?&amp;rdquo;. Apple has a very handy &lt;a href=&#34;https://developer.apple.com/library/content/releasenotes/General/ValidateAppStoreReceipt/Chapters/ReceiptFields.html&#34;&gt;list of all the values that are encoded&lt;/a&gt;, so I listed each property out in my struct according to their documentation.&lt;/p&gt;
&lt;p&gt;Parsing the receipt produces the most valuable piece of the whole process. Sure, it&amp;rsquo;s necessary to go through all of the other validation steps, but having a &lt;strong&gt;decoded receipt&lt;/strong&gt; with actual &lt;strong&gt;human-readable values&lt;/strong&gt; is, to me, a huge step.&lt;/p&gt;
&lt;p&gt;Full disclaimer though: parsing the receipt is not very… Swifty.&lt;/p&gt;
&lt;p&gt;We&amp;rsquo;re going to be working with all kinds of ugly things like &lt;code&gt;UnsafeMutablePointers&lt;/code&gt;, and cryptically-named C Types.&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s take it one step at a time though…&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;visualizing-receipt-structure&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;visualizing-the-encoded-receipts-structure&#34;&gt;Visualizing the encoded receipt&amp;rsquo;s structure&lt;/h2&gt;
&lt;p&gt;Up to now, we&amp;rsquo;ve been working only with the PKCS #7 &lt;em&gt;container&lt;/em&gt; for the receipt. Now it&amp;rsquo;s time to dig into the container and see what it actually &lt;em&gt;contains&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;If you crack open the container, what you find is a long series of bytes that encode the actual structure of the receipt.&lt;/p&gt;
&lt;p&gt;From beginning to end, the bytes &lt;em&gt;should&lt;/em&gt; encode what&amp;rsquo;s called an &amp;ldquo;ASN.1 Set&amp;rdquo;. In fact, if you open the PKCS #7 container and it &lt;em&gt;doesn&amp;rsquo;t&lt;/em&gt; encode an ASN.1 Set, that&amp;rsquo;d warrant a receipt validation failure…&lt;a href=&#34;#handling-error-conditions&#34;&gt;more about handling that in a minute&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Here&amp;rsquo;s a visual representation of an ASN.1 Set:&lt;br&gt;
&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2017/07/ASN1Set.jpeg&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2017/07/ASN1Set.jpeg&#34; alt=&#34;ASN.1 Set&#34; width=&#34;923&#34; height=&#34;338&#34; class=&#34;alignnone size-full wp-image-13513&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2017/07/ASN1Set.jpeg 923w, https://www.andrewcbancroft.com/wp-content/uploads/2017/07/ASN1Set-300x110.jpeg 300w, https://www.andrewcbancroft.com/wp-content/uploads/2017/07/ASN1Set-768x281.jpeg 768w&#34; sizes=&#34;(max-width: 923px) 100vw, 923px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Since we&amp;rsquo;ve just got a bunch of bytes encoding things, there&amp;rsquo;s got to be some way to say, &amp;ldquo;This byte, or these series of bytes, represent [this human understandable thing]&amp;rdquo;.&lt;/p&gt;
&lt;p&gt;That&amp;rsquo;s exactly what we&amp;rsquo;ve got, as you can see by the visual representation.&lt;/p&gt;
&lt;p&gt;The first byte in the receipt payload (the green box in the visualization) signals that the bytes that follow encode an ASN.1 Set.&lt;/p&gt;
&lt;p&gt;The next bytes in the series (the blue box) encode how long the ASN.1 Set is, so that as you&amp;rsquo;re going along parsing and decoding the contents of the Set, you know when to stop.&lt;/p&gt;
&lt;p&gt;The final series of bytes (the yellow boxes) encode chunks of information that can be decoded to give you human readable receipt attributes. Those chunks, themselves, are encoded as ASN.1 &lt;em&gt;Sequences&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;So what does an ASN.1 Sequence look like? Here&amp;rsquo;s a visual:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2017/07/ASN1Sequence.jpeg&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2017/07/ASN1Sequence.jpeg&#34; alt=&#34;ASN.1 Sequence&#34; width=&#34;919&#34; height=&#34;337&#34; class=&#34;alignnone size-full wp-image-13514&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2017/07/ASN1Sequence.jpeg 919w, https://www.andrewcbancroft.com/wp-content/uploads/2017/07/ASN1Sequence-300x110.jpeg 300w, https://www.andrewcbancroft.com/wp-content/uploads/2017/07/ASN1Sequence-768x282.jpeg 768w&#34; sizes=&#34;(max-width: 919px) 100vw, 919px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;When it comes to app receipts, ASN.1 &lt;em&gt;Sequences&lt;/em&gt; are used to say, &amp;ldquo;Hey, this series of bytes encodes &lt;em&gt;the bundle identifier&lt;/em&gt; or &lt;em&gt;the original app version&lt;/em&gt; or &lt;em&gt;some other receipt attribute&lt;/em&gt;.&amp;rdquo;&lt;/p&gt;
&lt;p&gt;Each ASN.1 Sequence has a flag (the pink box in the visualization) to signal that the bytes that follow do, in fact, encode an ASN.1 Sequence.&lt;/p&gt;
&lt;p&gt;Then, just like an ASN.1 Set, the next bytes in line (the blue box) encode how long the Sequence is. Then comes what we&amp;rsquo;re really after in all this Set/Sequence talk:&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;type&lt;/em&gt; of attribute (bundle identifier, for example) is encoded next in the series of bytes as an ASN.1 Integer (note that this isn&amp;rsquo;t a Swift Int…yet…we&amp;rsquo;ll decode it soon). Each attribute type has a unique ASN.1 Integer value, kind of like an ID. &lt;a href=&#34;https://developer.apple.com/library/content/releasenotes/General/ValidateAppStoreReceipt/Chapters/ReceiptFields.html&#34;&gt;Apple&amp;rsquo;s documentation&lt;/a&gt; is helpful in figuring out which ASN.1 Integer value maps to which receipt attribute.&lt;/p&gt;
&lt;p&gt;After the attribute type comes some bytes that encode an &amp;ldquo;attribute version&amp;rdquo;, also as an ASN.1 Integer. At the time of this guide&amp;rsquo;s publication, &amp;ldquo;attribute version&amp;rdquo; isn&amp;rsquo;t used for anything. Nonetheless, the series of bytes right after the attribute type within the ASN.1 Sequence is reserved and will always represent the &amp;ldquo;attribute version&amp;rdquo;.&lt;/p&gt;
&lt;p&gt;The remaining bytes in the ASN.1 Sequence encode the actual &lt;em&gt;value&lt;/em&gt; of the attribute as an ASN.1 Octet String (don&amp;rsquo;t let the word &amp;ldquo;Octet String&amp;rdquo; trick you into thinking that it&amp;rsquo;s actually a String… they&amp;rsquo;re &lt;em&gt;bytes&lt;/em&gt; that we&amp;rsquo;ll have to decode shortly…)&lt;/p&gt;
&lt;p&gt;Knowing how the receipt payload is structured will help us formulate a strategy around parsing it. Let&amp;rsquo;s imagine a simple algorithm to do it now.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;parsing-strategy&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;receipt-parsing-strategy&#34;&gt;Receipt parsing strategy&lt;/h2&gt;
&lt;p&gt;Let&amp;rsquo;s take it step by step. What if we approach parsing the receipt like this:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;1)&lt;/strong&gt; Do some preliminary checks to ensure that the receipt payload is in the correct structural format (it should be an ASN.1 Set, for example).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;2)&lt;/strong&gt; For each ASN.1 Sequence within the ASN.1 Set, check to see what type of attribute it is.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;3)&lt;/strong&gt; Decode its Octet String value into actual, human-readable values. The decoded values would be represented by Swift Types (Int, String, Date are sufficient to cover all of the possibilities for receipts). The final decoded value depends on what &lt;em&gt;type&lt;/em&gt; of attribute it is.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;4)&lt;/strong&gt; Create and return a ParsedReceipt instance as the final product.&lt;/p&gt;
&lt;p&gt;If at any point the receipt payload fails to live up to the expected structure, receipt validation will fail, and we can signal that by throwing an error.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;note-on-iap-receipts&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;a-note-on-in-app-purchase-receipts&#34;&gt;A note on in-app purchase receipts&lt;/h4&gt;
&lt;p&gt;As we follow the receipt parsing strategy steps that I just described, there&amp;rsquo;s going to come a point where we run into the ASN.1 Sequence that encodes the in-app purchase receipts.&lt;/p&gt;
&lt;p&gt;These are special.&lt;/p&gt;
&lt;p&gt;In-app purchase receipts are encoded as ASN.1 Sets (with ASN.1 Sequences within) &lt;em&gt;inside&lt;/em&gt; the primary ASN.1 Set receipt payload. In other words, they&amp;rsquo;re &lt;em&gt;nested&lt;/em&gt; ASN.1 Sets within the &lt;em&gt;overall&lt;/em&gt; ASN.1 Set that encodes the whole receipt. The nested Set contains the &lt;em&gt;in-app purchase&lt;/em&gt; receipt attributes.&lt;/p&gt;
&lt;p&gt;So in order to decode these, we&amp;rsquo;ll have to apply the receipt parsing strategy &lt;em&gt;within&lt;/em&gt; the receipt parsing strategy. Fun, huh? We&amp;rsquo;ll only have to do it for the in-app purchase receipt attributes though.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;helper-functions&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;preparation-step-helper-decoding-functions&#34;&gt;Preparation step: Helper decoding functions&lt;/h2&gt;
&lt;p&gt;If you saw the &lt;code&gt;ParsedReceipt&lt;/code&gt; struct that I proposed earlier in the guide, you&amp;rsquo;ll notice that there are essentially four Swift Types that the receipt attributes (and in-app purchase receipt attributes) get decoded into:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Int?&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;String?&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;NSData?&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Date?&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;code&gt;NSData&lt;/code&gt; has a constructor that can work with &lt;code&gt;UnsafeRawPointers&lt;/code&gt; directly, but &lt;code&gt;Int?&lt;/code&gt;, &lt;code&gt;String?&lt;/code&gt;, and &lt;code&gt;Date?&lt;/code&gt; need some help converting from the ASN.1 versions of those Types to the &lt;em&gt;Swift&lt;/em&gt; versions of those Types.&lt;/p&gt;
&lt;p&gt;Let me put the code before you and follow up with what I&amp;rsquo;m doing here:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;12
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;13
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;14
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;15
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;16
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;17
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;18
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;19
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;20
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;21
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;22
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;23
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;24
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;25
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;26
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;27
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;28
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;29
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;30
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;31
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;32
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;33
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;34
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;35
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;36
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;37
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;38
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;39
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;40
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;41
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;42
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;43
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;44
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;45
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;46
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;47
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;48
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;49
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;50
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;51
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;52
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;53
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;DecodeASN1Integer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;startOfInt&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;intPointer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;kr&#34;&gt;inout&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;UnsafePointer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;UInt8&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;?,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;length&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 class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// These will be set by ASN1_get_object&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;type&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;Int32&lt;/span&gt;&lt;span class=&#34;p&#34;&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;xclass&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;Int32&lt;/span&gt;&lt;span class=&#34;p&#34;&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;intLength&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;    &lt;span class=&#34;n&#34;&gt;ASN1_get_object&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;intPointer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;intLength&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;&amp;amp;&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;p&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;xclass&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;length&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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;k&#34;&gt;guard&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;V_ASN1_INTEGER&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;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;kc&#34;&gt;nil&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;integer&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;c2i_ASN1_INTEGER&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;&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;intPointer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;intLength&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;result&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ASN1_INTEGER_get&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;integer&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;cl&#34;&gt;    &lt;span class=&#34;n&#34;&gt;ASN1_INTEGER_free&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;integer&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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;result&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;DecodeASN1String&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;startOfString&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;stringPointer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;kr&#34;&gt;inout&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;UnsafePointer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;UInt8&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;?,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;length&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;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;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// These will be set by ASN1_get_object&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;type&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;Int32&lt;/span&gt;&lt;span class=&#34;p&#34;&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;xclass&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;Int32&lt;/span&gt;&lt;span class=&#34;p&#34;&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;stringLength&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;    &lt;span class=&#34;n&#34;&gt;ASN1_get_object&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;stringPointer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;stringLength&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;&amp;amp;&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;p&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;xclass&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;length&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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;type&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;==&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;V_ASN1_UTF8STRING&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;cl&#34;&gt;        &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;mutableStringPointer&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UnsafeMutableRawPointer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;kr&#34;&gt;mutating&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;stringPointer&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;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;return&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;bytesNoCopy&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;mutableStringPointer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;length&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;stringLength&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;encoding&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;Encoding&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;utf8&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;freeWhenDone&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;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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;type&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;==&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;V_ASN1_IA5STRING&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;cl&#34;&gt;        &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;mutableStringPointer&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UnsafeMutableRawPointer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;kr&#34;&gt;mutating&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;stringPointer&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;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;return&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;bytesNoCopy&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;mutableStringPointer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;length&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;stringLength&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;encoding&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;Encoding&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;ascii&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;freeWhenDone&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;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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;nil&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;DecodeASN1Date&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;startOfDate&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;datePointer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;kr&#34;&gt;inout&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;UnsafePointer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;UInt8&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;?,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;length&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;n&#34;&gt;Date&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;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Date formatter code from https://www.objc.io/issues/17-security/receipt-validation/#parsing-the-receipt&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;dateFormatter&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;DateFormatter&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;cl&#34;&gt;    &lt;span class=&#34;n&#34;&gt;dateFormatter&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;locale&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Locale&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;s&#34;&gt;&amp;#34;en_US_POSIX&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;cl&#34;&gt;    &lt;span class=&#34;n&#34;&gt;dateFormatter&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;dateFormat&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;yyyy&amp;#39;-&amp;#39;MM&amp;#39;-&amp;#39;dd&amp;#39;T&amp;#39;HH&amp;#39;:&amp;#39;mm&amp;#39;:&amp;#39;ss&amp;#39;Z&amp;#39;&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;n&#34;&gt;dateFormatter&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;timeZone&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;TimeZone&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;secondsFromGMT&lt;/span&gt;&lt;span class=&#34;p&#34;&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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;dateString&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;DecodeASN1String&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;startOfString&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;datePointer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;length&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;length&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;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;dateFormatter&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;date&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;from&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;dateString&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;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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;nil&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Each of these decoding functions are dealing with the receipt attribute &lt;em&gt;value&lt;/em&gt; portion of the ASN.1 Sequence that we&amp;rsquo;re working on at the time. Recall the structure:&lt;br&gt;
&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2017/07/ASN1Sequence.jpeg&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2017/07/ASN1Sequence.jpeg&#34; alt=&#34;ASN.1 Sequence&#34; width=&#34;919&#34; height=&#34;337&#34; class=&#34;alignnone size-full wp-image-13514&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2017/07/ASN1Sequence.jpeg 919w, https://www.andrewcbancroft.com/wp-content/uploads/2017/07/ASN1Sequence-300x110.jpeg 300w, https://www.andrewcbancroft.com/wp-content/uploads/2017/07/ASN1Sequence-768x282.jpeg 768w&#34; sizes=&#34;(max-width: 919px) 100vw, 919px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;So we take in a pointer that&amp;rsquo;s pointing &lt;em&gt;to&lt;/em&gt; the start of one of the attribute &lt;em&gt;values&lt;/em&gt; (a yellow box). The yellow box&amp;rsquo;s ASN.1 Octet String encodes either an integer, a string, or a date. (Okay, technically I guess you could include NSData, but this doesn&amp;rsquo;t need to be &amp;ldquo;decoded&amp;rdquo; really. And the in-app purchase receipts will be parsed and decoded into the stated Types as well, so it all boils down to the three I just mentioned…thus the reason for only three helper functions).&lt;/p&gt;
&lt;p&gt;The strategy for the first two functions is basically to take what we&amp;rsquo;re pointing to, and call &lt;code&gt;ASN1_get_object&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;This function call gets us enough information to decode the bytes from the start of the object to the end of the object into either an &lt;code&gt;Int?&lt;/code&gt; or a &lt;code&gt;String?&lt;/code&gt;. If it fails, &lt;code&gt;nil&lt;/code&gt; is returned.&lt;/p&gt;
&lt;p&gt;Decoding dates simply involves initializing a &lt;code&gt;DateFormatter&lt;/code&gt; with the appropriate locale and date format. The datePointer parameter actually points to an encoded &lt;em&gt;string&lt;/em&gt;, so the strategy is to use the &lt;code&gt;DecodeASN1String&lt;/code&gt; function we made, and pass the date string to the date formatter.&lt;/p&gt;
&lt;p&gt;So long as the string can be decoded, the date formatter instance is used to create an actual &lt;code&gt;Date?&lt;/code&gt; instance and return it. Otherwise, &lt;code&gt;nil&lt;/code&gt; is returned.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;handling-error-conditions&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;handling-error-conditions&#34;&gt;Handling error conditions&lt;/h2&gt;
&lt;p&gt;The kinds of errors that can occur when parsing the receipt payload all have to do with unexpected structure.&lt;/p&gt;
&lt;p&gt;For example, if we&amp;rsquo;re expecting to be stepping through an ASN.1 Payload or an ASN.1 Sequence but instead find that it&amp;rsquo;s not what we expect, this is a situation where reeipt validation should fail, because there&amp;rsquo;s no way to decode the receipt attributes if the bytes of the payload don&amp;rsquo;t conform to the expected structure.&lt;/p&gt;
&lt;p&gt;In situations where the receipt payload or one of its in-app purchase receipt payloads is &amp;ldquo;malformed&amp;rdquo; in some way, we can throw an &lt;code&gt;Error&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;ve highlighted two new &lt;code&gt;ReceiptValidationError&lt;/code&gt; cases here:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;9
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;enum&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;ReceiptValidationError&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&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;couldNotFindReceipt&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;emptyReceiptContents&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;receiptNotSigned&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;appleRootCertificateNotFound&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;receiptSignatureInvalid&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;malformedReceipt&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;malformedInAppPurchaseReceipt&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;a name=&#34;implementing-receipt-parser&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;implenting-receiptparser&#34;&gt;Implenting ReceiptParser&lt;/h2&gt;
&lt;p&gt;OK! We&amp;rsquo;ve got a few helper functions to decode the receipt attributes, and we&amp;rsquo;ve got some &lt;code&gt;ReceiptValidationError&lt;/code&gt; cases to throw in case parsing fails.&lt;/p&gt;
&lt;p&gt;At a very high level, the ReceiptParser will take the following skeletal structure:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;12
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;13
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;14
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;15
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;16
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;17
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;18
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;19
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;20
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;21
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;22
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;23
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;24
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;25
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;26
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;27
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;28
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;29
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;30
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;31
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;32
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;33
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;34
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;35
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;36
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;37
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;38
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;39
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;40
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;41
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;42
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;43
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;44
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;45
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;46
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;47
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;48
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;struct&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;ReceiptParser&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;parse&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;PKCS7Container&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;UnsafeMutablePointer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;PKCS7&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;)&lt;/span&gt; &lt;span class=&#34;kr&#34;&gt;throws&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ParsedReceipt&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;cl&#34;&gt;        &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;bundleIdentifier&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;cl&#34;&gt;        &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;bundleIdData&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSData&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;cl&#34;&gt;        &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;appVersion&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;cl&#34;&gt;        &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;opaqueValue&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSData&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;cl&#34;&gt;        &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;sha1Hash&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSData&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;cl&#34;&gt;        &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;inAppPurchaseReceipts&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;ParsedInAppPurchaseReceipt&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;cl&#34;&gt;        &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;originalAppVersion&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;cl&#34;&gt;        &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;receiptCreationDate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Date&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;cl&#34;&gt;        &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;expirationDate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Date&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;cl&#34;&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;c1&#34;&gt;// Parse the receipt, setting each variable&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ParsedReceipt&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;bundleIdentifier&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;bundleIdentifier&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;cl&#34;&gt;                             &lt;span class=&#34;n&#34;&gt;bundleIdData&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;bundleIdData&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;cl&#34;&gt;                             &lt;span class=&#34;n&#34;&gt;appVersion&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;appVersion&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;cl&#34;&gt;                             &lt;span class=&#34;n&#34;&gt;opaqueValue&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;opaqueValue&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;cl&#34;&gt;                             &lt;span class=&#34;n&#34;&gt;sha1Hash&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;sha1Hash&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;cl&#34;&gt;                             &lt;span class=&#34;n&#34;&gt;inAppPurchaseReceipts&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;inAppPurchaseReceipts&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;cl&#34;&gt;                             &lt;span class=&#34;n&#34;&gt;originalAppVersion&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;originalAppVersion&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;cl&#34;&gt;                             &lt;span class=&#34;n&#34;&gt;receiptCreationDate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;receiptCreationDate&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;cl&#34;&gt;                             &lt;span class=&#34;n&#34;&gt;expirationDate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;expirationDate&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;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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;parseInAppPurchaseReceipt&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;currentInAppPurchaseASN1PayloadLocation&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;kr&#34;&gt;inout&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;UnsafePointer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;UInt8&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;?,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;payloadLength&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;kr&#34;&gt;throws&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ParsedInAppPurchaseReceipt&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;cl&#34;&gt;        &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;quantity&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&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;productIdentifier&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;cl&#34;&gt;        &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;transactionIdentifier&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;cl&#34;&gt;        &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;originalTransactionIdentifier&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;cl&#34;&gt;        &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;purchaseDate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Date&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;cl&#34;&gt;        &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;originalPurchaseDate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Date&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;cl&#34;&gt;        &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;subscriptionExpirationDate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Date&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;cl&#34;&gt;        &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;cancellationDate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Date&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;cl&#34;&gt;        &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;webOrderLineItemId&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&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;        &lt;span class=&#34;c1&#34;&gt;// Parse the in-app purchase receipt, setting each variable&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ParsedInAppPurchaseReceipt&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;quantity&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;quantity&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;cl&#34;&gt;                                          &lt;span class=&#34;n&#34;&gt;productIdentifier&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;productIdentifier&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;cl&#34;&gt;                                          &lt;span class=&#34;n&#34;&gt;transactionIdentifier&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;transactionIdentifier&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;cl&#34;&gt;                                          &lt;span class=&#34;n&#34;&gt;originalTransactionIdentifier&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;originalTransactionIdentifier&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;cl&#34;&gt;                                          &lt;span class=&#34;n&#34;&gt;purchaseDate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;purchaseDate&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;cl&#34;&gt;                                          &lt;span class=&#34;n&#34;&gt;originalPurchaseDate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;originalPurchaseDate&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;cl&#34;&gt;                                          &lt;span class=&#34;n&#34;&gt;subscriptionExpirationDate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;subscriptionExpirationDate&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;cl&#34;&gt;                                          &lt;span class=&#34;n&#34;&gt;cancellationDate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;cancellationDate&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;cl&#34;&gt;                                          &lt;span class=&#34;n&#34;&gt;webOrderLineItemId&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;webOrderLineItemId&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;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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;So a total of two functions: one to parse the overall receipt, and one to parse each in-app purchase receipt nested &lt;em&gt;within&lt;/em&gt; the overall receipt.&lt;/p&gt;
&lt;p&gt;Now comes the hard part. Actually doing all the decoding. Don&amp;rsquo;t forget the &lt;a href=&#34;#parsing-strategy&#34;&gt;strategy&lt;/a&gt; we&amp;rsquo;re going to take! That&amp;rsquo;ll help you walk through this code without getting insanely overwhelmed (hopefully).&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;parse-function-implementation&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;parse-function-implementation&#34;&gt;&lt;code&gt;parse&lt;/code&gt; function implementation&lt;/h4&gt;
&lt;p&gt;First, the implementation of &lt;code&gt;parse(_:)&lt;/code&gt; with comments throughout to help you find where each step of the &lt;a href=&#34;#parsing-strategy&#34;&gt;strategy&lt;/a&gt; is being implemented:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;  1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;  2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;  3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;  4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;  5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;  6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;  7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;  8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;  9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 11
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 12
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 13
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 14
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 15
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 16
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 17
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 18
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 19
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 20
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 21
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 22
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 23
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 24
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 25
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 26
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 27
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 28
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 29
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 30
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 31
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 32
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 33
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 34
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 35
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 36
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 37
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 38
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 39
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 40
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 41
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 42
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 43
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 44
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 45
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 46
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 47
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 48
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 49
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 50
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 51
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 52
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 53
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 54
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 55
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 56
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 57
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 58
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 59
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 60
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 61
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 62
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 63
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 64
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 65
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 66
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 67
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 68
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 69
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 70
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 71
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 72
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 73
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 74
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 75
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 76
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 77
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 78
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 79
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 80
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 81
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 82
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 83
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 84
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 85
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 86
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 87
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 88
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 89
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 90
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 91
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 92
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 93
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 94
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 95
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 96
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 97
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 98
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 99
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;100
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;101
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;102
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;103
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;104
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;105
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;106
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;107
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;108
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;109
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;110
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;parse&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;PKCS7Container&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;UnsafeMutablePointer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;PKCS7&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;)&lt;/span&gt; &lt;span class=&#34;kr&#34;&gt;throws&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ParsedReceipt&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;bundleIdentifier&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;bundleIdData&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSData&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;appVersion&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;opaqueValue&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSData&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;sha1Hash&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSData&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;inAppPurchaseReceipts&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;ParsedInAppPurchaseReceipt&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;originalAppVersion&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;receiptCreationDate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Date&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;expirationDate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Date&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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Strategy Step 1: Preliminary structure checks&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Must have data to work with&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;k&#34;&gt;guard&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;contents&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;PKCS7Container&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;pointee&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;d&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;sign&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;pointee&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;contents&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;octets&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;contents&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;pointee&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;d&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;data&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;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;throw&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ReceiptValidationError&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;malformedReceipt&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Determine the start and end of the receipt payload&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;currentASN1PayloadLocation&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;UnsafePointer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;octets&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;pointee&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;data&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;endOfPayload&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;currentASN1PayloadLocation&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;!.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;advanced&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;by&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;n&#34;&gt;octets&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;pointee&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;length&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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;type&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;Int32&lt;/span&gt;&lt;span class=&#34;p&#34;&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;xclass&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;Int32&lt;/span&gt;&lt;span class=&#34;p&#34;&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;length&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;    &lt;span class=&#34;n&#34;&gt;ASN1_get_object&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;currentASN1PayloadLocation&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;length&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;&amp;amp;&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;p&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;xclass&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;n&#34;&gt;octets&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;pointee&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;length&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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Payload must be an ASN1 Set&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;k&#34;&gt;guard&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;V_ASN1_SET&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;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;throw&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ReceiptValidationError&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;malformedReceipt&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Decode Payload&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Strategy Step 2: Walk through payload (ASN1 Set) and parse each ASN1 Sequence &lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// within (ASN1 Sets contain one or more ASN1 Sequences)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;k&#34;&gt;while&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;currentASN1PayloadLocation&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;endOfPayload&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;cl&#34;&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;c1&#34;&gt;// Get next ASN1 Sequence&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;ASN1_get_object&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;#&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;038&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;currentASN1PayloadLocation&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;#&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;038&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;length&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;#&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;038&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;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;#&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;038&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;xclass&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;currentASN1PayloadLocation&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;!.&lt;/span&gt;&lt;span class=&#34;bp&#34;&gt;distance&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;to&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;endOfPayload&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;cl&#34;&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;c1&#34;&gt;// ASN1 Object type must be an ASN1 Sequence&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;guard&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;V_ASN1_SEQUENCE&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;cl&#34;&gt;            &lt;span class=&#34;k&#34;&gt;throw&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ReceiptValidationError&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;malformedReceipt&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;c1&#34;&gt;// Attribute type of ASN1 Sequence must be an Integer&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;guard&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;attributeType&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;DecodeASN1Integer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;startOfInt&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;#&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;038&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;currentASN1PayloadLocation&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;length&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;currentASN1PayloadLocation&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;!.&lt;/span&gt;&lt;span class=&#34;bp&#34;&gt;distance&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;to&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;endOfPayload&lt;/span&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;cl&#34;&gt;            &lt;span class=&#34;k&#34;&gt;throw&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ReceiptValidationError&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;malformedReceipt&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;c1&#34;&gt;// Attribute version of ASN1 Sequence must be an Integer&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;guard&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;DecodeASN1Integer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;startOfInt&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;#&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;038&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;currentASN1PayloadLocation&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;length&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;currentASN1PayloadLocation&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;!.&lt;/span&gt;&lt;span class=&#34;bp&#34;&gt;distance&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;to&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;endOfPayload&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;kc&#34;&gt;nil&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;cl&#34;&gt;            &lt;span class=&#34;k&#34;&gt;throw&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ReceiptValidationError&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;malformedReceipt&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;c1&#34;&gt;// Get ASN1 Sequence value&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;ASN1_get_object&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;#&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;038&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;currentASN1PayloadLocation&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;#&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;038&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;length&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;#&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;038&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;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;#&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;038&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;xclass&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;currentASN1PayloadLocation&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;!.&lt;/span&gt;&lt;span class=&#34;bp&#34;&gt;distance&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;to&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;endOfPayload&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;cl&#34;&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;c1&#34;&gt;// ASN1 Sequence value must be an ASN1 Octet String&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;guard&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;V_ASN1_OCTET_STRING&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;cl&#34;&gt;            &lt;span class=&#34;k&#34;&gt;throw&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ReceiptValidationError&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;malformedReceipt&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;c1&#34;&gt;// Strategy Step 3: Decode attributes&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;attributeType&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;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;case&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;2&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;cl&#34;&gt;            &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;startOfBundleId&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;currentASN1PayloadLocation&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;            &lt;span class=&#34;n&#34;&gt;bundleIdData&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSData&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;bytes&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;startOfBundleId&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;length&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;length&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;cl&#34;&gt;            &lt;span class=&#34;n&#34;&gt;bundleIdentifier&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;DecodeASN1String&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;startOfString&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;#&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;038&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;startOfBundleId&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;length&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;length&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;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;case&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;3&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;cl&#34;&gt;            &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;startOfAppVersion&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;currentASN1PayloadLocation&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;            &lt;span class=&#34;n&#34;&gt;appVersion&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;DecodeASN1String&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;startOfString&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;#&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;038&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;startOfAppVersion&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;length&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;length&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;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;case&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;4&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;cl&#34;&gt;            &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;startOfOpaqueValue&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;currentASN1PayloadLocation&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;            &lt;span class=&#34;n&#34;&gt;opaqueValue&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSData&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;bytes&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;startOfOpaqueValue&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;length&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;length&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;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;case&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;5&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;cl&#34;&gt;            &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;startOfSha1Hash&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;currentASN1PayloadLocation&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;            &lt;span class=&#34;n&#34;&gt;sha1Hash&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSData&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;bytes&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;startOfSha1Hash&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;length&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;length&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;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;case&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;17&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;cl&#34;&gt;            &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;startOfInAppPurchaseReceipt&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;currentASN1PayloadLocation&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;iapReceipt&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;try&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;parseInAppPurchaseReceipt&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;currentInAppPurchaseASN1PayloadLocation&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;#&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;038&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;startOfInAppPurchaseReceipt&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;payloadLength&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;length&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;cl&#34;&gt;            &lt;span class=&#34;n&#34;&gt;inAppPurchaseReceipts&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;append&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;iapReceipt&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;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;case&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;12&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;cl&#34;&gt;            &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;startOfReceiptCreationDate&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;currentASN1PayloadLocation&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;            &lt;span class=&#34;n&#34;&gt;receiptCreationDate&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;DecodeASN1Date&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;startOfDate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;#&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;038&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;startOfReceiptCreationDate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;length&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;length&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;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;case&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;19&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;cl&#34;&gt;            &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;startOfOriginalAppVersion&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;currentASN1PayloadLocation&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;            &lt;span class=&#34;n&#34;&gt;originalAppVersion&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;DecodeASN1String&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;startOfString&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;#&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;038&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;startOfOriginalAppVersion&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;length&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;length&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;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;case&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;21&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;cl&#34;&gt;            &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;startOfExpirationDate&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;currentASN1PayloadLocation&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;            &lt;span class=&#34;n&#34;&gt;expirationDate&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;DecodeASN1Date&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;startOfDate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;#&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;038&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;startOfExpirationDate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;length&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;length&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;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;cl&#34;&gt;            &lt;span class=&#34;k&#34;&gt;break&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;currentASN1PayloadLocation&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;currentASN1PayloadLocation&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;?.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;advanced&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;by&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;length&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;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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Strategy Step 4: Return ParsedReceipt&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;ParsedReceipt&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;bundleIdentifier&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;bundleIdentifier&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;cl&#34;&gt;                            &lt;span class=&#34;n&#34;&gt;bundleIdData&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;bundleIdData&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;cl&#34;&gt;                            &lt;span class=&#34;n&#34;&gt;appVersion&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;appVersion&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;cl&#34;&gt;                            &lt;span class=&#34;n&#34;&gt;opaqueValue&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;opaqueValue&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;cl&#34;&gt;                            &lt;span class=&#34;n&#34;&gt;sha1Hash&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;sha1Hash&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;cl&#34;&gt;                            &lt;span class=&#34;n&#34;&gt;inAppPurchaseReceipts&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;inAppPurchaseReceipts&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;cl&#34;&gt;                            &lt;span class=&#34;n&#34;&gt;originalAppVersion&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;originalAppVersion&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;cl&#34;&gt;                            &lt;span class=&#34;n&#34;&gt;receiptCreationDate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;receiptCreationDate&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;cl&#34;&gt;                            &lt;span class=&#34;n&#34;&gt;expirationDate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;expirationDate&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;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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Aside from the work with pointers and the Open SSL function names, the strategy is pretty straight-forward when you look it from a bird&amp;rsquo;s-eye point of view.&lt;/p&gt;
&lt;p&gt;Once again, if you&amp;rsquo;re curious about how I knew to map each &lt;code&gt;case&lt;/code&gt; within the &lt;code&gt;switch&lt;/code&gt; to the correct receipt attribute, I simply got them from &lt;a href=&#34;https://developer.apple.com/library/content/releasenotes/General/ValidateAppStoreReceipt/Chapters/ReceiptFields.html&#34;&gt;Apple&amp;rsquo;s documentation&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;parse-in-app-purchase-receipt-implementation&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;parseinapppurchaserectipt-function-implementation&#34;&gt;&lt;code&gt;parseInAppPurchaseRectipt&lt;/code&gt; function implementation&lt;/h4&gt;
&lt;p&gt;Now it&amp;rsquo;s time to see how to parse an in-app purchase receipt payload. Take a look:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;  1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;  2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;  3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;  4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;  5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;  6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;  7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;  8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;  9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 11
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 12
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 13
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 14
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 15
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 16
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 17
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 18
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 19
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 20
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 21
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 22
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 23
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 24
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 25
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 26
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 27
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 28
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 29
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 30
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 31
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 32
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 33
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 34
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 35
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 36
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 37
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 38
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 39
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 40
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 41
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 42
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 43
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 44
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 45
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 46
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 47
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 48
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 49
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 50
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 51
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 52
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 53
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 54
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 55
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 56
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 57
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 58
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 59
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 60
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 61
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 62
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 63
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 64
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 65
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 66
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 67
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 68
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 69
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 70
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 71
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 72
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 73
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 74
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 75
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 76
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 77
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 78
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 79
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 80
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 81
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 82
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 83
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 84
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 85
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 86
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 87
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 88
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 89
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 90
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 91
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 92
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 93
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 94
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 95
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 96
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 97
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 98
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 99
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;100
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;parseInAppPurchaseReceipt&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;currentInAppPurchaseASN1PayloadLocation&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;kr&#34;&gt;inout&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;UnsafePointer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;UInt8&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;?,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;payloadLength&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;kr&#34;&gt;throws&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ParsedInAppPurchaseReceipt&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;quantity&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&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;productIdentifier&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;transactionIdentifier&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;originalTransactionIdentifier&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;purchaseDate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Date&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;originalPurchaseDate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Date&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;subscriptionExpirationDate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Date&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;cancellationDate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Date&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;webOrderLineItemId&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&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Find the end of the in-app purchase receipt payload&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;endOfPayload&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;currentInAppPurchaseASN1PayloadLocation&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;!.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;advanced&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;by&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;payloadLength&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;type&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;Int32&lt;/span&gt;&lt;span class=&#34;p&#34;&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;xclass&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;Int32&lt;/span&gt;&lt;span class=&#34;p&#34;&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;length&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;    &lt;span class=&#34;n&#34;&gt;ASN1_get_object&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;currentInAppPurchaseASN1PayloadLocation&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;length&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;&amp;amp;&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;p&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;xclass&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;payloadLength&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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Payload must be an ASN1 Set&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;k&#34;&gt;guard&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;V_ASN1_SET&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;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;throw&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ReceiptValidationError&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;malformedInAppPurchaseReceipt&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Decode Payload&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Step through payload (ASN1 Set) and parse each ASN1 Sequence within (ASN1 Sets contain one or more ASN1 Sequences)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;k&#34;&gt;while&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;currentInAppPurchaseASN1PayloadLocation&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;endOfPayload&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;cl&#34;&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;c1&#34;&gt;// Get next ASN1 Sequence&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;ASN1_get_object&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;#&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;038&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;currentInAppPurchaseASN1PayloadLocation&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;#&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;038&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;length&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;#&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;038&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;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;#&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;038&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;xclass&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;currentInAppPurchaseASN1PayloadLocation&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;!.&lt;/span&gt;&lt;span class=&#34;bp&#34;&gt;distance&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;to&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;endOfPayload&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;cl&#34;&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;c1&#34;&gt;// ASN1 Object type must be an ASN1 Sequence&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;guard&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;V_ASN1_SEQUENCE&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;cl&#34;&gt;            &lt;span class=&#34;k&#34;&gt;throw&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ReceiptValidationError&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;malformedInAppPurchaseReceipt&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;c1&#34;&gt;// Attribute type of ASN1 Sequence must be an Integer&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;guard&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;attributeType&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;DecodeASN1Integer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;startOfInt&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;#&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;038&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;currentInAppPurchaseASN1PayloadLocation&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;length&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;currentInAppPurchaseASN1PayloadLocation&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;!.&lt;/span&gt;&lt;span class=&#34;bp&#34;&gt;distance&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;to&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;endOfPayload&lt;/span&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;cl&#34;&gt;            &lt;span class=&#34;k&#34;&gt;throw&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ReceiptValidationError&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;malformedInAppPurchaseReceipt&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;c1&#34;&gt;// Attribute version of ASN1 Sequence must be an Integer&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;guard&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;DecodeASN1Integer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;startOfInt&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;#&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;038&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;currentInAppPurchaseASN1PayloadLocation&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;length&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;currentInAppPurchaseASN1PayloadLocation&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;!.&lt;/span&gt;&lt;span class=&#34;bp&#34;&gt;distance&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;to&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;endOfPayload&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;kc&#34;&gt;nil&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;cl&#34;&gt;            &lt;span class=&#34;k&#34;&gt;throw&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ReceiptValidationError&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;malformedInAppPurchaseReceipt&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;c1&#34;&gt;// Get ASN1 Sequence value&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;ASN1_get_object&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;#&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;038&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;currentInAppPurchaseASN1PayloadLocation&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;#&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;038&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;length&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;#&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;038&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;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;#&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;038&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;xclass&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;currentInAppPurchaseASN1PayloadLocation&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;!.&lt;/span&gt;&lt;span class=&#34;bp&#34;&gt;distance&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;to&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;endOfPayload&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;cl&#34;&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;c1&#34;&gt;// ASN1 Sequence value must be an ASN1 Octet String&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;guard&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;V_ASN1_OCTET_STRING&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;cl&#34;&gt;            &lt;span class=&#34;k&#34;&gt;throw&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ReceiptValidationError&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;malformedInAppPurchaseReceipt&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;c1&#34;&gt;// Decode attributes&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;attributeType&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;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;case&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;1701&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;cl&#34;&gt;            &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;startOfQuantity&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;currentInAppPurchaseASN1PayloadLocation&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;            &lt;span class=&#34;n&#34;&gt;quantity&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;DecodeASN1Integer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;startOfInt&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;#&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;038&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;startOfQuantity&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;length&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;length&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;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;case&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;1702&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;cl&#34;&gt;            &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;startOfProductIdentifier&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;currentInAppPurchaseASN1PayloadLocation&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;            &lt;span class=&#34;n&#34;&gt;productIdentifier&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;DecodeASN1String&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;startOfString&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;#&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;038&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;startOfProductIdentifier&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;length&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;length&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;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;case&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;1703&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;cl&#34;&gt;            &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;startOfTransactionIdentifier&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;currentInAppPurchaseASN1PayloadLocation&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;            &lt;span class=&#34;n&#34;&gt;transactionIdentifier&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;DecodeASN1String&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;startOfString&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;#&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;038&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;startOfTransactionIdentifier&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;length&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;length&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;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;case&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;1705&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;cl&#34;&gt;            &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;startOfOriginalTransactionIdentifier&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;currentInAppPurchaseASN1PayloadLocation&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;            &lt;span class=&#34;n&#34;&gt;originalTransactionIdentifier&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;DecodeASN1String&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;startOfString&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;#&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;038&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;startOfOriginalTransactionIdentifier&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;length&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;length&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;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;case&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;1704&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;cl&#34;&gt;            &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;startOfPurchaseDate&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;currentInAppPurchaseASN1PayloadLocation&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;            &lt;span class=&#34;n&#34;&gt;purchaseDate&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;DecodeASN1Date&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;startOfDate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;#&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;038&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;startOfPurchaseDate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;length&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;length&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;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;case&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;1706&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;cl&#34;&gt;            &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;startOfOriginalPurchaseDate&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;currentInAppPurchaseASN1PayloadLocation&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;            &lt;span class=&#34;n&#34;&gt;originalPurchaseDate&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;DecodeASN1Date&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;startOfDate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;#&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;038&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;startOfOriginalPurchaseDate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;length&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;length&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;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;case&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;1708&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;cl&#34;&gt;            &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;startOfSubscriptionExpirationDate&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;currentInAppPurchaseASN1PayloadLocation&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;            &lt;span class=&#34;n&#34;&gt;subscriptionExpirationDate&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;DecodeASN1Date&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;startOfDate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;#&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;038&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;startOfSubscriptionExpirationDate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;length&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;length&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;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;case&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;1712&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;cl&#34;&gt;            &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;startOfCancellationDate&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;currentInAppPurchaseASN1PayloadLocation&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;            &lt;span class=&#34;n&#34;&gt;cancellationDate&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;DecodeASN1Date&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;startOfDate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;#&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;038&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;startOfCancellationDate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;length&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;length&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;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;case&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;1711&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;cl&#34;&gt;            &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;startOfWebOrderLineItemId&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;currentInAppPurchaseASN1PayloadLocation&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;            &lt;span class=&#34;n&#34;&gt;webOrderLineItemId&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;DecodeASN1Integer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;startOfInt&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;#&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;038&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;startOfWebOrderLineItemId&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;length&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;length&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;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;cl&#34;&gt;            &lt;span class=&#34;k&#34;&gt;break&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;currentInAppPurchaseASN1PayloadLocation&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;currentInAppPurchaseASN1PayloadLocation&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;!.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;advanced&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;by&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;length&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;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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;ParsedInAppPurchaseReceipt&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;quantity&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;quantity&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;cl&#34;&gt;                                        &lt;span class=&#34;n&#34;&gt;productIdentifier&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;productIdentifier&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;cl&#34;&gt;                                        &lt;span class=&#34;n&#34;&gt;transactionIdentifier&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;transactionIdentifier&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;cl&#34;&gt;                                        &lt;span class=&#34;n&#34;&gt;originalTransactionIdentifier&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;originalTransactionIdentifier&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;cl&#34;&gt;                                        &lt;span class=&#34;n&#34;&gt;purchaseDate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;purchaseDate&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;cl&#34;&gt;                                        &lt;span class=&#34;n&#34;&gt;originalPurchaseDate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;originalPurchaseDate&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;cl&#34;&gt;                                        &lt;span class=&#34;n&#34;&gt;subscriptionExpirationDate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;subscriptionExpirationDate&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;cl&#34;&gt;                                        &lt;span class=&#34;n&#34;&gt;cancellationDate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;cancellationDate&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;cl&#34;&gt;                                        &lt;span class=&#34;n&#34;&gt;webOrderLineItemId&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;webOrderLineItemId&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;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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;As you can see, parsing an in-app purchase receipt uses the same strategy as parsing the overall receipt does.&lt;/p&gt;
&lt;p&gt;A receipt may contain zero or more in-app purchase receipts, so this function may get called zero, one, or many times, depending on what your app offers as in-app purchases, and of course, what your users have actually purchased.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;final-receipt-parser&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;final-receiptparser&#34;&gt;Final &lt;code&gt;ReceiptParser&lt;/code&gt;&lt;/h2&gt;
&lt;p&gt;I realize that breaking the code apart like I&amp;rsquo;ve done is good for teaching purposes, but perhaps not so much for &amp;ldquo;I just wanna copy-paste and &lt;em&gt;use&lt;/em&gt; this&amp;rdquo; purposes.&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;ll spare you having to scroll through &lt;em&gt;all&lt;/em&gt; that code again. If you&amp;rsquo;d like to see the full &lt;code&gt;ReceiptParser&lt;/code&gt;, &lt;a href=&#34;https://github.com/andrewcbancroft/SwiftyLocalReceiptValidator&#34;&gt;check out the Swifty Local Receipt Validator repo on GitHub&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;using-receipt-parser&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;using-receiptparser&#34;&gt;Using &lt;code&gt;ReceiptParser&lt;/code&gt;&lt;/h2&gt;
&lt;p&gt;I initialize an instance of &lt;code&gt;ReceiptParser&lt;/code&gt; in my &lt;a href=&#34;https://www.andrewcbancroft.com/2015/10/13/loading-a-receipt-for-validation-with-swift/#receipt-validator&#34;&gt;&lt;code&gt;ReceiptValidator&lt;/code&gt; struct&lt;/a&gt;, and then call the &lt;code&gt;parse(_:)&lt;/code&gt; function from &lt;code&gt;validateReceipt()&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;12
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;13
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;14
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;15
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;16
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;17
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;18
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;19
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;20
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;21
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;22
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;23
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;24
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;25
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;26
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;enum&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;ReceiptValidationResult&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;cl&#34;&gt;    &lt;span class=&#34;k&#34;&gt;case&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;success&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;ParsedReceipt&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;// Now has ParsedReceipt for an associated value&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;error&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;ReceiptValidationError&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;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;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;struct&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;ReceiptValidator&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;receiptLoader&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ReceiptLoader&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;receiptExtractor&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ReceiptExtractor&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;receiptSignatureValidator&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ReceiptSignatureValidator&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;receiptParser&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ReceiptParser&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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;validateReceipt&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;ReceiptValidationResult&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;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;cl&#34;&gt;            &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;receiptData&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;try&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;receiptLoader&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;loadReceipt&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;cl&#34;&gt;            &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;receiptContainer&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;try&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;receiptExtractor&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;extractPKCS7Container&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;receiptData&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;cl&#34;&gt;            
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;receiptSignatureValidator&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;checkSignaturePresence&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;receiptContainer&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;cl&#34;&gt;            &lt;span class=&#34;k&#34;&gt;try&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;receiptSignatureValidator&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;checkSignatureAuthenticity&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;receiptContainer&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;cl&#34;&gt;            
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;parsedReceipt&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;try&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;receiptParser&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;parse&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;receiptContainer&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;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;success&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;parsedReceipt&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;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;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;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;k&#34;&gt;as&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;!&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ReceiptValidationError&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;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;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;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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;a name=&#34;preparing-to-finish-receipt-validation&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;preparing-to-finish-receipt-validation&#34;&gt;Preparing to finish receipt validation!&lt;/h2&gt;
&lt;p&gt;What a journey this has been! We&amp;rsquo;re &lt;em&gt;almost done&lt;/em&gt; with this receipt validation process.&lt;/p&gt;
&lt;p&gt;What&amp;rsquo;s left? After this guide, you still need to&amp;hellip;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;https://www.andrewcbancroft.com/2017/07/31/finalizing-receipt-validation-in-swift-computing-a-guid-hash/&#34;&gt;Compute the GUID hash of your app&lt;/a&gt; to compare with the hash that&amp;rsquo;s found within the receipt.&lt;/li&gt;
&lt;li&gt;You&amp;rsquo;ll also have to inspect the receipt data to perform any app-specific verification steps.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;We&amp;rsquo;re that much closer now though! See you next time.&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...
  &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/10/05/preparing-to-test-receipt-validation-for-ios/&#34; title=&#34;Preparing to Test Receipt Validation for iOS&#34;&gt;Preparing to Test Receipt Validation for iOS&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/10/13/loading-a-receipt-for-validation-with-swift/&#34; title=&#34;Loading a Receipt for Validation with Swift&#34;&gt;Loading a Receipt for Validation with 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/2015/09/21/openssl-for-ios-swift-the-easy-way/&#34; title=&#34;OpenSSL for iOS &amp;#038; Swift the Easy Way&#34;&gt;OpenSSL for iOS &amp; Swift the Easy Way&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/06/09/extracting-a-pkcs7-container-for-receipt-validation-with-swift/&#34; title=&#34;Extracting a PKCS7 Container for Receipt Validation with Swift&#34;&gt;Extracting a PKCS7 Container for Receipt Validation with 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/2017/07/16/receipt-validation-verifying-a-receipt-signature-in-swift/&#34; title=&#34;Receipt Validation – Verifying a Receipt Signature in Swift&#34;&gt;Receipt Validation – Verifying a Receipt Signature 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/2017/07/31/finalizing-receipt-validation-in-swift-computing-a-guid-hash/&#34; title=&#34;Finalizing Receipt Validation in Swift – Computing a GUID Hash&#34;&gt;Finalizing Receipt Validation in Swift – Computing a GUID Hash&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>Receipt Validation – Verifying a Receipt Signature in Swift</title>
      <link>https://www.andrewcbancroft.com/2017/07/16/receipt-validation-verifying-a-receipt-signature-in-swift/</link>
      <pubDate>Sun, 16 Jul 2017 21:43:21 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2017/07/16/receipt-validation-verifying-a-receipt-signature-in-swift/</guid>
      <description>&lt;ul&gt;
&lt;li&gt;You&amp;rsquo;ve &lt;a href=&#34;https://www.andrewcbancroft.com/2015/10/05/preparing-to-test-receipt-validation-for-ios/&#34;&gt;prepared to test receipt validation&lt;/a&gt; by setting up your app in iTunes Connect.&lt;/li&gt;
&lt;li&gt;You&amp;rsquo;ve brought in a cryptography library like OpenSSL to be able to work with the PKCS #7 container that acts as the &amp;ldquo;envelope” for the receipt. Perhaps you&amp;rsquo;ve even done it &lt;a href=&#34;https://www.andrewcbancroft.com/2015/09/21/openssl-for-ios-swift-the-easy-way/&#34;&gt;the &amp;ldquo;easy way” with CocoaPods&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;You&amp;rsquo;ve &lt;a href=&#34;https://www.andrewcbancroft.com/2015/10/13/loading-a-receipt-for-validation-with-swift/&#34;&gt;located and loaded&lt;/a&gt; the receipt for validation.&lt;/li&gt;
&lt;li&gt;You&amp;rsquo;ve &lt;a href=&#34;https://www.andrewcbancroft.com/2016/06/09/extracting-a-pkcs7-container-for-receipt-validation-with-swift/&#34;&gt;extracted the PKCS #7 container&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The aim of this guide is to help you take a look &lt;em&gt;inside&lt;/em&gt; the PKCS #7 container, and verify the presence and authenticity of the signature on the receipt.&lt;/p&gt;
&lt;p&gt;Just want the code? Here you go!&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;fab fa-github fa-lg&#34;&gt;&lt;/i&gt; &lt;a href=&#34;https://github.com/andrewcbancroft/SwiftyLocalReceiptValidator&#34;&gt;Swifty Local Receipt Validator&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;
&lt;p&gt;&lt;a name=&#34;recap&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;recap-from-the-previous-guide&#34;&gt;Recap from the previous guide&lt;/h2&gt;
&lt;p&gt;In &lt;a href=&#34;https://www.andrewcbancroft.com/2015/10/13/loading-a-receipt-for-validation-with-swift/&#34;&gt;Loading a Receipt for Validation with Swift&lt;/a&gt;, I began the process of breaking out the various steps of the receipt validation process into separate single-responsibility structs with clearly named functions to help clarify what each piece of code is doing.&lt;/p&gt;
&lt;p&gt;Recall that I’ve &lt;a href=&#34;https://www.andrewcbancroft.com/2015/10/13/loading-a-receipt-for-validation-with-swift/#receipt-validator-implementation&#34;&gt;created a main Type called &lt;code&gt;ReceiptValidator&lt;/code&gt;&lt;/a&gt;, with references to several smaller single-responsibility Types that it uses to accomplish the overall validation process.&lt;/p&gt;
&lt;p&gt;Accordingly, I’ve &lt;a href=&#34;https://www.andrewcbancroft.com/2015/10/13/loading-a-receipt-for-validation-with-swift/#receipt-loader-implementation&#34;&gt;created a ReceiptLoader&lt;/a&gt; that finds the receipt on the file system and loads it into memory.&lt;/p&gt;
&lt;p&gt;As of the last entry in this series of guides, I&amp;rsquo;ve also &lt;a href=&#34;https://www.andrewcbancroft.com/2016/06/09/extracting-a-pkcs7-container-for-receipt-validation-with-swift/#receiptextractor-implementation&#34;&gt;got a &lt;code&gt;ReceiptExtractor&lt;/code&gt;&lt;/a&gt; to extract the receipt contents from its PKCS #7 container.&lt;/p&gt;
&lt;p&gt;If a validation step ever fails along the way, I’ve decided to take advantage of Swift’s error throwing features to clearly describe what failed. So far, there’s only two cases:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;4
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;enum&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;ReceiptValidationError&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&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;couldNotFindReceipt&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;emptyReceiptContents&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;a name=&#34;apple-root-cert&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;preparation-step-download-apples-root-certificate&#34;&gt;Preparation step: Download Apple&amp;rsquo;s root certificate&lt;/h2&gt;
&lt;p&gt;You need a copy of Apple&amp;rsquo;s root certificate in order to fully complete this phase of receipt validation.&lt;/p&gt;
&lt;p&gt;How do you get a copy of it? Great question (with an answer)!&lt;/p&gt;
&lt;p&gt;If you go to &lt;a href=&#34;https://www.apple.com/certificateauthority/&#34;&gt;https://www.apple.com/certificateauthority/&lt;/a&gt;, you can get your hands on a copy by downloading the &amp;ldquo;Apple Inc. Root Certificate” file:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/10/Apple_PKI.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/10/Apple_PKI.png&#34; alt=&#34;Apple Certificate Page&#34; width=&#34;798&#34; height=&#34;688&#34; class=&#34;alignnone size-full wp-image-12375&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/10/Apple_PKI.png 798w, https://www.andrewcbancroft.com/wp-content/uploads/2015/10/Apple_PKI-300x259.png 300w&#34; sizes=&#34;(max-width: 798px) 100vw, 798px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Once you have it, you need to add the certificate to your Xcode project, and add it to your app&amp;rsquo;s target:&lt;br&gt;
&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/06/root_cert_target_membership.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/06/root_cert_target_membership-1024x621.png&#34; alt=&#34;Apple Root Certificate Target Membership&#34; width=&#34;1024&#34; height=&#34;621&#34; class=&#34;alignnone size-large wp-image-12920&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/06/root_cert_target_membership-1024x621.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2016/06/root_cert_target_membership-300x182.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2016/06/root_cert_target_membership.png 1223w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;what-can-go-wrong&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;what-can-go-wrong-with-receipt-signature-verification&#34;&gt;What can go wrong with receipt signature verification?&lt;/h2&gt;
&lt;p&gt;I&amp;rsquo;ll start off the code piece of this guide by asking, &amp;ldquo;What could go wrong?”. That&amp;rsquo;ll help define a few more &lt;code&gt;ReceiptValidationError&lt;/code&gt; cases, and might point us in a direction when it comes to implementing a new Type to use within the &lt;code&gt;ReceiptValidator&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Right off the bat, I can think of two or three things that could go awry at this stage of the receipt validation process:&lt;/p&gt;
&lt;p&gt;1 – The receipt that we loaded may not be signed at all&lt;br&gt;
2 – We don&amp;rsquo;t have a copy of Apple&amp;rsquo;s root certificate to validate the signature with&lt;br&gt;
3 – The signature on the receipt is invalid because it doesn&amp;rsquo;t match against Apple&amp;rsquo;s root certificate&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;ll add those three new error states to the &lt;code&gt;ReceiptValidationError&lt;/code&gt; enum now:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;7
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;enum&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;ReceiptValidationError&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&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;couldNotFindReceipt&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;emptyReceiptContents&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;receiptNotSigned&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;appleRootCertificateNotFound&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;receiptSignatureInvalid&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;a name=&#34;concept&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;receiptsignaturevalidator-concept&#34;&gt;ReceiptSignatureValidator concept&lt;/h2&gt;
&lt;p&gt;Another step, another Type. This has been my strategy so far, so I&amp;rsquo;m stickin&amp;rsquo; to it!&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;m validating the &lt;strong&gt;presence&lt;/strong&gt; and &lt;strong&gt;authenticity&lt;/strong&gt; of the signature on the receipt, so I picked the name &lt;code&gt;ReceiptSignatureValidator&lt;/code&gt; for this one.&lt;/p&gt;
&lt;p&gt;When I identified three new &lt;code&gt;ReceiptValidationError&lt;/code&gt; cases earlier, I had in mind that they could potentially point me in a direction when implementing this new &lt;code&gt;ReceiptSignatureValidator&lt;/code&gt; Type.&lt;/p&gt;
&lt;p&gt;What if this Type had two functions?&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;checkSignaturePresence&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;checkSignatureAuthenticity&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In &lt;code&gt;checkSignaturePresence&lt;/code&gt;, I&amp;rsquo;ll look, and if the receipt isn&amp;rsquo;t signed at all, I&amp;rsquo;ll throw the &lt;code&gt;receiptNotSigned&lt;/code&gt; Error case.&lt;/p&gt;
&lt;p&gt;In &lt;code&gt;checkSignatureAuthenticity&lt;/code&gt;, I&amp;rsquo;ll look, and if the Apple root certificate is missing from the bundle for some reason, I&amp;rsquo;ll throw &lt;code&gt;appleRootCertificateNotFound&lt;/code&gt;. And if the signature on the receipt doesn&amp;rsquo;t jive with Apple&amp;rsquo;s root certificate, I&amp;rsquo;ll throw &lt;code&gt;receiptSignatureInvalid&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Here&amp;rsquo;s the skeleton of the struct:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;9
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;struct&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;ReceiptSignatureValidator&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;checkSignaturePresence&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;PKCS7Container&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;UnsafeMutablePointer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;PKCS7&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;)&lt;/span&gt; &lt;span class=&#34;kr&#34;&gt;throws&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;cl&#34;&gt;        &lt;span class=&#34;c1&#34;&gt;// Implementation coming&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;checkSignatureAuthenticity&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;PKCS7Container&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;UnsafeMutablePointer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;PKCS7&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;)&lt;/span&gt; &lt;span class=&#34;kr&#34;&gt;throws&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;cl&#34;&gt;        &lt;span class=&#34;c1&#34;&gt;// Implementation coming&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Both &lt;code&gt;checkSignaturePresence&lt;/code&gt; and &lt;code&gt;checkSignatureAuthenticity&lt;/code&gt; need to peek into the PKCS #7 container that encapsulates the receipt data, so each function asks for a reference to an &lt;code&gt;UnsafeMutablePointer&amp;lt;PKCS7&amp;gt;&lt;/code&gt; as one of its arguments.&lt;/p&gt;
&lt;p&gt;If you&amp;rsquo;re following along with the series, you&amp;rsquo;ll be glad to know that &lt;a href=&#34;https://www.andrewcbancroft.com/2016/06/09/extracting-a-pkcs7-container-for-receipt-validation-with-swift/#receiptextractor-implementation&#34;&gt;the &lt;code&gt;ReceiptExtractor&lt;/code&gt; that we built previously&lt;/a&gt; has a method called &lt;code&gt;extractPKCS7Container&lt;/code&gt; that actually &lt;em&gt;returns&lt;/em&gt; a &lt;code&gt;UnsafeMutablePointer&amp;lt;PKCS7&amp;gt;&lt;/code&gt;, so you can just use the a call to &lt;code&gt;extractPKCS7Container&lt;/code&gt; with the new &lt;code&gt;ReceiptSignatureValidator&#39;s&lt;/code&gt; functions.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;implementation&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;receiptsignaturevalidator-implementation&#34;&gt;ReceiptSignatureValidator implementation&lt;/h2&gt;
&lt;p&gt;Now to actually &lt;em&gt;implement&lt;/em&gt; &lt;code&gt;ReceiptSignatureValidator&lt;/code&gt;…&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;signature-presence&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;checking-signature-presence&#34;&gt;Checking signature presence&lt;/h3&gt;
&lt;p&gt;Checking for the presence of a signature is actually relatively simple. Take a look:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;12
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;13
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;struct&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;ReceiptSignatureValidator&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;checkSignaturePresence&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;PKCS7Container&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;UnsafeMutablePointer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;PKCS7&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;)&lt;/span&gt; &lt;span class=&#34;kr&#34;&gt;throws&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;cl&#34;&gt;        &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;pkcs7SignedTypeCode&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;OBJ_obj2nid&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;PKCS7Container&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;pointee&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&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;guard&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;pkcs7SignedTypeCode&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;==&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NID_pkcs7_signed&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;cl&#34;&gt;            &lt;span class=&#34;k&#34;&gt;throw&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ReceiptValidationError&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;receiptNotSigned&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;checkSignatureAuthenticity&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;PKCS7Container&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;UnsafeMutablePointer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;PKCS7&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;)&lt;/span&gt; &lt;span class=&#34;kr&#34;&gt;throws&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;cl&#34;&gt;        &lt;span class=&#34;c1&#34;&gt;// Implementation coming&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The PKCS #7 container has a type code associated with it if it&amp;rsquo;s &lt;em&gt;signed&lt;/em&gt;. All we need to do is access that type code, and compare it against the &lt;code&gt;NID_pkcs7_signed&lt;/code&gt; constant.&lt;/p&gt;
&lt;p&gt;In order to be valid, the receipt &lt;em&gt;must&lt;/em&gt; be signed, so I&amp;rsquo;ve implemented this as a guard.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;signature-authenticity&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;checking-signature-authenticity&#34;&gt;Checking signature authenticity&lt;/h3&gt;
&lt;p&gt;&lt;a name=&#34;load-apple-root-cert&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;loading-apples-root-certificate&#34;&gt;Loading Apple&amp;rsquo;s root certificate&lt;/h4&gt;
&lt;p&gt;Now comes the part where we check whether the signature on the receipt is authentic or not.&lt;/p&gt;
&lt;p&gt;First, we&amp;rsquo;ve got to load up Apple&amp;rsquo;s root certificate (assuming it exists in the app bundle). Here&amp;rsquo;s a function that can be nested inside of &lt;code&gt;ReceiptSignatureValidator&lt;/code&gt; to do the job:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;12
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;13
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;14
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;15
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;16
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;17
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;18
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;19
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;n&#34;&gt;fileprivate&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;loadAppleRootCertificate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;kr&#34;&gt;throws&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;UnsafeMutablePointer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;X509&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&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;k&#34;&gt;guard&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;appleRootCertificateURL&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Bundle&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;main&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;n&#34;&gt;forResource&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;AppleIncRootCertificate&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;withExtension&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;cer&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;cl&#34;&gt;        &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;appleRootCertificateData&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;try&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;?&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Data&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;contentsOf&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;appleRootCertificateURL&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;cl&#34;&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;cl&#34;&gt;            &lt;span class=&#34;k&#34;&gt;throw&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ReceiptValidationError&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;appleRootCertificateNotFound&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;appleRootCertificateBIO&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;BIO_new&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;BIO_s_mem&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;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;    &lt;span class=&#34;n&#34;&gt;BIO_write&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;appleRootCertificateBIO&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;appleRootCertificateData&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;as&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSData&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;).&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;bytes&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;Int32&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;appleRootCertificateData&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;p&#34;&gt;))&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;appleRootCertificateX509&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;d2i_X509_bio&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;appleRootCertificateBIO&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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;appleRootCertificateX509&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;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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This function guards against the absence of Apple&amp;rsquo;s root certificate. If it can&amp;rsquo;t be found in the main bundle, the function throws &lt;code&gt;appleRootCertificateNotFound&lt;/code&gt;. This error is obviously preventable, but hey – never hurts to protect yourself if you&amp;rsquo;re using this code in multiple projects and forget to grab a copy of Apple&amp;rsquo;s root certificate.&lt;/p&gt;
&lt;p&gt;① As long as the .cer file exists in the app bundle, the next step is to create a new in-memory BIO (basic input-output) pointer. That&amp;rsquo;s what &lt;code&gt;let appleRootCertificateBIO = BIO_new(BIO_s_mem())&lt;/code&gt; does.&lt;/p&gt;
&lt;p&gt;② Next, we&amp;rsquo;ve got to write the contents of the certificate to memory so we can work with it:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;n&#34;&gt;BIO_write&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;appleRootCertificateBIO&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;appleRootCertificateData&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;as&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSData&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;).&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;bytes&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;Int32&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;appleRootCertificateData&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;p&#34;&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;code&gt;BIO_write&lt;/code&gt; needs a location to write to, namely, our &lt;code&gt;appleRootCertificateBIO&lt;/code&gt; pointer.&lt;/p&gt;
&lt;p&gt;It also needs to know &lt;em&gt;what&lt;/em&gt; to write: &lt;code&gt;(appleRootCertificateData as NSData).bytes&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Finally, it needs to know the length of the data to write: &lt;code&gt;Int32(appleRootCertificateData.count)&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;③ Once that&amp;rsquo;s complete, we can obtain pointer to an &lt;code&gt;X509&lt;/code&gt;, which will be used for the next step: verifying the authenticity of the signature on the receipt with the x509 certificate from Apple&amp;rsquo;s root certificate authority. &lt;code&gt;let appleRootCertificateX509 = d2i_X509_bio(appleRootCertificateBIO, nil)&lt;/code&gt; gives us our return value!&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;verify-authenticity&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;verifying-signature-authenticity&#34;&gt;Verifying signature authenticity&lt;/h4&gt;
&lt;p&gt;The final step is to take the &lt;code&gt;X509&lt;/code&gt; pointer, and use it to verify the authenticity of the signature on the PKCS #7 Container.&lt;/p&gt;
&lt;p&gt;Once again, here&amp;rsquo;s a function that can take both items and do the work:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;12
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;13
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;14
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;15
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;16
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;17
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;18
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;n&#34;&gt;fileprivate&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;verifyAuthenticity&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;x509Certificate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;UnsafeMutablePointer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;X509&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;PKCS7Container&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;UnsafeMutablePointer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;PKCS7&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;)&lt;/span&gt; &lt;span class=&#34;kr&#34;&gt;throws&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;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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;x509CertificateStore&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;X509_STORE_new&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;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;    &lt;span class=&#34;n&#34;&gt;X509_STORE_add_cert&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;x509CertificateStore&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;x509Certificate&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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;    &lt;span class=&#34;n&#34;&gt;OpenSSL_add_all_digests&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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;result&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;PKCS7_verify&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;PKCS7Container&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;x509CertificateStore&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;kc&#34;&gt;nil&lt;/span&gt;&lt;span class=&#34;p&#34;&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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;    &lt;span class=&#34;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;result&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;!=&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;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;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;throw&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ReceiptValidationError&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;receiptSignatureInvalid&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;① The X509 Store is what holds the information for verification, so we use &lt;code&gt;X509_STORE_new()&lt;/code&gt; to create one.&lt;/p&gt;
&lt;p&gt;② Next, &lt;code&gt;X509_STORE_add_cert&lt;/code&gt; function is used to prepare the X509 Store, and the X509 &lt;em&gt;Certificate&lt;/em&gt; for verification purposes.&lt;/p&gt;
&lt;p&gt;③ OpenSSL keeps an internal table of digest algorithms and ciphers. It uses this table to lookup ciphers via certain functions. &lt;code&gt;OpenSSL_add_all_digests()&lt;/code&gt; is called to load the necessary digest algorithms for verification.&lt;/p&gt;
&lt;p&gt;④ The final step is to use the &lt;code&gt;PKCS7_verify&lt;/code&gt; function, passing it the PKCS #7 Container, and the x509 Certificate Store.&lt;/p&gt;
&lt;p&gt;⑤ &lt;code&gt;PKCS7_Verify&lt;/code&gt; will return &lt;strong&gt;1&lt;/strong&gt; if the signature is valid. If &lt;code&gt;PKCS7_Verify&lt;/code&gt; returns any integer value &lt;em&gt;other than&lt;/em&gt; 1, the signature is to be interpreted as &lt;em&gt;invalid&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;putting-it-all-together&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;putting-it-all-together&#34;&gt;Putting it all together&lt;/h2&gt;
&lt;p&gt;&lt;a name=&#34;final-implementation&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;final-receiptsignaturevalidator-implementation&#34;&gt;Final ReceiptSignatureValidator implementation&lt;/h3&gt;
&lt;p&gt;The final version of the &lt;code&gt;ReceiptSignatureValidator&lt;/code&gt; looks like this:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;12
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;13
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;14
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;15
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;16
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;17
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;18
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;19
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;20
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;21
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;22
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;23
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;24
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;25
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;26
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;27
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;28
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;29
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;30
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;31
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;32
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;33
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;34
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;35
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;36
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;37
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;38
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;39
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;40
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;41
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;42
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;43
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;struct&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;ReceiptSignatureValidator&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;checkSignaturePresence&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;PKCS7Container&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;UnsafeMutablePointer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;PKCS7&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;)&lt;/span&gt; &lt;span class=&#34;kr&#34;&gt;throws&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;cl&#34;&gt;        &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;pkcs7SignedTypeCode&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;OBJ_obj2nid&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;PKCS7Container&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;pointee&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&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;guard&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;pkcs7SignedTypeCode&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;==&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NID_pkcs7_signed&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;cl&#34;&gt;            &lt;span class=&#34;k&#34;&gt;throw&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ReceiptValidationError&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;receiptNotSigned&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;checkSignatureAuthenticity&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;PKCS7Container&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;UnsafeMutablePointer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;PKCS7&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;)&lt;/span&gt; &lt;span class=&#34;kr&#34;&gt;throws&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;cl&#34;&gt;        &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;appleRootCertificateX509&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;try&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;loadAppleRootCertificate&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;cl&#34;&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;verifyAuthenticity&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;appleRootCertificateX509&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;PKCS7Container&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;PKCS7Container&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;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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;n&#34;&gt;fileprivate&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;loadAppleRootCertificate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;kr&#34;&gt;throws&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;UnsafeMutablePointer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;X509&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&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;guard&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;appleRootCertificateURL&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Bundle&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;main&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;n&#34;&gt;forResource&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;AppleIncRootCertificate&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;withExtension&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;cer&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;cl&#34;&gt;            &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;appleRootCertificateData&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;try&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;?&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Data&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;contentsOf&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;appleRootCertificateURL&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;cl&#34;&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;cl&#34;&gt;                &lt;span class=&#34;k&#34;&gt;throw&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ReceiptValidationError&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;appleRootCertificateNotFound&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;appleRootCertificateBIO&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;BIO_new&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;BIO_s_mem&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;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;BIO_write&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;appleRootCertificateBIO&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;appleRootCertificateData&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;as&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSData&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;).&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;bytes&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;Int32&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;appleRootCertificateData&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;p&#34;&gt;))&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;appleRootCertificateX509&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;d2i_X509_bio&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;appleRootCertificateBIO&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;cl&#34;&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;appleRootCertificateX509&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;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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;n&#34;&gt;fileprivate&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;verifyAuthenticity&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;x509Certificate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;UnsafeMutablePointer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;X509&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;PKCS7Container&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;UnsafeMutablePointer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;PKCS7&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;)&lt;/span&gt; &lt;span class=&#34;kr&#34;&gt;throws&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;cl&#34;&gt;        &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;x509CertificateStore&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;X509_STORE_new&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;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;X509_STORE_add_cert&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;x509CertificateStore&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;x509Certificate&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;cl&#34;&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;OpenSSL_add_all_digests&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;cl&#34;&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;result&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;PKCS7_verify&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;PKCS7Container&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;x509CertificateStore&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;kc&#34;&gt;nil&lt;/span&gt;&lt;span class=&#34;p&#34;&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;cl&#34;&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;result&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;!=&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;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;cl&#34;&gt;            &lt;span class=&#34;k&#34;&gt;throw&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ReceiptValidationError&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;receiptSignatureInvalid&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;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;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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;a name=&#34;additions-to-receipt-validator&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;additions-to-receiptvalidator&#34;&gt;Additions to ReceiptValidator&lt;/h3&gt;
&lt;p&gt;The &lt;code&gt;ReceiptValidator&lt;/code&gt; struct that&amp;rsquo;s been growing to accommodate each of the steps now looks like this (additions highlighted):&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;12
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;13
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;14
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;15
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;16
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;17
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;18
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;19
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;struct&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;ReceiptValidator&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;receiptLoader&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ReceiptLoader&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;receiptExtractor&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ReceiptExtractor&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;receiptSignatureValidator&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ReceiptSignatureValidator&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;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;validateReceipt&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;ReceiptValidationResult&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;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;cl&#34;&gt;            &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;receiptData&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;try&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;receiptLoader&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;loadReceipt&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;cl&#34;&gt;            &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;receiptContainer&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;try&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;receiptExtractor&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;extractPKCS7Container&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;receiptData&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;cl&#34;&gt;            
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;receiptSignatureValidator&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;checkSignaturePresence&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;receiptContainer&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;cl&#34;&gt;            &lt;span class=&#34;k&#34;&gt;try&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;receiptSignatureValidator&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;checkSignatureAuthenticity&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;receiptContainer&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;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;success&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;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;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;k&#34;&gt;as&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;!&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ReceiptValidationError&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;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;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;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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;a name=&#34;handling-errors&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;handling-errors&#34;&gt;Handling errors&lt;/h3&gt;
&lt;p&gt;The final piece is to attempt to do something intelligent with any of the possible error conditions that could be included with the &lt;code&gt;ReceiptValidator&lt;/code&gt; validation result. Here&amp;rsquo;s a sample implementation at the call site for &lt;code&gt;validateReceipt()&lt;/code&gt; (probably in a view controller somewhere in your app:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;12
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;13
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;kr&#34;&gt;override&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;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;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;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;validationResult&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;receiptValidator&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;validateReceipt&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;cl&#34;&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;validationResult&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;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;success&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;cl&#34;&gt;        &lt;span class=&#34;c1&#34;&gt;// Enable app features&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;error&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;error&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;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;n&#34;&gt;error&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;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;receiptRequest&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;start&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;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;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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;In the case where the receipt signature is invalid, my only thought right now is to request a new receipt from the app store and attempt to re-validate it.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;upcoming-hurdles&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;upcoming-hurdles&#34;&gt;Upcoming hurdles&lt;/h1&gt;
&lt;p&gt;Wait, there&amp;rsquo;s more? In short, yes. We&amp;rsquo;ve made significant progress, but &lt;a href=&#34;https://developer.apple.com/library/content/releasenotes/General/ValidateAppStoreReceipt/Chapters/ValidateLocally.html#//apple_ref/doc/uid/TP40010573-CH1-SW2&#34;&gt;there&amp;rsquo;s still more work to be done&lt;/a&gt; if you want to fully validate a receipt for your app, or for an in-app purchase.&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/10/05/preparing-to-test-receipt-validation-for-ios/&#34; title=&#34;Preparing to Test Receipt Validation for iOS&#34;&gt;Preparing to Test Receipt Validation for iOS&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/10/13/loading-a-receipt-for-validation-with-swift/&#34; title=&#34;Loading a Receipt for Validation with Swift&#34;&gt;Loading a Receipt for Validation with 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/2015/09/21/openssl-for-ios-swift-the-easy-way/&#34; title=&#34;OpenSSL for iOS &amp;#038; Swift the Easy Way&#34;&gt;OpenSSL for iOS &amp; Swift the Easy Way&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/06/09/extracting-a-pkcs7-container-for-receipt-validation-with-swift/&#34; title=&#34;Extracting a PKCS7 Container for Receipt Validation with Swift&#34;&gt;Extracting a PKCS7 Container for Receipt Validation with 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/2017/07/27/receipt-validation-parsing-a-receipt-with-swift/&#34; title=&#34;Receipt Validation – Parse and Decode a Receipt with Swift&#34;&gt;Receipt Validation – Parse and Decode a Receipt with 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/2017/07/31/finalizing-receipt-validation-in-swift-computing-a-guid-hash/&#34; title=&#34;Finalizing Receipt Validation in Swift – Computing a GUID Hash&#34;&gt;Finalizing Receipt Validation in Swift – Computing a GUID Hash&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>Using an Existing SQLite Store with NSPersistentContainer</title>
      <link>https://www.andrewcbancroft.com/2017/07/10/using-an-existing-sqlite-store-with-nspersistentcontainer/</link>
      <pubDate>Mon, 10 Jul 2017 18:02:05 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2017/07/10/using-an-existing-sqlite-store-with-nspersistentcontainer/</guid>
      <description>&lt;p&gt;If you&amp;rsquo;ve been creating the Core Data Stack &lt;a href=&#34;https://www.andrewcbancroft.com/2017/04/16/creating-the-core-data-stack-with-backwards-compatibility-in-swift/&#34;&gt;without NSPersistentContainer&lt;/a&gt; and you&amp;rsquo;re thinking about switching things up to &lt;em&gt;use&lt;/em&gt; &lt;code&gt;NSPersistentContainer&lt;/code&gt;, you need to think about your existing SQLite persistent store.&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://www.andrewcbancroft.com/2017/05/15/a-swift-implementation-of-the-core-data-stack-using-nspersistentcontainer/&#34;&gt;Creating the stack with NSPersistentContainer&lt;/a&gt; handles the creation of SQLite stores internally, so brand new setups using &lt;code&gt;NSPersistentContainer&lt;/code&gt; are pretty easy.&lt;/p&gt;
&lt;p&gt;But how do you go about telling &lt;code&gt;NSPersistentContainer&lt;/code&gt; about your &lt;em&gt;existing&lt;/em&gt; SQLite store?&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s take a look now at how you can configure the container to use your previously-created persistent store.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;before&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;before-nspersistentcontainer&#34;&gt;Before NSPersistentContainer&lt;/h1&gt;
&lt;p&gt;Supposing that your &lt;strong&gt;previous&lt;/strong&gt; Core Data stack creation step pointed to a .sqlite file somewhere in your user&amp;rsquo;s documents directory:&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;// Configure NSPersistentStoreCoordinator with an NSPersistentStore&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;psc&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;model&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;// model instance creation not shown here...&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;storeURL&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;try&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;!&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;FileManager&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 class=&#34;k&#34;&gt;default&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 class=&#34;n&#34;&gt;url&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;k&#34;&gt;for&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;documentDirectory&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&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 class=&#34;n&#34;&gt;userDomainMask&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;appropriateFor&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;create&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&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 class=&#34;n&#34;&gt;appendingPathComponent&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;NameOfDataModel.sqlite&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&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;p&#34;&gt;!&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;psc&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;addPersistentStore&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;ofType&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSSQLiteStoreType&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;configurationName&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;at&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;storeURL&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;a name=&#34;after&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;after-nspersistentcontainer&#34;&gt;After NSPersistentContainer&lt;/h1&gt;
&lt;p&gt;The key to migrating to &lt;code&gt;NSPersistentStore&lt;/code&gt; with an existing SQLite persistent store is the &lt;code&gt;NSPersistentStoreDescription&lt;/code&gt; class. Take a look at the following code to see how to configure an instance of &lt;code&gt;NSPersistentStoreDescription&lt;/code&gt; and assign it to the &lt;code&gt;NSPersistentContainer&lt;/code&gt; instance&amp;rsquo;s &lt;code&gt;persistentStoreDescriptions&lt;/code&gt; property:&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;container&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSPersistentContainer&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;s&#34;&gt;&amp;#34;NameOfDataModel&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; 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;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;storeURL&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;try&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;!&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;FileManager&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 class=&#34;k&#34;&gt;default&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 class=&#34;n&#34;&gt;url&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;k&#34;&gt;for&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;documentDirectory&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&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 class=&#34;n&#34;&gt;userDomainMask&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;appropriateFor&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;create&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&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 class=&#34;n&#34;&gt;appendingPathComponent&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;NameOfDataModel.sqlite&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&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;storeDescription&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSPersistentStoreDescription&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;n&#34;&gt;storeURL&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;container&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;persistentStoreDescriptions&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;storeDescription&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;// Remaining setup for NSPersistentContainer&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;breakdown&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;breaking-it-down&#34;&gt;Breaking it down&lt;/h2&gt;
&lt;p&gt;The &lt;code&gt;let storeURL =&lt;/code&gt; portion of the code is identical in both snippets.&lt;/p&gt;
&lt;p&gt;The difference is how you tell the Stack where the persistent store is located. In times past, you&amp;rsquo;d tell the NSPersistentStore&lt;strong&gt;Coordinator&lt;/strong&gt; this information through its &lt;code&gt;addPersistentStore&lt;/code&gt; method.&lt;/p&gt;
&lt;p&gt;With NSPersistent&lt;strong&gt;Container&lt;/strong&gt;, you need to do two things:&lt;br&gt;
&lt;strong&gt;1&lt;/strong&gt; – Initialize an &lt;code&gt;NSPersistentStoreDescription&lt;/code&gt; instance with the &lt;code&gt;storeURL&lt;/code&gt; (i.e. the URL to where your existing persistent store is located).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;2&lt;/strong&gt; – Assign the &lt;code&gt;NSPersistentStoreDescription&lt;/code&gt; instance to the &lt;code&gt;NSPersistentContainer&#39;s&lt;/code&gt; &lt;code&gt;persistentStoreDescriptions&lt;/code&gt; property.&lt;/p&gt;
&lt;p&gt;One subtlety to note is that the property&amp;rsquo;s name is &lt;em&gt;plural&lt;/em&gt;: persistentStoreDescription&lt;strong&gt;s&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Even though may only a single persistent store description instance to assign, such as in the example code above, &lt;strong&gt;you still need to wrap it in an array&lt;/strong&gt; before you assign it, since it&amp;rsquo;s possible to add more than one description to the container.&lt;/p&gt;
&lt;p&gt;With that adjustment to the &lt;a href=&#34;https://www.andrewcbancroft.com/2017/04/16/creating-the-core-data-stack-with-backwards-compatibility-in-swift/&#34;&gt;&lt;code&gt;NSPersistentContainer&lt;/code&gt; stack creation process&lt;/a&gt;, your app will once again use the persistent store it used to use!&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/2017/04/16/creating-the-core-data-stack-with-backwards-compatibility-in-swift/&#34; title=&#34;Creating the Core Data Stack with Backwards Compatibility in Swift&#34;&lt;/a&gt;Creating the Core Data Stack with Backwards Compatibility in 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/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;/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>Why is it called a “Closure” anyway? A Swift Conceptual Look.</title>
      <link>https://www.andrewcbancroft.com/2017/06/05/why-is-it-called-a-closure-anyway-a-swift-conceptual-look/</link>
      <pubDate>Tue, 06 Jun 2017 04:00:41 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2017/06/05/why-is-it-called-a-closure-anyway-a-swift-conceptual-look/</guid>
      <description>&lt;p&gt;&lt;a name=&#34;whats-in-a-name&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;whats-in-a-name&#34;&gt;What&amp;rsquo;s in a name?&lt;/h1&gt;
&lt;p&gt;If a programming language comes with a named feature, you can be pretty sure it was named that way for a reason. It&amp;rsquo;s a safe bet that the language designers were &lt;em&gt;purposeful&lt;/em&gt; in their naming, so if you can figure out that essential &amp;ldquo;Why&amp;rsquo;d they name it that?” question, you might be that much closer to understanding the feature or concept.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;why-closure&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;why-closure8221&#34;&gt;Why &amp;ldquo;closure”?&lt;/h1&gt;
&lt;p&gt;Now the challenge: Actually figuring out the answer to that &amp;ldquo;Why?” question.&lt;/p&gt;
&lt;p&gt;Swift is being developed in the open, and often times that gives curious minds insight into things like naming decisions. But closures in Swift have been around for longer than Swift has been open sourced.&lt;/p&gt;
&lt;p&gt;That being the case, I really couldn&amp;rsquo;t find much in terms of the conversation around why closures are named &amp;ldquo;closures”.&lt;/p&gt;
&lt;p&gt;Closures exist in other languages, so maybe Swift just &lt;em&gt;borrowed&lt;/em&gt; the name. But that doesn&amp;rsquo;t help us answer the &lt;em&gt;bottom line&lt;/em&gt; reason for naming closures &amp;ldquo;closures”.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;unpacking&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;unpacking-closure8221-thanks-thesauruscom&#34;&gt;Unpacking &amp;ldquo;closure” (thanks, thesaurus.com!)&lt;/h2&gt;
&lt;p&gt;So… I set out on a word adventure to help shed some light on what meaning is to be conveyed by the term &amp;ldquo;closure”.&lt;/p&gt;
&lt;p&gt;It&amp;rsquo;s one thing to know that &amp;ldquo;closures are self-contained blocks of functionality that can be passed around and used in your code.” (&lt;a href=&#34;https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html&#34;&gt;Apple Developer Docs&lt;/a&gt;), but seriously — why not just call them &amp;ldquo;functions”??&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s unpack the word if we can. I put &amp;ldquo;closure” into thesaurus.com, and among the synonyms that I think best clarify the term &amp;ldquo;closure” in programming languages are these:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Plug&lt;/li&gt;
&lt;li&gt;Seal&lt;/li&gt;
&lt;li&gt;Cork&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In this sense, &amp;ldquo;closure” seems to convey the idea of keeping something in… &lt;em&gt;enclosing&lt;/em&gt; it.&lt;/p&gt;
&lt;p&gt;Okay, so what is a closure enclosing? &lt;strong&gt;Great question!&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Short answer? &lt;strong&gt;Variables&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Inevitably, if you peruse the Internet for what closures are, you&amp;rsquo;ll come across similar definitions to Apple&amp;rsquo;s, quoted above. Often, intermingled in the various definitions I&amp;rsquo;ve found, are phrases like &amp;ldquo;capturing &lt;strong&gt;variables&lt;/strong&gt;” or &amp;ldquo;closing over &lt;strong&gt;variables&lt;/strong&gt;&amp;rdquo;.&lt;/p&gt;
&lt;p&gt;These phrases always seemed to muddy the water (for me, anyways). It wasn&amp;rsquo;t entirely clear what &amp;ldquo;capturing variables” or &amp;ldquo;closing over variables” meant. &lt;em&gt;Until&lt;/em&gt; I saw the synonyms, &amp;ldquo;plug”, &amp;ldquo;seal”, &amp;ldquo;cork”.&lt;/p&gt;
&lt;p&gt;Then it clicked…&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;h2 id=&#34;an-example-for-your-imagination&#34;&gt;An example for your imagination&lt;/h2&gt;
&lt;p&gt;I want you to take a function in Swift, and I want you to imagine the function as an empty swimming pool. Got it in your mind&amp;rsquo;s eye? Swimming pool. Empty (for now).&lt;/p&gt;
&lt;p&gt;It&amp;rsquo;s runtime, and your Swift program is happily executing along. Then there&amp;rsquo;s a moment in your program where your function gets called. It begins executing, and suddenly, water comes rushing in filling the pool. A dozen diving coins get tossed into the water as well!&lt;/p&gt;
&lt;p&gt;The coins represent the local variables and constants that your function creates to do its work.&lt;/p&gt;
&lt;p&gt;Your function executes all the way to the &lt;code&gt;return&lt;/code&gt; keyword, and at the precise moment that your function returns, the drain kicks in. Within &lt;em&gt;milliseconds&lt;/em&gt; and with &lt;em&gt;tremendous&lt;/em&gt; force, all the water is sucked out of the pool.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;What do you think will happen to all the coins in the pool?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;For this example, let&amp;rsquo;s just say the answer is, &amp;ldquo;they&amp;rsquo;ll go down the drain”. That would correlate pretty well with what normally happens to variables and constants that had been declared within your function when it returns.&lt;/p&gt;
&lt;p&gt;Any variables and constants that are created inside a function are purged from memory whenever the function returns… down the drain, if you will.&lt;/p&gt;
&lt;h3 id=&#34;unleeeees8230&#34;&gt;Unleeeees…&lt;/h3&gt;
&lt;p&gt;What if, at some point while there&amp;rsquo;s still water in the pool, you were able to dive in, grab a few of the coins, and bottle them up?&lt;/p&gt;
&lt;p&gt;What if you could stick them in a container and &lt;strong&gt;plug&lt;/strong&gt; it with a &lt;strong&gt;cork&lt;/strong&gt; or some other kind of &lt;strong&gt;seal&lt;/strong&gt;?&lt;/p&gt;
&lt;p&gt;If you could do that, the coins would &lt;em&gt;survive&lt;/em&gt; the torrential drainage of the pool when the plug is pulled (i.e. the function returns). And that&amp;rsquo;s what I want you to take away from the example.&lt;/p&gt;
&lt;p&gt;A closure acts as a mechanism for diving into the pool, scooping up the coins (i.e. variables and constants) it needs, and &lt;strong&gt;sealing&lt;/strong&gt; itself, such that whenever its parent function (the pool) drains, it and the coins inside &lt;strong&gt;survive&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;in-practice&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;closures-in-practice&#34;&gt;Closures in practice&lt;/h1&gt;
&lt;p&gt;When you hear that closures are &amp;ldquo;self-contained blocks of functionality that can be passed around and used in your code.”, you may not realize that they can go grab variables from their environment, package them up, and seal themselves tight, thus safeguarding those variables.&lt;/p&gt;
&lt;p&gt;As it turns out, every closure you create has this capability built in.&lt;/p&gt;
&lt;p&gt;Not all closures &lt;em&gt;need&lt;/em&gt; to go out and capture variables, but all closures in Swift &lt;em&gt;can&lt;/em&gt; do so if it&amp;rsquo;s relevant.&lt;/p&gt;
&lt;p&gt;And &lt;em&gt;that&lt;/em&gt; is what makes closures &amp;ldquo;closures”.&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/2017/04/26/what-in-the-world-is-an-escaping-closure-in-swift/&#34; title=&#34;What in the World is an “Escaping Closure” in Swift?&#34;&gt;What in the World is an “Escaping Closure” 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/2017/05/11/why-do-we-need-to-annotate-escaping-closures-in-swift/&#34; title=&#34;Why Do We Need to Annotate Escaping Closures in Swift?&#34;&gt;Why Do We Need to Annotate Escaping Closures in Swift?&lt;/a&gt;
    &lt;/li&gt;&lt;/div&gt; 
&lt;pre&gt;&lt;code&gt;&amp;lt;p&amp;gt;
  &amp;lt;a name=&amp;quot;share&amp;quot; class=&amp;quot;jump-target&amp;quot;&amp;gt;&amp;lt;/a&amp;gt;
&amp;lt;/p&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
</description>
    </item>
    
    <item>
      <title>A Swift Implementation of the Core Data Stack Using NSPersistentContainer</title>
      <link>https://www.andrewcbancroft.com/2017/05/15/a-swift-implementation-of-the-core-data-stack-using-nspersistentcontainer/</link>
      <pubDate>Mon, 15 May 2017 18:19:46 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2017/05/15/a-swift-implementation-of-the-core-data-stack-using-nspersistentcontainer/</guid>
      <description>&lt;p&gt;If you&amp;rsquo;re targeting &lt;strong&gt;iOS 10 and above&lt;/strong&gt;, you&amp;rsquo;ve got the opportunity to pick your poison when it comes to creating the Core Data stack.&lt;/p&gt;
&lt;p&gt;You could opt for the process I described in &lt;a href=&#34;https://www.andrewcbancroft.com/2017/04/16/creating-the-core-data-stack-with-backwards-compatibility-in-swift/&#34;&gt;Creating the Core Data Stack with Backwards Compatibility in Swift&lt;/a&gt; — that process still works perfectly fine in iOS 10+.&lt;/p&gt;
&lt;p&gt;Alternatively, you could use the new &lt;code&gt;NSPersistentContainer&lt;/code&gt; class if you want to write a few less lines of code and keep up-to-date with the latest framework enhancements!&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;the-point&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;whats-the-point-of-the-core-data-stack&#34;&gt;What&amp;rsquo;s the point of the &amp;ldquo;Core Data stack&amp;rdquo;?&lt;/h2&gt;
&lt;p&gt;It&amp;rsquo;s important to keep in mind the fundamental goal with creating the &amp;ldquo;Core Data stack&amp;rdquo;. At the end of the day, we&amp;rsquo;re all just trying to get our hands on an instance of &lt;code&gt;NSManagedObjectContext&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Nearly every Core Data framework Type that we interact with requires an instance of &lt;code&gt;NSManagedObjectContext&lt;/code&gt; to do its work. Whether it&amp;rsquo;s &lt;code&gt;NSEntityDescription&lt;/code&gt; to initialize new &lt;code&gt;NSManagedObject&lt;/code&gt; instances, an &lt;code&gt;NSFetchRequest&lt;/code&gt; to retrieve data, or an &lt;code&gt;NSFetchedResultsController&lt;/code&gt; to keep our UIs in sync, we&amp;rsquo;re always depending on &lt;code&gt;NSManagedObjectContext&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;So the bottom-line goal of setting up the &amp;ldquo;stack&amp;rdquo; is to configure all the plumbing necessary for our apps to work with the &amp;ldquo;under the hood&amp;rdquo; stuff of Core Data. The &lt;em&gt;thing&lt;/em&gt; we interact with constantly is that all-valuable &lt;code&gt;NSManagedObjectContext&lt;/code&gt; instance.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;create&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;creating-the-stack-with-nspersistentcontainer&#34;&gt;Creating the stack with NSPersistentContainer&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;NSPersistentContainer&lt;/code&gt; saves a few keystrokes and abstracts away a few of the moving parts when it comes to setting up the Core Data stack. Compare my &lt;a href=&#34;https://www.andrewcbancroft.com/2017/04/16/creating-the-core-data-stack-with-backwards-compatibility-in-swift/&#34;&gt;Backwards Compatibility&lt;/a&gt; post if you&amp;rsquo;re curious about what&amp;rsquo;s being simplified.&lt;/p&gt;
&lt;p&gt;So&amp;hellip; how&amp;rsquo;s it done?&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;First&lt;/strong&gt;: Make sure you&amp;rsquo;re targeting &lt;strong&gt;iOS 10+, macOS Sierra (10.12)+, watchOS 3+, or tvOS 10+&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;NSPersistentContainer&lt;/code&gt; is only available in Apple&amp;rsquo;s latest platform versions (latest as of May, 2017).&lt;/p&gt;
&lt;p&gt;Once you&amp;rsquo;ve ticked the build target prerequisite above, check out this code example:&lt;/p&gt;
&lt;p&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;12
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;13
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;//Step 1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;container&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSPersistentContainer&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;s&#34;&gt;&amp;#34;NameOfModel&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;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;// Step 2&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;// Happens asynchronously!&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;n&#34;&gt;container&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;loadPersistentStores&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;completionHandler&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;cl&#34;&gt;    &lt;span class=&#34;n&#34;&gt;persistentStoreDescription&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;k&#34;&gt;in&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Step 3&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;k&#34;&gt;guard&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;k&#34;&gt;else&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt; &lt;span class=&#34;bp&#34;&gt;fatalError&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;Failed to load store: &lt;/span&gt;&lt;span class=&#34;si&#34;&gt;\(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;error&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 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;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;mainContext&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;container&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;viewContext&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Use the context to do Core Data stuff in your app&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;a name=&#34;step-by-step&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;step-by-step-through-the-code&#34;&gt;Step-by-step through the code&lt;/h3&gt;
&lt;p&gt;Using &lt;code&gt;NSPersistentContainer&lt;/code&gt; can be seen as a 3 step process.&lt;/p&gt;
&lt;p&gt;1 &amp;ndash; Initialize an &lt;code&gt;NSPersistentContainer&lt;/code&gt; instance. Its only parameter is the &lt;code&gt;name&lt;/code&gt; of the model (look at your .xcdatamodeld file for this)&lt;br&gt;
2 &amp;ndash; Call &lt;code&gt;loadPersistentStores&lt;/code&gt; on the container instance. This function &lt;em&gt;executes asynchronously&lt;/em&gt;, so to hook back in and continue doing things when the function is finished with its work, you supply a completionHandler.&lt;br&gt;
3 &amp;ndash; Guard against errors and use the container&amp;rsquo;s &lt;code&gt;viewContext&lt;/code&gt; property, which is the &lt;code&gt;NSManagedObjectContext&lt;/code&gt; instance you need. I haven&amp;rsquo;t written in detail about this (yet), but in my &lt;a href=&#34;http://bit.ly/ps-core-data-swift&#34;&gt;Pluralsight course on Core Data in Swift&lt;/a&gt;, I go over how to take the managed object context and follow a dependency injection pattern so that you isolate where it&amp;rsquo;s created, and use the single instance throughout your app in a way that&amp;rsquo;s testable.&lt;/p&gt;
&lt;p&gt;One consideration that I would encourage you to think about is the &lt;code&gt;guard&lt;/code&gt; statement. Ask: &amp;ldquo;Is &lt;code&gt;fatalError&lt;/code&gt; really the best thing for me to do?&amp;rdquo;&lt;/p&gt;
&lt;p&gt;A lot of apps that use Core Data really &lt;em&gt;can&amp;rsquo;t&lt;/em&gt; continue past this point if the persistent store can&amp;rsquo;t be loaded. In that case, the &lt;code&gt;guard&lt;/code&gt; may not be so bad. If you &lt;em&gt;can&lt;/em&gt; fall back to an alternate screen that doesn&amp;rsquo;t rely on Core Data though, that might be a more pleasant experience for your users than simply crashing with a fatal error.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;wrap-up&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;wrap-up&#34;&gt;Wrap up&lt;/h2&gt;
&lt;p&gt;This is the simplest way to use &lt;code&gt;NSPersistentContainer&lt;/code&gt; to create your Core Data stack, and that&amp;rsquo;s where I want to leave it for today!&lt;/p&gt;
&lt;p&gt;Thank you, as always, for your interest in the content I&amp;rsquo;m creating!&lt;/p&gt;
&lt;p&gt;And yes, if you&amp;rsquo;re learning Core Data, I would love to contribute to your understanding of the framework. That&amp;rsquo;s why I built &lt;a href=&#34;http://bit.ly/ps-core-data-swift&#34;&gt;Core Data Fundamentals with Swift&lt;/a&gt; for Pluralsight! If you&amp;rsquo;re a subscriber, I&amp;rsquo;d be thrilled to engage with you on the course!&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>Why Do We Need to Annotate Escaping Closures in Swift?</title>
      <link>https://www.andrewcbancroft.com/2017/05/11/why-do-we-need-to-annotate-escaping-closures-in-swift/</link>
      <pubDate>Thu, 11 May 2017 17:58:46 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2017/05/11/why-do-we-need-to-annotate-escaping-closures-in-swift/</guid>
      <description>&lt;p&gt;My last entry on &lt;a href=&#34;https://www.andrewcbancroft.com/2017/04/26/what-in-the-world-is-an-escaping-closure-in-swift/&#34;&gt;escaping closures&lt;/a&gt; ended up having way more interaction than I anticipated! It goes to show that you never know what the impact of a piece of writing will have. But that&amp;rsquo;s a different story… :]&lt;/p&gt;
&lt;p&gt;Several readers commented here and on Medium with the question, &amp;ldquo;Why? Why do we need to mark escaping closures with &lt;code&gt;@escaping&lt;/code&gt;?”&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;do-it-self&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;do-it-yourself-compiler&#34;&gt;Do it yourself, compiler!&lt;/h1&gt;
&lt;p&gt;Observant minds looked at the compiler&amp;rsquo;s ability to say, &amp;ldquo;Hey! This closure can escape – Annotate it with &lt;code&gt;@escaping&lt;/code&gt;!” and asked, &amp;ldquo;Well… &lt;em&gt;why&lt;/em&gt;?? If you, compiler, are smart enough to figure out that I need to annotate it, why can&amp;rsquo;t you just deal with it automatically and let me go about my business?”&lt;/p&gt;
&lt;p&gt;Good. Question.&lt;/p&gt;
&lt;p&gt;And one that I didn&amp;rsquo;t know the answer to. So I researched!&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;insight&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;insight-from-the-swift-gurus&#34;&gt;Insight from the Swift gurus&lt;/h1&gt;
&lt;p&gt;As I dug around in the &lt;a href=&#34;https://github.com/apple/swift&#34;&gt;Swift GitHub repository&lt;/a&gt; and the &lt;a href=&#34;https://lists.swift.org/mailman/listinfo&#34;&gt;Swift Evolution list&lt;/a&gt; I found &lt;a href=&#34;https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160530/019880.html&#34;&gt;a quote from Chris Lattner&lt;/a&gt; that stuck out:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;The compiler has enough logic in it to provide a great QoI (Quality of Implementation) experience when a developer doesn’t think about escaping, and tries to escape a closure – it can provide a fixit that suggests adding @escaping.”&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Did you catch that?&lt;/p&gt;
&lt;p&gt;When the Swift compiler requires us to annotate our APIs with &lt;code&gt;@escaping&lt;/code&gt;, that&amp;rsquo;s its way of doing &amp;ldquo;quality of implementation”.&lt;/p&gt;
&lt;p&gt;Essentially, it&amp;rsquo;s a red flag saying, &amp;ldquo;You really ought to think about this whole escaping thing that you just introduced (whether you knew you were doing it or not)… it has significant impact on your implementation &lt;em&gt;and&lt;/em&gt; on the caller of your function!”&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;help&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;i-see-what-you-did-there8230-let-me-help-you-out8230&#34;&gt;I see what you did there… Let me help you out…&lt;/h1&gt;
&lt;p&gt;The Swift compiler, for better or worse, wants to be very… &amp;ldquo;helpful”…&lt;/p&gt;
&lt;p&gt;Some people hate it. I love it.&lt;/p&gt;
&lt;p&gt;I &lt;em&gt;want&lt;/em&gt; to be told, &amp;ldquo;Hey, Andrew… you&amp;rsquo;re about to break stuff – don&amp;rsquo;t do that.”&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;d rather head off issues during development than at run-time.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;how-helpful&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;how-helpful-is-it-really&#34;&gt;How helpful is it, really?&lt;/h1&gt;
&lt;p&gt;But really… How helpful is this particular compiler error? What value does this fix-it option provide?&lt;/p&gt;
&lt;p&gt;Try this: Think about what would happen if the compiler &lt;em&gt;didn&amp;rsquo;t&lt;/em&gt; tell you about the possibility of your function&amp;rsquo;s closure escaping.&lt;/p&gt;
&lt;p&gt;I was working the other day, cruising along writing what I thought was perfectly normal code when I hit this &amp;ldquo;annotate your function&amp;rsquo;s closure parameter with &lt;code&gt;@escaping&lt;/code&gt;” error, myself.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;&lt;code&gt;@escaping&lt;/code&gt;, Huh? What&amp;rsquo;s that?!”&lt;/p&gt;
&lt;p&gt;1 – I wasn&amp;rsquo;t thinking about escaping closures because&lt;br&gt;
2 – I had no idea that I &lt;em&gt;could&lt;/em&gt; think about escaping closures!!&lt;/p&gt;
&lt;p&gt;It turns out that this could have been really bad, agreed? Asynchronous behavior in software requires a little more thought than your normal top-down procedural approach.&lt;/p&gt;
&lt;p&gt;If the compiler hadn&amp;rsquo;t stopped me, I could have inadvertently imposed the need to think in terms of dispatch queues or asynchronous callbacks on the users of my API.&lt;/p&gt;
&lt;p&gt;Is this only something that &amp;ldquo;newbies” need to have the compiler&amp;rsquo;s help with though?&lt;/p&gt;
&lt;p&gt;One could argue that I probably should have been aware of this particular feature of Swift — had I been a better developer, maybe I wouldn&amp;rsquo;t need the compiler to hold my hand so much.&lt;/p&gt;
&lt;p&gt;Okay – Fair point. That leads me to one last gleaning from Lattner&amp;rsquo;s Swift Evolution entry that I&amp;rsquo;ll comment on before wrapping up.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;resilience&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;writing-resilient-code&#34;&gt;Writing resilient code&lt;/h1&gt;
&lt;p&gt;Let me reflect really quickly on one more quote from the &lt;a href=&#34;https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160530/019880.html&#34;&gt;Swift Evolution entry&lt;/a&gt; before I end for today.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;John McCall pointed out that resilience in the type system is different than resilience in practice: &lt;strong&gt;An API changing to capture a closure and use it long after it was originally passed&lt;/strong&gt; is &lt;strong&gt;likely to break the clients&lt;/strong&gt; regardless of whether the type system captures this as an issue. He argues (and the argument is strong IMO) it is &lt;em&gt;better&lt;/em&gt; for resilient APIs to default to @noescape, &lt;strong&gt;since that forces the author of V2 to think about whether they are breaking their clients&lt;/strong&gt;. &lt;small&gt;(emphasis mine)&lt;/small&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Resilience is all about how clients are affected when you &lt;em&gt;change&lt;/em&gt; the API or its implementation.&lt;/p&gt;
&lt;p&gt;Suppose that a team mate of mine writes a function… &lt;a href=&#34;https://www.andrewcbancroft.com/2017/04/25/every-developer-api-designer/&#34;&gt;She&amp;rsquo;s just created an API&lt;/a&gt;, true?&lt;/p&gt;
&lt;p&gt;Suppose that the function asks for a closure, but for now it&amp;rsquo;s just a regular old closure. No escaping or anything.&lt;/p&gt;
&lt;p&gt;Others on my team are going to start coding against that function&amp;rsquo;s signature. If the function is honest, it allows me and my team to have expectations about what it&amp;rsquo;ll do when it executes.&lt;/p&gt;
&lt;p&gt;Now suppose that I&amp;rsquo;m going to work on version 2 of that same API. Only &lt;em&gt;now&lt;/em&gt;, I&amp;rsquo;m going to introduce the opportunity for that closure to &lt;a href=&#34;https://www.andrewcbancroft.com/2017/04/26/what-in-the-world-is-an-escaping-closure-in-swift/&#34;&gt;escape&lt;/a&gt; (maybe intentionally, or maybe accidentally out of ignorance).&lt;/p&gt;
&lt;p&gt;Thaaaat&amp;rsquo;s gonna break my team. For sure.&lt;/p&gt;
&lt;p&gt;They&amp;rsquo;re using the API as it is expecting normal synchronous behavior from the function. Now &lt;em&gt;I&amp;rsquo;ve&lt;/em&gt; gone in and fundamentally changed what their expectations should be.&lt;/p&gt;
&lt;p&gt;In other words, I haven&amp;rsquo;t been very resilient.&lt;/p&gt;
&lt;p&gt;The default behavior of closures passed to functions is to be &lt;code&gt;@noescape&lt;/code&gt;. If I introduce the possibility for that closure to escape, I need to be red-flag warned about that, because I need to come up with some way to communicate this breaking change to the clients of the API. And/or provide an alternative so that clients of API v1 can still use it as expected.&lt;/p&gt;
&lt;p&gt;Most of all, I need to &lt;strong&gt;think&lt;/strong&gt; about the implications of what I&amp;rsquo;m doing, and sometimes (a lot of the times) that takes prompting.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;bottom-line&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;the-bottom-line--the-compiler-is-making-you-think&#34;&gt;The bottom line – The compiler is making you think&lt;/h1&gt;
&lt;p&gt;All of this to say: The compiler is making you think. Not only that, it&amp;rsquo;s forcing you to conform.&lt;/p&gt;
&lt;p&gt;From what I&amp;rsquo;ve found, that&amp;rsquo;s the idea behind &amp;ldquo;why” we need to mark escaping closures with &lt;code&gt;@escaping&lt;/code&gt; in Swift.&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/2017/04/26/what-in-the-world-is-an-escaping-closure-in-swift/&#34; title=&#34;What in the World is an “Escaping Closure” in Swift?&#34;&lt;/a&gt;What in the World is an “Escaping Closure” in Swift?
    &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>What in the World is an “Escaping Closure” in Swift?</title>
      <link>https://www.andrewcbancroft.com/2017/04/26/what-in-the-world-is-an-escaping-closure-in-swift/</link>
      <pubDate>Wed, 26 Apr 2017 14:41:07 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2017/04/26/what-in-the-world-is-an-escaping-closure-in-swift/</guid>
      <description>&lt;p&gt;If you&amp;rsquo;re mostly in the business of coding up closures to pass off to other functions as &lt;a href=&#34;https://www.andrewcbancroft.com/2016/02/15/fundamentals-of-callbacks-for-swift-developers/&#34;&gt;callbacks&lt;/a&gt;, you may not have run into the concept of an &amp;ldquo;escaping closure” yet.&lt;/p&gt;
&lt;p&gt;When you step out of the role of consuming other peoples&amp;rsquo; APIs in to the realm of creating your own (and &lt;a href=&#34;https://www.andrewcbancroft.com/2017/04/25/every-developer-api-designer/&#34;&gt;you do this all the time&lt;/a&gt;!), &lt;em&gt;this&lt;/em&gt; is where you&amp;rsquo;ll likely run into the concept of an &amp;ldquo;escaping closure” in certain scenarios.&lt;/p&gt;
&lt;p&gt;I want to start off by defining the term. Then I&amp;rsquo;ll throw out a couple of usage scenarios that cause us to need to think in terms of a closure &amp;ldquo;escaping”.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;definition&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;definition&#34;&gt;Definition&lt;/h1&gt;
&lt;p&gt;First, a definition, shall we?&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;A closure is said to escape a function when the closure is passed as an argument to the function, but is called after the function returns.&lt;br&gt;
–&lt;a href=&#34;https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html#//apple_ref/doc/uid/TP40014097-CH11-ID546&#34;&gt;Apple Developer Documentation&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;So apparently, you can get yourself into the situation where you&amp;rsquo;re designing a function that takes in a closure as one of its parameters:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;func doSomething(completion: () -&amp;gt; Void) { ... }&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Furthermore, it appears that it&amp;rsquo;s possible to find yourself in a situation where the closure will execute, but &lt;em&gt;somehow&lt;/em&gt;, it doesn&amp;rsquo;t get executed until &lt;em&gt;after&lt;/em&gt; the function it got passed into returns. So it would go something like this:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Call &lt;code&gt;doSomething&lt;/code&gt; and pass it a closure &lt;a href=&#34;https://www.andrewcbancroft.com/2016/03/18/swift-functions-as-types/&#34;&gt;of Type&lt;/a&gt; &lt;code&gt;() -&amp;gt; Void&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;doSomething&lt;/code&gt; performs its work and &lt;em&gt;returns&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;The closure you passed (the one of Type &lt;code&gt;() -&amp;gt; Void&lt;/code&gt;) gets executed&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Weird, huh? How in the world can that happen? &lt;a href=&#34;#usage&#34;&gt;I&amp;rsquo;ll talk about that in a second&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The point for now is this: whenever you&amp;rsquo;re in a situation like this where the closure that you pass to a function gets executed &lt;em&gt;after&lt;/em&gt; the function you passed it &lt;em&gt;to&lt;/em&gt; returns, you&amp;rsquo;ve got an &amp;ldquo;escaping closure” on your hands.&lt;/p&gt;
&lt;p&gt;As an API &lt;em&gt;consumer&lt;/em&gt;, you might not know or care about the escap-y-ness of the closure.&lt;/p&gt;
&lt;p&gt;As an API &lt;em&gt;designer&lt;/em&gt; (which again, &lt;a href=&#34;https://www.andrewcbancroft.com/2017/04/25/every-developer-api-designer/&#34;&gt;could be &lt;em&gt;yourself&lt;/em&gt;&lt;/a&gt;, if you&amp;rsquo;re the one writing the definition of &lt;code&gt;doSomething(completion:)&lt;/code&gt;), you &lt;em&gt;have&lt;/em&gt; to care, because the Swift compiler will be angry with errors if you don&amp;rsquo;t.&lt;/p&gt;
&lt;p&gt;So how do &amp;ldquo;escaping closure” scenarios happen?&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;usage&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;escaping-closure-scenarios&#34;&gt;Escaping closure scenarios&lt;/h1&gt;
&lt;p&gt;Here are a few scenarios that give rise to escaping closures.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;store-as-state&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;storing-the-closure-as-state&#34;&gt;Storing the closure as &lt;em&gt;state&lt;/em&gt;&lt;/h2&gt;
&lt;p&gt;&lt;a href=&#34;https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html#//apple_ref/doc/uid/TP40014097-CH11-ID546&#34;&gt;Apple&amp;rsquo;s docs&lt;/a&gt; give an example of appending a closure that&amp;rsquo;s passed into a function to a mutable array of closures within your class/struct:&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;completionHandlers&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;-&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&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;doSomething&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;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&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;completionHandlers&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;append&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;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;Presumabley then, at some later time after &lt;code&gt;doSomething&lt;/code&gt; returns, all of the completion handlers in the array will be looped over and executed (or something like that)…&lt;/p&gt;
&lt;p&gt;As you can see, this follows the 1. Pass closure, 2. &lt;code&gt;doSomething&lt;/code&gt; returns, 3. Closure executed pattern we had before, doesn&amp;rsquo;t it?&lt;/p&gt;
&lt;p&gt;So this is one scenario that could give rise to an escaping closure, IF you designed your system this way.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Whenever you take the closure, store it as state, and then execute it at a later time, the closure is &amp;ldquo;escaping” the function it got passed into.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;async-callbacks&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;asynchronous-asynchronous-callbacks&#34;&gt;Asynchronous asynchronous callbacks&lt;/h2&gt;
&lt;p&gt;No, I didn&amp;rsquo;t get repetitively redundant there. Well… I did, but it was on purpose. :]&lt;/p&gt;
&lt;p&gt;Supposing that you&amp;rsquo;re working on your &lt;code&gt;doSomething(completion:)&lt;/code&gt; function.&lt;/p&gt;
&lt;p&gt;Within it, you make a call to &lt;em&gt;another&lt;/em&gt; function that performs an asynchronous action and asks for a completion closure of its own.&lt;/p&gt;
&lt;p&gt;What if you only want to call the completion handler that was passed into &lt;code&gt;doSomething&lt;/code&gt; &lt;em&gt;after&lt;/em&gt; the asynchronous action of the &lt;em&gt;other&lt;/em&gt; function completes. That is, what if you only want the two completion handlers to be executed &lt;em&gt;together&lt;/em&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;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;doSomething&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;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&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;doSomeOtherAsynchronousThing&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;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;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;4&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// code that executes after the other asynchronous thing is done&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;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;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;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Here, you&amp;rsquo;ve got this nested asynchronous behavior going on, don&amp;rsquo;t you? Asynchronous asynchrony is happening.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Whenever you &lt;em&gt;defer the execution of a closure&lt;/em&gt; to a time that&amp;rsquo;s &lt;em&gt;after&lt;/em&gt; the &amp;ldquo;parent” function returns, you&amp;rsquo;ve got an &amp;ldquo;escaping closure” on your hands.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;declaring-escaping&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;declaring-this-is-an-escaping-closure8221-in-code&#34;&gt;Declaring &amp;ldquo;this is an escaping closure!” in code&lt;/h1&gt;
&lt;p&gt;Whenever you&amp;rsquo;re implementing a function that introduces the possibility for a closure passed to it to escape, you&amp;rsquo;ll know it.&lt;/p&gt;
&lt;p&gt;The Swift compiler will complain, and your app won&amp;rsquo;t build:&lt;br&gt;
&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2017/04/escaping-closure.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2017/04/escaping-closure.png&#34; alt=&#34;Compiler error - Closure use of non-escaping parameter &amp;#039;completion&amp;#039; may allow it to escape&#34; width=&#34;891&#34; height=&#34;33&#34; class=&#34;alignnone size-full wp-image-13321&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2017/04/escaping-closure.png 891w, https://www.andrewcbancroft.com/wp-content/uploads/2017/04/escaping-closure-300x11.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2017/04/escaping-closure-768x28.png 768w&#34; sizes=&#34;(max-width: 891px) 100vw, 891px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;What do you do to fix it?&lt;/p&gt;
&lt;p&gt;It&amp;rsquo;s pretty simple. In the declaration line of your function, you need to add the &lt;code&gt;@escaping&lt;/code&gt; attribute right before the closure&amp;rsquo;s Type declaration:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;doSomething(completion: @escaping () -&amp;gt; Void)&lt;/code&gt;&lt;/p&gt;
&lt;h1 id=&#34;wrapping-up&#34;&gt;Wrapping up&lt;/h1&gt;
&lt;p&gt;My goal was to shed some light on the concept of &amp;ldquo;escaping closures”. With the definition and the example scenarios that give rise to escaping closures, my hope is that things are a little more clear for you. Sound off in the comments if you&amp;rsquo;re still struggling, or if you&amp;rsquo;ve run across other scenarios requiring you to use the &lt;code&gt;@escaping&lt;/code&gt; attribute!&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>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>Core Data Model Attributes and NSManagedObject Property Types Must Match!</title>
      <link>https://www.andrewcbancroft.com/2017/04/23/core-data-model-attributes-and-nsmanagedobject-property-types-must-match/</link>
      <pubDate>Sun, 23 Apr 2017 20:03:34 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2017/04/23/core-data-model-attributes-and-nsmanagedobject-property-types-must-match/</guid>
      <description>&lt;p&gt;I admit – it might have taken me less time to figure out my runtime exception if I hadn&amp;rsquo;t just migrated my project to Swift 3 when I encountered the bug.&lt;/p&gt;
&lt;p&gt;That&amp;rsquo;s the problem isn&amp;rsquo;t it? You go in… you intend to do one thing. Before long, you&amp;rsquo;ve got 15 files with an &lt;code&gt;M&lt;/code&gt; out to the right. The project builds (finally), but ah – then there&amp;rsquo;s &lt;em&gt;runtime&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;When the crash occurs, you&amp;rsquo;re never sure if it was an &lt;em&gt;existing&lt;/em&gt; problem, or if it was caused by the code conversion. Who knows – maybe it&amp;rsquo;s both?&lt;/p&gt;
&lt;p&gt;Perhaps this is a story more about code conversion than it is about Core Data. In any case, perhaps it&amp;rsquo;ll help a poor soul struggling to figure out why &lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2017/04/EXC_BAD_INSTRUCTION.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2017/04/EXC_BAD_INSTRUCTION.png&#34; alt=&#34;EXC_BAD_INSTRUCTION&#34; width=&#34;364&#34; height=&#34;12&#34; class=&#34;alignnone size-full wp-image-13265&#34; style=&#34;display:inline-block;&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2017/04/EXC_BAD_INSTRUCTION.png 364w, https://www.andrewcbancroft.com/wp-content/uploads/2017/04/EXC_BAD_INSTRUCTION-300x10.png 300w&#34; sizes=&#34;(max-width: 364px) 100vw, 364px&#34; /&gt;&lt;/a&gt; just happened.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;match&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;data-model-attributes-and-nsmanagedobject-property-types--match-em&#34;&gt;Data model Attributes and NSManagedObject property types – Match &amp;rsquo;em!&lt;/h1&gt;
&lt;p&gt;Whenever you&amp;rsquo;re creating a subclass of &lt;code&gt;NSManagedObject&lt;/code&gt; for the Entities in your data model, you&amp;rsquo;re in a mapping process.&lt;/p&gt;
&lt;p&gt;Each Attribute on an Entity maps over to a &lt;em&gt;property&lt;/em&gt; on the &lt;code&gt;NSManagedObjectSubclass&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Not only must the &lt;em&gt;names&lt;/em&gt; of those Attributes and properties match, but the &lt;em&gt;Types&lt;/em&gt; of each must match as well.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;optionals-types&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;optionals-are-types&#34;&gt;Optionals are Types&lt;/h1&gt;
&lt;p&gt;&lt;code&gt;String&lt;/code&gt; isn&amp;rsquo;t the same as &lt;code&gt;String?&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;Date&lt;/code&gt; isn&amp;rsquo;t the same as &lt;code&gt;Date?&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;They&amp;rsquo;re different &lt;em&gt;Types&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;So what happens if you specify that a certain Attribute named, say, &lt;code&gt;createdOn&lt;/code&gt; is a &lt;code&gt;Date&lt;/code&gt; with the Optional checkbox checked in the data model designer like this:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2017/04/optional-attribute.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2017/04/optional-attribute.png&#34; alt=&#34;createdOn as Optional attribute&#34; width=&#34;358&#34; height=&#34;110&#34; class=&#34;alignnone size-full wp-image-13268&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2017/04/optional-attribute.png 358w, https://www.andrewcbancroft.com/wp-content/uploads/2017/04/optional-attribute-300x92.png 300w&#34; sizes=&#34;(max-width: 358px) 100vw, 358px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;And over in the implementation of your &lt;code&gt;NSManagedObject&lt;/code&gt; subclass, you have code that&amp;rsquo;s written like this:&lt;/p&gt;
&lt;p&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;kr&#34;&gt;@NSManaged&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;createdOn&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Date&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;//instead of Date? (ie, the ? is missing)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
What&amp;rsquo;ll happen? Well, I can tell you what&amp;rsquo;ll happen. :]&lt;/p&gt;
&lt;p&gt;When you run the app and attempt to load objects from your persistent store that have been saved with &lt;code&gt;nil&lt;/code&gt; for the &lt;code&gt;createdOn&lt;/code&gt; value, your app will blow up:&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2017/04/EXC_BAD_INSTRUCTION.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2017/04/EXC_BAD_INSTRUCTION.png&#34; alt=&#34;EXC_BAD_INSTRUCTION&#34; width=&#34;364&#34; height=&#34;12&#34; class=&#34;alignnone size-full wp-image-13265&#34; style=&#34;display:inline-block;&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2017/04/EXC_BAD_INSTRUCTION.png 364w, https://www.andrewcbancroft.com/wp-content/uploads/2017/04/EXC_BAD_INSTRUCTION-300x10.png 300w&#34; sizes=&#34;(max-width: 364px) 100vw, 364px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Xcode isn&amp;rsquo;t entirely unhelpful. While the &lt;code&gt;EXC_BAD_INSTRUCTION&lt;/code&gt; message in the text editor isn&amp;rsquo;t very illuminating, the Debug Navigator on the left (&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2017/04/debug-nav.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2017/04/debug-nav.png&#34; alt=&#34;Debug navigator - 6th icon from left in the left sidebar&#34; width=&#34;251&#34; height=&#34;30&#34; class=&#34;alignnone size-full wp-image-13279&#34; style=&#34;display:inline-block;&#34; /&gt;&lt;/a&gt;) provides some clues.&lt;/p&gt;
&lt;p&gt;For me, it displayed just the breadcrumb that made me go, &amp;ldquo;Oh! Let me go check and see if I&amp;rsquo;ve got a Type mis-match between my data model and my &lt;code&gt;NSManagedObject&lt;/code&gt; subclass”:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2017/04/TypeMismatch.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2017/04/TypeMismatch.png&#34; alt=&#34;Unconditionally bridge from Objective C NSDate? to Date&#34; width=&#34;452&#34; height=&#34;88&#34; class=&#34;alignnone size-full wp-image-13271&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2017/04/TypeMismatch.png 452w, https://www.andrewcbancroft.com/wp-content/uploads/2017/04/TypeMismatch-300x58.png 300w&#34; sizes=&#34;(max-width: 452px) 100vw, 452px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Interesting… It looks like there was an attempt to go from an &lt;code&gt;NSDate?&lt;/code&gt; (&lt;em&gt;optional&lt;/em&gt;) instance to a &lt;code&gt;Date&lt;/code&gt; (&lt;em&gt;non&lt;/em&gt;-optional).&lt;/p&gt;
&lt;p&gt;The issue isn&amp;rsquo;t that I&amp;rsquo;ve got a mismatch between &lt;code&gt;NSDate&lt;/code&gt; and &lt;code&gt;Date&lt;/code&gt;. The runtime can swap those around and substitute them easily.&lt;/p&gt;
&lt;p&gt;Rather, it&amp;rsquo;s that I&amp;rsquo;m trying to go from &lt;em&gt;optional&lt;/em&gt;, where &lt;code&gt;nil&lt;/code&gt; is fine, to &lt;em&gt;non&lt;/em&gt;-optional, where &lt;code&gt;nil&lt;/code&gt;…well…crashes things.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;lessons-learned&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;lessons-learned&#34;&gt;Lessons learned&lt;/h1&gt;
&lt;p&gt;What have I learned?&lt;/p&gt;
&lt;p&gt;1 – Map Attributes to &lt;code&gt;NSManagedObject&lt;/code&gt; subclass properties carefully.&lt;br&gt;
2 – Don&amp;rsquo;t accidentally miss a &lt;code&gt;?&lt;/code&gt; to indicate that a property is optional if I&amp;rsquo;ve got it marked as optional in the data model&lt;br&gt;
3 – Read the Debug Navigator. It&amp;rsquo;s not just a list of gibberish – it can actually provide helpful clues so you know where to go look to solve your problem!&lt;/p&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>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>3 Effective Ways to Keep You Focused as a Creator</title>
      <link>https://www.andrewcbancroft.com/2017/04/20/3-effective-ways-to-keep-you-focused-as-a-creator/</link>
      <pubDate>Thu, 20 Apr 2017 18:04:08 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2017/04/20/3-effective-ways-to-keep-you-focused-as-a-creator/</guid>
      <description>&lt;h1 id=&#34;away-with-the-phone&#34;&gt;Away with the Phone!&lt;/h1&gt;
&lt;p&gt;Put your phone away. Far away.&lt;/p&gt;
&lt;p&gt;Not in your pocket.&lt;/p&gt;
&lt;p&gt;Not on your desk.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;In a drawer&lt;/strong&gt; – &lt;em&gt;away&lt;/em&gt; from you.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Disconnect&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;You should be able to tell if you&amp;rsquo;ve got an emergency call coming in, but past that, aim to be completely oblivious to every other kind of notification flowing into your smart device.&lt;/p&gt;
&lt;p&gt;If you use a time management technique like &lt;a href=&#34;https://en.wikipedia.org/wiki/Pomodoro_Technique&#34;&gt;Pomodoro&lt;/a&gt; (there are plenty of timers and apps in the app store to keep you on track with this strategy), you get 5 minutes every half hour to pause from making awesome stuff. Use &lt;em&gt;that&lt;/em&gt; time to get up, stretch, and, if you must, check your phone.&lt;/p&gt;
&lt;p&gt;Being able to easily reach my phone is a &lt;em&gt;huge&lt;/em&gt;, huge productivity killer.&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;ve started putting it in a drawer with the ringer on at a reasonable volume level so as not to miss an emergency. Life has been much more productive since.&lt;/p&gt;
&lt;h1 id=&#34;music-to-code-by&#34;&gt;Music to Code By&lt;/h1&gt;
&lt;p&gt;Putting your phone away minimizes physical distractions from your environment.&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://mtcb.pwop.com/&#34;&gt;Music to Code By&lt;/a&gt; minimizes &lt;em&gt;mental&lt;/em&gt; distractions and keeps you focused.&lt;/p&gt;
&lt;p&gt;Don&amp;rsquo;t be mislead by the title. It&amp;rsquo;s not just for programmers!&lt;/p&gt;
&lt;p&gt;If you need to write, organize, or just &lt;em&gt;think&lt;/em&gt; in general, I highly recommend this invaluable resource.&lt;/p&gt;
&lt;p&gt;The problem I always have with music when it comes to productivity is that…&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The music has words and I wanna sing! (sorry, teammates)&lt;/li&gt;
&lt;li&gt;The music is too mellow and causes me to want to sleep.&lt;/li&gt;
&lt;li&gt;The music is too up-beat or interesting… interesting enough to distract me.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;a href=&#34;http://mtcb.pwop.com/&#34;&gt;Music to Code By&lt;/a&gt; is literally the Goldilocks of music for concentration purposes. No words. Not too slow. Not too fast or &amp;ldquo;interesting”. &lt;strong&gt;Just right&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;I bought the &lt;a href=&#34;https://secure.pwop.com/music-to-code-by-download-only&#34;&gt;original 3 tracks&lt;/a&gt;, and it&amp;rsquo;s been the best $18 I&amp;rsquo;ve spent to improve my focus.&lt;/p&gt;
&lt;p&gt;I know what you&amp;rsquo;re thinking: I have to spend money??!&lt;/p&gt;
&lt;p&gt;Well, you&amp;rsquo;d spend money to buy a hammer or a saw if you needed to build something, wouldn&amp;rsquo;t you?&lt;/p&gt;
&lt;p&gt;You&amp;rsquo;d drop a few dollars on an app if you knew it&amp;rsquo;d be worth it for the job you needed it to do, right?&lt;/p&gt;
&lt;p&gt;Think of Music to Code By as a tool for your brain&amp;rsquo;s focus… a &amp;ldquo;clamp” if you will, to keep you riveted on what you need to accomplish.&lt;/p&gt;
&lt;p&gt;If you can&amp;rsquo;t afford $20 right now, can you afford $10? Save for two months and &lt;em&gt;then&lt;/em&gt; buy it!&lt;/p&gt;
&lt;p&gt;I haven&amp;rsquo;t encountered any other tool apart from these sound tracks that I can spend dollars on that keeps me focused.&lt;/p&gt;
&lt;p&gt;Full disclosure: No one paid me to say all that. I&amp;rsquo;ve really just been &lt;em&gt;that impacted&lt;/em&gt; by the product that I couldn&amp;rsquo;t keep from commending it to you.&lt;/p&gt;
&lt;h1 id=&#34;keep-a-text-editor-window-open-but-minimized&#34;&gt;Keep a Text Editor Window Open (but minimized)&lt;/h1&gt;
&lt;p&gt;Whenever a distracting thought comes to mind, open the text editor and jot down the thought.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Oh! I&amp;rsquo;ve got to remember to call ___”…&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Open Text editor window –&amp;gt;&lt;/li&gt;
&lt;li&gt;Type &amp;ldquo;I&amp;rsquo;ve got to call ___” –&amp;gt;&lt;/li&gt;
&lt;li&gt;Minimize –&amp;gt;&lt;/li&gt;
&lt;li&gt;Back to making great things!&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&amp;ldquo;Blog post idea!”…&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Open Text editor window –&amp;gt;&lt;/li&gt;
&lt;li&gt;Type &amp;ldquo;[Blog post idea]” –&amp;gt;&lt;/li&gt;
&lt;li&gt;Minimize –&amp;gt;&lt;/li&gt;
&lt;li&gt;Continue that awesome project!&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Always have a text editor window open and ready to record &lt;em&gt;whatever&lt;/em&gt; your random ramblings may be. Your mind can effectively &amp;ldquo;let go” because you just preserved what it alerted you to in the text file.&lt;/p&gt;
&lt;p&gt;Don&amp;rsquo;t keep the window on the screen all the time though. You&amp;rsquo;ll be tempted to glance over at it too often. Jot down the thought and then minimize it.&lt;/p&gt;
&lt;p&gt;Oh. And be sure to &lt;em&gt;save&lt;/em&gt; the file. :]&lt;/p&gt;
&lt;h1 id=&#34;what-do-you-do&#34;&gt;What do &lt;em&gt;you&lt;/em&gt; do?&lt;/h1&gt;
&lt;p&gt;These are my top three right now. What do &lt;em&gt;you&lt;/em&gt; do to keep focused on what matters?&lt;/p&gt;
&lt;p&gt;Sound off in the comments – I&amp;rsquo;d love to hear your productivity hacks!&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>How to Unwrap Multiple Optionals with One If-Let in Swift</title>
      <link>https://www.andrewcbancroft.com/2017/04/19/how-to-unwrap-multiple-optionals-with-one-if-let-in-swift/</link>
      <pubDate>Wed, 19 Apr 2017 17:25:41 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2017/04/19/how-to-unwrap-multiple-optionals-with-one-if-let-in-swift/</guid>
      <description>&lt;p&gt;What do you do when you&amp;rsquo;ve got two (or more) optionals that you need to safely unwrap and work with?&lt;/p&gt;
&lt;h1 id=&#34;code-examples&#34;&gt;Code examples&lt;/h1&gt;
&lt;p&gt;Suppose that you&amp;rsquo;ve got two arrays, both of which are optional. What I want to do right now is walk through a couple of scenarios where I unwrap them at the same time and print them to the console with a single &lt;code&gt;if-let&lt;/code&gt; statement.&lt;/p&gt;
&lt;p&gt;First, watch &lt;em&gt;how&lt;/em&gt; it&amp;rsquo;s done to accomplish the goal of this article&amp;rsquo;s title. :]&lt;/p&gt;
&lt;p&gt;Then, compare what you expected to be the print output, to the &lt;em&gt;actual&lt;/em&gt; output to make sure your understanding of how the syntax works is complete.&lt;/p&gt;
&lt;p&gt;Ready?&lt;/p&gt;
&lt;h2 id=&#34;scenario-1-both-arrays-are-initialized-non-nil&#34;&gt;Scenario 1: Both arrays are initialized (non-nil)&lt;/h2&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;greetings&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;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;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;Howdy!&amp;#34;&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;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;salutations&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;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;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;Hi!&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;Hey!&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;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;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;g&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;greetings&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;s&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;salutations&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;print&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;g&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;print&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;s&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;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
[&amp;ldquo;Howdy!”, &amp;ldquo;Hello!”]&lt;br&gt;
[&amp;ldquo;Hi!”, &amp;ldquo;Hey!”]&lt;/p&gt;
&lt;h3 id=&#34;breaking-it-down&#34;&gt;Breaking it down&lt;/h3&gt;
&lt;p&gt;The syntax for unwrapping multiple optionals with a single if-let block is straightforward. It&amp;rsquo;s &lt;code&gt;if&lt;/code&gt; followed by a series of &lt;code&gt;let [constantName] = [optionalName]&lt;/code&gt; statements, separated by commas.&lt;/p&gt;
&lt;p&gt;The output of this one is pretty much what you&amp;rsquo;d expect, too. The string form of the arrays is printed to the console window in Xcode or in your Playground.&lt;/p&gt;
&lt;h2 id=&#34;scenario-2-one-array-is-initialized-non-nil-and-the-other-is-nil&#34;&gt;Scenario 2: One array is initialized (non-nil), and the other is nil&lt;/h2&gt;
&lt;p&gt;Now suppose that the arrays looked 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;greetings&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;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;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;Howdy!&amp;#34;&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;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;salutations&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&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&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;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;g&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;greetings&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;s&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;salutations&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;print&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;g&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;print&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;s&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;Question: What do you think will be printed?&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;[&amp;ldquo;Howdy!”, &amp;ldquo;Hello!”] and &amp;ldquo;nil”&lt;/li&gt;
&lt;li&gt;Just [&amp;ldquo;Howdy!”, &amp;ldquo;Hello!”]&lt;/li&gt;
&lt;li&gt;Nothing will be printed&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;If you chose door number &lt;strong&gt;3&lt;/strong&gt;, you&amp;rsquo;d be correct.&lt;/p&gt;
&lt;p&gt;The if-let block between the {}&amp;rsquo;s is only executed if &lt;strong&gt;both&lt;/strong&gt; &lt;code&gt;greetings&lt;/code&gt; and &lt;code&gt;salutations&lt;/code&gt; are non-nil.&lt;/p&gt;
&lt;h1 id=&#34;takeaway&#34;&gt;Takeaway&lt;/h1&gt;
&lt;p&gt;Unwrapping multiple optionals with a single if-let statement is pretty easy: &lt;code&gt;if&lt;/code&gt; followed by a series of &lt;code&gt;let [constantName] = [optionalName]&lt;/code&gt; statements, separated by commas.&lt;/p&gt;
&lt;p&gt;The behavior is similar to using the &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt; operator in a normal &lt;code&gt;if&lt;/code&gt; condition. It&amp;rsquo;s like saying &amp;ldquo;if this optional is non-nil AND this optional is non-nil, then do this”&lt;/p&gt;
&lt;p&gt;If you expect to work with one of the optionals in the list even if the other is nil, you&amp;rsquo;re going to need to split that up into multiple if-lets:&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;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;g&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;greetings&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;print&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;g&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;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;s&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;salutations&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;print&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;s&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;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>Creating the Core Data Stack with Backwards Compatibility in Swift</title>
      <link>https://www.andrewcbancroft.com/2017/04/16/creating-the-core-data-stack-with-backwards-compatibility-in-swift/</link>
      <pubDate>Sun, 16 Apr 2017 21:23:46 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2017/04/16/creating-the-core-data-stack-with-backwards-compatibility-in-swift/</guid>
      <description>&lt;p&gt;In 2017, we live in a world where there are still non-iOS 10 devices out in the wild. If your app is targeting an iOS version earlier than iOS 10, or macOS Sierra (10.12), you&amp;rsquo;ll be unable to take advantage of Core Data&amp;rsquo;s latest &amp;ldquo;stack creation” class called &lt;code&gt;NSPersistentContainer&lt;/code&gt;. So what can you do?&lt;/p&gt;
&lt;p&gt;While &lt;code&gt;NSPersistentContainer&lt;/code&gt; &lt;em&gt;does&lt;/em&gt; aim to simplify the stack creation process, at the end of the day, it&amp;rsquo;s not terrible to have to mess with some of this Core Data plumbing.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;end-goal&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;end-goal-of-creating-the-core-data-stack&#34;&gt;End goal of creating the Core Data stack&lt;/h1&gt;
&lt;p&gt;The end goal of Creating the Core Data stack is to get an instance of &lt;code&gt;NSManagedObjectContext&lt;/code&gt;. That&amp;rsquo;s it.&lt;/p&gt;
&lt;p&gt;Most apps that rely on Core Data will end up making fetch requests to obtain data, or wire up other classes like &lt;code&gt;NSFetchedResultsController&lt;/code&gt;. To make these things work, you&amp;rsquo;ve got to provide an instance of &lt;code&gt;NSManagedObjectContext&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;NSManagedObjectContext&lt;/code&gt; is sort of that central gear in the whole system that makes the other gears turn.&lt;/p&gt;
&lt;p&gt;So. Bottom line: Once you have an instance of &lt;code&gt;NSManagedObjectContext&lt;/code&gt;, you&amp;rsquo;re golden. That&amp;rsquo;s what creating the Core Data stack gives you in the end.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;3-steps&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;3-steps-to-creating-the-core-data-stack&#34;&gt;3 steps to creating the Core Data stack&lt;/h1&gt;
&lt;p&gt;The Core Data stack can be created in about 3 steps:&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;managed-object-model&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;1-initialize-an-instance-of-nsmanagedobjectmodel&#34;&gt;1) Initialize an instance of NSManagedObjectModel&lt;/h2&gt;
&lt;p&gt;This corresponds to your .xcdatamodeld file. You&amp;rsquo;ll want to glance over to the project navigator on the left and locate the .xcdatamodeld file to record its name for this step.&lt;/p&gt;
&lt;p&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;// Initialize NSManagedObjectModel&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;modelURL&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Bundle&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;main&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;n&#34;&gt;forResource&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;NameOfDataModel&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;withExtension&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;momd&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;cl&#34;&gt;&lt;span class=&#34;k&#34;&gt;guard&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;model&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;contentsOf&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;modelURL&lt;/span&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 class=&#34;bp&#34;&gt;fatalError&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;model not found&amp;#34;&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;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;a name=&#34;persistent-store-coordinator&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;2-initialize-and-configure-an-instance-of-nspersistentstorecoordinator-with-the-nsmanagedobjectmodel-instance-and-an-nspersistentstoretype&#34;&gt;2) Initialize and configure an instance of NSPersistentStoreCoordinator with the NSManagedObjectModel instance and an NSPersistentStoreType&lt;/h2&gt;
&lt;p&gt;The reason you create the &lt;code&gt;NSManagedObjectModel&lt;/code&gt; instance first is because the &lt;em&gt;next&lt;/em&gt; step depends on it. &lt;code&gt;NSPersistentStoreCoordinator&lt;/code&gt; will use your &lt;code&gt;NSManagedObjectModel&lt;/code&gt; instance to configure itself and prepare to create the correct kind of persistent store based on what &lt;code&gt;NSPersistentStoreType&lt;/code&gt; you tell it to use.&lt;/p&gt;
&lt;p&gt;In the code example that follows, I used &lt;code&gt;NSSQLiteStoreType&lt;/code&gt; to create a SQLite persistent store. Regardless of what kind of persistent store you use though, &lt;code&gt;NSPersistentStoreCoordinator&lt;/code&gt; needs your managed object model instance.&lt;/p&gt;
&lt;p&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;9
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;// Configure NSPersistentStoreCoordinator with an NSPersistentStore&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;psc&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;model&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;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;storeURL&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;try&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;!&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;FileManager&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;default&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;url&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;k&#34;&gt;for&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;documentDirectory&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&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 class=&#34;n&#34;&gt;userDomainMask&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;appropriateFor&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;create&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&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;appendingPathComponent&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;NameOfDataModel.sqlite&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;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;k&#34;&gt;try&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;!&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;psc&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;addPersistentStore&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;ofType&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSSQLiteStoreType&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;configurationName&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;at&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;storeURL&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;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;a name=&#34;managed-object-context&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;3-initialize-an-instance-of-nsmanagedobjectcontext-and-assign-it-the-nspersistentstorecoordinator-instance&#34;&gt;3) Initialize an instance of NSManagedObjectcontext and assign it the NSPersistentStoreCoordinator instance&lt;/h2&gt;
&lt;p&gt;The last step is to initialize an instance of &lt;code&gt;NSManagedObjectContext&lt;/code&gt; and assign it the &lt;code&gt;NSPersistentStoreCoordinator&lt;/code&gt; instance.&lt;/p&gt;
&lt;p&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;// Create and return NSManagedObjectContext&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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 class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;concurrencyType&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;mainQueueConcurrencyType&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;cl&#34;&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;persistentStoreCoordinator&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;psc&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;a name=&#34;code-example&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;putting-it-all-together--creating-the-core-data-stack-in-code&#34;&gt;Putting it all together – creating the Core Data stack in code&lt;/h1&gt;
&lt;p&gt;When I create the Core Data stack, I like to encapsulate the code in a stand-alone function that returns an instance of &lt;code&gt;NSManagedObjectContext&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Here are all three steps put together:&lt;/p&gt;
&lt;p&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;12
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;13
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;14
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;15
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;16
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;17
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;18
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;19
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;20
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;21
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;22
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;createMainContext&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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Initialize NSManagedObjectModel&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;modelURL&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Bundle&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;main&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;n&#34;&gt;forResource&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;NameOfDataModel&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;withExtension&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;momd&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;cl&#34;&gt;    &lt;span class=&#34;k&#34;&gt;guard&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;model&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;contentsOf&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;modelURL&lt;/span&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 class=&#34;bp&#34;&gt;fatalError&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;model not found&amp;#34;&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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Configure NSPersistentStoreCoordinator with an NSPersistentStore&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;psc&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;model&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;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;storeURL&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;try&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;!&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;FileManager&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;default&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;url&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;k&#34;&gt;for&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;documentDirectory&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&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 class=&#34;n&#34;&gt;userDomainMask&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;appropriateFor&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;create&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&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;appendingPathComponent&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;NameOfDataModel.sqlite&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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;k&#34;&gt;try&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;!&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;psc&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;addPersistentStore&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;ofType&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSSQLiteStoreType&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;configurationName&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;at&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;storeURL&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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Create and return NSManagedObjectContext&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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 class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;concurrencyType&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;mainQueueConcurrencyType&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;cl&#34;&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;persistentStoreCoordinator&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;psc&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;    &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;context&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
Once you&amp;rsquo;ve got a function like &lt;code&gt;createMainContext()&lt;/code&gt;, you&amp;rsquo;ll be able to call it to obtain a fully-configured &lt;code&gt;NSManagedObjectContext&lt;/code&gt; instance.&lt;/p&gt;
&lt;p&gt;I &lt;em&gt;highly&lt;/em&gt; recommend you avoid calling it inside any of your view controllers. Instead, my recommendation is to call it to obtain your &lt;code&gt;NSManagedObjectContext&lt;/code&gt; instance inside of the &lt;code&gt;AppDelegate&#39;s&lt;/code&gt; &lt;code&gt;application(_:didFinishLaunchingWithOptions:)&lt;/code&gt; function. From there, you can pass it &lt;em&gt;to&lt;/em&gt; your first view controller, and from that first view controller on to &lt;em&gt;other&lt;/em&gt; view controllers that need it through &lt;code&gt;prepare(for segue:sender:)&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;For more on this &amp;ldquo;dependency injection” strategy, or if you&amp;rsquo;re more of a visual learner, check out my Pluralsight course, &lt;a href=&#34;http://bit.ly/ps-core-data-swift&#34;&gt;Core Data Fundamentals with Swift&lt;/a&gt;!&lt;br&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;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;&gt;Core Data Cheat Sheet for Swift iOS Developers&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/03/05/displaying-data-with-nsfetchedresultscontroller-and-swift/&#34; title=&#34;Displaying Data With NSFetchedResultsController and Swift&#34;&gt;Displaying Data With NSFetchedResultsController and 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/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;
    &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/2014/07/17/implement-nsmanagedobject-subclass-in-swift/&#34; title=&#34;Implement NSManagedObject Subclass in Swift&#34;&gt;Implement NSManagedObject Subclass 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 Tip – Accessing a User’s Documents Directory with URL Extension</title>
      <link>https://www.andrewcbancroft.com/2017/04/10/swift-tip-accessing-a-users-documents-directory-with-url-extension/</link>
      <pubDate>Mon, 10 Apr 2017 18:01:23 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2017/04/10/swift-tip-accessing-a-users-documents-directory-with-url-extension/</guid>
      <description>&lt;p&gt;When you need to access the documents directory on a user&amp;rsquo;s device, what do you do?&lt;/p&gt;
&lt;p&gt;In the past, I know I&amp;rsquo;ve gotten used to typing out the same set of code over and over throughout my apps. I know, I know – DRY – &lt;em&gt;don&amp;rsquo;t repeat yourself&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;One way to simplify this is to use a Swift extension to &lt;code&gt;URL&lt;/code&gt;. Doing this will help you centralize where this code is located in your app, and keep your code DRY. If you need to access the documents directory in more than one spot, you&amp;rsquo;re just a &lt;code&gt;URL&lt;/code&gt; property call away from it.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;implementation&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;extension-implementation&#34;&gt;Extension implementation&lt;/h1&gt;
&lt;p&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;extension&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;URL&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;static&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;documentsURL&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&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;try&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;!&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;FileManager&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;default&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;url&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;k&#34;&gt;for&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;documentDirectory&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;cl&#34;&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 class=&#34;n&#34;&gt;userDomainMask&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;cl&#34;&gt;                 &lt;span class=&#34;n&#34;&gt;appropriateFor&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;cl&#34;&gt;                 &lt;span class=&#34;n&#34;&gt;create&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&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;a name=&#34;usage&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;extension-usage&#34;&gt;Extension usage&lt;/h1&gt;
&lt;p&gt;Here&amp;rsquo;s an example of using the extension during the routine to create the Core Data stack. For that process, you need to grab a URL to where your SQLite database file will be located. Take a look:&lt;/p&gt;
&lt;p&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;5
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;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;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;storeURL&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;n&#34;&gt;documentsURL&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;appendingPathComponent&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;AppDatabase.sqlite&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;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;// use the URL&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
You could be needing to access the directory for saving files or retrieving files totally unrelated to Core Data. No matter what you&amp;rsquo;re doing, being able to write &lt;code&gt;URL.documentsURL&lt;/code&gt; is pretty convenient!&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>Core Data Fundamentals with Swift</title>
      <link>https://www.andrewcbancroft.com/2017/04/02/core-data-fundamentals-swift/</link>
      <pubDate>Sun, 02 Apr 2017 22:04:02 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2017/04/02/core-data-fundamentals-swift/</guid>
      <description>&lt;p&gt;Core Data is an extremely powerful framework for managing and persisting data on your users&amp;rsquo; devices. And let&amp;rsquo;s face it: saving data to your users&amp;rsquo; devices is inevitable. The question is, which data management and persistence technology you use when the time comes for &lt;em&gt;you&lt;/em&gt; to tackle this challenge?&lt;/p&gt;
&lt;p&gt;&amp;ldquo;&lt;a href=&#34;http://bit.ly/ps-core-data-swift&#34;&gt;Core Data Fundamentals with Swift&lt;/a&gt;&amp;rdquo; will give you the essential skills you need to architect an app to use Core Data.&lt;/p&gt;
&lt;h1 id=&#34;course-trailer&#34;&gt;Course Trailer&lt;/h1&gt;
&lt;p&gt;You can check out a trailer of the course over at Pluralsight completely free.&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;h1 id=&#34;topics&#34;&gt;Topics&lt;/h1&gt;
&lt;h2 id=&#34;1--getting-started-with-core-data&#34;&gt;1 – Getting Started with Core Data&lt;/h2&gt;
&lt;p&gt;Module 1 is the introduction to the course with discussion of the motivating factors for using Core Data, course prerequisites, storyline overview, and a demo of the end product of the course.&lt;/p&gt;
&lt;p&gt;I had a blast making the demo app. Essentially, I put you in the scenario where we team up to build a prototype of an app that our Human Resources department has come to us with. They came to us with an idea – Over the past year or two, they’ve noticed that employee morale is down a bit. To improve the situation, they’re implementing a new Employee Recognition program. As part of the new program, they’ve come to our internal software development team to see if it might be possible to build some kind of an app that allows employees to recognize one another for jobs well-done – electronic “Shout Outs” if you will.&lt;/p&gt;
&lt;p&gt;And that’s where you and I come in – HR wants us to prototype something up so that they have an idea of what’s possible.&lt;/p&gt;
&lt;p&gt;Throughout the course, we work on this prototype app together!&lt;/p&gt;
&lt;h2 id=&#34;2--setting-up-the-core-data-stack&#34;&gt;2 -Setting up the Core Data Stack&lt;/h2&gt;
&lt;p&gt;Working with Core Data begins with setting up the Core Data Stack. You will learn how to implement the stack, how to architect your app for testability and maintenance, and how to share Core Data components (such as NSManagedObjectContext) between view controllers.&lt;/p&gt;
&lt;h2 id=&#34;3--creating-a-data-model&#34;&gt;3 – Creating a Data Model&lt;/h2&gt;
&lt;p&gt;Building a data model is the foundation for persisting information with Core Data. You will learn to create a data model in Xcode, and you will understand how to work with that model in Swift code.&lt;/p&gt;
&lt;h2 id=&#34;4--building-relationships-between-entities&#34;&gt;4 – Building Relationships Between Entities&lt;/h2&gt;
&lt;p&gt;Understanding the importance of relationships and how to build them is vital to the efficiency and expressiveness of your Core Data model. You will learn why building relationships is worth the investment, and then you will gain the skills you need to be able to build these strategic links between Entities in Xcode.&lt;/p&gt;
&lt;h2 id=&#34;5--saving-accessing-and-deleting-data&#34;&gt;5 – Saving, Accessing, and Deleting Data&lt;/h2&gt;
&lt;p&gt;Having the ability to save data to a Core Data persistent store is essential. Being able to retrieve that data later on for display, for modification, or even for removal from the persistent store is also a critical. You will learn the mechanics of how to use the Core Data framework classes that are involved with saving, accessing, and deleting data in this module.&lt;/p&gt;
&lt;h2 id=&#34;6--showing-and-synchronizing-data-with-nsfetchedresultscontroller&#34;&gt;6 – Showing and Synchronizing Data with NSFetchedResultsController&lt;/h2&gt;
&lt;p&gt;Displaying data in a user interface and keeping it synchronized with the state of the persistent store is a critical piece of the Core Data puzzle. You will learn how to use NSFetchedResultsController for keeping table views in sync. You will also examine using the iOS Notification Center as an alternative way to detect and respond to changes in the persistent store.&lt;/p&gt;
&lt;h2 id=&#34;7--changing-core-data-models&#34;&gt;7 – Changing Core Data Models&lt;/h2&gt;
&lt;p&gt;It&amp;rsquo;s inevitable: You&amp;rsquo;re going to have to change your data model from its original form sooner or later. This module will guide you in creating new versions of your data model. You will also learn how to migrate your persistent store to new data model versions so that no data is lost as you make changes.&lt;/p&gt;
&lt;h2 id=&#34;8--understanding-core-data-changes-in-ios-10-and-macos-sierra&#34;&gt;8 – Understanding Core Data Changes in iOS 10 and macOS Sierra&lt;/h2&gt;
&lt;p&gt;Change is constant. Be prepared for the latest enhancements to the Core Data framework by understanding what&amp;rsquo;s new in Core Data with Apple&amp;rsquo;s latest platforms. You will learn about a new framework class called NSPersistentContainer that is designed to simplify the Core Data stack creation process. This concluding module will also point you to resources that will help you build on your new Core Data foundation.&lt;/p&gt;
&lt;h1 id=&#34;course-resources&#34;&gt;Course Resources&lt;/h1&gt;
&lt;p&gt;Demos of technology in action are &lt;em&gt;super&lt;/em&gt; important to me. That being the case, I tried to pack as much demo content into the course as possible. You&amp;rsquo;ll get to see the full array of dev-time experiences, not just the final &amp;ldquo;it works perfectly” outcomes.&lt;/p&gt;
&lt;p&gt;The course also comes complete with working Xcode projects that gradually build you up to the final version of the app, one step at a time through each module.&lt;/p&gt;
&lt;h1 id=&#34;feedback-welcome&#34;&gt;Feedback Welcome!&lt;/h1&gt;
&lt;p&gt;I welcome feedback on this course, and on other iOS development courses you might be interested in seeing in the Pluralsight library. Happy learning!&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Entity Framework Tip – Specifying Decimal Precision</title>
      <link>https://www.andrewcbancroft.com/2017/03/29/entity-framework-tip-specifying-decimal-precision/</link>
      <pubDate>Wed, 29 Mar 2017 20:57:27 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2017/03/29/entity-framework-tip-specifying-decimal-precision/</guid>
      <description>&lt;p&gt;By default, Entity Framework takes the .Net &lt;code&gt;decimal&lt;/code&gt; Type and maps it to SQL Server&amp;rsquo;s &lt;code&gt;decimal(18,2)&lt;/code&gt; data type.&lt;/p&gt;
&lt;p&gt;If you&amp;rsquo;ve got a property on an Entity that is of Type &lt;code&gt;decimal&lt;/code&gt;, but down in your database, you&amp;rsquo;re allowing for greater precision than 2 decimal places (&lt;a href=&#34;https://docs.microsoft.com/en-us/sql/t-sql/data-types/precision-scale-and-length-transact-sql&#34;&gt;&lt;em&gt;scale&lt;/em&gt;&lt;/a&gt; is actually the proper term for the number of places after the decimal), you need to tell Entity Framework this information. Otherwise, decimal values that you save to your database will be truncated at the default &lt;strong&gt;2&lt;/strong&gt; decimal places.&lt;/p&gt;
&lt;h2 id=&#34;updating-decimal-scale-with-entity-framework&#34;&gt;Updating decimal scale with Entity Framework&lt;/h2&gt;
&lt;p&gt;To tell Entity Framework that you need different precision than &lt;code&gt;decimal(18,2)&lt;/code&gt;, you need to &lt;strong&gt;open your DbContext&lt;/strong&gt; implementation.&lt;/p&gt;
&lt;p&gt;Then take a look at your &lt;code&gt;OnModelCreating&lt;/code&gt; override.&lt;/p&gt;
&lt;p&gt;If you haven&amp;rsquo;t implemented an override yet, go ahead and create one. If you&amp;rsquo;ve already got some Code First stuff in your &lt;code&gt;OnModelCreating&lt;/code&gt; override, add to it by following 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-c#&#34; data-lang=&#34;c#&#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;protected&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;override&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;void&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;OnModelCreating&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;DbModelBuilder&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;modelBuilder&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&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;modelBuilder&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;Entity&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;NameOfEntity&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;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 class=&#34;n&#34;&gt;Property&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;p&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;p&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;NameOfProperty&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 class=&#34;n&#34;&gt;HasPrecision&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;m&#34;&gt;9&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;m&#34;&gt;4&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;);&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;// or whatever your schema specifies&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;It&amp;rsquo;s as simple as that! Once you make this update, your decimal values won&amp;rsquo;t be truncated at the second decimal place anymore. They&amp;rsquo;ll be as precise as you&amp;rsquo;ve specified in your call to &lt;code&gt;HasPrecision()&lt;/code&gt;.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Core Data: Solving Ambiguous Type / Redeclaration of Type</title>
      <link>https://www.andrewcbancroft.com/2017/03/28/core-data-solving-ambiguous-type-redeclaration-of-type/</link>
      <pubDate>Wed, 29 Mar 2017 02:57:05 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2017/03/28/core-data-solving-ambiguous-type-redeclaration-of-type/</guid>
      <description>&lt;p&gt;I just finished roasting some coffee from Peru. I&amp;rsquo;ve never had it before, but it got me thinking about my &lt;a href=&#34;https://github.com/andrewcbancroft/RoasterOnTheGo&#34;&gt;Roaster On the Go&lt;/a&gt; &amp;ldquo;app” as I thought about the context that I could use to show you the solution to the compiler error described in the title. I used this to demonstrate &lt;a href=&#34;https://www.andrewcbancroft.com/2015/12/18/working-with-unwind-segues-programmatically-in-swift/&#34;&gt;Working with Unwind Segues Programmatically in Swift&lt;/a&gt;, but now I&amp;rsquo;m going to use it to show you how to resolve these errors that can crop up when you&amp;rsquo;re implementing &lt;code&gt;NSManagedObject&lt;/code&gt; subclasses for your Core Data Entities.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;scenario&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;scenario--adding-a-data-model&#34;&gt;Scenario – Adding a data model&lt;/h1&gt;
&lt;p&gt;The &amp;ldquo;app” doesn&amp;rsquo;t currently use Core Data, but for this article, I&amp;rsquo;ve created a rudimentary data model that includes a single Entity called &lt;code&gt;Product&lt;/code&gt;:&lt;br&gt;
&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2017/03/RoasterOnTheGo_xcdatamodel.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2017/03/RoasterOnTheGo_xcdatamodel.png&#34; alt=&#34;RoasterOnTheGo_xcdatamodel&#34; width=&#34;529&#34; height=&#34;148&#34; class=&#34;alignnone size-full wp-image-13091&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2017/03/RoasterOnTheGo_xcdatamodel.png 529w, https://www.andrewcbancroft.com/wp-content/uploads/2017/03/RoasterOnTheGo_xcdatamodel-300x84.png 300w&#34; sizes=&#34;(max-width: 529px) 100vw, 529px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Now suppose that I&amp;rsquo;m ready to &lt;a href=&#34;https://www.andrewcbancroft.com/2014/07/17/implement-nsmanagedobject-subclass-in-swift/&#34;&gt;implement a subclass of NSManagedObject&lt;/a&gt; for the &lt;code&gt;Product&lt;/code&gt; Entity.&lt;/p&gt;
&lt;p&gt;Doing so right now using Xcode 8&amp;rsquo;s default settings is going to give me all kinds of issues.&lt;/p&gt;
&lt;p&gt;To be clear, I&amp;rsquo;m using &lt;strong&gt;Xcode 8.2&lt;/strong&gt; for this scenario.&lt;/p&gt;
&lt;p&gt;The error that I&amp;rsquo;m getting when I attempt to build after creating a subclass of &lt;code&gt;NSManagedObject&lt;/code&gt; for the &lt;code&gt;Product&lt;/code&gt; Entity is one of the following (depending on how you choose to create the &lt;code&gt;NSManagedObject&lt;/code&gt; subclass):&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Invalid redeclaration of ‘Product&#39;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;or&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;‘Product&amp;rsquo; is ambiguous for type lookup in this context&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;or in the compiler output&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;filename &amp;ldquo;Product+CoreDataClass.swift” used twice&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;What&amp;rsquo;s happening??&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;This is a conflict with Xcode 8&amp;rsquo;s auto codegen for &lt;code&gt;NSManagedObject&lt;/code&gt; subclasses.&lt;/strong&gt; In some versions of Xcode 8 (version 8.2, for example), this auto codegen is turned &lt;strong&gt;on by default&lt;/strong&gt;. This can present a problem if you intend to manually create &lt;code&gt;NSManagedObjectSubclasses&lt;/code&gt; for your Entities.&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;solution&#34;&gt;Solution&lt;/h1&gt;
&lt;p&gt;To solve this problem, or to avoid it before it occurs, you essentially need to turn off Xcode 8&amp;rsquo;s auto code generation feature for &lt;code&gt;NSManagedObject&lt;/code&gt; subclasses if you intend to implement your &lt;code&gt;NSManagedObject&lt;/code&gt; subclasses manually (my personal preference).&lt;/p&gt;
&lt;p&gt;Take a look at the steps below to get your project building again:&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;step1&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;1--turn-off-automatic-code-generation-for-nsmanagedobject-subclasses&#34;&gt;1 – Turn off automatic code generation for NSManagedObject subclasses&lt;/h2&gt;
&lt;p&gt;Open your data model (the .xcdatamodeld file in your project).&lt;/p&gt;
&lt;p&gt;Click on each Entity in your data model and in the Data Model Inspector of the Utilities Pane, change the Codegen property from &amp;ldquo;Class Definition” or &amp;ldquo;Category/Extension” to &lt;strong&gt;&amp;ldquo;Manual/None”&lt;/strong&gt;:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2017/03/CodeGenOff.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2017/03/CodeGenOff-1024x297.png&#34; alt=&#34;Turn code generation off&#34; width=&#34;1024&#34; height=&#34;297&#34; class=&#34;alignnone size-large wp-image-13107&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2017/03/CodeGenOff-1024x297.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2017/03/CodeGenOff-300x87.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2017/03/CodeGenOff.png 1284w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;step2&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;2--choose-module&#34;&gt;2 – Choose module&lt;/h2&gt;
&lt;p&gt;You also need to tell Xcode where it can look to find a definition for the &lt;code&gt;NSManagedObject&lt;/code&gt; subclass that you will implement for this Entity by choosing &amp;ldquo;Current Product Module” for the Module value:&lt;br&gt;
&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2017/03/Module_CurrentProject.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2017/03/Module_CurrentProject-1024x312.png&#34; alt=&#34;Choose &amp;quot;Current Product Module&amp;quot; as module value&#34; width=&#34;1024&#34; height=&#34;312&#34; class=&#34;alignnone size-large wp-image-13109&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2017/03/Module_CurrentProject-1024x312.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2017/03/Module_CurrentProject-300x91.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2017/03/Module_CurrentProject.png 1284w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Save&lt;/strong&gt; your .xcdatamodeld file.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;step3&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;3--clear-derived-data&#34;&gt;3 – Clear derived data&lt;/h2&gt;
&lt;p&gt;Behind the scenes, Xcode creates files and implements Types for the Entities in your data model. It puts them in your derived data folder and automatically traverses this folder to look for Type definitions for Core Data Entities. So in order to get your project building again, you need to get rid of those files that Xcode created automatically.&lt;/p&gt;
&lt;p&gt;The &amp;ldquo;big hammer” for this is to simply clear all of your project&amp;rsquo;s derived data. To do this, click on the Products menu, hold down the Option key, and click &amp;ldquo;Clean build folder…”:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2017/03/CleanBuildFolder.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2017/03/CleanBuildFolder.png&#34; alt=&#34;Product -&gt; Press Option Key -&gt; Clean Build Folder...&#34; width=&#34;224&#34; height=&#34;321&#34; class=&#34;alignnone size-full wp-image-13118&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2017/03/CleanBuildFolder.png 224w, https://www.andrewcbancroft.com/wp-content/uploads/2017/03/CleanBuildFolder-209x300.png 209w&#34; sizes=&#34;(max-width: 224px) 100vw, 224px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Alternatively&lt;/em&gt;, you could opt to go in and delete the &lt;em&gt;only&lt;/em&gt; auto-generated Swift files, but you&amp;rsquo;ve got to do some digging.&lt;/p&gt;
&lt;p&gt;Click on File -&amp;gt; Project Settings:&lt;br&gt;
&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2017/03/ProjectSettings1.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2017/03/ProjectSettings1.png&#34; alt=&#34;File -&gt; Project Settings&#34; width=&#34;353&#34; height=&#34;495&#34; class=&#34;alignnone size-full wp-image-13121&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2017/03/ProjectSettings1.png 353w, https://www.andrewcbancroft.com/wp-content/uploads/2017/03/ProjectSettings1-214x300.png 214w&#34; sizes=&#34;(max-width: 353px) 100vw, 353px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Next, click on the arrow next to the path leading to your default derived data folder:&lt;br&gt;
&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2017/03/DerivedData.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2017/03/DerivedData.png&#34; alt=&#34;Click derived data folder arrow&#34; width=&#34;534&#34; height=&#34;320&#34; class=&#34;alignnone size-full wp-image-13122&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2017/03/DerivedData.png 534w, https://www.andrewcbancroft.com/wp-content/uploads/2017/03/DerivedData-300x180.png 300w&#34; sizes=&#34;(max-width: 534px) 100vw, 534px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Next, find the folder for your project at the root of your default derived data directory (where you were taken in Finder when you clicked the arrow in Project Settings). Then get ready to dig…&lt;/p&gt;
&lt;p&gt;Click Build -&amp;gt; Intermediates -&amp;gt; ProjectName.build -&amp;gt; Debug-iphonesimulator -&amp;gt; ProjectName.build -&amp;gt; DerivedSources -&amp;gt; CoreDataGenerated -&amp;gt; ProjectName&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Delete the .swift files&lt;/strong&gt; in this folder.&lt;br&gt;
&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2017/03/PathToAutoGeneratedFiles.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2017/03/PathToAutoGeneratedFiles-1024x198.png&#34; alt=&#34;Path to auto-generated Entities&#34; width=&#34;1024&#34; height=&#34;198&#34; class=&#34;alignnone size-large wp-image-13123&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2017/03/PathToAutoGeneratedFiles-1024x198.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2017/03/PathToAutoGeneratedFiles-300x58.png 300w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;step4&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;4--rebuild&#34;&gt;4 – Rebuild&lt;/h2&gt;
&lt;p&gt;After you rebuild, any compiler errors you previously had, related to &amp;ldquo;ambiguous Type” or &amp;ldquo;redeclaration of Type” should be resolved!&lt;/p&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>Solving ‘Spatial types and functions are not available’ with Entity Framework</title>
      <link>https://www.andrewcbancroft.com/2017/03/27/solving-spatial-types-and-functions-are-not-available-with-entity-framework/</link>
      <pubDate>Mon, 27 Mar 2017 16:38:33 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2017/03/27/solving-spatial-types-and-functions-are-not-available-with-entity-framework/</guid>
      <description>&lt;p&gt;Using SQL Server&amp;rsquo;s Geospatial features with Entity Framework is awesome. I was crusing along just fine with using &lt;code&gt;DbGeography&lt;/code&gt; for an ASP.Net application I&amp;rsquo;m working on… Right up until I deployed to the server. Yep. It worked on &lt;em&gt;my&lt;/em&gt; machine, but alas, I was hitting a runtime exception on my test server.&lt;/p&gt;
&lt;p&gt;This was the exception:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Spatial types and functions are not available for this provider because the assembly ‘Microsoft.SqlServer.Types&amp;rsquo; version 10 or higher could not be found.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Luckily, I ran across a &lt;a href=&#34;http://stackoverflow.com/questions/13174197/microsoft-sqlserver-types-version-10-or-higher-could-not-be-found-on-azure&#34;&gt;Stack Overflow answer&lt;/a&gt; that &lt;em&gt;almost&lt;/em&gt; helped me get all the way there in solving this exception. It was actually &lt;a href=&#34;http://stackoverflow.com/a/40166192/3198996&#34;&gt;the second-most up-voted answer&lt;/a&gt; that helped me the most. You&amp;rsquo;re welcome to reference these if you want, but since the info is scattered between the question and the two answers, I&amp;rsquo;m assembling it here for your convenience.&lt;/p&gt;
&lt;h1 id=&#34;fixing-8216spatial-types-and-functions-are-not-available&#34;&gt;Fixing ‘Spatial types and functions are not available&#39;&lt;/h1&gt;
&lt;h2 id=&#34;1--install-the-microsoftsqlservertypes-package-from-nuget&#34;&gt;1 – Install the Microsoft.SqlServer.Types package from NuGet&lt;/h2&gt;
&lt;p&gt;You can install it using the NuGet Package Manager UI, or from the command line:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2017/03/NuGetPM_MSSqlServerTypes1.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2017/03/NuGetPM_MSSqlServerTypes1.png&#34; alt=&#34;NuGetPM_MSSqlServerTypes&#34; width=&#34;985&#34; height=&#34;270&#34; class=&#34;alignnone size-full wp-image-13100&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2017/03/NuGetPM_MSSqlServerTypes1.png 985w, https://www.andrewcbancroft.com/wp-content/uploads/2017/03/NuGetPM_MSSqlServerTypes1-300x82.png 300w&#34; sizes=&#34;(max-width: 985px) 100vw, 985px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;or&lt;/p&gt;
&lt;p&gt;&lt;code&gt;&amp;gt; Install-Package Microsoft.SqlServer.Types&lt;/code&gt;&lt;/p&gt;
&lt;h2 id=&#34;2--follow-readmehtm-instructions&#34;&gt;2 – Follow readme.htm instructions&lt;/h2&gt;
&lt;p&gt;After you install the NuGet package, a readme.htm file is opened and displayed to you. If you don&amp;rsquo;t see it for some reason, a new SqlServerTypes folder was added to your project. You can expand it and double-click readme.htm to open it.&lt;/p&gt;
&lt;p&gt;Follow the instructions within.&lt;/p&gt;
&lt;p&gt;Depending on the type of app you&amp;rsquo;re deploying, you need to perform different steps. I happened to be deploying an ASP.Net MVC app, so I followed the instructions for adding a line of code to Global.asax.cs. Your situation may be different, but the Readme instructions are clear on what to do.&lt;/p&gt;
&lt;h3 id=&#34;aspnet-web-applications&#34;&gt;ASP.Net Web &lt;em&gt;Applications&lt;/em&gt;&lt;/h3&gt;
&lt;p&gt;Open Global.asax.cs and add the following to the list of registrations in &lt;code&gt;Application_Start()&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-c#&#34; data-lang=&#34;c#&#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;protected&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;void&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Application_Start&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&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;// Enables use of spatial data types&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;SqlServerTypes&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;Utilities&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;LoadNativeAssemblies&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;Server&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;MapPath&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;~/bin&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&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;// Other registrations...&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;h3 id=&#34;aspnet-websites&#34;&gt;Asp.Net &lt;em&gt;Websites&lt;/em&gt;&lt;/h3&gt;
&lt;p&gt;Open Default.aspx.cs and add&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-c#&#34; data-lang=&#34;c#&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 1&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;partial&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;_Default&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;System&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;Web&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;UI&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;Page&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&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;static&lt;/span&gt; &lt;span class=&#34;kt&#34;&gt;bool&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;_isSqlTypesLoaded&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; 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;public&lt;/span&gt; &lt;span class=&#34;n&#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; 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;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;(!&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;_isSqlTypesLoaded&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 class=&#34;n&#34;&gt;SqlServerTypes&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;Utilities&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;LoadNativeAssemblies&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;Server&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;MapPath&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;~&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;n&#34;&gt;_isSqlTypesLoaded&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&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;p&#34;&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;h3 id=&#34;desktop-applications&#34;&gt;Desktop Applications&lt;/h3&gt;
&lt;p&gt;Add the following before any spatial operations are performed.&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-c#&#34; data-lang=&#34;c#&#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;SqlServerTypes&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;Utilities&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;LoadNativeAssemblies&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;AppDomain&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;CurrentDomain&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;BaseDirectory&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;h3 id=&#34;3--tell-entity-framework-which-version-of-the-assembly-to-use&#34;&gt;3 – Tell Entity Framework which version of the assembly to use&lt;/h3&gt;
&lt;p&gt;Most of the explanations of how to solve the &amp;ldquo;‘Microsoft.SqlServer.Types&amp;rsquo; version 10 or higher” error stopped at step 2.&lt;/p&gt;
&lt;p&gt;After performing the steps that readme.htm outlined, I re-deployed the app, and things were still broken. Same exception. What gives??&lt;/p&gt;
&lt;p&gt;This is where that &lt;a href=&#34;http://stackoverflow.com/a/40166192/3198996&#34;&gt;second-most-popular answer&lt;/a&gt; came into play. It gave me the clue I needed.&lt;/p&gt;
&lt;p&gt;Back in Global.asax.cs (or wherever you performed the steps for #2 above), add the following”&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;    SqlProviderServices.SqlServerTypesAssemblyName =
    &amp;#34;Microsoft.SqlServer.Types, Version=14.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91&amp;#34;;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Depending on which version of the NuGet package you installed, you need to adjust the Version number from 14.0.0.0 to the correct version, as appropriate. I installed the latest, which at the time is version 14.x, so that&amp;rsquo;s what I put in.&lt;/p&gt;
&lt;h4 id=&#34;-the-sqlservertypesassemblyname-property-doesnt-exist-&#34;&gt;??? The SqlServerTypesAssemblyName property doesn&amp;rsquo;t exist! ???&lt;/h4&gt;
&lt;p&gt;In my case, I ran into another small hurdle. When I tried to set the &lt;code&gt;SqlServerTypesAssemblyName&lt;/code&gt; property, it didn&amp;rsquo;t exist!&lt;/p&gt;
&lt;p&gt;Upon further inspection, I discovered that I only had Entity Framework version 6.1.**** installed. I updated to 6.1.&lt;strong&gt;3&lt;/strong&gt;, and the property lit up. I set it appropriately, redeployed the app, and it magically worked.&lt;/p&gt;
&lt;p&gt;Here&amp;rsquo;s hoping that this helps bring together several pieces of information to get you going with using SQL Server Geospatial data types with Entity Framework!&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>Using a Core Data Model in Swift Playgrounds</title>
      <link>https://www.andrewcbancroft.com/2016/07/10/using-a-core-data-model-in-swift-playgrounds/</link>
      <pubDate>Sun, 10 Jul 2016 20:48:54 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2016/07/10/using-a-core-data-model-in-swift-playgrounds/</guid>
      <description>&lt;p&gt;Did you know that you can tinker with Core Data inside of Swift playgrounds in Xcode? You can!&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://twitter.com/JCubedApps&#34;&gt;Jeremiah Jessel&lt;/a&gt;, author at &lt;a href=&#34;http://www.learncoredata.com&#34;&gt;http://www.learncoredata.com&lt;/a&gt;, wrote up an article in 2015 &lt;a href=&#34;http://www.learncoredata.com/core-data-and-playgrounds/&#34;&gt;detailing how you can use the Core Data framework inside a playground&lt;/a&gt;. He shows how you can do everything from setting up the Core Data stack, to creating NSManagedObjects programmatically in code. Great stuff!&lt;/p&gt;
&lt;p&gt;After I read his guide, I got to thinking: &lt;em&gt;I wonder&lt;/em&gt; if you can take an .xcdatamodeld file created with Xcode&amp;rsquo;s Data Model designer and use &lt;em&gt;it&lt;/em&gt; in a Playground….&lt;/p&gt;
&lt;p&gt;The short answer is, &lt;strong&gt;kinda&lt;/strong&gt;. You can&amp;rsquo;t use the .xcdatamodeld file (at least, I couldn&amp;rsquo;t find a way), BUT, you &lt;em&gt;can&lt;/em&gt; use the &lt;em&gt;compiled&lt;/em&gt; &amp;ldquo;momd” file that gets created when you build your app.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;limitations&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;limitations&#34;&gt;Limitations&lt;/h1&gt;
&lt;p&gt;There&amp;rsquo;s at least two limitations / caveats I&amp;rsquo;ve come across as I&amp;rsquo;ve been playing with this concept:&lt;/p&gt;
&lt;h2 id=&#34;no-nsmanagedobject-subclasses&#34;&gt;No NSManagedObject subclasses&lt;/h2&gt;
&lt;p&gt;While you can create instances of the Entities in the model, if you&amp;rsquo;ve created &lt;code&gt;NSManagedObject&lt;/code&gt; subclasses for your Entities, you won&amp;rsquo;t be able to use those in the playground. You&amp;rsquo;d have to resort back to setting properties on your &lt;code&gt;NSManagedObject&lt;/code&gt; instances using &lt;code&gt;setValue(_: forKey:)&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;But this is a minor drawback, especially if you&amp;rsquo;re just wanting to tinker.&lt;/p&gt;
&lt;h2 id=&#34;model-updates&#34;&gt;Model updates&lt;/h2&gt;
&lt;p&gt;After you read the &lt;a href=&#34;#walkthrough&#34;&gt;walkthrough&lt;/a&gt;, you&amp;rsquo;ll know how to get the model into your playground.&lt;/p&gt;
&lt;p&gt;Here&amp;rsquo;s the deal though: If you ever make &lt;em&gt;changes&lt;/em&gt; to your model, you&amp;rsquo;ll need to go through the steps necessary to &lt;em&gt;re&lt;/em&gt;-add a freshly-compiled model to the playground&amp;rsquo;s Resources folder that includes the changes. This is because resources that are added to a playground are &lt;em&gt;copied&lt;/em&gt;, not referenced.&lt;/p&gt;
&lt;p&gt;I don&amp;rsquo;t think that&amp;rsquo;s a terrible draw-back, especially once you know how to do it.&lt;/p&gt;
&lt;p&gt;So how do you do it? Here&amp;rsquo;s how:&lt;/p&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;walkthrough&#34;&gt;Walkthrough&lt;/h1&gt;
&lt;p&gt;Get started by adding a Data Model to your project. If you&amp;rsquo;ve got a project already going that uses Core Data, you probably already have a .xcdatamodeld file in your project. If you don&amp;rsquo;t, though, one is easily add-able from the File menu:&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;add-data-model&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;add-data-model-file-unless-you-already-have-one&#34;&gt;Add Data Model file (unless you already have one)&lt;/h2&gt;
&lt;p&gt;File -&amp;gt; New -&amp;gt; File…&lt;br&gt;
&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/07/new-model.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/07/new-model.png&#34; alt=&#34;New Data Model&#34; width=&#34;728&#34; height=&#34;516&#34; class=&#34;alignnone size-full wp-image-12999&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/07/new-model.png 728w, https://www.andrewcbancroft.com/wp-content/uploads/2016/07/new-model-300x213.png 300w&#34; sizes=&#34;(max-width: 728px) 100vw, 728px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;For my &amp;ldquo;smoke test”, just to see if it was possible, I left the default value for the model name as &amp;ldquo;Model.xcdatamodeld”.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;add-entity&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;add-entity-with-attribute&#34;&gt;Add entity with attribute&lt;/h2&gt;
&lt;p&gt;Once I had the data model added to the project, I went in and added an entity (named &amp;ldquo;Entity”) with an attribute (named &amp;ldquo;attribute” of type Integer 16):&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/07/add-entity-and-attributes.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/07/add-entity-and-attributes-1024x653.png&#34; alt=&#34;Add an entity with an attribute.&#34; width=&#34;1024&#34; height=&#34;653&#34; class=&#34;alignnone size-large wp-image-13003&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/07/add-entity-and-attributes-1024x653.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2016/07/add-entity-and-attributes-300x191.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2016/07/add-entity-and-attributes.png 1129w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;add-playground&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;add-a-playground&#34;&gt;Add a playground&lt;/h2&gt;
&lt;p&gt;Next up, I added a new playground to my project:&lt;/p&gt;
&lt;p&gt;File -&amp;gt; New -&amp;gt; Playground…&lt;br&gt;
&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/07/new-playground.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/07/new-playground.png&#34; alt=&#34;Add new playground&#34; width=&#34;877&#34; height=&#34;396&#34; class=&#34;alignnone size-full wp-image-13006&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/07/new-playground.png 877w, https://www.andrewcbancroft.com/wp-content/uploads/2016/07/new-playground-300x135.png 300w&#34; sizes=&#34;(max-width: 877px) 100vw, 877px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;build-project&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;build-project-locate-momd8221-file&#34;&gt;Build project; Locate &amp;ldquo;momd” file&lt;/h2&gt;
&lt;p&gt;With a playground and a data model has some structure to it, I built the project (CMD + B) so that the .xcdatamodeld file would be compiled into an &amp;ldquo;momd” file. It&amp;rsquo;s the &lt;em&gt;momd&lt;/em&gt; file that needs to be added to the playground as a resource.&lt;/p&gt;
&lt;p&gt;To find the &amp;ldquo;momd” file, expand &amp;ldquo;Products” in your project navigator, right-click the .app file, and click &amp;ldquo;Show in Finder”:&lt;br&gt;
&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/07/show-product-in-finder.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/07/show-product-in-finder.png&#34; alt=&#34;Show product in finder&#34; width=&#34;620&#34; height=&#34;515&#34; class=&#34;alignnone size-full wp-image-13008&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/07/show-product-in-finder.png 620w, https://www.andrewcbancroft.com/wp-content/uploads/2016/07/show-product-in-finder-300x249.png 300w&#34; sizes=&#34;(max-width: 620px) 100vw, 620px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;show-app-contents&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;show-app-package-contents&#34;&gt;Show .app package contents&lt;/h2&gt;
&lt;p&gt;In the finder window, right-click the .app file, and click &amp;ldquo;Show package contents”:&lt;br&gt;
&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/07/show-package-contents.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/07/show-package-contents.png&#34; alt=&#34;Show package contents&#34; width=&#34;749&#34; height=&#34;488&#34; class=&#34;alignnone size-full wp-image-13010&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/07/show-package-contents.png 749w, https://www.andrewcbancroft.com/wp-content/uploads/2016/07/show-package-contents-300x195.png 300w&#34; sizes=&#34;(max-width: 749px) 100vw, 749px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;drag-to-resources&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;drag-momd8221-file-from-finder-to-playground-resources-folder&#34;&gt;Drag &amp;ldquo;momd” file from Finder to playground Resources folder&lt;/h2&gt;
&lt;p&gt;&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/07/locate-momd-file.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/07/locate-momd-file.png&#34; alt=&#34;Locate &amp;quot;momd&amp;quot; file&#34; width=&#34;549&#34; height=&#34;304&#34; class=&#34;alignnone size-full wp-image-13013&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/07/locate-momd-file.png 549w, https://www.andrewcbancroft.com/wp-content/uploads/2016/07/locate-momd-file-300x166.png 300w&#34; sizes=&#34;(max-width: 549px) 100vw, 549px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/07/drag-momd-to-resources.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/07/drag-momd-to-resources-1024x447.png&#34; alt=&#34;Drag &amp;quot;momd&amp;quot; to Resources&#34; width=&#34;1024&#34; height=&#34;447&#34; class=&#34;alignnone size-large wp-image-13012&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/07/drag-momd-to-resources-1024x447.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2016/07/drag-momd-to-resources-300x131.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2016/07/drag-momd-to-resources.png 1029w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;write-code&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;write-core-data-code-to-use-model&#34;&gt;Write Core Data code to use model&lt;/h2&gt;
&lt;p&gt;That&amp;rsquo;s it! Now that the &amp;ldquo;momd” file is in the playground&amp;rsquo;s Resources folder, you&amp;rsquo;re set to write code against it. You can insert &lt;code&gt;NSManagedObject&lt;/code&gt; instances, perform fetch requests, etc. Here&amp;rsquo;s an example that I wrote up:&lt;/p&gt;
&lt;p&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;12
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;13
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;14
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;15
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;16
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;17
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;18
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;19
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;20
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;21
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;22
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;23
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;24
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;25
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;26
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;27
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;28
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;29
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;30
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;31
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;32
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;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;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;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;// Core Data Stack Setup for In-Memory Store&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;createMainContext&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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Replace &amp;#34;Model&amp;#34; with the name of your model&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;modelUrl&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;n&#34;&gt;URLForResource&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;Model&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;withExtension&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;momd&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;cl&#34;&gt;    &lt;span class=&#34;k&#34;&gt;guard&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;model&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;kd&#34;&gt;init&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;contentsOfurl&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;modelUrl&lt;/span&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 class=&#34;bp&#34;&gt;fatalError&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;model not found&amp;#34;&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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;psc&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;model&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;cl&#34;&gt;    &lt;span class=&#34;k&#34;&gt;try&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;!&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;psc&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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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 class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;concurrencyType&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;MainQueueConcurrencyType&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;cl&#34;&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;persistentStoreCoordinator&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;psc&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;    &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;context&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;createMainContext&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;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;// Insert a new Entity&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;ent&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;Entity&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;context&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;cl&#34;&gt;&lt;span class=&#34;n&#34;&gt;ent&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;setValue&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;42&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;attribute&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;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;k&#34;&gt;try&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;save&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;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;// Perform a fetch request&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;fr&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;Entity&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;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;result&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;try&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;fr&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;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;n&#34;&gt;result&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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/07/printed-result.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/07/printed-result.png&#34; alt=&#34;Fetch request result&#34; width=&#34;968&#34; height=&#34;73&#34; class=&#34;alignnone size-full wp-image-13018&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/07/printed-result.png 968w, https://www.andrewcbancroft.com/wp-content/uploads/2016/07/printed-result-300x23.png 300w&#34; sizes=&#34;(max-width: 968px) 100vw, 968px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Woohoo! I thought this was pretty cool.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Don&amp;rsquo;t forget&lt;/strong&gt;: If you make updates to your model, you need to re-build your app, delete the &amp;ldquo;momd” folder from your playground&amp;rsquo;s resources, re-drag the freshly-compiled &amp;ldquo;momd” file to your playground again to work with the latest version of the model.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;potential-usefulness&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;potential-usefulness&#34;&gt;Potential usefulness&lt;/h1&gt;
&lt;p&gt;The other important question to ask, besides &amp;ldquo;I wonder if this is possible?” is &amp;ldquo;How is this useful?”&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Learning. Playgrounds in and of themselves make sense as a learning tool. How cool is it to be able to build the model you&amp;rsquo;re thinking of in the Xcode designer, import that into a playground, and tinker with it as a learning exercise??&lt;/li&gt;
&lt;li&gt;This could also be useful when you need to try out your data model but don&amp;rsquo;t really want to wire it up to an actual user interface yet. Strip away all the UI complexity and just work with the data model… in a playground! It just seems like a more elegant solution to the &amp;ldquo;print it out to the console” method of experimenting with the model.&lt;/li&gt;
&lt;li&gt;There might be situations when you&amp;rsquo;re building semi-complicated &lt;code&gt;NSPredicate&lt;/code&gt; instances for a fetch request – why not get it working in a playground first, then migrate it over to your app? Just an idea!&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/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>TypeScript Build Errors When Publishing ASP.Net Web Project</title>
      <link>https://www.andrewcbancroft.com/2016/06/29/typescript-build-errors-when-publishing-asp-net-web-project/</link>
      <pubDate>Wed, 29 Jun 2016 18:41:59 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2016/06/29/typescript-build-errors-when-publishing-asp-net-web-project/</guid>
      <description>&lt;p&gt;I had TypeScript files building and running perfectly when I&amp;rsquo;d debug locally, but when I changed over to publish the project, I got build errors galore:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Cannot find module ___&lt;/p&gt;
&lt;p&gt;TS6053 – File ___ not found&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;I had forgotten that I set up my TypeScript Build configuration for &lt;em&gt;Debug&lt;/em&gt;, but I hadn&amp;rsquo;t changed anything for the &lt;em&gt;Release&lt;/em&gt; configuration in Visual Studio.&lt;/p&gt;
&lt;p&gt;Obviously, that would produce different build results, right? It was just a little mysterious to me until I noticed the configuration differences.&lt;/p&gt;
&lt;p&gt;If you&amp;rsquo;re running into similar errors when building / publishing your project using the Release configuration, right-click your ASP.Net web project, go down to TypeScript Build, and make sure that your configuration settings are the same between Debug and Release. In particular, my &amp;ldquo;Module System” settings were different until I adjusted things.&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/06/2016-06-29_13-39-22.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/06/2016-06-29_13-39-22.png&#34; alt=&#34;TypeScript Configuration&#34; width=&#34;830&#34; height=&#34;714&#34; class=&#34;alignnone size-full wp-image-12988&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/06/2016-06-29_13-39-22.png 830w, https://www.andrewcbancroft.com/wp-content/uploads/2016/06/2016-06-29_13-39-22-300x258.png 300w&#34; sizes=&#34;(max-width: 830px) 100vw, 830px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Force Unwrapping Swift Optionals:  Code Smell!</title>
      <link>https://www.andrewcbancroft.com/2016/06/13/force-unwrapping-swift-optionals-code-smell/</link>
      <pubDate>Tue, 14 Jun 2016 03:54:57 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2016/06/13/force-unwrapping-swift-optionals-code-smell/</guid>
      <description>&lt;p&gt;Do you find your Swift code riddled with &lt;code&gt;!&lt;/code&gt;‘s?&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;m becoming more and more uncomfortable with seeing &lt;code&gt;!&lt;/code&gt; throughout my Swift code. It&amp;rsquo;s just a matter of time before it&amp;rsquo;s going to bite me.&lt;/p&gt;
&lt;p&gt;Often, I do it because it&amp;rsquo;s the &amp;ldquo;easy thing” to do at the time. But it&amp;rsquo;s &lt;em&gt;dangerous&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;Swift optionals are trying to &lt;em&gt;help&lt;/em&gt; us. They force us to deal with the possibility of something not having a value.&lt;/p&gt;
&lt;p&gt;By force unwrapping an optional with the &lt;code&gt;!&lt;/code&gt; operator, we&amp;rsquo;re declaring, &amp;ldquo;This will &lt;em&gt;never&lt;/em&gt; be without a value”. Really? Never? Are you sure? Only a Sith deals in those kinds of absolutes.&lt;/p&gt;
&lt;p&gt;When a function returns an optional, or a property is declared as optional, we are, &lt;em&gt;at the very least&lt;/em&gt;, meant to assume that there is a &lt;em&gt;possibility&lt;/em&gt; of &lt;code&gt;nil&lt;/code&gt; lying underneath.&lt;/p&gt;
&lt;p&gt;Therefore, I&amp;rsquo;m considering it a sort of &amp;ldquo;code smell” when I see it in my own code. It&amp;rsquo;s not too much to throw an &lt;code&gt;if let&lt;/code&gt; or a &lt;code&gt;guard let&lt;/code&gt; in there to handle the possibility of &lt;code&gt;nil&lt;/code&gt;.&lt;/p&gt;
&lt;h1 id=&#34;two-exceptions&#34;&gt;Two exceptions&lt;/h1&gt;
&lt;p&gt;Two exceptions to the code smell rule:&lt;/p&gt;
&lt;h2 id=&#34;1--iboutlet-and-ibaction&#34;&gt;1 – IBOutlet and IBAction&lt;/h2&gt;
&lt;p&gt;IBOutlets and IBActions are force-unwrapped, but that&amp;rsquo;s because they get injected when the Storyboard is loaded at run-time. It&amp;rsquo;s assumed that these are connected and will be supplied when the scene is loaded. If they get unwired somehow, we &lt;em&gt;want&lt;/em&gt; an instant crash so we know to go back to the Storyboard and re-wire things to the view controller.&lt;/p&gt;
&lt;h2 id=&#34;2--required-properties-to-be-set-in-prepareforsegue&#34;&gt;2 – Required properties to be set in prepareForSegue&lt;/h2&gt;
&lt;p&gt;Along those same lines, I consider properties that &lt;em&gt;must&lt;/em&gt; be set when navigating to a new view controller to be in the same category as an IBOutlet or an IBAction. I want to know right away if I forget to set those in &lt;code&gt;prepareForSegue&lt;/code&gt; in the parent view controller. So I&amp;rsquo;ll often force unwrap the optional in the declaration so that there&amp;rsquo;s an as-immediate-as-possible crash if it&amp;rsquo;s not set.&lt;/p&gt;
&lt;p&gt;Hopefully when you&amp;rsquo;re working with optionals from here on, you&amp;rsquo;ll take a second sniff when you see the &lt;code&gt;!&lt;/code&gt; operator.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Is Protocol Oriented Swift Better than Object Oriented Swift?</title>
      <link>https://www.andrewcbancroft.com/2016/06/12/is-protocol-oriented-swift-better-than-object-oriented-swift/</link>
      <pubDate>Sun, 12 Jun 2016 20:59:48 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2016/06/12/is-protocol-oriented-swift-better-than-object-oriented-swift/</guid>
      <description>&lt;p&gt;A &lt;a href=&#34;https://www.reddit.com/r/swift/comments/4nme0c/eli5_what_does_protocol_oriented_programming_mean/&#34;&gt;question was asked on Reddit&lt;/a&gt; and showed up in my feed as I was browsing this weekend:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;What does protocol oriented programming mean, and why is it better than OOP?&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;I really like a few things that were said in &lt;a href=&#34;https://www.reddit.com/r/swift/comments/4nme0c/eli5_what_does_protocol_oriented_programming_mean/d453ryv&#34;&gt;one of the top &amp;ldquo;Explain Like I&amp;rsquo;m 5” comments&lt;/a&gt; that was posted in response to the question by &lt;a href=&#34;https://www.reddit.com/user/CodaFi&#34;&gt;CodaFi&lt;/a&gt;.&lt;/p&gt;
&lt;h1 id=&#34;explain-like-im-five8221--a-laundry-service-example&#34;&gt;&amp;ldquo;Explain Like I&amp;rsquo;m Five” – A laundry service example&lt;/h1&gt;
&lt;p&gt;First, the example provided was very understandable. I highly recommend you &lt;a href=&#34;https://www.reddit.com/r/swift/comments/4nme0c/eli5_what_does_protocol_oriented_programming_mean/d453ryv&#34;&gt;jump over&lt;/a&gt; and just read through what the commenter wrote.&lt;/p&gt;
&lt;p&gt;The theme was &amp;ldquo;laundry service”. Supposing that there was a &lt;code&gt;Laundry&lt;/code&gt; object that encapsulated certain laundry-related functions… &amp;ldquo;Launder my clothes, please” — &amp;ldquo;Okay, here&amp;rsquo;s your clothes!”. You as a client simply interact with &lt;code&gt;Laundry&lt;/code&gt; by saying &amp;ldquo;Launder, please!”. The &lt;code&gt;Laundry&lt;/code&gt; object goes off and does its thing, which could range from simple to incredibly complex – the beauty is that as a client, as a person needing laundry done, you don&amp;rsquo;t care. As long as the laundry gets done and you get your clothes back, life is great!&lt;/p&gt;
&lt;h1 id=&#34;problems8221-with-object-orientation&#34;&gt;&amp;ldquo;Problems” with Object Orientation&lt;/h1&gt;
&lt;p&gt;In my guide on &lt;a href=&#34;https://www.andrewcbancroft.com/2016/02/15/fundamentals-of-callbacks-for-swift-developers/&#34;&gt;the Fundamentals of Callbacks for Swift Developers&lt;/a&gt;, I describe the scenario that we&amp;rsquo;re all in as developers:&lt;/p&gt;
&lt;p&gt;When we’re building software, we’re either &lt;em&gt;using&lt;/em&gt; code built by others, or &lt;em&gt;creating&lt;/em&gt; code for others to use, are we not? We’re either using code that “hooks into” what other developers have designed and made available to us, or we’re creating code that other code will “hook into” and interact with, even if the “other code” is written by us in our own app.&lt;/p&gt;
&lt;p&gt;We wear two hats at once, when it comes to being clients and creators of code.&lt;/p&gt;
&lt;p&gt;But what if we&amp;rsquo;re working with a &lt;code&gt;Laundry&lt;/code&gt; object as a &lt;em&gt;developer/creator&lt;/em&gt;, and not as a &lt;em&gt;client&lt;/em&gt; (ie, someone who needs laundry done). What if we, as developers, get handed a &lt;code&gt;Laundry&lt;/code&gt; object in a library and we want to customize its behavior… maybe improve the performance of &lt;code&gt;launderClothes()&lt;/code&gt;, or override the implementation it to use some amazing new laundry service.&lt;/p&gt;
&lt;p&gt;The way we&amp;rsquo;d do that is by crating a &lt;em&gt;subclass&lt;/em&gt;. Here&amp;rsquo;s the beef with Object Orientation, according to the commenter:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;[Object Orientation] encourages &amp;ldquo;encapsulating complexity” by exposing state and internals through inheritance.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Unpacking that: Software has innate complexity. Objects are &amp;ldquo;things” that encapsulate that complexity. They do it in a certain &lt;em&gt;way&lt;/em&gt; though: They expose certain pieces of state and pieces of functionality. The way those abstractions over complexity get propagated and customized through the system is through this mechanism called inheritance.&lt;/p&gt;
&lt;p&gt;But the commenter cites this methodology as &amp;ldquo;trouble” though. Why? Well…&lt;/p&gt;
&lt;p&gt;&amp;ldquo;I may not want to know how my dry cleaning got done, but if I wanted to design a better route to go from dirty clothes to clean clothes, I would necessarily &lt;em&gt;have&lt;/em&gt; to know every last detail of the steps that were performed so I can try to refine them in my subclass.”&lt;/p&gt;
&lt;p&gt;So the commenter is coming at it from the point of a developer/creator. It&amp;rsquo;s pointed out that to truly be able to improve performance or refine algorithms in a subclass, we necessarily have to know every detail of the steps that were performed in the superclass. And it&amp;rsquo;s not always the case that we can discover that superclass implementation to improve upon it.&lt;/p&gt;
&lt;h1 id=&#34;shifting-to-protocol-orientation&#34;&gt;Shifting to Protocol Orientation&lt;/h1&gt;
&lt;p&gt;So… Protocol Orientation? If it&amp;rsquo;s better, how is it better?&lt;/p&gt;
&lt;p&gt;I love how the commenter takes the object oriented example of laundry service, and refines it with nuances that come out of thinking in terms of protocols rather than just objects. Really. &lt;a href=&#34;https://www.reddit.com/r/swift/comments/4nme0c/eli5_what_does_protocol_oriented_programming_mean/d453ryv&#34;&gt;Have a look&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The primary difference we see is that rather than having a &lt;code&gt;Laundry&lt;/code&gt; object… a single one… that does laundry in its own particular way, a shift occurs: We begin to deal in terms of describing &lt;em&gt;ways&lt;/em&gt; to get laundry done.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;If I want my laundry done one particular way by &lt;em&gt;one particular guy&lt;/em&gt; I go Object-Oriented and stop caring about how things get done. But if I want to generalize over the &lt;em&gt;ways&lt;/em&gt; to get laundry done I need to go Protocol-Oriented and stop caring about everything else.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;In Protocol Orientation, the only thing that matters is the &lt;em&gt;interface&lt;/em&gt;… the things that clients will interact with. Describe that to the best of your ability with a protocol, and let something else come along and worry about the &lt;em&gt;how&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;In the end, the only thing that a person needing laundry done needs to know is what interface is available for them to accomplish the laundering they have need of completing.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Being able to sit down and think about how something would look if you removed state and inheritance from the picture and just thought about the bare minimum required interface to that particular task is a far more powerful and simple approach to programming that enables reuse far easier than OO ever did.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h1 id=&#34;takeaways&#34;&gt;Takeaways&lt;/h1&gt;
&lt;p&gt;The take-away point for me in this thread on Protocol Orientation is this: Protocols are about &lt;em&gt;generalizing&lt;/em&gt;. They&amp;rsquo;re about the &lt;em&gt;interface&lt;/em&gt;. It&amp;rsquo;s about a mindset and a focus (an &lt;em&gt;orientation&lt;/em&gt;) toward thinking of &lt;em&gt;ways&lt;/em&gt; to do certain things, and describing them clearly in the form of a Type in Swift.&lt;/p&gt;
&lt;p&gt;Once the &lt;em&gt;way to do&lt;/em&gt; something is described, other things, concrete &lt;em&gt;Types&lt;/em&gt;, can come along and implement those in an infinite variety of ways.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Extracting a PKCS7 Container for Receipt Validation with Swift</title>
      <link>https://www.andrewcbancroft.com/2016/06/09/extracting-a-pkcs7-container-for-receipt-validation-with-swift/</link>
      <pubDate>Fri, 10 Jun 2016 03:44:58 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2016/06/09/extracting-a-pkcs7-container-for-receipt-validation-with-swift/</guid>
      <description>&lt;p&gt;So you&amp;rsquo;ve &lt;a href=&#34;https://www.andrewcbancroft.com/2015/10/05/preparing-to-test-receipt-validation-for-ios/&#34;&gt;prepared to test receipt validation&lt;/a&gt; by setting up your app in iTunes Connect.&lt;/p&gt;
&lt;p&gt;You&amp;rsquo;ve brought in a cryptography library like OpenSSL to be able to work with the PKCS #7 container that acts as the &amp;ldquo;envelope&amp;rdquo; for the receipt. Perhaps you&amp;rsquo;ve even done it &lt;a href=&#34;https://www.andrewcbancroft.com/2015/09/21/openssl-for-ios-swift-the-easy-way/&#34;&gt;the &amp;ldquo;easy way&amp;rdquo; with CocoaPods&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;You&amp;rsquo;ve &lt;a href=&#34;https://www.andrewcbancroft.com/2015/10/13/loading-a-receipt-for-validation-with-swift/&#34;&gt;located and loaded&lt;/a&gt; the receipt for validation.&lt;/p&gt;
&lt;p&gt;Now you&amp;rsquo;re ready to extract the PKCS #7 container and work with it.&lt;/p&gt;
&lt;p&gt;The aim of this guide is to get you started using the OpenSSL library in your Swift code by employing it to extract the receipt contents from its PKCS #7 container.&lt;/p&gt;
&lt;p&gt;Just want the code? Here you go!&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;fab fa-github fa-lg&#34;&gt;&lt;/i&gt; &lt;a href=&#34;https://github.com/andrewcbancroft/SwiftyLocalReceiptValidator&#34;&gt;Swifty Local Receipt Validator&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;
&lt;p&gt;&lt;a name=&#34;recap&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;recap-from-the-previous-guide&#34;&gt;Recap from the previous guide&lt;/h2&gt;
&lt;p&gt;In &lt;a href=&#34;https://www.andrewcbancroft.com/2015/10/13/loading-a-receipt-for-validation-with-swift/&#34;&gt;Loading a Receipt for Validation with Swift&lt;/a&gt;, I began the process of breaking out the various steps of the receipt validation process into separate single-responsibility structs with clearly named functions to help clarify what each piece of code is doing.&lt;/p&gt;
&lt;p&gt;Recall that I&amp;rsquo;ve created a &lt;a href=&#34;https://www.andrewcbancroft.com/2015/10/13/loading-a-receipt-for-validation-with-swift/#receipt-validator-implementation&#34;&gt;main Type called &lt;code&gt;ReceiptValidator&lt;/code&gt;&lt;/a&gt;, with references to several smaller single-responsibility Types that it uses to accomplish the overall validation process. So accordingly, as of my last post in the series, I&amp;rsquo;ve &lt;a href=&#34;https://www.andrewcbancroft.com/2015/10/13/loading-a-receipt-for-validation-with-swift/#receipt-loader-implementation&#34;&gt;created a &lt;code&gt;ReceiptLoader&lt;/code&gt;&lt;/a&gt; that finds the receipt on the file system and loads it into memory.&lt;/p&gt;
&lt;p&gt;If a validation step ever fails along the way, I&amp;rsquo;ve decided to take advantage of Swift&amp;rsquo;s error throwing features to clearly describe what failed. So far, there&amp;rsquo;s only one case:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;enum&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;ReceiptValidationError&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&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;couldNotFindReceipt&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;I&amp;rsquo;ll expand this enum Type to cover more failure conditions in this guide.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;receipt-extractor-outline&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;receiptextractor-struct-outline&#34;&gt;ReceiptExtractor struct outline&lt;/h2&gt;
&lt;p&gt;The OpenSSL library comes to us in the form of a C static library. It&amp;rsquo;s not a beautiful API to say the least. The names of the Types and functions are really cryptic at times, so I&amp;rsquo;ve decided it&amp;rsquo;s best for my own memory to wrap each step in small function routines that are named for what they do.&lt;/p&gt;
&lt;p&gt;So supposing you&amp;rsquo;ve &lt;a href=&#34;https://www.andrewcbancroft.com/2015/10/13/loading-a-receipt-for-validation-with-swift/&#34;&gt;located and loaded&lt;/a&gt; the receipt data, or used &lt;a href=&#34;https://developer.apple.com/library/ios/documentation/StoreKit/Reference/StoreKit_Collection/&#34;&gt;Store Kit&lt;/a&gt; to request a receipt from Apple… Take a look at this new &lt;code&gt;ReceiptExtractor&lt;/code&gt; skeleton of a struct to get an idea of what&amp;rsquo;s going to be required to extract the PKCS7 container for the receipt:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;6
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;struct&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;ReceiptExtractor&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;extractPKCS7Container&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;receiptData&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Data&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;kr&#34;&gt;throws&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;UnsafeMutablePointer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;PKCS7&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&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;c1&#34;&gt;// use Open SSL to extract the PKCS7 container&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;c1&#34;&gt;// throw a ReceiptValidationError if something goes wrong in this process&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;a name=&#34;new-receiptvalidationerror-cases&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;new-receiptvalidationerror-cases&#34;&gt;New ReceiptValidationError cases&lt;/h2&gt;
&lt;p&gt;When extracting the receipt information from the PKCS7 container, there are going to be things that would cause overall validation to fail. For example, if the &lt;a href=&#34;https://www.andrewcbancroft.com/2015/10/13/loading-a-receipt-for-validation-with-swift/#receipt-loader-implementation&#34;&gt;receipt &lt;code&gt;Data&lt;/code&gt; instance&lt;/a&gt; ends up being empty, that&amp;rsquo;s a validation failure. The PKCS7 container needs to have information inside of it for validation to pass (obviously).&lt;/p&gt;
&lt;p&gt;So in this guide, I&amp;rsquo;ll expand the &lt;code&gt;ReceiptValidationError&lt;/code&gt; enum to have the following cases:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;4
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;enum&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;ReceiptValidationError&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&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;couldNotFindReceipt&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;emptyReceiptContents&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;a name=&#34;prep-pkcs7-union-accessors&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;preparation-step-pkcs7-union-accessors&#34;&gt;Preparation step: PKCS7 union accessors&lt;/h2&gt;
&lt;p&gt;Before attempting to work with OpenSSL&amp;rsquo;s PKCS7 functions, you&amp;rsquo;ve got to do a little prep work to get the functions to play nicely with Swift.&lt;/p&gt;
&lt;p&gt;Unfortunately, Swift doesn&amp;rsquo;t work well with &lt;a href=&#34;https://en.wikipedia.org/wiki/Union_type#C.2FC.2B.2B&#34;&gt;C union types&lt;/a&gt;. It simply can&amp;rsquo;t see things defined with a C union.&lt;/p&gt;
&lt;p&gt;Thankfully, we can work around the problem by creating some wrappers. If you&amp;rsquo;ll add two new files to your project and implement them, you&amp;rsquo;ll be on your way. They are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;pkcs7_union_accessors.h&lt;/li&gt;
&lt;li&gt;pkcs7_union_accessors.c&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;a name=&#34;pkcs7-union-accessors-h-implementation&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;pkcs7_union_accessorsh-implementation&#34;&gt;pkcs7_union_accessors.h implementation&lt;/h4&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;12
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;13
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;14
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;15
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-c&#34; data-lang=&#34;c&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;cp&#34;&gt;#ifndef pkcs7_union_accessors_h
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;cp&#34;&gt;#define pkcs7_union_accessors_h
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;&lt;span class=&#34;cp&#34;&gt;#include&lt;/span&gt; &lt;span class=&#34;cpf&#34;&gt;&amp;lt;openssl/pkcs7.h&amp;gt;&lt;/span&gt;&lt;span class=&#34;cp&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;&lt;span class=&#34;kt&#34;&gt;char&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;*&lt;/span&gt;&lt;span class=&#34;nf&#34;&gt;pkcs7_d_char&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;PKCS7&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;*&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;ptr&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;cl&#34;&gt;&lt;span class=&#34;n&#34;&gt;ASN1_OCTET_STRING&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;*&lt;/span&gt;&lt;span class=&#34;nf&#34;&gt;pkcs7_d_data&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;PKCS7&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;*&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;ptr&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;cl&#34;&gt;&lt;span class=&#34;n&#34;&gt;PKCS7_SIGNED&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;*&lt;/span&gt;&lt;span class=&#34;nf&#34;&gt;pkcs7_d_sign&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;PKCS7&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;*&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;ptr&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;cl&#34;&gt;&lt;span class=&#34;n&#34;&gt;PKCS7_ENVELOPE&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;*&lt;/span&gt;&lt;span class=&#34;nf&#34;&gt;pkcs7_d_enveloped&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;PKCS7&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;*&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;ptr&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;cl&#34;&gt;&lt;span class=&#34;n&#34;&gt;PKCS7_SIGN_ENVELOPE&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;*&lt;/span&gt;&lt;span class=&#34;nf&#34;&gt;pkcs7_d_signed_and_enveloped&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;PKCS7&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;*&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;ptr&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;cl&#34;&gt;&lt;span class=&#34;n&#34;&gt;PKCS7_DIGEST&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;*&lt;/span&gt;&lt;span class=&#34;nf&#34;&gt;pkcs7_d_digest&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;PKCS7&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;*&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;ptr&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;cl&#34;&gt;&lt;span class=&#34;n&#34;&gt;PKCS7_ENCRYPT&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;*&lt;/span&gt;&lt;span class=&#34;nf&#34;&gt;pkcs7_d_encrypted&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;PKCS7&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;*&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;ptr&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;cl&#34;&gt;&lt;span class=&#34;n&#34;&gt;ASN1_TYPE&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;*&lt;/span&gt;&lt;span class=&#34;nf&#34;&gt;pkcs7_d_other&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;PKCS7&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;*&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;ptr&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;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;cp&#34;&gt;#endif &lt;/span&gt;&lt;span class=&#34;cm&#34;&gt;/* pkcs7_union_accessors_h */&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;a name=&#34;pkcs7-union-accessors-c-implementation&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;pkcs7_union_accessorsc-implementation&#34;&gt;pkcs7_union_accessors.c implementation&lt;/h4&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;12
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;13
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;14
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;15
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;16
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;17
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;18
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;19
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;20
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;21
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;22
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;23
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;24
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;25
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;26
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;27
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;28
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;29
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;30
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;31
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;32
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;33
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-c&#34; data-lang=&#34;c&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;cp&#34;&gt;#include&lt;/span&gt; &lt;span class=&#34;cpf&#34;&gt;&amp;#34;pkcs7_union_accessors.h&amp;#34;&lt;/span&gt;&lt;span class=&#34;cp&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;&lt;span class=&#34;kr&#34;&gt;inline&lt;/span&gt; &lt;span class=&#34;kt&#34;&gt;char&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;*&lt;/span&gt;&lt;span class=&#34;nf&#34;&gt;pkcs7_d_char&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;PKCS7&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;*&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;ptr&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;cl&#34;&gt;    &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ptr&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;d&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;ptr&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;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;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kr&#34;&gt;inline&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ASN1_OCTET_STRING&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;*&lt;/span&gt;&lt;span class=&#34;nf&#34;&gt;pkcs7_d_data&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;PKCS7&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;*&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;ptr&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;cl&#34;&gt;    &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ptr&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;d&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;data&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;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;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kr&#34;&gt;inline&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;PKCS7_SIGNED&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;*&lt;/span&gt;&lt;span class=&#34;nf&#34;&gt;pkcs7_d_sign&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;PKCS7&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;*&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;ptr&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;cl&#34;&gt;    &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ptr&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;d&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;sign&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;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;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kr&#34;&gt;inline&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;PKCS7_ENVELOPE&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;*&lt;/span&gt;&lt;span class=&#34;nf&#34;&gt;pkcs7_d_enveloped&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;PKCS7&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;*&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;ptr&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;cl&#34;&gt;    &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ptr&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;d&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;enveloped&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;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;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kr&#34;&gt;inline&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;PKCS7_SIGN_ENVELOPE&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;*&lt;/span&gt;&lt;span class=&#34;nf&#34;&gt;pkcs7_d_signed_and_enveloped&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;PKCS7&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;*&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;ptr&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;cl&#34;&gt;    &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ptr&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;d&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;signed_and_enveloped&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;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;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kr&#34;&gt;inline&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;PKCS7_DIGEST&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;*&lt;/span&gt;&lt;span class=&#34;nf&#34;&gt;pkcs7_d_digest&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;PKCS7&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;*&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;ptr&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;cl&#34;&gt;    &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ptr&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;d&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;digest&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;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;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kr&#34;&gt;inline&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;PKCS7_ENCRYPT&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;*&lt;/span&gt;&lt;span class=&#34;nf&#34;&gt;pkcs7_d_encrypted&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;PKCS7&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;*&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;ptr&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;cl&#34;&gt;    &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ptr&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;d&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;encrypted&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;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;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kr&#34;&gt;inline&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ASN1_TYPE&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;*&lt;/span&gt;&lt;span class=&#34;nf&#34;&gt;pkcs7_d_other&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;PKCS7&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;*&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;ptr&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;cl&#34;&gt;    &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ptr&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;d&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;other&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;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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;a name=&#34;bridging-header-updates&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;bridging-header-updates&#34;&gt;Bridging header updates&lt;/h4&gt;
&lt;p&gt;After you create the union accessor files, you need to update your project&amp;rsquo;s bridging header to import the new header file:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-c&#34; data-lang=&#34;c&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;cp&#34;&gt;#import &amp;lt;openssl/pkcs7.h&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;cp&#34;&gt;#import &amp;lt;openssl/objects.h&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;cp&#34;&gt;#import &amp;#34;pkcs7_union_accessors.h&amp;#34;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;a name=&#34;receiptextractor-implementation&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;receiptextractor-struct-implementation&#34;&gt;ReceiptExtractor struct implementation&lt;/h2&gt;
&lt;p&gt;Now it&amp;rsquo;s time to dive into the actual implementation of what I&amp;rsquo;m calling a &lt;code&gt;ReceiptExtractor&lt;/code&gt;. Have a look at the code with some explanatory comments following:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;12
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;13
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;14
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;15
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;16
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;17
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;18
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;19
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;struct&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;ReceiptExtractor&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;extractPKCS7Container&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;receiptData&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Data&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;kr&#34;&gt;throws&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;UnsafeMutablePointer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;PKCS7&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&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;receiptBIO&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;BIO_new&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;BIO_s_mem&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;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;BIO_write&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;receiptBIO&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;receiptData&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;as&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSData&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;).&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;bytes&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;Int32&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;receiptData&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;p&#34;&gt;))&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;receiptPKCS7Container&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;d2i_PKCS7_bio&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;receiptBIO&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;cl&#34;&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;guard&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;receiptPKCS7Container&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;!=&lt;/span&gt; &lt;span class=&#34;kc&#34;&gt;nil&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;cl&#34;&gt;            &lt;span class=&#34;k&#34;&gt;throw&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ReceiptValidationError&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;emptyReceiptContents&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;pkcs7DataTypeCode&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;OBJ_obj2nid&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;pkcs7_d_sign&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;receiptPKCS7Container&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;).&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;pointee&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;contents&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;pointee&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&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;guard&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;pkcs7DataTypeCode&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;==&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NID_pkcs7_data&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;cl&#34;&gt;            &lt;span class=&#34;k&#34;&gt;throw&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ReceiptValidationError&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;emptyReceiptContents&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;receiptPKCS7Container&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;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;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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;a name=&#34;receiptextractor-explanation&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;receiptextractor-struct-explanation&#34;&gt;ReceiptExtractor struct explanation&lt;/h2&gt;
&lt;p&gt;Most of the code above is a Swift translation of what&amp;rsquo;s found at &lt;a href=&#34;https://www.objc.io/issues/17-security/receipt-validation/&#34;&gt;Objc.io&amp;rsquo;s Receipt Validation guide&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;I did a little research over at the OpenSSL site though, and thought it might be helpful for the curious to know what some of these non-intuitive function names stand for and what they do.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;BIO_new&lt;/code&gt; for example. &amp;ldquo;BIO&amp;rdquo; stands for &amp;ldquo;Basic I/O&amp;rdquo;. It&amp;rsquo;s an abstraction over the underlying basic input and output operations that your app uses for cryptographic operations.&lt;/p&gt;
&lt;p&gt;What we&amp;rsquo;re doing with &lt;code&gt;BIO_new(BIO_s_mem())&lt;/code&gt; is saying that we want a new Basic I/O mechanism that uses &lt;em&gt;memory&lt;/em&gt; for its I/O operations.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;BIO_write&lt;/code&gt; takes the &lt;a href=&#34;https://www.andrewcbancroft.com/2015/10/13/loading-a-receipt-for-validation-with-swift/&#34;&gt;&lt;code&gt;receiptData&lt;/code&gt; that was located and loaded&lt;/a&gt;, and writes the entire length of its bytes to memory (the &lt;code&gt;receiptBIO&lt;/code&gt; that was created first).&lt;/p&gt;
&lt;p&gt;To get the actual PKCS #7 container, the &lt;code&gt;d2i_PKCS7_bio&lt;/code&gt; function is used.&lt;/p&gt;
&lt;p&gt;Once we have the container in hand, it&amp;rsquo;s a matter of making sure it has contents.&lt;/p&gt;
&lt;p&gt;I couldn&amp;rsquo;t find a lot of information about the call to &lt;code&gt;pkcs7_d_sign&lt;/code&gt;, but the primary point of line 13 above is to get a &amp;ldquo;numerical identifier&amp;rdquo;, which is what &amp;ldquo;NID&amp;rdquo; stands for in &lt;code&gt;OBJ_obj2nid&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Digging into the PKCS #7 container, you can access the right property and convert it to a numerical identifier that you can check.&lt;/p&gt;
&lt;p&gt;As long as the NID returned is equal to the &lt;code&gt;NID_pkcs7_data&lt;/code&gt; constant value, things are good. If they&amp;rsquo;re not, that means the receipt has no information and validation fails (thus, the guard and throw statement in lines 14-15).&lt;/p&gt;
&lt;p&gt;If everything passes the guard, though, the PKCS #7 container is returned, and we&amp;rsquo;re ready for the next step of the receipt validation process, which is to verify the signature on the receipt with Apple&amp;rsquo;s root certificate. &lt;em&gt;That&lt;/em&gt;, however, will happen in another entry to this series.&lt;/p&gt;
&lt;p&gt;Until next time!&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...
  &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/10/05/preparing-to-test-receipt-validation-for-ios/&#34; title=&#34;Preparing to Test Receipt Validation for iOS&#34;&gt;Preparing to Test Receipt Validation for iOS&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/10/13/loading-a-receipt-for-validation-with-swift/&#34; title=&#34;Loading a Receipt for Validation with Swift&#34;&gt;Loading a Receipt for Validation with 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/2015/09/21/openssl-for-ios-swift-the-easy-way/&#34; title=&#34;OpenSSL for iOS &amp;#038; Swift the Easy Way&#34;&gt;OpenSSL for iOS &amp; Swift the Easy Way&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/2017/07/16/receipt-validation-verifying-a-receipt-signature-in-swift/&#34; title=&#34;Receipt Validation – Verifying a Receipt Signature in Swift&#34;&gt;Receipt Validation – Verifying a Receipt Signature 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/2017/07/27/receipt-validation-parsing-a-receipt-with-swift/&#34; title=&#34;Receipt Validation – Parse and Decode a Receipt with Swift&#34;&gt;Receipt Validation – Parse and Decode a Receipt with 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/2017/07/31/finalizing-receipt-validation-in-swift-computing-a-guid-hash/&#34; title=&#34;Finalizing Receipt Validation in Swift – Computing a GUID Hash&#34;&gt;Finalizing Receipt Validation in Swift – Computing a GUID Hash&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>Creating Calendar Events with Event Kit and Swift</title>
      <link>https://www.andrewcbancroft.com/2016/06/02/creating-calendar-events-with-event-kit-and-swift/</link>
      <pubDate>Thu, 02 Jun 2016 17:37:15 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2016/06/02/creating-calendar-events-with-event-kit-and-swift/</guid>
      <description>&lt;p&gt;Folks have asked more about working with Event Kit and Swift, so the series continues with this guide on how to create calendar events with Event Kit and Swift!&lt;/p&gt;
&lt;p&gt;Previous guides in the series include the following:&lt;/p&gt;
&lt;div class=&#34;resources&#34;&gt;
  &lt;div class=&#34;resources-header&#34;&gt;
    Previous Guides
  &lt;/div&gt;
  &lt;ul class=&#34;resources-content&#34;&gt;
    &lt;li&gt;
      &lt;i class=&#34;fab fa-link&#34;&gt;&lt;/i&gt; &lt;a href=&#34;https://www.andrewcbancroft.com/2015/05/14/beginners-guide-to-eventkit-in-swift-requesting-permission/&#34; title=&#34;Beginner’s Guide to Event Kit in Swift – Requesting Permission&#34;&gt;Beginner’s Guide to Event Kit in Swift – Requesting Permission&lt;/a&gt;
    &lt;/li&gt;
    &lt;li&gt;
      &lt;i class=&#34;fab fa-link&#34;&gt;&lt;/i&gt; &lt;a href=&#34;https://www.andrewcbancroft.com/2015/06/17/creating-calendars-with-event-kit-and-swift/&#34; title=&#34;Creating Calendars with Event Kit and Swift&#34;&gt;Creating Calendars with Event Kit and Swift&lt;/a&gt;
    &lt;/li&gt;
    &lt;li&gt;
      &lt;i class=&#34;fab fa-link&#34;&gt;&lt;/i&gt; &lt;a href=&#34;https://www.andrewcbancroft.com/2016/04/28/listing-calendar-events-with-event-kit-and-swift/&#34; title=&#34;Listing Calendar Events with Event Kit and Swift&#34;&gt;Listing Calendar Events with Event Kit and Swift&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;
&lt;p&gt;&lt;a name=&#34;demo&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;demo&#34;&gt;Demo&lt;/h1&gt;
&lt;p&gt;Here&amp;rsquo;s what we&amp;rsquo;re going for by the end of this:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/05/create-events-demo.gif&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/05/create-events-demo.gif&#34; alt=&#34;Create Events Demo&#34; width=&#34;468&#34; height=&#34;847&#34; class=&#34;alignnone size-full wp-image-12890&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;As with my other guides, I&amp;rsquo;ve included a working project on GitHub for you to peruse and tinker with:&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;fab fa-github fa-lg&#34;&gt;&lt;/i&gt; &lt;a href=&#34;https://github.com/andrewcbancroft/EventTracker/tree/create-events&#34; title=&#34;Event Tracker - Create Events&#34;&gt;Event Tracker – Create Events&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;
&lt;p&gt;&lt;a name=&#34;code-example&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;code-example&#34;&gt;Code Example&lt;/h1&gt;
&lt;p&gt;The majority of the work to create an event happens in the example project&amp;rsquo;s AddEventViewController.swift file, so that&amp;rsquo;s where I&amp;rsquo;ll spend my time in this guide. First, let&amp;rsquo;s put the relevant code before us:&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;AddEventViewController&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&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;calendar&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;EKCalendar&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;!&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;// Intended to be set in parent controller&amp;#39;s prepareForSegue event&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;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;eventNameTextField&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UITextField&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;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;eventStartDatePicker&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIDatePicker&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;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;eventEndDatePicker&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIDatePicker&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&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&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;addEventButtonTapped&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;UIBarButtonItem&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;c1&#34;&gt;// Create an Event Store instance&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;eventStore&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;EKEventStore&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;// Use Event Store to create a new calendar instance&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;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;calendarForEvent&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;eventStore&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;calendarWithIdentifier&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;calendar&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;calendarIdentifier&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;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;newEvent&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;EKEvent&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;eventStore&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;eventStore&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&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;newEvent&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;calendar&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;calendarForEvent&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;n&#34;&gt;newEvent&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;kc&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;eventNameTextField&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;Some Event Name&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;22&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;            &lt;span class=&#34;n&#34;&gt;newEvent&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;startDate&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;eventStartDatePicker&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;date&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;newEvent&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;endDate&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;eventEndDatePicker&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;date&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&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;c1&#34;&gt;// Save the calendar using the Event Store instance&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;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;28&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;eventStore&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;saveEvent&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;newEvent&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;span&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;ThisEvent&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;commit&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&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;n&#34;&gt;delegate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;?.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;eventDidAdd&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;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;kc&#34;&gt;self&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;32&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;33&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;alert&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIAlertController&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;Event could not save&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;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;error&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;as&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;n&#34;&gt;localizedDescription&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;preferredStyle&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;Alert&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;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;OKAction&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIAlertAction&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;OK&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;style&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;Default&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;handler&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;35&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;                &lt;span class=&#34;n&#34;&gt;alert&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;addAction&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;OKAction&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;36&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;37&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;presentViewController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;alert&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;38&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;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;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;41&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;42&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;43&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;code-walkthrough&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;code-walkthrough&#34;&gt;Code Walkthrough&lt;/h1&gt;
&lt;p&gt;The general outline of the code is this:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Create an instance of &lt;code&gt;EKEventStore&lt;/code&gt; – this will let you create &lt;code&gt;EKEvents&lt;/code&gt; and save them.&lt;/li&gt;
&lt;/ul&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;newEvent&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;EKEvent&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;eventStore&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;eventStore&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;ul&gt;
&lt;li&gt;Pull an &lt;code&gt;EKCalendar&lt;/code&gt; instance from the Event Store – this will let you associate an event with a calendar.&lt;/li&gt;
&lt;/ul&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;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;calendarForEvent&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;eventStore&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;calendarWithIdentifier&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;calendar&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;calendarIdentifier&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;// ... &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;ul&gt;
&lt;li&gt;Create a new &lt;code&gt;EKEvent&lt;/code&gt; instance and set its properties.&lt;/li&gt;
&lt;/ul&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;newEvent&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;EKEvent&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;eventStore&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;eventStore&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 class=&#34;n&#34;&gt;newEvent&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;calendar&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;calendarForEvent&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;newEvent&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;kc&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;eventNameTextField&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;Some Event Name&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;n&#34;&gt;newEvent&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;startDate&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;eventStartDatePicker&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;date&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;newEvent&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;endDate&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;eventEndDatePicker&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;date&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;ul&gt;
&lt;li&gt;Attempt to save the event with the &lt;code&gt;EKEventStore&lt;/code&gt; instance that was created first.&lt;/li&gt;
&lt;/ul&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;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; 2&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;eventStore&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;saveEvent&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;newEvent&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;span&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;ThisEvent&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;commit&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&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;delegate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;?.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;eventDidAdd&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;kc&#34;&gt;self&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; 6&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; 7&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;alert&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIAlertController&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;Event could not save&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;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;error&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;as&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;n&#34;&gt;localizedDescription&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;preferredStyle&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;Alert&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 8&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;OKAction&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIAlertAction&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;OK&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;style&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;Default&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;handler&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;n&#34;&gt;alert&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;addAction&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;OKAction&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;kc&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&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;alert&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;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;&lt;a name=&#34;gotchas&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;gotchas&#34;&gt;Gotchas&lt;/h1&gt;
&lt;p&gt;As I was working through this example, I was initially running into trouble when associating my &lt;code&gt;newEvent&lt;/code&gt; with the &lt;code&gt;calendar&lt;/code&gt; that was passed into &lt;code&gt;AddEventViewController&lt;/code&gt; from the parent controller&amp;rsquo;s &lt;code&gt;prepareForSegue&lt;/code&gt; method.&lt;/p&gt;
&lt;p&gt;It turns out that if you retrieve a calendar from one &lt;code&gt;EKEventStore&lt;/code&gt; instance, say, in the previous view controller, and attempt to assign it to an &lt;code&gt;EKEvent&lt;/code&gt; that is associated with a &lt;em&gt;different&lt;/em&gt; &lt;code&gt;EKEventStore&lt;/code&gt; instance, things become really unhappy when it comes time to save the event.&lt;/p&gt;
&lt;p&gt;Specifically, you&amp;rsquo;ll get an error that says &amp;ldquo;Thread 1: signal SIGKILL” followed by &amp;ldquo;Thread 1: EXC_BAD_ACCESS”:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/06/SaveEventSigKillError.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/06/SaveEventSigKillError.png&#34; alt=&#34;Save Event Sig Kill Error&#34; width=&#34;753&#34; height=&#34;177&#34; class=&#34;alignnone size-full wp-image-12898&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/06/SaveEventSigKillError.png 753w, https://www.andrewcbancroft.com/wp-content/uploads/2016/06/SaveEventSigKillError-300x71.png 300w&#34; sizes=&#34;(max-width: 753px) 100vw, 753px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/06/SaveEventBadAccessError.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/06/SaveEventBadAccessError.png&#34; alt=&#34;Save Event Bad Access Error&#34; width=&#34;755&#34; height=&#34;343&#34; class=&#34;alignnone size-full wp-image-12899&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/06/SaveEventBadAccessError.png 755w, https://www.andrewcbancroft.com/wp-content/uploads/2016/06/SaveEventBadAccessError-300x136.png 300w&#34; sizes=&#34;(max-width: 755px) 100vw, 755px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The fix for this was easy, once I figured out what was going on: Simply re-retrieve a calendar instance using an appropriate &lt;code&gt;calendarIdentifier&lt;/code&gt; &lt;em&gt;from the &lt;code&gt;EKEventStore&lt;/code&gt; instance you&amp;rsquo;re using to save the &lt;code&gt;EKEvent&lt;/code&gt; with&lt;/em&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;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;calendarForEvent&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;eventStore&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;calendarWithIdentifier&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;calendar&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;calendarIdentifier&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&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;newEvent&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;EKEvent&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;eventStore&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;eventStore&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;n&#34;&gt;newEvent&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;calendar&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;calendarForEvent&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;// ...&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;h1 id=&#34;wrapping-up&#34;&gt;Wrapping up&lt;/h1&gt;
&lt;p&gt;My focus in this guide was to demonstrate how to create and save and event to a user&amp;rsquo;s calendar. I intentionally left out a lot of the navigation aspects and &lt;code&gt;prepareForSegue&lt;/code&gt; calls in order to highlight the event creation process itself. Be sure to check out the &lt;a href=&#34;https://github.com/andrewcbancroft/EventTracker/tree/create-events&#34;&gt;accompanying Xcode project on GitHub&lt;/a&gt; for full details on the flow of the application.&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/05/14/beginners-guide-to-eventkit-in-swift-requesting-permission/&#34; title=&#34;Beginner’s Guide to Event Kit in Swift – Requesting Permission&#34;&gt;Beginner’s Guide to Event Kit in Swift – Requesting Permission&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/06/17/creating-calendars-with-event-kit-and-swift/&#34; title=&#34;Creating Calendars with Event Kit and Swift&#34;&gt;Creating Calendars with Event Kit and 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/04/28/listing-calendar-events-with-event-kit-and-swift/&#34; title=&#34;Listing Calendar Events with Event Kit and Swift&#34;&gt;Listing Calendar Events with Event Kit and 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 Cheat Sheet for Dates, Formatters, and Date Components</title>
      <link>https://www.andrewcbancroft.com/2016/05/26/swift-cheat-sheet-for-dates-formatters-date-components/</link>
      <pubDate>Thu, 26 May 2016 18:04:01 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2016/05/26/swift-cheat-sheet-for-dates-formatters-date-components/</guid>
      <description>&lt;p&gt;Working with &lt;code&gt;NSDate&lt;/code&gt;, &lt;code&gt;NSDateFormatter&lt;/code&gt;, and &lt;code&gt;NSDateComponents&lt;/code&gt; can be a little convoluted, so I&amp;rsquo;ve created myself a cheat sheet that will be updated as I discover new tips and tricks in this realm.&lt;/p&gt;
&lt;p&gt;The new cheat sheet can be found over at GitHub in the form of an Xcode Playground:&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;fab fa-github fa-lg&#34;&gt;&lt;/i&gt; &lt;a href=&#34;https://github.com/andrewcbancroft/SwiftDatesCheatSheet&#34; title=&#34;Swift Dates Cheat Sheet Playground&#34; target=&#34;_blank&#34;&gt;Swift Dates Cheat Sheet Playground&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;
&lt;p&gt;Topics that are included in the Playground are as follows:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Getting today&amp;rsquo;s date&lt;/li&gt;
&lt;li&gt;Converting &lt;code&gt;NSDate&lt;/code&gt; to &lt;code&gt;String&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Converting &lt;code&gt;String&lt;/code&gt; to &lt;code&gt;NSDate&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Getting components of an &lt;code&gt;NSDate&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Setting components of an &lt;code&gt;NSDate&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Creating new &lt;code&gt;NSDate&lt;/code&gt; instances from &lt;code&gt;NSDateComponent&lt;/code&gt; instances&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The best way to view this cheat sheet is by &lt;a href=&#34;https://github.com/andrewcbancroft/SwiftDatesCheatSheet&#34; target=&#34;_blank&#34;&gt;downloading the playground from GitHub&lt;/a&gt;, but here&amp;rsquo;s a straight copy-paste from the repo in case you just want to copy and paste it into a playground of your own from here:&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;cm&#34;&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;cm&#34;&gt;# Overview
&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;cm&#34;&gt;This is a cheat sheet for working with dates, date formatters, and date components.
&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;cm&#34;&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;cm&#34;&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;//: # Today&amp;#39;s Date&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;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;today&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSDate&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;thisTimeTomrrow&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSDate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;timeIntervalSinceNow&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;86400&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;thisTimeYesterday&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSDate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;timeIntervalSinceNow&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;mi&#34;&gt;86400&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;cm&#34;&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;cm&#34;&gt;# Date Formatting
&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;cm&#34;&gt;Working with NSDateFormatter to convert dates to stings and vice-versa
&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;cm&#34;&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;c1&#34;&gt;//: ## Converting dates to strings&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;dateFormatter&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSDateFormatter&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;dateFormatter&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;dateFormat&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;yyyy-MM-dd&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;23&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;
&lt;/span&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;bp&#34;&gt;print&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;dateFormatter&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;stringFromDate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;today&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&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;dateFormatter&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;dateFormat&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;MMM dd, yyyy&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;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;bp&#34;&gt;print&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;dateFormatter&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;stringFromDate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;today&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;29&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;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;c1&#34;&gt;//: ## Converting strings to dates&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&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;c1&#34;&gt;// Consider what might potentially come back from an API of some sort and set a dateFormatter&amp;#39;s dateFormat property appropriately...&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&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 class=&#34;n&#34;&gt;dateFormatter&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;dateFormat&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;yyyy-MM-dd&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;36&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;stringFromApi&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;2016-01-01&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;37&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;date&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;dateFormatter&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;dateFromString&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;stringFromApi&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&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;n&#34;&gt;stringFromApi&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;May 26, 2016&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;40&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;n&#34;&gt;date&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;dateFormatter&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;dateFromString&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;stringFromApi&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;41&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;42&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;cm&#34;&gt;/* Notice how the format set to the date formatter (yyyy-MM-dd) differs from the format that came back from the API (MMM dd, yyyy), which resulted in `date` being nil.
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;43&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;cm&#34;&gt;*/&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;44&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;45&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;cm&#34;&gt;/*:
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;46&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;cm&#34;&gt;# Date Components
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;47&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;cm&#34;&gt;Working with NSCalendar and NSCalendarUnit to work with components of dates.
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;48&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;cm&#34;&gt;*/&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;49&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;50&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;//: ## Getting date components&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;51&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;calendarUnitFlags&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSCalendarUnit&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;Year&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;Month&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;Day&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;Hour&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;Minute&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;Second&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;52&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;dateComponents&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSCalendar&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;currentCalendar&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;().&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;components&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;calendarUnitFlags&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;fromDate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSDate&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;53&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;n&#34;&gt;dateComponents&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;year&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;54&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;n&#34;&gt;dateComponents&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;month&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;55&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;n&#34;&gt;dateComponents&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;day&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;56&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;n&#34;&gt;dateComponents&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;hour&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;57&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;n&#34;&gt;dateComponents&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;minute&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;58&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;n&#34;&gt;dateComponents&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;second&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;59&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;60&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;// Observe how leaving out .Hour, .Minute, and .Second affects the values of those components of a date:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;61&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;n&#34;&gt;calendarUnitFlags&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;Year&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;Month&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;Day&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;62&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;n&#34;&gt;dateComponents&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSCalendar&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;currentCalendar&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;().&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;components&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;calendarUnitFlags&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;fromDate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSDate&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;63&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;n&#34;&gt;dateComponents&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;year&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;64&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;n&#34;&gt;dateComponents&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;month&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;65&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;n&#34;&gt;dateComponents&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;day&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;66&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;n&#34;&gt;dateComponents&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;hour&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;67&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;n&#34;&gt;dateComponents&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;minute&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;68&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;n&#34;&gt;dateComponents&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;second&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;69&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;70&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;//: ## Setting date component values&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;71&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;72&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;n&#34;&gt;dateComponents&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;year&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;dateComponents&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;year&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;+&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;73&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;n&#34;&gt;dateComponents&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;month&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;74&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;n&#34;&gt;dateComponents&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;day&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;75&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;n&#34;&gt;dateComponents&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;hour&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;76&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;n&#34;&gt;dateComponents&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;minute&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;77&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;n&#34;&gt;dateComponents&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;second&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;78&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;79&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;//: ## Creating dates from components&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;80&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;newDate&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSCalendar&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;currentCalendar&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;().&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;dateFromComponents&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;dateComponents&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;</description>
    </item>
    
    <item>
      <title>Parse PFCloud – “JSON text did not start with array or object”</title>
      <link>https://www.andrewcbancroft.com/2016/05/19/parse-pfcloud-json-text-did-not-start-with-array-or-object/</link>
      <pubDate>Fri, 20 May 2016 03:33:03 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2016/05/19/parse-pfcloud-json-text-did-not-start-with-array-or-object/</guid>
      <description>&lt;p&gt;It&amp;rsquo;s always the little things that lead to flatter foreheads (or at least it is for me).&lt;/p&gt;
&lt;p&gt;Banging my head against the desk this evening working on a Parse migration, I finally figured out what was causing an error condition in a &lt;code&gt;PFCloud&lt;/code&gt; function call.&lt;/p&gt;
&lt;p&gt;The request to the server succeeded, but the response was malformed somehow:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Error Domain=NSCocoaErrorDomain Code=3840 &amp;ldquo;JSON text did not start with array or object and option to allow fragments not set.” UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&amp;ldquo;What?? I &lt;em&gt;know&lt;/em&gt; the response is supposed to be JSON – I can even test it out and it works great in &lt;a href=&#34;https://chrome.google.com/webstore/detail/postman/fhbjgbiflinjbdggehcddcbncdddomop?hl=en&#34;&gt;Postman&lt;/a&gt;.”&lt;/p&gt;
&lt;p&gt;Weeeelll, it turns out that if you don&amp;rsquo;t get the URL to your self-hosted Parse Server correct, you&amp;rsquo;re going to get a response that&amp;rsquo;s not JSON. Doh!&lt;/p&gt;
&lt;p&gt;I had left off the &amp;ldquo;/parse” portion of the URL to my self-hosted parse server:&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;application&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;application&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIApplication&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;didFinishLaunchingWithOptions&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;launchOptions&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;NSObject&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;-&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; 2&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Override point for customization after application launch.&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;configuration&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ParseClientConfiguration&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;nv&#34;&gt;$0&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;applicationId&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;YourAppId&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;nv&#34;&gt;$0&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;clientKey&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;YourClientKey&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; 6&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;nv&#34;&gt;$0&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;server&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;https://your-self-hosted-parse-server/parse&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;n&#34;&gt;Parse&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;initializeWithConfiguration&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&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;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;kc&#34;&gt;true&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;So, the bottom-line take-away? Make sure you get the &lt;em&gt;full&lt;/em&gt; URL to your self-hosted Parse Server set to the &lt;code&gt;server&lt;/code&gt; property of your &lt;code&gt;ParseClientConfiguration&lt;/code&gt; instance. That endpoint could vary based on how you deployed the server to your cloud host of choice. For me, it was my goofy mistake of leaving off &amp;ldquo;/parse”.&lt;/p&gt;
&lt;p&gt;Go forth and may your forehead be ever-more round than mine.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Working with Swift:  Adopt a Protocol or Pass a Function?</title>
      <link>https://www.andrewcbancroft.com/2016/05/05/working-with-swift-adopt-a-protocol-or-pass-a-function/</link>
      <pubDate>Thu, 05 May 2016 17:59:16 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2016/05/05/working-with-swift-adopt-a-protocol-or-pass-a-function/</guid>
      <description>&lt;p&gt;Without fail, any time &lt;a href=&#34;http://robnapier.net/&#34;&gt;Rob Napier&lt;/a&gt; (&lt;a href=&#34;https://twitter.com/cocoaphony&#34;&gt;@cocoaphony&lt;/a&gt;) speaks or writes, I gain insight into new and deeper ways to solve problems with Swift.&lt;/p&gt;
&lt;p&gt;In January 2016, he &lt;a href=&#34;http://www.thedotpost.com/2016/01/rob-napier-beyond-crusty-real-world-protocols&#34;&gt;gave a talk at dotSwift&lt;/a&gt;, and I wanted to record my thoughts on something he said that made a lot of sense when it comes to the topic of, &amp;ldquo;Should I create and adopt a protocol for this Type I&amp;rsquo;m creating, or should I just pass it a function instead?”&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;two-insights&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;two-insights&#34;&gt;Two insights&lt;/h1&gt;
&lt;p&gt;During the talk, he compared some scenarios that were meant to help determine when to create a protocol, or when doing so would be overly complex, and passing a function might be the simpler thing to do instead.&lt;/p&gt;
&lt;p&gt;Two phrases caught my attention:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;A protocol is really just a &lt;em&gt;promise&lt;/em&gt; to implement some functions, and a struct is mostly just a bundle of functions that implement the promise.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;and&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;I can pass you an object that &lt;em&gt;promises&lt;/em&gt; a function, &lt;strong&gt;or&lt;/strong&gt;, I could just pass you the function.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;strong&gt;[mind blown]&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;So in other words, there are times when, rather than going through the formality of…&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Creating a protocol defining one or more functions that should be implemented&lt;/li&gt;
&lt;li&gt;Creating a Type that &lt;em&gt;adopts&lt;/em&gt; that protocol to promise that &amp;ldquo;I (as a class/struct/enum) will implement this/these function(s)”&lt;/li&gt;
&lt;li&gt;Creating an instance of that Type&lt;/li&gt;
&lt;li&gt;Passing off the instance to &lt;em&gt;another&lt;/em&gt; Type that needs to &lt;em&gt;call&lt;/em&gt; that promised function&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;…life might be simpler and code might be more clear and more concise when you just… &lt;strong&gt;pass the function&lt;/strong&gt; instead!&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;when-to-pass-a-function&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;when-to-pass-a-function&#34;&gt;When to pass a function&lt;/h1&gt;
&lt;p&gt;Rob gave us a couple or three &lt;strong&gt;rules of thumb&lt;/strong&gt;. Not hard-and-fast, &amp;ldquo;It should always be this way”, but just some guiding thoughts to filter our decision-making about our architecture.&lt;/p&gt;
&lt;p&gt;When it comes to finding opportunities that lend themselves to going the &amp;ldquo;just pass the function” route, consider the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If you&amp;rsquo;re creating a Type that depends on a single piece of functionality (a single function), maybe try depending on / passing just the function, rather than creating a protocol.&lt;/li&gt;
&lt;li&gt;If you&amp;rsquo;re creating a Type that depends on more than a single function, but the nature of the dependent relationship is short-lived, maybe try depending on / passing just the function. How do you know if it&amp;rsquo;s short-lived? Ask, &amp;ldquo;How many times am I going to call the function(s) that I depend on? Once, and then I&amp;rsquo;m done? Or multiple times throughout the application life-cycle?” If it&amp;rsquo;s a once and done kind of relationship, much like a &lt;a href=&#34;https://www.andrewcbancroft.com/2016/02/15/fundamentals-of-callbacks-for-swift-developers/&#34;&gt;callback&lt;/a&gt;, then perhaps just depending on and passing the function, rather than creating a protocol, is the simpler route.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;a name=&#34;when-to-use-a-protocol&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;when-to-use-a-protocol&#34;&gt;When to use a protocol&lt;/h1&gt;
&lt;p&gt;For some rules of thumb when it comes to choosing a protocol over just passing a function, you might consider:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If you&amp;rsquo;re creating a Type that depends on 3 or more related functions, wrapping those functions up in a protocol might be cleaner and more clear.&lt;/li&gt;
&lt;li&gt;If you&amp;rsquo;re creating a Type that depends on some functions for a long period of time, consider a protocol. Long-lived relationships are better-described in a protocol. Think of something like a table view&amp;rsquo;s data source. This is a good example of when to use a protocol to describe the dependency and the relationship, because as data changes, the table view will need to constantly call into those protocol methods to refresh itself.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;a name=&#34;how-to-depend-on-a-function&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;how-to-depend-on-a-function&#34;&gt;How to depend on a function&lt;/h1&gt;
&lt;p&gt;In order to fully grasp how to go the &amp;ldquo;just depend on / pass the function” route, you need to have an understanding of how &lt;a href=&#34;https://www.andrewcbancroft.com/2016/03/18/swift-functions-as-types/&#34;&gt;function Types are described in Swift&lt;/a&gt;. With this knowledge, you&amp;rsquo;re set to do a couple of things:&lt;/p&gt;
&lt;p&gt;1 – Create a property on the Type you&amp;rsquo;re implementing that is of some function Type. For 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;struct&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;Vehicle&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;Fuel&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&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;move&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;Fuel&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&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;c1&#34;&gt;// Fuel Types&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;struct&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;Gas&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;kd&#34;&gt;struct&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;RocketFuel&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&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;car&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Vehicle&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;Gas&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;move&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;_&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;in&lt;/span&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;use gasoline to move&amp;#34;&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&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;rocket&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Vehicle&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;RocketFuel&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;move&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;_&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;in&lt;/span&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;use rocket fuel to move&amp;#34;&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&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;car&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;move&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;Gas&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;rocket&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;move&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;RocketFuel&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;A full explanation of indicating function Types can be found by reviewing my guide on &lt;a href=&#34;https://www.andrewcbancroft.com/2016/03/18/swift-functions-as-types/&#34;&gt;Swift Functions as Types&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;2 – Declare that such-and-such parameter on a function within your Type must be a function Type. For 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;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;getData&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;NSData&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&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;data&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSData&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;// do something to go get data&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;// call completion handler when getting data is done&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;completion&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;data&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The above is an example of a callback scenario, which I give full treatment in &lt;a href=&#34;https://www.andrewcbancroft.com/2016/02/15/fundamentals-of-callbacks-for-swift-developers/&#34;&gt;Fundamentals of Callbacks for Swift Developers&lt;/a&gt;&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/03/18/swift-functions-as-types/&#34; title=&#34;Swift Functions as Types&#34;&gt;Swift Functions as Types&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>Listing Calendar Events with Event Kit and Swift</title>
      <link>https://www.andrewcbancroft.com/2016/04/28/listing-calendar-events-with-event-kit-and-swift/</link>
      <pubDate>Thu, 28 Apr 2016 17:50:22 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2016/04/28/listing-calendar-events-with-event-kit-and-swift/</guid>
      <description>&lt;p&gt;This is the continuation of a series of articles I&amp;rsquo;ve writing for Swift developers working with Event Kit.&lt;/p&gt;
&lt;p&gt;Supposing that after &lt;a href=&#34;https://www.andrewcbancroft.com/2015/05/14/beginners-guide-to-eventkit-in-swift-requesting-permission/&#34;&gt;asking the user for permission&lt;/a&gt; to use their calendars, and even allowing users to &lt;a href=&#34;https://www.andrewcbancroft.com/2015/06/17/creating-calendars-with-event-kit-and-swift/&#34;&gt;create &lt;em&gt;new&lt;/em&gt; calendars&lt;/a&gt; from within your iOS app, one possible next step could be to list out all of the events for a calendar that they tap on.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;demo&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;demo&#34;&gt;Demo&lt;/h1&gt;
&lt;p&gt;Here&amp;rsquo;s what we&amp;rsquo;re going for by the end of this:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/04/list-events-demo.gif&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/04/list-events-demo.gif&#34; alt=&#34;List Events Demo&#34; width=&#34;468&#34; height=&#34;844&#34; class=&#34;alignnone size-full wp-image-12819&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;In this walk-through, I&amp;rsquo;ll explore the Event Kit API that allows us to query the user&amp;rsquo;s device to find and list out events for a calendar that match a date range.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;example-project&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;example-project&#34;&gt;Example project&lt;/h1&gt;
&lt;p&gt;I&amp;rsquo;ve got an example project up on GitHub that contains all the code necessary to list out events for a calendar inside of a table view. You can download that project here:&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;fab fa-github fa-lg&#34;&gt;&lt;/i&gt; &lt;a href=&#34;https://github.com/andrewcbancroft/EventTracker/tree/list-events-for-calendar&#34; title=&#34;Event Tracker Example Project&#34;&gt;Event Tracker Example Project&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;
&lt;p&gt;&lt;a name=&#34;querying-for-events&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;querying-for-events&#34;&gt;Querying for events&lt;/h1&gt;
&lt;p&gt;The primary thing you&amp;rsquo;ll need to do in order to display a list of events from a calendar is to query the event store for events matching a properly configured &lt;code&gt;NSPredicate&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Here&amp;rsquo;s a snippet of code, followed by an analysis of what&amp;rsquo;s going on:&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;EventsViewController&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;UITableViewDataSource&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;calendar&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;EKCalendar&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;!&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;// Passed in from previous 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; 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;events&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;EKEvent&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;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; 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;loadEvents&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;// Create a date formatter instance to use for converting a string to a date&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;dateFormatter&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSDateFormatter&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;n&#34;&gt;dateFormatter&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;dateFormat&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;yyyy-MM-dd&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;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;// Create start and end date NSDate instances to build a predicate for which events to select&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;startDate&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;dateFormatter&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;dateFromString&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;2016-01-01&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;endDate&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;dateFormatter&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;dateFromString&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;2016-12-31&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&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;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;startDate&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;startDate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;endDate&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;endDate&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;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;eventStore&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;EKEventStore&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&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;// Use an event store instance to create and properly configure an NSPredicate&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;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;eventsPredicate&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;eventStore&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;predicateForEventsWithStartDate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;startDate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;endDate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;endDate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;calendars&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;calendar&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;c1&#34;&gt;// Use the configured NSPredicate to find and return events in the store that match&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;events&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;eventStore&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;eventsMatchingPredicate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;eventsPredicate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;).&lt;/span&gt;&lt;span class=&#34;bp&#34;&gt;sort&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 class=&#34;n&#34;&gt;e1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;EKEvent&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;e2&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;EKEvent&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;25&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;e1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;startDate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;compare&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;e2&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;startDate&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;NSComparisonResult&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;OrderedAscending&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;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;p&#34;&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;p&#34;&gt;}&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;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;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;&lt;a name=&#34;context-view-controller&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;context-view-controller&#34;&gt;Context: View controller&lt;/h2&gt;
&lt;p&gt;The context of the above snippet is a view controller. Inside the view controller class, there&amp;rsquo;s a calendar instance that is presumably set in the previous view controller&amp;rsquo;s &lt;code&gt;prepareForSegue&lt;/code&gt; method. There&amp;rsquo;s also an optional array of &lt;code&gt;EKEvent&lt;/code&gt; instances that acts as the data source for a table view, and a function called &lt;code&gt;loadEvents&lt;/code&gt; in this view controller.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;configure-start-end-dates&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;configure-start-and-end-dates&#34;&gt;Configure start and end dates&lt;/h2&gt;
&lt;p&gt;The first few lines of code within the &lt;code&gt;loadEvents&lt;/code&gt; function are to facilitate configuring a start and end date to use for querying the event store.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;generate-nspredicate&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;use-the-event-store-to-generate-nspredicate&#34;&gt;Use the event store to generate NSPredicate&lt;/h2&gt;
&lt;p&gt;To actually query the store, you need to create an &lt;code&gt;NSPredicate&lt;/code&gt; instance. However, rather than calling &lt;code&gt;NSPredicate&#39;s&lt;/code&gt; initializer, you will use an &lt;code&gt;EKEventStore&lt;/code&gt; instance to &lt;em&gt;generate&lt;/em&gt; a predicate. Apparently there&amp;rsquo;s some under-the-hood work that goes on to get a properly configured &lt;code&gt;NSPredicate&lt;/code&gt; instance to use in your query.&lt;/p&gt;
&lt;p&gt;So assuming you&amp;rsquo;ve got a valid start date, end date (both &lt;code&gt;NSDate&lt;/code&gt; instances), and one or more calendars to search for events within, you&amp;rsquo;ll use the event store&amp;rsquo;s &lt;code&gt;predicateForEventsWithStartDate(_: endDate: calendars:)&lt;/code&gt; method to get an &lt;code&gt;NSPredicate&lt;/code&gt; instance.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;query-with-nspredicate&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;query-the-event-store-with-generated-nspredicate&#34;&gt;Query the event store with generated NSPredicate&lt;/h2&gt;
&lt;p&gt;The last step is to call &lt;code&gt;eventsMatchingPredicate(_:)&lt;/code&gt; on the event store, and use the predicate you just generated. You can optionally sort them, as I&amp;rsquo;ve done in the snippet.&lt;/p&gt;
&lt;h1 id=&#34;wrapping-up&#34;&gt;Wrapping up&lt;/h1&gt;
&lt;p&gt;That&amp;rsquo;s the meat of querying the event store for a list of events. To learn how display them in a table view, I would recommend going ahead and &lt;a href=&#34;https://github.com/andrewcbancroft/EventTracker/tree/list-events-for-calendar&#34;&gt;grabbing the example project that I&amp;rsquo;ve provided&lt;/a&gt;. I&amp;rsquo;ve also got a couple of guides on working with table views if you need assistance with that part:&lt;/p&gt;
&lt;div class=&#34;resources&#34;&gt;
  &lt;div class=&#34;resources-header&#34;&gt;
    Table View Guides
  &lt;/div&gt;
  &lt;ul class=&#34;resources-content&#34;&gt;
    &lt;li&gt;
      &lt;i class=&#34;fab fa-link&#34;&gt;&lt;/i&gt; &lt;a href=&#34;https://www.andrewcbancroft.com/2015/05/18/swift-how-to-setting-up-a-table-view/&#34; title=&#34;Swift How-To: Setting up a Table View&#34;&gt;Swift How-To: Setting up a Table View&lt;/a&gt;
    &lt;/li&gt;
    &lt;li&gt;
      &lt;i class=&#34;fab fa-link&#34;&gt;&lt;/i&gt; &lt;a href=&#34;https://www.andrewcbancroft.com/2014/11/24/swift-uitableviewdatasource-cheat-sheet/&#34; title=&#34;Swift UITableViewDataSource Cheat Sheet&#34;&gt;Swift UITableViewDataSource Cheat Sheet&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&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/05/14/beginners-guide-to-eventkit-in-swift-requesting-permission/&#34; title=&#34;Beginner’s Guide to Event Kit in Swift – Requesting Permission&#34;&gt;Beginner’s Guide to Event Kit in Swift – Requesting Permission&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/06/17/creating-calendars-with-event-kit-and-swift/&#34; title=&#34;Creating Calendars with Event Kit and Swift&#34;&gt;Creating Calendars with Event Kit and 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/06/02/creating-calendar-events-with-event-kit-and-swift/&#34; title=&#34;Creating Calendar Events with Event Kit and Swift&#34;&gt;Creating Calendar Events with Event Kit and 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>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>Adding Buttons to the Navigation Bar with Storyboards</title>
      <link>https://www.andrewcbancroft.com/2016/04/14/adding-buttons-to-the-navigation-bar-with-storyboards/</link>
      <pubDate>Thu, 14 Apr 2016 18:20:41 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2016/04/14/adding-buttons-to-the-navigation-bar-with-storyboards/</guid>
      <description>&lt;p&gt;&lt;small&gt;Updated on March 20, 2017 – Xcode 8 &amp;amp; Swift 3&lt;/small&gt;&lt;/p&gt;
&lt;p&gt;If you&amp;rsquo;re having trouble figuring out how to add buttons to the navigation bar in your Storyboard-based iOS app, my goal in this article is to help you through the process. Knowing what to click and where to drag to get things wired up isn&amp;rsquo;t entirely intuitive, so I want to be of help if I can.&lt;/p&gt;
&lt;p&gt;Lots of StackOverflow posts point us to just adding the buttons in code, but for those who lean toward trying to do as much in the Storyboard designer as possible, this approach feels out of place.&lt;/p&gt;
&lt;p&gt;The example I&amp;rsquo;m outlining here is done solely in the Storyboard.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;example-scenario&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;example-scenario&#34;&gt;Example scenario&lt;/h1&gt;
&lt;p&gt;The scenario is as follows:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;We have a main screen with an add button in the top right of the navigation bar&lt;/li&gt;
&lt;li&gt;Pressing Add will display a modal screen with two buttons in the navigation bar: Cancel, and Save&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;The Goal&lt;/strong&gt;: Use only the Storyboard to add the ‘Add&amp;rsquo; button, and the Cancel and Save buttons to the navigation bar. The end result will be something like this:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/04/navigation-bar-buttons-demo.gif&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/04/navigation-bar-buttons-demo.gif&#34; alt=&#34;Navigation bar buttons demo&#34; width=&#34;470&#34; height=&#34;849&#34; class=&#34;alignnone size-full wp-image-12753&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;An example git repository has been created for this walk-through over at GitHub:&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;fab fa-github fa-lg&#34;&gt;&lt;/i&gt; &lt;a href=&#34;https://github.com/andrewcbancroft/StoryboardNavigationControllerExamples&#34; title=&#34;Storyboard Navigation Controller Examples&#34;&gt;Storyboard Navigation Controller Example&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;
&lt;p&gt;&lt;a name=&#34;main-scene-setup&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;main-storyboard-scene-setup&#34;&gt;Main Storyboard scene setup&lt;/h1&gt;
&lt;p&gt;The very first thing you need to do in order to get the add button to show up in the navigation bar is to embed your view controller in a navigation controller.&lt;/p&gt;
&lt;p&gt;Select your view controller and choose Editor &amp;gt; Embed In &amp;gt; Navigation Controller:&lt;br&gt;
&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/04/EmbedInNavigationController.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/04/EmbedInNavigationController-1024x560.png&#34; alt=&#34;Embed in Navigation Controller&#34; width=&#34;1024&#34; height=&#34;560&#34; class=&#34;alignnone size-large wp-image-12756&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/04/EmbedInNavigationController-1024x560.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2016/04/EmbedInNavigationController-300x164.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2016/04/EmbedInNavigationController.png 1043w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Next, you need to search for a Bar Button Item from the Object Library in the Utilities Pane. Once you find it in the list, drag one up to the upper-right of the navigation bar:&lt;br&gt;
&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/04/BarButtonItemToNavBar1.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/04/BarButtonItemToNavBar1-1024x788.png&#34; alt=&#34;Bar Button Item to Navigation Bar&#34; width=&#34;1024&#34; height=&#34;788&#34; class=&#34;alignnone size-large wp-image-12760&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/04/BarButtonItemToNavBar1-1024x788.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2016/04/BarButtonItemToNavBar1-300x231.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2016/04/BarButtonItemToNavBar1.png 1064w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;To turn it from an &amp;ldquo;Item” button into a &amp;ldquo;+” button, you need to adjust the Bar Button Item&amp;rsquo;s attributes. Click on the button, either in the navigation bar itself or on the document outline for the Storyboard, and choose the Attributes Inspector for the control.&lt;/p&gt;
&lt;p&gt;Then change the System Item property from &amp;ldquo;Custom” to &amp;ldquo;Add”:&lt;br&gt;
&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/04/ChangeBarButtonItemAttributes.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/04/ChangeBarButtonItemAttributes-1024x292.png&#34; alt=&#34;Change Bar Button Item Attributes&#34; width=&#34;1024&#34; height=&#34;292&#34; class=&#34;alignnone size-large wp-image-12763&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/04/ChangeBarButtonItemAttributes-1024x292.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2016/04/ChangeBarButtonItemAttributes-300x86.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2016/04/ChangeBarButtonItemAttributes.png 1463w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;That&amp;rsquo;s all there is to setting up that first scene. You&amp;rsquo;ll wire up the add button to modally show the &amp;ldquo;Add” scene in the next section.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;add-scene-setup&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;add-storyboard-scene-setup&#34;&gt;Add Storyboard scene setup&lt;/h1&gt;
&lt;p&gt;Next up, you need an &amp;ldquo;Add” scene. For that, another View Controller is required. Drag out a new View Controller onto the Storyboard design surface. While you&amp;rsquo;re at it, go ahead and create a new &lt;code&gt;AddScreenViewController&lt;/code&gt; class:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/04/NewVC.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/04/NewVC-1024x703.png&#34; alt=&#34;New View Controller&#34; width=&#34;1024&#34; height=&#34;703&#34; class=&#34;alignnone size-large wp-image-12765&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/04/NewVC-1024x703.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2016/04/NewVC-300x206.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2016/04/NewVC.png 1158w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Once you&amp;rsquo;ve created a new &lt;code&gt;AddScreenViewController&lt;/code&gt; class from the File &amp;gt; New &amp;gt; File dialog, make sure you&amp;rsquo;ve wired it up appropriately:&lt;br&gt;
&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/04/AddScreenVCMapped.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/04/AddScreenVCMapped.png&#34; alt=&#34;Add Screen View Controller Mapped&#34; width=&#34;935&#34; height=&#34;324&#34; class=&#34;alignnone size-full wp-image-12767&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/04/AddScreenVCMapped.png 935w, https://www.andrewcbancroft.com/wp-content/uploads/2016/04/AddScreenVCMapped-300x104.png 300w&#34; sizes=&#34;(max-width: 935px) 100vw, 935px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Now, to get this &amp;ldquo;Add” screen to show modally, you need to control + drag from the &amp;ldquo;plus (+)” button on the first view controller, over to the new AddScreenViewController you just dragged onto the Storyboard. Select &amp;ldquo;Present Modally” from the popover:&lt;br&gt;
&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/04/PresentModally.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/04/PresentModally.png&#34; alt=&#34;Present Add Screen Modally&#34; width=&#34;831&#34; height=&#34;682&#34; class=&#34;alignnone size-full wp-image-12768&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/04/PresentModally.png 831w, https://www.andrewcbancroft.com/wp-content/uploads/2016/04/PresentModally-300x246.png 300w&#34; sizes=&#34;(max-width: 831px) 100vw, 831px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Now, the problem you&amp;rsquo;ll run into at this point if you run the app is that, while tapping the ‘Add&amp;rsquo; (+) button does in fact present the Add screen modally, 1) There&amp;rsquo;s no way to dismiss the modal controller to go back to the main screen, and 2) There&amp;rsquo;s no navigation bar to add buttons to!&lt;/p&gt;
&lt;p&gt;So, the trick to get a navigation bar in place so that you can add our ‘Cancel&amp;rsquo; and ‘Save&amp;rsquo; buttons is to &lt;strong&gt;embed the Add Screen View Controller in… you guessed it… a Navigation Controller&lt;/strong&gt;:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/04/EmbedAddScreen.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/04/EmbedAddScreen-1024x774.png&#34; alt=&#34;Embed Add View Controller in Navigation Controller&#34; width=&#34;1024&#34; height=&#34;774&#34; class=&#34;alignnone size-large wp-image-12770&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/04/EmbedAddScreen-1024x774.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2016/04/EmbedAddScreen-300x227.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2016/04/EmbedAddScreen.png 1047w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Once this is done, your storyboard should look something like this:&lt;br&gt;
&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/04/OverviewOfSetup.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/04/OverviewOfSetup-1024x312.png&#34; alt=&#34;Storyboard Setup Overview&#34; width=&#34;1024&#34; height=&#34;312&#34; class=&#34;alignnone size-large wp-image-12771&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/04/OverviewOfSetup-1024x312.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2016/04/OverviewOfSetup-300x91.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2016/04/OverviewOfSetup.png 1450w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Now all that&amp;rsquo;s left to do is to drag two Bar Button Items up to the navigation bar, just like we did for the &amp;ldquo;Plus” (+) button on the main screen:&lt;br&gt;
&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/04/DragTwoBarButtonItems1.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/04/DragTwoBarButtonItems1-1024x847.png&#34; alt=&#34;Drag Two Bar Button Items to Navigation Bar&#34; width=&#34;1024&#34; height=&#34;847&#34; class=&#34;alignnone size-large wp-image-12774&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/04/DragTwoBarButtonItems1-1024x847.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2016/04/DragTwoBarButtonItems1-300x248.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2016/04/DragTwoBarButtonItems1.png 1113w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Changing each of their System Item properties in the Attribute Inspector, just like we did before, will give them their &amp;ldquo;Cancel” and &amp;ldquo;Save” appearance:&lt;br&gt;
&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/04/ChangeSystemItemProperties.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/04/ChangeSystemItemProperties.png&#34; alt=&#34;Change Bar Button Item System Item property to Cancel and Save&#34; width=&#34;954&#34; height=&#34;362&#34; class=&#34;alignnone size-full wp-image-12775&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/04/ChangeSystemItemProperties.png 954w, https://www.andrewcbancroft.com/wp-content/uploads/2016/04/ChangeSystemItemProperties-300x114.png 300w&#34; sizes=&#34;(max-width: 954px) 100vw, 954px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;If you control + drag the ‘Cancel&amp;rsquo; and ‘Save&amp;rsquo; buttons from the Storyboard scene to the .swift code file, you&amp;rsquo;ll be able to wire up IBActions to dismiss the view controller and return to the main screen. I won&amp;rsquo;t explain that step here, since the goal was to show you how to add the buttons themselves, but the &lt;a href=&#34;https://github.com/andrewcbancroft/StoryboardNavigationControllerExamples&#34;&gt;example project on GitHub&lt;/a&gt; has the view controller dismissal implemented if you care to review it.&lt;/p&gt;
&lt;h1 id=&#34;wrapping-up&#34;&gt;Wrapping up&lt;/h1&gt;
&lt;p&gt;That&amp;rsquo;s it! Buttons in the navigation bar, all done inside the Storyboard.&lt;/p&gt;
&lt;p&gt;Hopefully this quick walk-through removes any stumbling blocks you might have in designing your Storyboard-based app with navigation controllers.&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>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>Swift Functions as Types</title>
      <link>https://www.andrewcbancroft.com/2016/03/18/swift-functions-as-types/</link>
      <pubDate>Fri, 18 Mar 2016 18:11:02 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2016/03/18/swift-functions-as-types/</guid>
      <description>&lt;p&gt;For the well-versed functional programmer, the fact that [functions in Swift are &lt;em&gt;Types&lt;/em&gt;][1] is no surprise. But I&amp;rsquo;m relatively new to the game on that front, so when I first encountered the idea of thinking of a function as a Type back when Swift was announced in 2014, it was a real eye-opener for me.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;swift-types-general&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;swift-types-in-general&#34;&gt;Swift Types (in general)&lt;/h1&gt;
&lt;p&gt;In order to understand &lt;em&gt;functions&lt;/em&gt; as Types, it&amp;rsquo;s probably a good idea to know what folks are talking about when they use the term &amp;ldquo;Type” in a general sense.&lt;/p&gt;
&lt;p&gt;In Swift, we&amp;rsquo;ve got two kinds of Types when we talk about them generally:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Named Types&lt;/li&gt;
&lt;li&gt;Compound Types&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;a name=&#34;named-types&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;named-types&#34;&gt;Named Types&lt;/h2&gt;
&lt;p&gt;Named Types are those which are defined and identified by the &lt;em&gt;name&lt;/em&gt; that they&amp;rsquo;re given. Classes, structs, enums, and protocols fit this category of Type.&lt;/p&gt;
&lt;p&gt;To define a Named Type, you&amp;rsquo;d do 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;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;SomeNewClassType&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 class=&#34;kd&#34;&gt;struct&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;SomeNewStructType&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;kd&#34;&gt;enum&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;SomeNewEnumType&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;kd&#34;&gt;protocol&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;SomeNewProtocolType&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;Initializing instances of Types and using their properties and methods, passing them around to functions that require parameters of those Types, or setting them as values to properties of other Types are all pretty standard thoughts that come to mind when using named Types.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;compound-types&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;compound-types&#34;&gt;Compound Types&lt;/h2&gt;
&lt;p&gt;&lt;em&gt;Compound&lt;/em&gt; Types, on the other hand, don&amp;rsquo;t have names. Rather, they have &amp;ldquo;signatures” that define and identify them as Types. Swift has two compound Types: functions and tuples.&lt;/p&gt;
&lt;p&gt;Now I know what you might be thinking: &amp;ldquo;Functions have names!”&lt;/p&gt;
&lt;p&gt;Indeed many do. But when we&amp;rsquo;re thinking about them in terms of their &lt;em&gt;Type-ness&lt;/em&gt;, we&amp;rsquo;ve got to go beyond the name to the function&amp;rsquo;s &amp;ldquo;signature” characteristics.&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;name&lt;/em&gt; of a function (or tuple, since they can be type-aliased) is simply how we &lt;em&gt;refer&lt;/em&gt; to the function in code to execute it or pass it around as an argument.&lt;/p&gt;
&lt;p&gt;The &amp;ldquo;signature” of the function, however, is the part that characterizes the function as a &lt;em&gt;Type&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;I want to analyze what I&amp;rsquo;m talking about when I refer to a function&amp;rsquo;s &amp;ldquo;signature”, because that really is as the heart of my goal for this blog entry…&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;function-types&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;function-types&#34;&gt;Function Types&lt;/h1&gt;
&lt;p&gt;What exactly makes up a function&amp;rsquo;s &lt;em&gt;Type-ness&lt;/em&gt; or &amp;ldquo;signature” as I&amp;rsquo;ve been calling it? Two things:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The Type(s) of its &lt;strong&gt;parameters&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;The Type that the function &lt;strong&gt;returns&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Combining the Type(s) that the function receives as inputs, and the Type that it returns composes to give the function &lt;em&gt;its&lt;/em&gt; Type / &amp;ldquo;signature”.&lt;/p&gt;
&lt;h1 id=&#34;reading-a-functions-type&#34;&gt;Reading a function&amp;rsquo;s Type&lt;/h1&gt;
&lt;p&gt;It always helps me to visualize, so take apart an example.&lt;/p&gt;
&lt;p&gt;If you&amp;rsquo;ve lived long on the Internet, you&amp;rsquo;re bound to have run across a Star Wars name generator… Plug in your name, and maybe a birth year, and out comes some crazy &amp;ldquo;Star Wars name” for you.&lt;/p&gt;
&lt;p&gt;The function definition (minus the body) might look 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;generateStarWarsName&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;firstName&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;lastName&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;birthYear&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;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;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;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 someone were to ask you, &amp;ldquo;What is the &lt;code&gt;generateStarWarsName&lt;/code&gt; function&amp;rsquo;s Type?”, you could answer: &amp;ldquo;&lt;code&gt;generateStarWarsName&lt;/code&gt; is a function Type that has three parameters, the first two of Type String, the last of Type Int, and that returns a value of Type String.”&lt;/p&gt;
&lt;p&gt;Wordy? Yes. But it does explain in precise terms what the function&amp;rsquo;s Type is.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;function-type-notation&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;function-type-notation&#34;&gt;Function Type notation&lt;/h1&gt;
&lt;p&gt;Rather than write out the paragraph describing the function&amp;rsquo;s Type, it&amp;rsquo;s far more convenient to indicate a the Type of a function using a standard notation. This notation is also the syntax that the Swift compiler uses when it&amp;rsquo;s trying to work with function Types.&lt;/p&gt;
&lt;p&gt;Essentially, it boils down to stripping away the function&amp;rsquo;s name and the parameter names to leave behind the raw Type information that composes to give the function &lt;em&gt;its&lt;/em&gt; Type.&lt;/p&gt;
&lt;p&gt;Given the above &lt;code&gt;generateStarWarsName&lt;/code&gt; function, we could notate its Type as follows:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;(String, String, Int) -&amp;gt; String&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;See how that works?&lt;/p&gt;
&lt;p&gt;Remove &amp;ldquo;generateStarWarsName”, &amp;ldquo;firstName: &amp;ldquo;, &amp;ldquo;lastName: &amp;ldquo;, and &amp;ldquo;birthYear: ” and you&amp;rsquo;re left with that raw Type information. What remains is the function&amp;rsquo;s Type notation.&lt;/p&gt;
&lt;p&gt;It tells you (and the Swift compiler) everything you need to know to be able identify the Type of that function… it&amp;rsquo;s &amp;ldquo;signature”, if you will.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;gotchas&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;gotchas&#34;&gt;Gotchas&lt;/h2&gt;
&lt;p&gt;A couple of gotchas when it comes to notating a function&amp;rsquo;s Type:&lt;/p&gt;
&lt;p&gt;1 – If a function takes no parameters, the &amp;ldquo;parameter portion” of the Type notation will simply be &lt;code&gt;()&lt;/code&gt; with nothing between the parentheses.&lt;/p&gt;
&lt;p&gt;So for example, the Type notation of&lt;br&gt;
&lt;code&gt;func returnHelloString() -&amp;gt; String {}&lt;/code&gt;&lt;br&gt;
is &lt;code&gt;() -&amp;gt; String&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;2 – If a function has no return type (ie, it doesn&amp;rsquo;t return anything), the &amp;ldquo;return type portion” of the Type notation will be &lt;code&gt;-&amp;gt; Void&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;So for example, the Type notation of&lt;br&gt;
&lt;code&gt;func sayHello() {}&lt;/code&gt;&lt;br&gt;
is &lt;code&gt;() -&amp;gt; Void&lt;/code&gt;, since it takes no parameters, &lt;em&gt;and&lt;/em&gt; returns nothing.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;practice&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;practice&#34;&gt;Practice&lt;/h1&gt;
&lt;p&gt;Here are a few more examples for you to practice function Type identification. Can you write out the correct notation for each function&amp;rsquo;s Type?&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;complimentMe&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;name&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;String&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;String&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;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;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 id=&#34;show-answer-1&#34; style=&#34;cursor: pointer;&#34;&gt;Show answer&lt;/a&gt;&lt;/p&gt;
&lt;div id=&#34;answer-1&#34; style=&#34;display: none;&#34;&gt;
  `(String) -&gt; String`
&lt;/div&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;countToTen&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;// ... prints 1 to 10 to the console ...&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;&lt;a id=&#34;show-answer-2&#34; style=&#34;cursor: pointer;&#34;&gt;Show answer&lt;/a&gt;&lt;/p&gt;
&lt;div id=&#34;answer-2&#34; style=&#34;display: none;&#34;&gt;
  `() -&gt; Void`&lt;/p&gt; 
  &lt;p&gt;
    Notice that while `countToTen` doesn&#39;t have the &#34;-&gt; Void&amp;#8221; in its definition, it &lt;em&gt;is&lt;/em&gt; listed in the function&#39;s Type notation for clarity.
  &lt;/p&gt;
  &lt;p&gt;
    When you read this function&#39;s Type, you&#39;d say, &#34;This is a function Type which takes no parameters and returns Void.&amp;#8221;
  &lt;/p&gt;
&lt;/div&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;addInts&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;bp&#34;&gt;first&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;n&#34;&gt;second&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;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;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 id=&#34;show-answer-3&#34; style=&#34;cursor: pointer;&#34;&gt;Show answer&lt;/a&gt;&lt;/p&gt;
&lt;div id=&#34;answer-3&#34; style=&#34;display: none;&#34;&gt;
  `(Int, Int) -&gt; Int`
&lt;/div&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;fadeIn&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;n&#34;&gt;delay&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;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;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&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;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 id=&#34;show-answer-4&#34; style=&#34;cursor: pointer;&#34;&gt;Show answer&lt;/a&gt;&lt;/p&gt;
&lt;div id=&#34;answer-4&#34; style=&#34;display: none;&#34;&gt;
  `(NSTimeInterval, NSTimeInterval, (Bool) -&gt; Void)) -&gt; Void`&lt;/p&gt; 
  &lt;p&gt;
    This one&#39;s actually a more complicated &#34;compound Type&amp;#8221; – note the third parameter which indicates that `fadeIn` receives a function Type &lt;em&gt;as one of its inputs&lt;/em&gt;. Remember that since functions are Types, they carry the characteristic of being able to be passed around to other functions as parameters, or stored in variables/constants! &lt;/div&gt; 
&lt;pre&gt;&lt;code&gt;&amp;lt;p&amp;gt;
&amp;lt;/p&amp;gt;

```swift
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;func increment(input: Int) -&amp;gt; Int {
return input + 1
}&lt;/p&gt;
&lt;p&gt;func decrement(input: Int) -&amp;gt; Int {
return input - 1
}&lt;/p&gt;
&lt;p&gt;func chooseAdjustmentFunction(increase: Bool) -&amp;gt; (Int) -&amp;gt; Int {
return increase ? increment : decrement
}&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;    
    &amp;lt;p&amp;gt;
      &amp;lt;a id=&amp;#34;show-answer-5&amp;#34; style=&amp;#34;cursor: pointer;&amp;#34;&amp;gt;Show answer&amp;lt;/a&amp;gt;
    &amp;lt;/p&amp;gt;
    
    &amp;lt;div id=&amp;#34;answer-5&amp;#34; style=&amp;#34;display: none;&amp;#34;&amp;gt;
      increment: `(Int) -&amp;gt; Int`&amp;lt;br /&amp;gt; decrement: `(Int) -&amp;gt; Int`&amp;lt;/p&amp;gt; 
      
      &amp;lt;p&amp;gt;
        chooseAdjustmentFunction(_:): `(Bool) -&amp;gt; (Int) -&amp;gt; Int`
      &amp;lt;/p&amp;gt;
      
      &amp;lt;p&amp;gt;
        This one&amp;#39;s complicated in a slightly different way. This time, it&amp;#39;s the return Type of the function that&amp;#39;s kind of crazy.
      &amp;lt;/p&amp;gt;
      
      &amp;lt;p&amp;gt;
        Read this as, &amp;#34;A function Type which takes as Bool as a parameter and returns a function Type which takes an Int as a parameter and returns an Int.&amp;amp;#8221;
      &amp;lt;/p&amp;gt;
      
      &amp;lt;p&amp;gt;
        You can see how the Types of the `increment` and `decrement` functions &amp;lt;em&amp;gt;match&amp;lt;/em&amp;gt; the return Type of the `chooseAdjustmentFunction` function. &amp;lt;/div&amp;gt; 
        
        &amp;lt;h1&amp;gt;
          Wrapping up
        &amp;lt;/h1&amp;gt;
        
        &amp;lt;p&amp;gt;
          Knowing that functions are Types in Swift is a powerful thing. Being able to correctly articulate the Type of a function, and produce its notation in valid Swift syntax is even &amp;lt;em&amp;gt;more&amp;lt;/em&amp;gt; powerful, because it&amp;#39;s at that point when you&amp;#39;ll be able to recognize which kinds of functions are valid to pass as parameters to &amp;lt;em&amp;gt;other&amp;lt;/em&amp;gt; functions, or to assign as properties of other Types. It can also play a role in Swift&amp;#39;s pattern matching features. But alas, those topics for another day!
        &amp;lt;/p&amp;gt;
        
        &amp;lt;p&amp;gt;
          &amp;lt;a name=&amp;#34;related&amp;#34; class=&amp;#34;jump-target&amp;#34;&amp;gt;&amp;lt;/a&amp;gt;
        &amp;lt;/p&amp;gt;
        
        &amp;lt;div class=&amp;#34;resources&amp;#34;&amp;gt;
          &amp;lt;div class=&amp;#34;resources-header&amp;#34;&amp;gt;
            You might also enjoy&amp;amp;#8230;
          &amp;lt;/div&amp;gt;
          
          &amp;lt;ul class=&amp;#34;resources-content&amp;#34;&amp;gt;
            &amp;lt;li&amp;gt;
              &amp;lt;i class=&amp;#34;fa fa-angle-right&amp;#34;&amp;gt;&amp;lt;/i&amp;gt; &amp;lt;a href=&amp;#34;https://www.andrewcbancroft.com/2015/01/06/immutable-types-changing-state-swift/&amp;#34; title=&amp;#34;Immutable Types with Changing State in Swift&amp;#34;&amp;gt;Immutable Types with Changing State in Swift&amp;lt;/a&amp;gt;
            &amp;lt;/li&amp;gt;
            &amp;lt;li&amp;gt;
              &amp;lt;i class=&amp;#34;fa fa-angle-right&amp;#34;&amp;gt;&amp;lt;/i&amp;gt; &amp;lt;a href=&amp;#34;https://www.andrewcbancroft.com/2015/01/20/conveniently-transforming-immutable-types-swift/&amp;#34; title=&amp;#34;Conveniently Transforming Immutable Types in Swift&amp;#34;&amp;gt;Conveniently Transforming Immutable Types in Swift&amp;lt;/a&amp;gt;
            &amp;lt;/li&amp;gt;
          &amp;lt;/ul&amp;gt;
        &amp;lt;/div&amp;gt;
        
        &amp;lt;p&amp;gt;
          &amp;lt;a name=&amp;#34;share&amp;#34; class=&amp;#34;jump-target&amp;#34;&amp;gt;&amp;lt;/a&amp;gt;
        &amp;lt;/p&amp;gt;
        
        &amp;lt;p&amp;gt;
        &amp;lt;/p&amp;gt;

 [1]: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Functions.html#//apple_ref/doc/uid/TP40014097-CH10-ID158
&lt;/code&gt;&lt;/pre&gt;</description>
    </item>
    
    <item>
      <title>Fade Views In/Out with Fadeable – A Swift Protocol Extension</title>
      <link>https://www.andrewcbancroft.com/2016/02/22/fade-views-inout-with-fadeable-a-swift-protocol-extension/</link>
      <pubDate>Mon, 22 Feb 2016 19:07:27 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2016/02/22/fade-views-inout-with-fadeable-a-swift-protocol-extension/</guid>
      <description>&lt;p&gt;During the production of my &lt;a href=&#34;https://www.pluralsight.com/courses/cocoapods-xcode-project-dependencies&#34;&gt;Pluralsight course on Managing Xcode Project Dependencies with CocoaPods&lt;/a&gt;, I wanted to provide viewers of the course the opportunity to see how to create and deploy a simple library out to the CocoaPods Trunk.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;inspiration&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;inspiration&#34;&gt;Inspiration&lt;/h3&gt;
&lt;p&gt;A simple idea came to mind: Create something that allows client developers of the pod to easily fade views in or out on any UIView instance. If you&amp;rsquo;ve read andrewcbancroft.com for a long time, you might remember that I &lt;a href=&#34;https://www.andrewcbancroft.com/2014/07/27/fade-in-out-animations-as-class-extensions-with-swift/&#34;&gt;wrote on this very subject already&lt;/a&gt;, but there, I used an extension to UIView, because protocol extensions hadn&amp;rsquo;t been invented yet!&lt;/p&gt;
&lt;p&gt;For my course, I borrowed an idea that I first saw done by &lt;a href=&#34;https://twitter.com/FlexMonkey&#34;&gt;@NSFlexMonkey&lt;/a&gt; when he built the &lt;a href=&#34;http://flexmonkey.blogspot.co.uk/2015/10/rotatable-swift-protocol-extension-to.html&#34;&gt;Rotateable protocol extension&lt;/a&gt;. Only instead of rotating, I&amp;rsquo;m fading, so I named it &amp;ldquo;Fadeable”!&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;demo&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;demo&#34;&gt;Demo&lt;/h3&gt;
&lt;p&gt;&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/02/fadeable-gif.gif&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2016/02/fadeable-gif.gif&#34; alt=&#34;Fadeable Gif Demo&#34; width=&#34;374&#34; height=&#34;668&#34; class=&#34;alignnone size-full wp-image-12654&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;sample&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;fadeable-code-sample&#34;&gt;Fadeable code sample&lt;/h3&gt;
&lt;p&gt;The &amp;ldquo;library&amp;rsquo;s” source can be found over at GitHub:&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;fab fa-github fa-lg&#34;&gt;&lt;/i&gt; &lt;a href=&#34;https://github.com/andrewcbancroft/Fadeable&#34; title=&#34;Fadeable&#34;&gt;Fadeable&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;
&lt;p&gt;Here&amp;rsquo;s a snippet from the repository so you can see what the extension&amp;rsquo;s doing:&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;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;protocol&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;Fadeable&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;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;alpha&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;CGFloat&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;kr&#34;&gt;get&lt;/span&gt; &lt;span class=&#34;kr&#34;&gt;set&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;mutating&lt;/span&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;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;n&#34;&gt;delay&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;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;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&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;mutating&lt;/span&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;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;n&#34;&gt;delay&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;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;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&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;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;extension&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;Fadeable&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;public&lt;/span&gt; &lt;span class=&#34;kr&#34;&gt;mutating&lt;/span&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;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;delay&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;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;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;12&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;animateWithDuration&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;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;13&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;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;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;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;kd&#34;&gt;public&lt;/span&gt; &lt;span class=&#34;kr&#34;&gt;mutating&lt;/span&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;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;delay&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;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;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;17&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;animateWithDuration&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;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;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;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;19&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;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 class=&#34;p&#34;&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&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;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 class=&#34;n&#34;&gt;Fadeable&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;In the code snippet above, I define the &lt;code&gt;Fadeable&lt;/code&gt; protocol as [Some Type] that has an &lt;code&gt;alpha&lt;/code&gt; property, and a &lt;code&gt;fadeIn()&lt;/code&gt; and &lt;code&gt;fadeOut()&lt;/code&gt; function.&lt;/p&gt;
&lt;p&gt;Then I create an extension to the &lt;code&gt;Fadeable&lt;/code&gt; protocol and provide a simple, default implementation which will animate the alpha to 0, or to 1, depending on whether or not the client developer is fading in or out.&lt;/p&gt;
&lt;p&gt;Finally, I extend &lt;code&gt;UIView&lt;/code&gt; to conform to &lt;code&gt;Fadeable&lt;/code&gt;. And that&amp;rsquo;s it! Any &lt;code&gt;UIView&lt;/code&gt; instance can now fade in or out by simply calling the appropriate 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;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&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;@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;box&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#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&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;// ... Omitted for brevity   &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;// The storyboard has a button that can be tapped to toggle the fade action&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;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;fadeToggleTapped&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; 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;box&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;mi&#34;&gt;0&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;n&#34;&gt;box&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;11&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;12&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;            &lt;span class=&#34;n&#34;&gt;box&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&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;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;&lt;a name=&#34;creating-cocoapod-libraries&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;creating-cocoapod-libraries&#34;&gt;Creating CocoaPod libraries&lt;/h3&gt;
&lt;p&gt;If you&amp;rsquo;re interested in seeing a full walk-through of how I created and published the Fadeable Library to the CocoaPods Trunk, I would love it if you gave Module 3 of my Pluralsight course, titled a watch! It&amp;rsquo;s titled &lt;a href=&#34;https://www.pluralsight.com/courses/cocoapods-xcode-project-dependencies&#34;&gt;Creating CocoaPod Libraries&lt;/a&gt; and covers from beginning to end, the process of creating a library that&amp;rsquo;s compatible with CocoaPods.&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/03/managing-xcode-project-dependencies-with-cocoapods/&#34; title=&#34;Managing Xcode Project Dependencies with CocoaPods&#34;&gt;Managing Xcode Project Dependencies with CocoaPods&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/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;
&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>Fundamentals of Callbacks for Swift Developers</title>
      <link>https://www.andrewcbancroft.com/2016/02/15/fundamentals-of-callbacks-for-swift-developers/</link>
      <pubDate>Mon, 15 Feb 2016 18:21:33 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2016/02/15/fundamentals-of-callbacks-for-swift-developers/</guid>
      <description>&lt;p&gt;&lt;small&gt;Updated on October 12, 2016 – Swift 3.0&lt;/small&gt;&lt;/p&gt;
&lt;p&gt;Callbacks: What are they? How do they work? What are they used for in practice?&lt;/p&gt;
&lt;p&gt;My goal in this article is to provide answers to these questions so that you have a foundational understanding of this common programming pattern in iOS development.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;what-are-callbacks&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;what-are-callbacks&#34;&gt;What are callbacks?&lt;/h3&gt;
&lt;p&gt;Let&amp;rsquo;s approach the definition from a &amp;ldquo;big picture” scenario:&lt;/p&gt;
&lt;p&gt;When we&amp;rsquo;re building software, we&amp;rsquo;re either &lt;em&gt;using&lt;/em&gt; APIs, or &lt;em&gt;building&lt;/em&gt; APIs, are we not? We&amp;rsquo;re either &lt;em&gt;using&lt;/em&gt; code that &amp;ldquo;hooks into” what other developers have designed and made available to us, or we&amp;rsquo;re &lt;em&gt;creating&lt;/em&gt; code that other code will &amp;ldquo;hook into” and interact with, even if the &amp;ldquo;other code” is written by us in our own app.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;design-api-callbacks&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;learn-by-example-designing-an-api-for-callbacks&#34;&gt;Learn by example: Designing an API for callbacks&lt;/h4&gt;
&lt;p&gt;Since this is the case, let&amp;rsquo;s put on the &amp;ldquo;API Designer” hat for a moment and suppose that we&amp;rsquo;re working to create a hypothetical Type called an &lt;code&gt;ImageSketcher&lt;/code&gt;. One of the functions of &lt;code&gt;ImageSketcher&lt;/code&gt; is called &lt;code&gt;sketch()&lt;/code&gt; (parameters omitted for the moment). It will allow developers to pass it an image resource, such as a JPEG or a PNG, as one of its arguments. The function will then proceed to generate an animated sketch of that PNG for the user to view.&lt;/p&gt;
&lt;p&gt;In order to do the work of generating the animated sketch, &lt;code&gt;sketch()&lt;/code&gt; needs to do a lot of crunching. I have no idea what it&amp;rsquo;d take to do this in real life, honestly – let&amp;rsquo;s just work on the premise that it&amp;rsquo;ll take a few seconds to generate the animation so the end-user can watch it when it&amp;rsquo;s finished.&lt;/p&gt;
&lt;p&gt;In situations like this, it&amp;rsquo;d be nice to design &lt;code&gt;ImageSketcher&lt;/code&gt; where the start and end of the process are decoupled:&lt;/p&gt;
&lt;p&gt;Pass off the image. Let it do its thing to generate the animation. When it&amp;rsquo;s finished, &amp;ldquo;hook back in” and respond to the knowledge that the animation generation is complete. At that point, we could ask the end-user, &amp;ldquo;Hey, your sketch is done! Want to watch it now?”&lt;/p&gt;
&lt;p&gt;This particular example centers on a strategy that uses &amp;ldquo;asynchronous programming” techniques. It&amp;rsquo;s often done to boost app performance and/or responsiveness.&lt;/p&gt;
&lt;p&gt;During that middle part where we&amp;rsquo;re &amp;ldquo;disconnected” from the &lt;code&gt;ImageSketcher&#39;s&lt;/code&gt; &lt;code&gt;sketch()&lt;/code&gt; function, control of the app wouldn&amp;rsquo;t be tied up. Folks could continue to interact with the app.&lt;/p&gt;
&lt;p&gt;From a developer&amp;rsquo;s point of view, he/she can program against the API by calling the function, knowing that at [some unknown point in the future], it will finish, &lt;strong&gt;and that he/she will have the opportunity at that time to &amp;ldquo;hook back in” and respond to that completion event&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;That last bit is critical. Giving other developers the opportunity to re-insert themselves with custom application logic when the asynchronous task ends is very important as an API designer.&lt;/p&gt;
&lt;p&gt;Exactly what you as an API designer communicate back to the caller of your API is up to you, but put yourself in the &lt;em&gt;client&lt;/em&gt; developer&amp;rsquo;s shoes for a moment:&lt;/p&gt;
&lt;p&gt;Wouldn&amp;rsquo;t it be nice to know if something went wrong, or if data (the completed animation, for example) came out of that &lt;code&gt;sketch()&#39;s&lt;/code&gt; work? That&amp;rsquo;s exactly the kind of information that we&amp;rsquo;d expect an API designer would provide us with this completion event.&lt;/p&gt;
&lt;p&gt;So… just what are the options could we give callers of this method to &amp;ldquo;hook-in” and know that the work is done?&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;hook-in-options&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h5 id=&#34;hook-in8221-options&#34;&gt;&amp;ldquo;Hook-in” options&lt;/h5&gt;
&lt;p&gt;In scenarios like this, Swift developers have about 3 options to choose from:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Use &lt;a href=&#34;https://www.andrewcbancroft.com/2014/10/08/fundamentals-of-nsnotificationcenter-in-swift/&#34;&gt;NSNotificationCenter&lt;/a&gt; to alert &amp;ldquo;subscribers” that the &lt;code&gt;sketch()&lt;/code&gt; function has completed its job. I wrote about this option in &lt;a href=&#34;https://www.andrewcbancroft.com/2014/10/08/fundamentals-of-nsnotificationcenter-in-swift/&#34;&gt;Fundamentals of NSNotificationCenter in Swift&lt;/a&gt;&lt;/p&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Use the &lt;a href=&#34;https://www.andrewcbancroft.com/2015/04/08/how-delegation-works-a-swift-developer-guide/&#34;&gt;delegate pattern&lt;/a&gt; to create a contract between the &lt;code&gt;ImageSketcher&lt;/code&gt; and the caller of the &lt;code&gt;sketch()&lt;/code&gt; function. When &lt;code&gt;sketch()&lt;/code&gt; has completed its task, the appropriate &lt;em&gt;delegate&lt;/em&gt; method will be called. I wrote about this works in &lt;a href=&#34;https://www.andrewcbancroft.com/2015/04/08/how-delegation-works-a-swift-developer-guide/&#34;&gt;How Delegation Works – A Swift Developer’s Guide&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Use a callback&lt;/strong&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;So callbacks are used as another way for one piece of code to communicate with another piece of code somewhere else in the app.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;how-callbacks-work&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;how-do-callbacks-work&#34;&gt;How do callbacks work?&lt;/h3&gt;
&lt;p&gt;Here is a brief overview of the communication interaction using our hypothetical &lt;code&gt;ImageSketcher&lt;/code&gt; as a working example:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;An API designer has created the &lt;code&gt;sketch(image:completion:)&lt;/code&gt; function, and has chosen to accept a completion &amp;ldquo;callback” as the means of communicating the fact that the animation has been generated and is ready to show the end-user.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Data&lt;/strong&gt;, such as the completed sketch animation instance will be delivered through the completion callback&amp;rsquo;s parameter(s). The &lt;code&gt;completion&lt;/code&gt; parameter of our &lt;code&gt;sketch()&lt;/code&gt; function will have a signature that client developers must adhere to in order to facilitate the delivery of that data.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;A client developer writes up a routine (a function or closure) and passes it as the completion parameter&amp;rsquo;s argument. The function/closure that he/she writes will have a list of parameters that matches up to what the API designer required.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;When &lt;code&gt;sketch()&lt;/code&gt; is finished generating the sketch, the designer of the API has programmed &lt;em&gt;his/her function&lt;/em&gt; to call the callback that &lt;em&gt;you pass in&lt;/em&gt;. The API designer will pass along any data that was generated as arguments to the callback function&amp;rsquo;s parameters.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The client developer&amp;rsquo;s callback logic executes.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Callbacks are functions that often take the form of a &lt;a href=&#34;https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html#//apple_ref/doc/uid/TP40014097-CH11-ID94&#34;&gt;closure&lt;/a&gt; (basically an in-line function with no name that&amp;rsquo;s passed as a parameter to &lt;em&gt;another&lt;/em&gt; function), but they could technically be a named function.&lt;/p&gt;
&lt;p&gt;Perhaps it&amp;rsquo;s easiest to see in code itself. Here&amp;rsquo;s a skeleton view of what that looks like:&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;// API Designer World&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;struct&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;SketchAnimation&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;// represents some fully-generated animation that&amp;#39;s ready to play by the end user&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 4&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 5&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 6&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;struct&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;ImageSketcher&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;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;sketch&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;image&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIImage&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;kc&#34;&gt;_&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;sketchAnimation&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;SketchAnimation&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&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 some crunching to create the SketchAnimation instance...&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;animation&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;SketchAnimation&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;// invoke the completion callback&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;// pass along the completed sketch animation instance&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;completion&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;animation&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;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;// ---------------------------------------------------------------------&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;// Client Developer World&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;kd&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;MainViewController&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;21&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;22&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;23&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// end-user interacts with the app somehow to create an image sketch animation&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;c1&#34;&gt;// when they do, this function is called...&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;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;createSketchAnimation&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;imageToSketch&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIImage&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;26&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;sketcher&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ImageSketcher&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;n&#34;&gt;sketcher&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;sketch&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;image&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;imageToSketch&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;animation&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;SketchAnimation&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;29&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;            &lt;span class=&#34;c1&#34;&gt;// This is the callback.  It&amp;#39;s a closure, passed as the argument to the sketch function&amp;#39;s completion parameter&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;c1&#34;&gt;// Ask the end-user if they&amp;#39;d like to view the completed animation now...&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;c1&#34;&gt;// You as a develoepr have access to the completed animation through the animation parameter to this closure&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;p&#34;&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&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;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;You&amp;rsquo;ll notice a couple of things…&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;First&lt;/strong&gt;, I&amp;rsquo;ve separated the two &amp;ldquo;worlds” that exist: &amp;ldquo;API Designer World” and &amp;ldquo;Client Developer World”. Hopefully seeing both in action can give you the most complete picture of what&amp;rsquo;s going on with callbacks.&lt;/p&gt;
&lt;p&gt;In &amp;ldquo;API Designer World”, we&amp;rsquo;ve got the &lt;code&gt;ImageSketcher&lt;/code&gt; and its implementation.&lt;/p&gt;
&lt;p&gt;In &amp;ldquo;Client Developer World”, we&amp;rsquo;ve got someone &lt;em&gt;using&lt;/em&gt; an instance of &lt;code&gt;ImageSketcher&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Second&lt;/strong&gt;, notice the interaction. As an API designer, I was thinking, &amp;ldquo;Hey, when my sketching process is complete, I want to let the caller know that it&amp;rsquo;s finished and hand them the completed &lt;code&gt;SketchAnimation&lt;/code&gt; instance. To do that, I&amp;rsquo;ll need them to pass me a function that I can hand it off to via a parameter”.&lt;/p&gt;
&lt;p&gt;As a client developer, I&amp;rsquo;m thinking, &amp;ldquo;Okay, I&amp;rsquo;m going to call &lt;code&gt;sketch()&lt;/code&gt;, but how am I going to know when it&amp;rsquo;s done and how will I get the animation? Oh! I see – I need to give it a completion closure (a callback), and they&amp;rsquo;ll hand me the completed &lt;code&gt;SketchAnimation&lt;/code&gt; instance through my closure&amp;rsquo;s parameter. Sweet!”&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;m hoping the &amp;ldquo;thinking out loud” here helps you piece it together.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;examples-ios-sdk&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;examples-from-the-ios-sdk&#34;&gt;Examples from the iOS SDK&lt;/h3&gt;
&lt;p&gt;So how about a few &lt;em&gt;real&lt;/em&gt; examples, say, from the iOS SDK. Where are callbacks used there?&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;uialertcontroller&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;uialertcontroller&#34;&gt;UIAlertController&lt;/h4&gt;
&lt;p&gt;A really simple example of callbacks being used in the wild is when we work with &lt;a href=&#34;https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIAlertController_class/&#34;&gt;&lt;code&gt;UIAlertControllers&lt;/code&gt;&lt;/a&gt;. Take a look at 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;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;alertController&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIAlertController&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;My Alert&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;A Message&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;preferredStyle&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIAlertControllerStyle&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;alert&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 class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;OKAction&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIAlertAction&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;OK&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;style&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIAlertActionStyle&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;handler&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;action&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIAlertAction&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; 4&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Do something based on the user tapping this action button&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;c1&#34;&gt;// Notice that we get an instance of the UIAlertAction that was tapped if we need it&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;n&#34;&gt;alertController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;addAction&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;OKAction&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;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;alertController&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;12&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;// We could have provided a completion callback here, too,&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;// but we didn&amp;#39;t need to respond to the view controller&amp;#39;s presentation, so we passed nil&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;So the &lt;code&gt;UIAlertAction&lt;/code&gt; is actually the thing that takes the callback (the &lt;code&gt;handler&lt;/code&gt; parameter). There&amp;rsquo;s also an example on a View Controller&amp;rsquo;s &lt;code&gt;present()&lt;/code&gt; function. Both are intended to communicate something back to the caller.&lt;/p&gt;
&lt;p&gt;In the case of the &lt;code&gt;UIAlertAction&lt;/code&gt;, the &lt;code&gt;handler&lt;/code&gt; will be the logic to handle the user&amp;rsquo;s tapping on that specific alert button.&lt;/p&gt;
&lt;p&gt;In the case of the &lt;code&gt;present&lt;/code&gt; call, Apple has given us the opportunity to &amp;ldquo;hook in” to the presentation event and know when it&amp;rsquo;s complete, in case we need to perform additional logic at that moment.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;urlsession&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;urlsession&#34;&gt;URLSession&lt;/h4&gt;
&lt;p&gt;The world of HTTP is inherently asynchronous, so you&amp;rsquo;d expect to see some kind of pattern employed to deal with the &amp;ldquo;disconnectedness” of the start and finish of a process, such as making an HTTP request.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;URLSession&lt;/code&gt; encapsulates certain HTTP actions, such as retrieving the contents of a URL, in instances called &lt;code&gt;URLSessionDataTask&lt;/code&gt;. How does it communicate the fact that the HTTP request is complete, along with the data contained in the response? You guessed it: A callback.&lt;/p&gt;
&lt;p&gt;Take a look at this function signature from the &lt;a href=&#34;https://developer.apple.com/reference/foundation/urlsession/1410330-datatask&#34;&gt;Apple Developer Documentation on URLSession&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;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;dataTask&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;with&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;n&#34;&gt;URL&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;completionHandler&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;n&#34;&gt;Data&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;?,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;URLResponse&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;-&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;-&amp;gt;&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;URLSessionDataTask&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The &lt;code&gt;completionHandler&lt;/code&gt; parameter of this function is the interface that the API designers have created for delivering the resulting payload of the HTTP request when it&amp;rsquo;s finished and ready to hand off for further processing.&lt;/p&gt;
&lt;p&gt;Client developers of this API will be expected to make the call to &lt;code&gt;dataTask(with:completionHandler:)&lt;/code&gt; and supply it a completion callback to know when things are complete.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;animations&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;animations&#34;&gt;Animations&lt;/h4&gt;
&lt;p&gt;You&amp;rsquo;ll see &lt;em&gt;all kinds&lt;/em&gt; of &lt;code&gt;completion&lt;/code&gt; callbacks sprinkled throughout some of the simpler iOS animation APIs.&lt;/p&gt;
&lt;p&gt;If you take a look at the following function signatures from the &lt;a href=&#34;https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/#//apple_ref/occ/clm/UIView/animateWithDuration:animations:&#34;&gt;Apple Developer Documentation on UIViews&lt;/a&gt;, you&amp;rsquo;ll see the &lt;code&gt;completion&lt;/code&gt; parameters to many of these functions:&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;transition&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;with&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;options&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;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;2&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;n&#34;&gt;transition&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;from&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;to&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;options&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;3&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;n&#34;&gt;animateKeyframes&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;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;animations&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;4&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;n&#34;&gt;addKeyframe&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;withRelativeStartTime&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;relativeDuration&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&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;perform&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;p&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;on&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;animations&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;6&lt;/span&gt;&lt;span class=&#34;cl&#34;&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;delay&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;usingSpringWithDamping&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;initialSpringVelocity&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;animations&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id=&#34;wrapping-up&#34;&gt;Wrapping Up&lt;/h3&gt;
&lt;p&gt;Believe it or not, the usage of callbacks is one of the less-complicated ways of communicating between parts of code.&lt;/p&gt;
&lt;p&gt;My goal in this article was to show all of the sides and perspectives and players to give you insight into how this communication takes place.&lt;/p&gt;
&lt;p&gt;Now that the foundations are laid, it is my hope that you&amp;rsquo;ll be able to more confidently use callbacks and know what&amp;rsquo;s happening as you encounter them in your Swift code!&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;https://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;/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>Managing Xcode Project Dependencies with CocoaPods</title>
      <link>https://www.andrewcbancroft.com/2016/02/03/managing-xcode-project-dependencies-with-cocoapods/</link>
      <pubDate>Wed, 03 Feb 2016 18:23:49 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2016/02/03/managing-xcode-project-dependencies-with-cocoapods/</guid>
      <description>&lt;h1 id=&#34;course-outline&#34;&gt;Course Outline&lt;/h1&gt;
&lt;p&gt;I try to get as practical as possible in the course. As you&amp;rsquo;re watching you&amp;rsquo;ll be &amp;ldquo;embedded” into a team of iOS developers, and you&amp;rsquo;ll explore the problems that CocoaPods can solve for you by watching the team struggle through &lt;em&gt;not&lt;/em&gt; having a dependency manager, and then sharing in their victories as they bring in CocoaPods to ease that dependency management struggle.&lt;/p&gt;
&lt;p&gt;The course is a progressive journey. First, the team has problems managing dependencies without a dependency manager. Then, they bring in CocoaPods to help them find and organize 3rd party libraries. &lt;em&gt;Then&lt;/em&gt;, they start to think, &amp;ldquo;Hey, what if we could create our &lt;em&gt;own&lt;/em&gt; CocoaPod libraries?”. Finally, they use that knowledge of how to create a CocoaPod library to set up a private repository to share code internally between projects with their team.&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;ve got four modules that cover the following topics:&lt;/p&gt;
&lt;h2 id=&#34;1---introducing-project-dependency-management&#34;&gt;1 - Introducing Project Dependency Management&lt;/h2&gt;
&lt;p&gt;I teach the basic concepts and definitions associated with &amp;ldquo;project dependency management”. What is it all about? Why does it matter? And most importantly, how is it done in the Apple Ecosystem?&lt;/p&gt;
&lt;h2 id=&#34;2---managing-dependencies-with-cocoapods&#34;&gt;2 - Managing Dependencies with CocoaPods&lt;/h2&gt;
&lt;p&gt;I guide you through installing CocoaPods, configuring your Xcode project to use CocoaPods, and working with the CocoaPods command line utility.&lt;/p&gt;
&lt;h2 id=&#34;3---creating-cocoapod-libraries&#34;&gt;3 - Creating CocoaPod Libraries&lt;/h2&gt;
&lt;p&gt;This is where things turned really fun for me. :] In this module of the course, I teach you how to create a CocoaPod library, host its source code on GitHub, and publish the library to the CocoaPods Trunk.&lt;/p&gt;
&lt;h2 id=&#34;4---hosting-a-private-cocoapods-repository&#34;&gt;4 - Hosting a Private CocoaPods Repository&lt;/h2&gt;
&lt;p&gt;As the capstone to the course, I teach you how to set up a private spec repository and how to configure CocoaPods to utilize it. It really is the culmination of everything learned in the prior modules, but it&amp;rsquo;s also one of the most practical strategies for sharing code between your various Xcode projects that I&amp;rsquo;ve found to-date.&lt;/p&gt;
&lt;p&gt;In addition to the four modules of content, the course comes with an example Swift iOS app if you&amp;rsquo;re a Pluralsight Plus subscriber!&lt;/p&gt;
&lt;h1 id=&#34;something-for-everyone&#34;&gt;Something for Everyone&lt;/h1&gt;
&lt;p&gt;The course is targeted at beginning to intermediate level developers. I assume no prior knowledge of how to work with a dependency manager.&lt;/p&gt;
&lt;p&gt;That being the case, I do my best to offer something for everyone. If you&amp;rsquo;ve never used a dependency manager on another platform before, I&amp;rsquo;ve got you covered – I&amp;rsquo;ll take you step by step through using CocoaPods.&lt;/p&gt;
&lt;p&gt;If you&amp;rsquo;re already using CocoaPods in your Xcode project, maybe you can benefit from the modules on creating CocoaPod libraries yourself! If you&amp;rsquo;re already creating libraries and contributing to the open source community, what about setting up that private spec repository to share with your team internally?&lt;/p&gt;
&lt;h1 id=&#34;feedback-welcome&#34;&gt;Feedback Welcome!&lt;/h1&gt;
&lt;p&gt;I welcome feedback on this course, and on other iOS development courses you might be interested in seeing in the Pluralsight library. Happy learning!&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>401 Unauthorized Browsing Site From Local IIS Instance</title>
      <link>https://www.andrewcbancroft.com/2016/01/21/401-unauthorized-browsing-site-from-local-iis-instance/</link>
      <pubDate>Thu, 21 Jan 2016 17:16:23 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2016/01/21/401-unauthorized-browsing-site-from-local-iis-instance/</guid>
      <description>&lt;p&gt;I spent waaaaay way way way too long fighting this one, so I&amp;rsquo;m saving everything I found on the issue so that I never lose it.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;setup&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;setup&#34;&gt;Setup&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Running Internet Information Services (IIS) 8.5 on my laptop.&lt;/li&gt;
&lt;li&gt;Set up a site with an ASP.Net MVC web application Windows Authentication.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;a name=&#34;problem&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;problem&#34;&gt;Problem&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Browsing the site locally continually prompts for my domain username and password.&lt;/li&gt;
&lt;li&gt;Credentials are never accepted / I&amp;rsquo;m never authenticated.&lt;/li&gt;
&lt;li&gt;Cancelling the login prompt ultimately responds with 401 Unauthorized.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;When accessing the site with Fiddler, IIS simply responds with 401 Unauthorized.&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;h3 id=&#34;solution-with-original-sources&#34;&gt;Solution (with original sources)&lt;/h3&gt;
&lt;p&gt;An incredibly helpful StackOverflow titled &lt;a href=&#34;http://stackoverflow.com/questions/7387156/unable-to-get-windows-authentication-to-work-through-local-iis&#34;&gt;Unable to get windows authentication to work through local IIS&lt;/a&gt; provided the solution.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Its&lt;/em&gt; source was a KnowledgeBase article from Microsoft: &lt;a href=&#34;https://support.microsoft.com/en-us/kb/896861&#34;&gt;https://support.microsoft.com/en-us/kb/896861&lt;/a&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Set the&lt;br&gt;
DisableStrictNameChecking&lt;br&gt;
registry entry to 1. For more information about how to do this, refer to article &lt;a href=&#34;http://support.microsoft.com/kb/281308&#34;&gt;281308&lt;/a&gt; in the Microsoft Knowledge Base&lt;/li&gt;
&lt;li&gt;Click Start, click Run, type regedit, and then click OK.&lt;/li&gt;
&lt;li&gt;In Registry Editor, locate and then click the following registry key:&lt;br&gt;
&lt;strong&gt;HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa\MSV1_0&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Right-click &lt;strong&gt;MSV1_0&lt;/strong&gt;, point to New, and then click Multi-String Value.&lt;/li&gt;
&lt;li&gt;Type &lt;strong&gt;BackConnectionHostNames&lt;/strong&gt;, and then press ENTER.&lt;/li&gt;
&lt;li&gt;Right-click BackConnectionHostNames, and then click Modify.&lt;/li&gt;
&lt;li&gt;In the Value data box, type the host name or the host names for the sites that are on the local computer, and then click OK.&lt;/li&gt;
&lt;li&gt;Quit Registry Editor, and then restart the IISAdmin service. (to do this, I ran iisreset from a Powershell prompt)&lt;/li&gt;
&lt;/ol&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 Framework Woes – Unresolved Identifier? No Member?</title>
      <link>https://www.andrewcbancroft.com/2016/01/08/swift-framework-woes-unresolved-identifier-no-member/</link>
      <pubDate>Fri, 08 Jan 2016 18:40:55 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2016/01/08/swift-framework-woes-unresolved-identifier-no-member/</guid>
      <description>&lt;p&gt;It&amp;rsquo;s the simple things that get us, isn&amp;rsquo;t it?&lt;/p&gt;
&lt;p&gt;I was working on a simple little framework the other day, and I&amp;rsquo;d gotten things just how I wanted… or so I thought.&lt;/p&gt;
&lt;p&gt;I was ready to test things out and import the framework into my real app.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;import MyShinyNewFramework&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Sweet! No build errors!&lt;/p&gt;
&lt;p&gt;&lt;code&gt;// attempt to use things defined in the framework&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Not so sweet…&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Use of unresolved identifier ‘…&#39;&lt;/p&gt;
&lt;p&gt;Value of type ‘…&amp;rsquo; has no member ‘…&#39;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;I scrunched my forehead, puzzled, and immediately it came to me.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;public&lt;/code&gt; &lt;code&gt;public&lt;/code&gt; &lt;code&gt;public&lt;/code&gt; all the things! Or at least, the things that others need to use from the framework. :]&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Of course&lt;/em&gt; a framework&amp;rsquo;s usable API needs to be public, but I write far more code that doesn&amp;rsquo;t require thought of access control modifiers than code that &lt;em&gt;does&lt;/em&gt;, so there&amp;rsquo;s always that initial head-scratching that happens when you&amp;rsquo;re to the point of testing and go, &amp;ldquo;WHAT?? Why is this not working??!”&lt;/p&gt;
&lt;h4 id=&#34;key-takeaway&#34;&gt;Key Takeaway&lt;/h4&gt;
&lt;p&gt;Whenever you&amp;rsquo;re developing code (such as a framework) that&amp;rsquo;s intended to be used from the perspective of another Swift module, you need to include &lt;code&gt;public&lt;/code&gt; before Types and functions that are intended to be &amp;ldquo;seen” and called from that other module. Otherwise, you&amp;rsquo;ll get those same fun compiler errors and join me in saying to yourself, &amp;ldquo;Doh! Yep… public… &lt;em&gt;again&lt;/em&gt;.”&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Working with Unwind Segues Programmatically in Swift</title>
      <link>https://www.andrewcbancroft.com/2015/12/18/working-with-unwind-segues-programmatically-in-swift/</link>
      <pubDate>Fri, 18 Dec 2015 18:22:55 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2015/12/18/working-with-unwind-segues-programmatically-in-swift/</guid>
      <description>&lt;p&gt;&lt;small&gt;Updated on September 20, 2016 – Xcode 8 &amp;amp; Swift 3.0&lt;/small&gt;&lt;/p&gt;
&lt;p&gt;Navigating between screens is a critical component to building iOS applications. The mechanism for navigation in Storyboard-based applications is the &lt;em&gt;segue&lt;/em&gt;. Using segues, we can travel ahead to the next screen, which is extremely common. We can also travel &lt;em&gt;backward&lt;/em&gt; in the screen &amp;ldquo;navigation stack” by programming a special kind of segue called an &lt;em&gt;unwind&lt;/em&gt; segue.&lt;/p&gt;
&lt;p&gt;Embedding a view controller inside a navigation controller gives us some built-in forward and backward navigation, so you may be asking, &amp;ldquo;What&amp;rsquo;s the need for an unwind segue??”&lt;/p&gt;
&lt;p&gt;Well, suppose that we need to programmatically &lt;em&gt;trigger&lt;/em&gt; the backward navigation, based on an interaction with something other than the default &amp;ldquo;back” button on the navigation bar. How would you do it? Yep – you&amp;rsquo;ve got it: by using an unwind segue.&lt;/p&gt;
&lt;p&gt;This is a walk-through of how to work with unwind segues programmatically in Swift.&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/RoasterOnTheGo/tree/swift-2.3&#34;&gt;example project&lt;/a&gt;.&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;h3 id=&#34;example&#34;&gt;Example&lt;/h3&gt;
&lt;p&gt;An example app called &amp;ldquo;Roasters on the Go” has been created for this walk-through to help give you some context. It&amp;rsquo;s a mock mobile order system for purchasing green, un-roasted coffee beans (so that you can roast them yourself)!&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;fab fa-github fa-lg&#34;&gt;&lt;/i&gt; RoasterOnTheGo Example Project (&lt;a href=&#34;https://github.com/andrewcbancroft/RoasterOnTheGo/tree/swift-2.3&#34; title=&#34;RoasterOnTheGo - Swift 2.3&#34;&gt;Swift 2.3&lt;/a&gt; | &lt;a href=&#34;https://github.com/andrewcbancroft/RoasterOnTheGo/tree/master&#34; title=&#34;RoasterOnTheGo - Swift 3.0&#34;&gt;Swift 3.0&lt;/a&gt;)
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;We&amp;rsquo;ll start at a list of coffees categorized by region&lt;/li&gt;
&lt;li&gt;Tapping an coffee origin country will take you to the order screen&lt;/li&gt;
&lt;li&gt;Pressing the ‘Order Now&amp;rsquo; button will simulate the placement of an order&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Tapping &amp;ldquo;OK” on the alert will trigger the unwind segue&lt;/strong&gt; (which is the goal of this walk-through)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Here&amp;rsquo;s a sample of what the fake app does:&lt;br&gt;
&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/12/demo.gif&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/12/demo.gif&#34; alt=&#34;Unwind Segue Demo&#34; width=&#34;366&#34; height=&#34;663&#34; class=&#34;alignnone size-full wp-image-12483&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The goal is to go back to the list of coffee origins after ‘OK&amp;rsquo; is pressed on the alert. Let&amp;rsquo;s look at the steps to make this happen.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;create-action&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;1--create-an-unwindto___-ibaction&#34;&gt;1 – Create an unwindTo___ IBAction&lt;/h3&gt;
&lt;p&gt;The first step is to create an IBAction that we can wire up the unwind segue to.&lt;/p&gt;
&lt;p&gt;Supposing that you&amp;rsquo;ve got two view controller: The MenuViewController which lists out the menu of coffees to purchase, and the OrderViewController which allows your app users to buy that particular coffee.&lt;/p&gt;
&lt;p&gt;If we&amp;rsquo;re wanting to go &lt;em&gt;from&lt;/em&gt; OrderViewController &lt;em&gt;to&lt;/em&gt; MenuViewController, we need to &lt;em&gt;create the IBAction in the &lt;strong&gt;MenuViewController&lt;/strong&gt;&lt;/em&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;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;MenuViewController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UITableViewController&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 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&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;unwindToMenu&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;p&#34;&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;// ...&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;&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/12/01_unwind_action.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/12/01_unwind_action-1024x534.png&#34; alt=&#34;Create an Unwind Action&#34; width=&#34;1024&#34; height=&#34;534&#34; class=&#34;alignnone size-large wp-image-12474&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/12/01_unwind_action-1024x534.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2015/12/01_unwind_action-300x156.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2015/12/01_unwind_action.png 1218w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Placing the IBAction code in the right spot is critical to the functioning of the unwind segue. In this example where we want to go from Order to Menu, if you place the IBAction in the OrderViewController, the Storyboard will let you wire it up just fine, but the transition back to the menu screen will never happen at runtime.&lt;/p&gt;
&lt;p&gt;Therefore, it&amp;rsquo;s important to remember: Place the &amp;ldquo;unwindTo___” IBAction function in the view controller source file for the screen you&amp;rsquo;re unwinding back to.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;wire-unwind&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;2--wire-up-the-unwind-segue&#34;&gt;2 – Wire up the unwind segue&lt;/h3&gt;
&lt;p&gt;Next up is to wire connect the view controller to the unwind segue IBAction that we just created.&lt;/p&gt;
&lt;p&gt;Here, you&amp;rsquo;re going to be looking at the screen that you&amp;rsquo;re going to unwind &lt;em&gt;from&lt;/em&gt;. In the running example, this will be the Order View Controller&amp;rsquo;s scene in the Storyboard.&lt;/p&gt;
&lt;p&gt;To wire up the OrderViewController to its unwind segue, you need to right-click and drag (or control + drag) from the View Controller icon to the Exit icon in the Storyboard Scene:&lt;br&gt;
&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/12/02_drag_to_create_segue.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/12/02_drag_to_create_segue-1024x542.png&#34; alt=&#34;Drag to Create Segue&#34; width=&#34;1024&#34; height=&#34;542&#34; class=&#34;alignnone size-large wp-image-12473&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/12/02_drag_to_create_segue-1024x542.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2015/12/02_drag_to_create_segue-300x159.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2015/12/02_drag_to_create_segue.png 1199w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;You&amp;rsquo;ll be presented with list of IBActions to connect to. You&amp;rsquo;ll choose the unwind segue action that was created in step 1:&lt;br&gt;
&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/12/03_create_segue.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/12/03_create_segue-1024x542.png&#34; alt=&#34;Create Segue&#34; width=&#34;1024&#34; height=&#34;542&#34; class=&#34;alignnone size-large wp-image-12472&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/12/03_create_segue-1024x542.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2015/12/03_create_segue-300x159.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2015/12/03_create_segue.png 1201w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;identifier&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;3--specify-a-segue-identifier&#34;&gt;3 – Specify a segue identifier&lt;/h3&gt;
&lt;p&gt;For this step, you&amp;rsquo;ll want to make sure that the Document Outline of the Storyboard is expanded. This will allow you to easily select the unwind segue in the outline, and specify its identifier in the Attributes Inspector of the Utilities Pane:&lt;br&gt;
&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/12/04_specify_segue_id.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/12/04_specify_segue_id-1024x566.png&#34; alt=&#34;Specify Segue Identifier&#34; width=&#34;1024&#34; height=&#34;566&#34; class=&#34;alignnone size-large wp-image-12471&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/12/04_specify_segue_id-1024x566.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2015/12/04_specify_segue_id-300x166.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2015/12/04_specify_segue_id.png 1200w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Since we want to take the user back to the menu when placing an order is finished, we&amp;rsquo;ll give it the identifier of &amp;ldquo;unwindToMenu”.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;trigger&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;4--trigger-unwind-segue-programmatically&#34;&gt;4 – Trigger unwind segue programmatically&lt;/h3&gt;
&lt;p&gt;The final step is to write a bit of code to trigger the unwind segue at the appropriate time.&lt;/p&gt;
&lt;p&gt;In our example, we want to trigger it when the user taps on the ‘OK&amp;rsquo; button of the alert. Here&amp;rsquo;s a snippet of code that will accomplish that task:&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;orderButtonTapped&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; 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;alert&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIAlertController&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;Order Placed!&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;Thank you for your order.&lt;/span&gt;&lt;span class=&#34;se&#34;&gt;\n&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;We&amp;#39;ll ship it to you soon!&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;preferredStyle&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;alert&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;OKAction&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIAlertAction&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;OK&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;style&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIAlertActionStyle&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;handler&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;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;kc&#34;&gt;_&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&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; 5&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;performSegue&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;withIdentifier&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;unwindToMenu&amp;#34;&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;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; 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;n&#34;&gt;alert&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;addAction&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;OKAction&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;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;alert&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;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;h3 id=&#34;wrapping-up&#34;&gt;Wrapping up&lt;/h3&gt;
&lt;p&gt;Whenever you need to programmatically trigger backward navigation in your app, using an unwind segue can be a great feature to take advantage of. This walk-through took you step-by-step through working with unwind segues in Swift.&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>Using PowerShell to Install a DLL into the GAC</title>
      <link>https://www.andrewcbancroft.com/2015/12/16/using-powershell-to-install-a-dll-into-the-gac/</link>
      <pubDate>Wed, 16 Dec 2015 17:45:55 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2015/12/16/using-powershell-to-install-a-dll-into-the-gac/</guid>
      <description>&lt;p&gt;A couple of ways exist to install a DLL into the Global Assembly Cache (GAC). Using gacutil.exe is one, but this comes as part of a Visual Studio installation, and in a server environment, you may not have the luxury of installing Visual Studio, just to get the utility installed.&lt;/p&gt;
&lt;p&gt;An alternative, however, is to use PowerShell to install the DLL into the GAC. The following code snippet should take care of your needs.&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-powershell&#34; data-lang=&#34;powershell&#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;c&#34;&gt;#Note that you should be running PowerShell as an Administrator&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;no&#34;&gt;System.Reflection.Assembly&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]::&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;Load&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&amp;#34;System.EnterpriseServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a&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;nv&#34;&gt;$publish&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;New-Object&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;System&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;py&#34;&gt;EnterpriseServices&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;py&#34;&gt;Internal&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;py&#34;&gt;Publish&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;nv&#34;&gt;$publish&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;py&#34;&gt;GacInstall&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&amp;#34;C:\Path\To\DLL.dll&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&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;c&#34;&gt;#If installing into the GAC on a server hosting web applications in IIS, you need to restart IIS for the applications to pick up the change.&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;c&#34;&gt;#Uncomment the next line if necessary...&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;c&#34;&gt;#iisreset&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</description>
    </item>
    
    <item>
      <title>Get Social With Swift – Posting to Facebook and Twitter</title>
      <link>https://www.andrewcbancroft.com/2015/11/23/get-social-with-swift-posting-to-facebook-and-twitter/</link>
      <pubDate>Mon, 23 Nov 2015 19:17:38 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2015/11/23/get-social-with-swift-posting-to-facebook-and-twitter/</guid>
      <description>&lt;p&gt;Building social media features into our apps has never been easier! In this guide, I will walk you through how to get started with accessing the Social framework for iOS so that you can take advantage of Apple&amp;rsquo;s built-in support for two major social media platforms: Facebook, and Twitter.&lt;/p&gt;
&lt;p&gt;For your convenience, I&amp;rsquo;ve created a simple GitHub repository with an example Xcode project to help you see all of this code in action:&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;fab fa-github fa-lg&#34;&gt;&lt;/i&gt; &lt;a href=&#34;https://github.com/andrewcbancroft/SocialSwift&#34; title=&#34;SocialSwift&#34;&gt;SocialSwift Example Project&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: To try things out in this guide you need to run your app on a physical device, because the Social media features we&amp;rsquo;re looking at below are not available in the iOS Simulator.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;import&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;importing-the-social-module&#34;&gt;Importing the Social module&lt;/h3&gt;
&lt;p&gt;The first step is to import the Social module into your .swift file. It&amp;rsquo;s a simple declaration at the top of your file:&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;// ...&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;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;4&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;Social&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;c1&#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;check-availability&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;checking-service-availability&#34;&gt;Checking service availability&lt;/h3&gt;
&lt;p&gt;Next up is to make sure that the user has Facebook, Twitter, or both set up on their device.&lt;/p&gt;
&lt;p&gt;In the &lt;a href=&#34;https://github.com/andrewcbancroft/SocialSwift&#34;&gt;example that&amp;rsquo;s included with this guide&lt;/a&gt;, I will respond to a user&amp;rsquo;s tapping on a button in the UI to trigger the presentation of an &lt;code&gt;SLComposeViewController&lt;/code&gt; instance (which we&amp;rsquo;ll configure shortly).&lt;/p&gt;
&lt;p&gt;Within the body of the IBAction associated with the button, I will check whether or not the Facebook service is available:&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;postToFacebookTapped&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;k&#34;&gt;if&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;SLComposeViewController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;isAvailableForServiceType&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;SLServiceTypeFacebook&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;//...&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;service-types&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;service-types&#34;&gt;Service types&lt;/h4&gt;
&lt;p&gt;Currently there are 4 service types that represented by String constants in Swift. Passing one of the following into &lt;code&gt;SLComposeViewController.isAvailableForServiceType(_:)&lt;/code&gt; will check for that service&amp;rsquo;s availability on the user&amp;rsquo;s device:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;SLServiceTypeFacebook&lt;/li&gt;
&lt;li&gt;SLServiceTypeTwitter&lt;/li&gt;
&lt;li&gt;SLServiceTypeSinaWeibo&lt;/li&gt;
&lt;li&gt;SLServiceTypeTencentWeibo&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For the purposes of this guide, we&amp;rsquo;ll look at the first two, which map to Facebook and Twitter, respectively.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: Social media features in the Social module are not available for use/testing on the iOS Simulator, so you need to use a physical device to try things out.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;create-controller&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;creating-an-slcomposeviewcontroller&#34;&gt;Creating an SLComposeViewController&lt;/h3&gt;
&lt;p&gt;The Social framework comes with a Type that allows us to present some Apple-built UI to post to any of the four service types. To get at this out-of-the-box user interface, we look to &lt;code&gt;SLComposeViewController&#39;s&lt;/code&gt; initializer that takes an argument for the type of service we&amp;rsquo;d like to create a post for:&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;postToFacebookTapped&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;k&#34;&gt;if&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;SLComposeViewController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;isAvailableForServiceType&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;SLServiceTypeFacebook&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;socialController&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;SLComposeViewController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;forServiceType&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;SLServiceTypeFacebook&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;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;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;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In this code snippet, we&amp;rsquo;re creating an &lt;code&gt;SLComposeViewController&lt;/code&gt; instance to post to Facebook, since I&amp;rsquo;ve specified the &lt;code&gt;SLServiceTypeFacebook&lt;/code&gt; type to the initializer.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;configure&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;optional-configure-the-slcomposeviewcontroller&#34;&gt;(optional) Configure the SLComposeViewController&lt;/h3&gt;
&lt;p&gt;Optionally, you can choose to pre-populate the &amp;ldquo;create post” UI with some initial text, an image, or a URL. Here&amp;rsquo;s a sample of what that might look like:&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;postToFacebookTapped&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;k&#34;&gt;if&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;SLComposeViewController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;isAvailableForServiceType&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;SLServiceTypeFacebook&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;socialController&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;SLComposeViewController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;forServiceType&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;SLServiceTypeFacebook&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;//            socialController.setInitialText(&amp;#34;Hello World!&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;c1&#34;&gt;//            socialController.addImage(someUIImageInstance)&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;//            socialController.addURL(someNSURLInstance)&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;// ...&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;&lt;a name=&#34;present-controller&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;present-the-slcomposeviewcontroller&#34;&gt;Present the SLComposeViewController&lt;/h3&gt;
&lt;p&gt;The last step is to present the 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;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;postToFacebookTapped&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;k&#34;&gt;if&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;SLComposeViewController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;isAvailableForServiceType&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;SLServiceTypeFacebook&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;socialController&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;SLComposeViewController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;forServiceType&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;SLServiceTypeFacebook&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;//            socialController.setInitialText(&amp;#34;Hello World!&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;c1&#34;&gt;//            socialController.addImage(someUIImageInstance)&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;//            socialController.addURL(someNSURLInstance)&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;kc&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&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;socialController&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; 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;If you need to perform an action when the user has finished their post (or cancelled the action), the completion closure is your opportunity to hook into that event and execute the code you need.&lt;/p&gt;
&lt;h3 id=&#34;wrapping-up&#34;&gt;Wrapping up&lt;/h3&gt;
&lt;p&gt;As you can see, integrating social media sharing capability into your app has never been easier. Now, in this guide, I didn&amp;rsquo;t explicitly go over how to create posts for Twitter, but it&amp;rsquo;s as simple as switching &lt;code&gt;SLServiceTypeFacebook&lt;/code&gt; to &lt;code&gt;SLServiceTypeTwitter&lt;/code&gt;. The example I&amp;rsquo;ve provided with this guide does include code for Twitter, so feel free to &lt;a href=&#34;https://github.com/andrewcbancroft/SocialSwift&#34;&gt;download the full project over at GitHub&lt;/a&gt;!&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>Creating Trust-able Unit Tests in Swift</title>
      <link>https://www.andrewcbancroft.com/2015/11/03/creating-trust-able-unit-tests-in-swift/</link>
      <pubDate>Wed, 04 Nov 2015 02:42:08 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2015/11/03/creating-trust-able-unit-tests-in-swift/</guid>
      <description>&lt;p&gt;It may come as a surprise to you, but a green-lit passing unit test is not &lt;em&gt;always&lt;/em&gt; a sign that all is well with our software. Our apps and their corresponding unit test suites always start off clean and basic and simple.&lt;/p&gt;
&lt;p&gt;But they don&amp;rsquo;t stay that way for long.&lt;/p&gt;
&lt;p&gt;Cycles of code changes and additions and re-writes take place throughout the days, weeks, and months of the application development cycle. The app&amp;rsquo;s code-base and unit tests become more complex as the end-product progresses along its asymptotic approach to completion.&lt;/p&gt;
&lt;p&gt;Unit tests are only as good as they are &lt;em&gt;trust-able&lt;/em&gt;. As our products navigate their way through development iterations, how can we keep a handle on the trustworthiness of our unit tests? How can we be assured that they&amp;rsquo;re testing the right thing? How can we know that changing a unit test because a fundamental requirement has changed is still accurate when it lights up green? After all, we&amp;rsquo;re writing &lt;em&gt;code&lt;/em&gt; to test our code!&lt;/p&gt;
&lt;p&gt;Here, I explore several categories of thought that will help you think about creating trust-able unit tests.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;organization&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;test-organization&#34;&gt;Test Organization&lt;/h3&gt;
&lt;p&gt;How you organize the code within a single unit test can make all the difference in understanding it when you return to it later.&lt;/p&gt;
&lt;p&gt;Having a consistent strategy for organizing the body of a test function will save you time and effort in trying to understand what&amp;rsquo;s happening. Keeping things consistent across a team of developers will make everyone more productive as well.&lt;/p&gt;
&lt;p&gt;Two of the most common organizational structures for unit tests are the &amp;ldquo;Arrange-Act-Assert (AAA)” and the &amp;ldquo;Given-When-Then” structures:&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;testAppFeatureBehavior&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;// Arrange&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;sut&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;SystemUnderTest&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;// configure properties as necessary to prepare the system for the next step&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;c1&#34;&gt;// Act&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;sut&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;functionProducingObservableBehavior&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&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;// Assert&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;n&#34;&gt;XCTAssert&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(...&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;expectations&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;are&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;met&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;A similar pattern is the &amp;ldquo;Given-When-Then” structure:&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;testAppFeatureBehavior&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;// Given&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;sut&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;SystemUnderTest&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;// configure properties as necessary to prepare the system for the next step&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;c1&#34;&gt;// When&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;sut&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;functionProducingObservableBehavior&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&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;// Then&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;n&#34;&gt;XCTAssert&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(...&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;expectations&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;are&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;met&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;They&amp;rsquo;re exactly the same structures with different descriptive comments outlining the three phases of the test.&lt;/p&gt;
&lt;p&gt;The outline itself can help you look at the test and verify its trustworthiness as it pertains to its logical, clear order and organization.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;length&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;test-length&#34;&gt;Test Length&lt;/h3&gt;
&lt;p&gt;Another contributor to the trustworthiness of a given unit test is the number of lines of code it has.&lt;/p&gt;
&lt;p&gt;It&amp;rsquo;s been said that a 100% guaranteed bug-free line of code is the line of code that was never written in the first place!&lt;/p&gt;
&lt;p&gt;It makes sense that if we can keep our unit tests short and to the point, there will be a smaller chance for error than if the test body is extremely long.&lt;/p&gt;
&lt;p&gt;Small (&lt;strong&gt;&amp;lt;20ish&lt;/strong&gt; lines of code) tests are more-trusted than large tests.&lt;/p&gt;
&lt;p&gt;20 lines of code is sort of arbitrary, but can help alert you to when you might need to re-look at the test and apply DRY (Don&amp;rsquo;t Repeat Yourself) principles. Large test bodies may be a sign that there&amp;rsquo;s a possible opportunity to factor out common, repeated test code.&lt;/p&gt;
&lt;p&gt;Extracting out larger portions of code into smaller named functions may also help with the clarity of your unit test code, leading you to say with greater confidence, &amp;ldquo;I trust that this test is correct”.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;complexity&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;test-complexity&#34;&gt;Test Complexity&lt;/h3&gt;
&lt;p&gt;Unit tests should be as simple as possible. Reducing the complexity of a unit test results in clarity.&lt;/p&gt;
&lt;p&gt;Make sure there&amp;rsquo;s a clear path through the test case.&lt;/p&gt;
&lt;p&gt;Avoid branching logic (if-else statements) or looping constructs in your tests if at all possible. If you find the need to test two code paths of your system under test, write two tests for that, rather than applying if-else logic inside of a single unit test.&lt;/p&gt;
&lt;p&gt;If you&amp;rsquo;re using analytics, such as &lt;a href=&#34;https://en.wikipedia.org/wiki/Cyclomatic_complexity&#34;&gt;cyclomatic complexity&lt;/a&gt;, absolutely make sure your unit tests have as low of a cyclomatic complexity as possible.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;semantics&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;test-semantics&#34;&gt;Test Semantics&lt;/h3&gt;
&lt;p&gt;Applying simple clean coding practices to your unit tests can also go a long way in terms of being able to trust your tests.&lt;/p&gt;
&lt;p&gt;Use descriptive and meaningful phrases (DAMP) in your test names and variables. Be sure to update them as the semantics of your system under test change. This will provide a consistency and accuracy to the &lt;em&gt;names&lt;/em&gt; of things that will lead to a greater confidence that your tests are giving you an accurate picture of the health of your system.&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/2014/12/10/dont-write-legacy-swift/&#34; title=&#34;Don’t Write Legacy Swift&#34;&gt;Don’t Write Legacy 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/tag/unit-testing/&#34; title=&#34;Unit Testing @ andrewcbancroft.com&#34;&gt;Unit Testing @ andrewcbancroft.com&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/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;i class=&#34;fa fa-angle-right&#34;&gt;&lt;/i&gt; &lt;a href=&#34;https://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;/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>Considerations for Choosing 3rd Party Swift Libraries</title>
      <link>https://www.andrewcbancroft.com/2015/10/27/considerations-for-choosing-3rd-party-swift-libraries/</link>
      <pubDate>Tue, 27 Oct 2015 18:04:09 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2015/10/27/considerations-for-choosing-3rd-party-swift-libraries/</guid>
      <description>&lt;p&gt;While relying on 3rd party dependencies can provide you the benefit of not having to spend time implementing a portion of your app, realize that you’re essentially giving away little pieces of your app when you bring in a dependency.&lt;/p&gt;
&lt;p&gt;You’re delegating away a certain level of control off to someone else who has no knowledge of or interest in the final outcome of your team’s app.&lt;/p&gt;
&lt;p&gt;Adding dependencies to your project doesn’t come without cost, so it’s best to count that cost up front in order to make sure it’s a good idea to pursue this option.&lt;/p&gt;
&lt;p&gt;If you are going to seek out modules of code from a 3rd party, what are some key assessment factors that could help you in this important decision?&lt;/p&gt;
&lt;p&gt;Josh Brown wrote a &lt;a href=&#34;http://roadfiresoftware.com/2015/08/save-your-future-self-from-broken-apps/&#34;&gt;&lt;em&gt;really&lt;/em&gt; comprehensive list of questions&lt;/a&gt; that guided me a lot in my own considerations of choosing a 3rd party library. I&amp;rsquo;m really grateful for his article – if you&amp;rsquo; haven&amp;rsquo;t read it, go check it out!&lt;/p&gt;
&lt;p&gt;My own list that follows takes a more categorical approach for those of us who think in box-like structures. I thought of at least four broad categories that we should think about and ask questions about when it comes to choosing our dependencies.&lt;/p&gt;
&lt;p&gt;How can you avoid depending on bad Swift libraries? What are some markers of good ones? Let&amp;rsquo;s find out!&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;community&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;community-matters&#34;&gt;Community matters&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;A good library has a good community surrounding it. Try using your favorite search engine to see what kind of content comes up for the dependency you’re thinking about. Search on StackOverflow to see what questions are being asked and what issues folks are running into, and what kinds of answers (if any) are being given.&lt;/p&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Take a look at the GitHub stars and forks counts. Stars are a good way for developers to flag repositories that are interesting to them. Forks indicate potential contributors to the repository, since the typical flow is to fork the project, branch, publish the branch, and submit a pull request.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;“Buzz” and “popularity” can come and go, but if folks are talking about the dependency you’re considering, that’s not a terrible sign. Unless what they’re talking about all the problems the library has, haha. In all seriousness, though, utilize social media and observe / ask around to see what other developers are saying about it before bringing it into your app.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;a name=&#34;maintenance&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;maintenance-matters&#34;&gt;Maintenance matters&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;When was the last commit to the repository? If it was 5 years ago, this could be a red flag. Sure, it could just mean that the library is “done” and works great. But you and I both know that as the world of software moves forward, change is inevitable, and a library that doesn’t change with the times is more than likely doomed to failure. Checking the pulse of the repository on GitHub is one way to determine how well-maintained the library is.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;How many issues are open? How many are being closed as resolved? Repositories with a lot of issues open could indicate an active community… unless those issues never get closed. In which case the indicator is a less positive one. Once again, GitHub’s Pulse feature will be a good guide even on figuring out how the repository owner is responding to issues that come their way.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;a name=&#34;documentation&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;documentation-matters&#34;&gt;Documentation matters&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;If a library has no documentation to speak of, how are we as developers supposed to figure out how to use it? The better and more exhaustive the documentation, the better the chances are that the library is worth surviving the candidate list to incorporate into your project&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;Even better is when the repository owner provides examples of how to use their library. Seeing examples will give you an idea of what’s possible, how nice the API is, and is overall a good reference point for integrating it with your own app. Documentation without examples is less valuable than documentation &lt;em&gt;with&lt;/em&gt; examples.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;a name=&#34;quality&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;quality-matters&#34;&gt;Quality matters&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;If you&amp;rsquo;re using CocoaPods, know that each CocoaPod that’s submitted to the central ‘Trunk’, as they call it, receives a quality index that is based on various pieces of analysis that can be done on the repository and on the code within the pod itself. Some of the analysis that’s done is based on some of the things I just discussed, such as GitHub stars, documentation, etc. A full list of how the CocoaPods quality index is assessed and assigned can be found at &lt;a href=&#34;https://guides.cocoapods.org/making/quality-indexes&#34;&gt;https://guides.cocoapods.org/making/quality-indexes&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;wrapping-up&#34;&gt;Wrapping up&lt;/h3&gt;
&lt;p&gt;So there you have it – a few broad categories to think in terms of when trying to get direction on whether or not you should bring a given 3rd party library into your own Swift project.&lt;/p&gt;
&lt;p&gt;Do you have other &amp;ldquo;box-like” categories that you use to guide your dependency decisions? Feel free to sound off in the comments with your thoughts!&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>Loading a Receipt for Validation with Swift</title>
      <link>https://www.andrewcbancroft.com/2015/10/13/loading-a-receipt-for-validation-with-swift/</link>
      <pubDate>Wed, 14 Oct 2015 03:24:02 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2015/10/13/loading-a-receipt-for-validation-with-swift/</guid>
      <description>&lt;p&gt;I&amp;rsquo;m working through a progression of entries on the process of validating receipts with OpenSSL for iOS in Swift.&lt;/p&gt;
&lt;p&gt;Just want the code? Here you go!&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;fab fa-github fa-lg&#34;&gt;&lt;/i&gt; &lt;a href=&#34;https://github.com/andrewcbancroft/SwiftyLocalReceiptValidator&#34;&gt;Swifty Local Receipt Validator&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;
&lt;p&gt;To-date, I&amp;rsquo;ve explained how to &lt;a href=&#34;https://www.andrewcbancroft.com/2015/09/21/openssl-for-ios-swift-the-easy-way/&#34; title=&#34;OpenSSL for iOS &amp;amp; Swift the Easy Way&#34;&gt;get OpenSSL into your project (the easy way)&lt;/a&gt;, and I&amp;rsquo;ve walked through how to &lt;a href=&#34;https://www.andrewcbancroft.com/2015/10/05/preparing-to-test-receipt-validation-for-ios/&#34; title=&#34;Preparing to Test Receipt Validation for iOS&#34;&gt;prepare to test receipt validation&lt;/a&gt;, including how to set everything up in the Apple Developer member center, and in iTunes Connect.&lt;/p&gt;
&lt;p&gt;There are at least 5 steps to validate a receipt, as the &lt;a href=&#34;https://developer.apple.com/library/ios/releasenotes/General/ValidateAppStoreReceipt/Chapters/ValidateLocally.html#//apple_ref/doc/uid/TP40010573-CH1-SW2&#34;&gt;Receipt Validation Programming Guide&lt;/a&gt; outlines:&lt;/p&gt;
&lt;p&gt;1 – Locate the receipt.&lt;br&gt;
If no receipt is present, validation fails.&lt;/p&gt;
&lt;p&gt;2 – Verify that the receipt is properly signed by Apple.&lt;br&gt;
If it is not signed by Apple, validation fails.&lt;/p&gt;
&lt;p&gt;3 – Verify that the bundle identifier in the receipt matches a hard-coded constant containing the CFBundleIdentifier value you expect in the Info.plist file.&lt;br&gt;
If they do not match, validation fails.&lt;/p&gt;
&lt;p&gt;4 – Verify that the version identifier string in the receipt matches a hard-coded constant containing the CFBundleShortVersionString value (for macOS) or the CFBundleVersion value (for iOS) that you expect in the Info.plist file.&lt;br&gt;
If they do not match, validation fails.&lt;/p&gt;
&lt;p&gt;5 – Compute the hash of the GUID as described in Compute the Hash of the GUID.&lt;br&gt;
If the result does not match the hash in the receipt, validation fails.&lt;/p&gt;
&lt;p&gt;The thought I had when I saw 5 steps is, &amp;ldquo;This is going to become too much responsibility for a single Type to handle”. I easily got overwhelmed when I analyzed the &lt;a href=&#34;https://www.objc.io/issues/17-security/receipt-validation/&#34;&gt;most extensive write-up on the subject, found at Objc.io&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;organization-strategy&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;validation-organization-strategy-overview&#34;&gt;Validation organization strategy overview&lt;/h3&gt;
&lt;p&gt;To help keep my head wrapped around this process, I&amp;rsquo;ve developed a strategy that has kept me sane so far. It incorporates 3 components:&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;receipt-validator&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;1--receiptvalidator&#34;&gt;1 – ReceiptValidator&lt;/h4&gt;
&lt;p&gt;First, I&amp;rsquo;ve created a top-level Type called &lt;code&gt;ReceiptValidator&lt;/code&gt;. My idea is to have a single method, &lt;code&gt;validateReceipt()&lt;/code&gt; that will either run and succeed, or start propagating errors.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;separate-types&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;2--map-validation-steps-to-separate-types&#34;&gt;2 – Map validation steps to separate Types&lt;/h4&gt;
&lt;p&gt;Second, I&amp;rsquo;ve tried to take each of the steps involved in validating the receipt and create a simple Swift Type to encapsulate the logic that needs to happen in that step.&lt;/p&gt;
&lt;p&gt;So when step 1 says to &amp;ldquo;locate and load the receipt”, I created a struct called &lt;code&gt;ReceiptLoader&lt;/code&gt; that has two methods: &lt;code&gt;receiptFound()&lt;/code&gt; and &lt;code&gt;loadReceipt()&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;ReceiptValidator&lt;/code&gt; currently holds references to instances of each of these little helper Types, and the validator class itself calls methods on those instances to get the overall job of validating the receipt done.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;throw-errors&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;3--throw-errors-when-a-validation-step-fails&#34;&gt;3 – Throw errors when a validation step fails&lt;/h4&gt;
&lt;p&gt;Third, whenever some step along the way fails, I&amp;rsquo;m utilizing Swift&amp;rsquo;s error handling features. I&amp;rsquo;ve created an enum called &lt;code&gt;ReceiptValidationError&lt;/code&gt; with various descriptive values. Whenever a validation error occurs, one of the values in the &lt;code&gt;ReceiptValidationError&lt;/code&gt; enum is thrown.&lt;/p&gt;
&lt;p&gt;The enum&amp;rsquo;s definition is simple right now, but it will grow as time goes on with various other error conditions related to receipt validation (and why validation might fail):&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;enum&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;ReceiptValidationError&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&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;couldNotFindReceipt&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This enum simply implements the &lt;code&gt;Error&lt;/code&gt; &amp;ldquo;marker” protocol, which allows its values to be used in Swift&amp;rsquo;s error-throwing system. For this blog entry, we&amp;rsquo;ll stick with simply throwing the value &lt;code&gt;couldNotFindReceipt&lt;/code&gt; whenever a receipt can&amp;rsquo;t be found and needs to be re-requested from the App Store.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;receipt-validator-implementation&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;receiptvalidator-implementation&#34;&gt;ReceiptValidator implementation&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;ReceiptValidator&lt;/code&gt; is where everything for validating receipts launches for me at this point. Calling a single method, &lt;code&gt;validateReceipt()&lt;/code&gt; will kick off the 5+ step process that Apple describes.&lt;/p&gt;
&lt;p&gt;The first thing that needs to happen is to load a receipt that&amp;rsquo;ll be validated. If a receipt is not found on the device, a new receipt needs to be requested from the App Store.&lt;/p&gt;
&lt;p&gt;I mentioned &lt;code&gt;ReceiptLoader&lt;/code&gt; in the overview. An implementation will follow, but we&amp;rsquo;ll let this instance do the locating and loading of the receipt.&lt;/p&gt;
&lt;p&gt;With that architecture in mind, here&amp;rsquo;s what &lt;code&gt;ReceiptValidator&lt;/code&gt; looks like right now:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;12
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;13
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;14
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;15
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;16
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;17
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;18
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;19
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;enum&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;ReceiptValidationResult&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;cl&#34;&gt;    &lt;span class=&#34;k&#34;&gt;case&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;success&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;error&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;ReceiptValidationError&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;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;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;struct&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;ReceiptValidator&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;receiptLoader&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ReceiptLoader&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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;validateReceipt&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;ReceiptValidationResult&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;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;cl&#34;&gt;            &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;receiptData&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;try&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;receiptLoader&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;loadReceipt&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;cl&#34;&gt;            
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;            &lt;span class=&#34;c1&#34;&gt;// Continue validation steps with receiptData if error not thrown&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;success&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;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;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;k&#34;&gt;as&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;!&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ReceiptValidationError&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;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;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;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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;So the validator simply lets the &lt;code&gt;ReceiptLoader&lt;/code&gt; instance do it&amp;rsquo;s loading job. If it doesn&amp;rsquo;t return any data containing a receipt to work with, the validator will catch the error and return the &lt;code&gt;.error&lt;/code&gt; &lt;code&gt;ReceiptValidationResult&lt;/code&gt; case with the error cast to a &lt;code&gt;ReceiptValidationError&lt;/code&gt; as an associated value.&lt;/p&gt;
&lt;p&gt;The View Controller is what calls &lt;code&gt;validateReceipt()&lt;/code&gt;, so it will be waiting to deal with the &lt;code&gt;ReceiptValidationResult&lt;/code&gt; that&amp;rsquo;s returned by the &lt;code&gt;ReceiptValidator&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;receipt-loader-implementation&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;receiptloader-implementation&#34;&gt;ReceiptLoader implementation&lt;/h3&gt;
&lt;p&gt;The &lt;code&gt;ReceiptLoader&lt;/code&gt; has the sole responsibility of going to receipt storage location on a user&amp;rsquo;s device and attempting to discover the receipt and pull out the contents of that file in the form of a &lt;code&gt;Data&lt;/code&gt; instance if it&amp;rsquo;s there.&lt;/p&gt;
&lt;p&gt;Here&amp;rsquo;s the implementation with explanation to follow:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;12
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;13
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;14
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;15
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;16
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;17
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;18
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;19
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;20
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;21
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;22
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;23
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;24
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;25
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;26
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;struct&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;ReceiptLoader&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;receiptUrl&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Bundle&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;main&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;appStoreReceiptURL&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;loadReceipt&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;kr&#34;&gt;throws&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Data&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;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;receiptFound&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;cl&#34;&gt;            &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;receiptData&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;try&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;?&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Data&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;contentsOf&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;receiptUrl&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;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;receiptData&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;receiptData&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;cl&#34;&gt;                &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;receiptData&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;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;cl&#34;&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;throw&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ReceiptValidationError&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;couldNotFindReceipt&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;n&#34;&gt;fileprivate&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;receiptFound&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;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;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;isReachable&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;try&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;receiptUrl&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;?.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;checkResourceIsReachable&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;cl&#34;&gt;                &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;isReachable&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;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;kc&#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;cl&#34;&gt;            &lt;span class=&#34;k&#34;&gt;return&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;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;cl&#34;&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;false&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The &lt;code&gt;loadReceipt()&lt;/code&gt; method will do the job and either return a &lt;code&gt;Data&lt;/code&gt; instance with the receipt contents that can be extracted and parsed in later steps, or it will throw the &lt;code&gt;ReceiptValidationError.couldNotFindReceipt&lt;/code&gt; enum value.&lt;/p&gt;
&lt;p&gt;The rest of the implementation is all around making sure the receipt is there and accessible by utilizing standard &lt;code&gt;URL&lt;/code&gt; and &lt;code&gt;Data&lt;/code&gt; methods.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;view-controller-implementation&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;viewcontroller-implementation&#34;&gt;ViewController implementation&lt;/h3&gt;
&lt;p&gt;The View Controller is the kick-off point of all-things receipt validation. To have some code in front of us, take a look at this implementation. Explanatory details are below:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;12
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;13
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;14
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;15
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;16
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;17
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;18
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;19
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;20
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;21
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;22
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;23
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;24
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;25
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;26
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;27
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;28
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;29
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;30
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;31
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;32
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;33
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;34
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;35
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;36
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;37
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;public&lt;/span&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;SKRequestDelegate&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;receiptValidator&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ReceiptValidator&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;receiptRequest&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;SKReceiptRefreshRequest&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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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; View Controller Life Cycle Methods&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;public&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;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;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;receiptRequest&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&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;        &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;validationResult&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;receiptValidator&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;validateReceipt&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;cl&#34;&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;validationResult&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;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;success&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;cl&#34;&gt;            &lt;span class=&#34;c1&#34;&gt;// Enable app features&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;error&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;error&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;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;n&#34;&gt;error&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;cl&#34;&gt;            &lt;span class=&#34;n&#34;&gt;receiptRequest&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;start&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;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;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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;requestDidFinish&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;request&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;SKRequest&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;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;cl&#34;&gt;            &lt;span class=&#34;k&#34;&gt;try&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;receiptValidator&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;validateReceipt&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;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;cl&#34;&gt;            &lt;span class=&#34;c1&#34;&gt;// Log unsuccessful attempt and optionally begin grace period&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;            &lt;span class=&#34;c1&#34;&gt;// before disabling app functionality, or simply disable features&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;request&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;request&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;SKRequest&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;didFailWithError&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;cl&#34;&gt;        &lt;span class=&#34;c1&#34;&gt;// debug error condition&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;n&#34;&gt;error&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;localizedDescription&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;cl&#34;&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;c1&#34;&gt;// Log unsuccessful attempt and optionally begin grace period&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;c1&#34;&gt;// before disabling app functionality, or simply disable features&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;When the app starts and the controller has loaded, it will prepare itself in a couple of ways:&lt;/p&gt;
&lt;p&gt;First, it will initialize a &lt;code&gt;ReceiptValidator&lt;/code&gt; and an &lt;code&gt;SKReceiptRefreshRequest&lt;/code&gt; (in case a receipt isn&amp;rsquo;t present on the device).&lt;/p&gt;
&lt;p&gt;Subtle but important, the &lt;code&gt;SKReceiptRefreshRequest&lt;/code&gt; instance&amp;rsquo;s delegate is set to the view controller itself in &lt;code&gt;viewDidLoad()&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Control is then handed over to the &lt;code&gt;ReceiptValidator&lt;/code&gt; instance to begin its multi-step process (of which we&amp;rsquo;ve got step 1 down up to this point).&lt;/p&gt;
&lt;p&gt;The View Controller acts as the main error handler for now. If a receipt couldn&amp;rsquo;t be found, signaled by the throwing of &lt;code&gt;ReceiptValidationError.couldNotFindReceipt&lt;/code&gt; by the validator, the receipt refresh request is started.&lt;/p&gt;
&lt;p&gt;The View Controller also acts as the &lt;code&gt;SKRequestDelegate&lt;/code&gt;, and thus gets called back whenever the request finishes (or fails with an error).&lt;/p&gt;
&lt;p&gt;If receipt validation fails once the receipt is downloaded to the device, &lt;a href=&#34;https://developer.apple.com/library/ios/releasenotes/General/ValidateAppStoreReceipt/Chapters/ValidateLocally.html#//apple_ref/doc/uid/TP40010573-CH1-SW2&#34;&gt;Apple recommends&lt;/a&gt; that in iOS, we &lt;em&gt;do not attempt to terminate the app&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;Rather, they recommend logging that the receipt validation failed and then initiating a grace period, or disabling functionality in your app, depending on the situation.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;upcoming-hurdles&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;upcoming-hurdles&#34;&gt;Upcoming hurdles&lt;/h3&gt;
&lt;p&gt;That about wraps up locating and loading the receipt. The &lt;em&gt;real&lt;/em&gt; challenges of using OpenSSL to extract the receipt, verify its authenticity, parse it, and more are still ahead. I&amp;rsquo;ll be sure to chronicle my journey as I jump those hurdles. Stay tuned!&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/10/05/preparing-to-test-receipt-validation-for-ios/&#34; title=&#34;Preparing to Test Receipt Validation for iOS&#34;&gt;Preparing to Test Receipt Validation for iOS&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/09/21/openssl-for-ios-swift-the-easy-way/&#34; title=&#34;OpenSSL for iOS &amp;#038; Swift the Easy Way&#34;&gt;OpenSSL for iOS &amp; Swift the Easy Way&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/06/09/extracting-a-pkcs7-container-for-receipt-validation-with-swift/&#34; title=&#34;Extracting a PKCS7 Container for Receipt Validation with Swift&#34;&gt;Extracting a PKCS7 Container for Receipt Validation with 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/2017/07/16/receipt-validation-verifying-a-receipt-signature-in-swift/&#34; title=&#34;Receipt Validation – Verifying a Receipt Signature in Swift&#34;&gt;Receipt Validation – Verifying a Receipt Signature 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/2017/07/27/receipt-validation-parsing-a-receipt-with-swift/&#34; title=&#34;Receipt Validation – Parse and Decode a Receipt with Swift&#34;&gt;Receipt Validation – Parse and Decode a Receipt with 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/2017/07/31/finalizing-receipt-validation-in-swift-computing-a-guid-hash/&#34; title=&#34;Finalizing Receipt Validation in Swift – Computing a GUID Hash&#34;&gt;Finalizing Receipt Validation in Swift – Computing a GUID Hash&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>Preparing to Test Receipt Validation for iOS</title>
      <link>https://www.andrewcbancroft.com/2015/10/05/preparing-to-test-receipt-validation-for-ios/</link>
      <pubDate>Mon, 05 Oct 2015 18:25:09 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2015/10/05/preparing-to-test-receipt-validation-for-ios/</guid>
      <description>&lt;p&gt;After having to piece together each step along the path of preparing to test receipt validation for iOS apps, I&amp;rsquo;ve decided to combine everything into the following guide. Whether you&amp;rsquo;re working to implement receipt validation for a new iOS app, or for an existing one, this walk-through should provide guidance to get you ready to work with receipts in your iOS application.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;app-record-setup&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;itunes-connect-app-record-setup&#34;&gt;iTunes Connect App Record Setup&lt;/h3&gt;
&lt;h5 id=&#34;1--log-in-to-the-apple-developer-member-center&#34;&gt;1 – Log in to the Apple Developer Member Center.&lt;/h5&gt;
&lt;h5 id=&#34;2--make-sure-you-have-a-bundle-identifier-set-up-for-your-app&#34;&gt;2 – Make sure you have a bundle identifier set up for your app.&lt;/h5&gt;
&lt;p&gt;If this is an existing app you&amp;rsquo;re wanting to add receipt validation to, you&amp;rsquo;re already set. If this is a brand new, un-released app, you need to set up a bundle identifier first.&lt;/p&gt;
&lt;p&gt;2a) Click on Certificates, Identifiers &amp;amp; Profiles:&lt;br&gt;
&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/10/manage-bundle-identifier.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/10/manage-bundle-identifier-1024x296.png&#34; alt=&#34;Manage bundle identifier&#34; width=&#34;1024&#34; height=&#34;296&#34; class=&#34;alignnone size-large wp-image-12325&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/10/manage-bundle-identifier-1024x296.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2015/10/manage-bundle-identifier-300x87.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2015/10/manage-bundle-identifier.png 1102w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;2b) Click on Identifiers:&lt;br&gt;
&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/10/identifiers.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/10/identifiers.png&#34; alt=&#34;Identifiers&#34; width=&#34;1005&#34; height=&#34;455&#34; class=&#34;alignnone size-full wp-image-12326&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/10/identifiers.png 1005w, https://www.andrewcbancroft.com/wp-content/uploads/2015/10/identifiers-300x136.png 300w&#34; sizes=&#34;(max-width: 1005px) 100vw, 1005px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;2c) Add a new App ID that&amp;rsquo;s appropriate for your app:&lt;br&gt;
&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/10/add-app-id.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/10/add-app-id.png&#34; alt=&#34;Add app id&#34; width=&#34;1021&#34; height=&#34;495&#34; class=&#34;alignnone size-full wp-image-12327&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/10/add-app-id.png 1021w, https://www.andrewcbancroft.com/wp-content/uploads/2015/10/add-app-id-300x145.png 300w&#34; sizes=&#34;(max-width: 1021px) 100vw, 1021px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h5 id=&#34;3--log-into-itunes-connect&#34;&gt;3 – Log into iTunes Connect.&lt;/h5&gt;
&lt;h5 id=&#34;4--click-my-apps8221-from-the-main-itunes-connect-dashboard-page&#34;&gt;4 – Click &amp;ldquo;My Apps” from the main iTunes Connect dashboard page.&lt;/h5&gt;
&lt;p&gt;&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/10/itunes-connect-my-apps.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/10/itunes-connect-my-apps-1024x495.png&#34; alt=&#34;iTunes Connect - My Apps&#34; width=&#34;1024&#34; height=&#34;495&#34; class=&#34;alignnone size-large wp-image-12331&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/10/itunes-connect-my-apps-1024x495.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2015/10/itunes-connect-my-apps-300x145.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2015/10/itunes-connect-my-apps.png 1040w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h5 id=&#34;5--click-the--button-to-add-your-app-into-itunes-connect&#34;&gt;5 – Click the + button to add your app into iTunes Connect.&lt;/h5&gt;
&lt;p&gt;&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/10/add-app.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/10/add-app.png&#34; alt=&#34;Add App&#34; width=&#34;454&#34; height=&#34;252&#34; class=&#34;alignnone size-full wp-image-12332&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/10/add-app.png 454w, https://www.andrewcbancroft.com/wp-content/uploads/2015/10/add-app-300x167.png 300w&#34; sizes=&#34;(max-width: 454px) 100vw, 454px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;sandbox-tester-setup&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;sandbox-tester-setup&#34;&gt;Sandbox Tester Setup&lt;/h3&gt;
&lt;h4 id=&#34;1--create-a-new-e-mail-address-thats-not-associated-with-an-apple-id-already&#34;&gt;1 – Create a new e-mail address that&amp;rsquo;s not associated with an Apple ID already&lt;/h4&gt;
&lt;p&gt;Your sandbox user account is actually going to be a separate Apple ID. Since it&amp;rsquo;s probably going to be &lt;em&gt;you&lt;/em&gt; that&amp;rsquo;s testing the purchasing and receipt features of your app, you will need to create a new e-mail address to associate with a new Apple ID, since Apple does not allow you to use an e-mail address already associated with an existing Apple ID.&lt;/p&gt;
&lt;h5 id=&#34;2--log-in-to-itunes-connect-if-youre-not-still-logged-in-from-the-previous-steps&#34;&gt;2 – Log in to iTunes Connect if you&amp;rsquo;re not still logged in from the previous steps&lt;/h5&gt;
&lt;h5 id=&#34;3--click-on-users-and-roles&#34;&gt;3 – Click on Users and Roles&lt;/h5&gt;
&lt;p&gt;&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/10/users-and-roles.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/10/users-and-roles-1024x445.png&#34; alt=&#34;iTunes Connect - Users and Roles&#34; width=&#34;1024&#34; height=&#34;445&#34; class=&#34;alignnone size-large wp-image-12335&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/10/users-and-roles-1024x445.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2015/10/users-and-roles-300x130.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2015/10/users-and-roles.png 1165w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h5 id=&#34;4--click-sandbox-testers&#34;&gt;4 – Click Sandbox Testers&lt;/h5&gt;
&lt;p&gt;&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/10/sandbox-testers1.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/10/sandbox-testers1.png&#34; alt=&#34;Sandbox Testers&#34; width=&#34;762&#34; height=&#34;177&#34; class=&#34;alignnone size-full wp-image-12337&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/10/sandbox-testers1.png 762w, https://www.andrewcbancroft.com/wp-content/uploads/2015/10/sandbox-testers1-300x70.png 300w&#34; sizes=&#34;(max-width: 762px) 100vw, 762px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h5 id=&#34;5--click-the-add--button&#34;&gt;5 – Click the Add (+) button&lt;/h5&gt;
&lt;h5 id=&#34;6--fill-out-all-of-the-information-on-this-page-and-use-the-new-e-mail-address-you-created-as-the-email-field-value&#34;&gt;6 – Fill out all of the information on this page, and use the new e-mail address you created as the Email field value.&lt;/h5&gt;
&lt;h5 id=&#34;7--save-the-new-sandbox-user&#34;&gt;7 – Save the new sandbox user.&lt;/h5&gt;
&lt;h5 id=&#34;8--you-will-receive-an-e-mail-from-apple-asking-you-to-verify-your-new-apple-id-verify-it&#34;&gt;8 – You will receive an e-mail from Apple asking you to verify your new Apple ID. Verify it.&lt;/h5&gt;
&lt;p&gt;&lt;a name=&#34;ios-device-setup&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;ios-device-setup&#34;&gt;iOS Device Setup&lt;/h3&gt;
&lt;h5 id=&#34;1--on-your-physical-device-not-in-the-simulator-open-settings-and-navigate-to-app-and-itunes-stores8221&#34;&gt;1 – On your physical device (not in the Simulator), open Settings and navigate to &amp;ldquo;App and iTunes Stores”.&lt;/h5&gt;
&lt;h5 id=&#34;2--sign-out-of-your-regular-apple-id-but-do-not-re-sign-in-using-your-new-sandbox-tester-apple-id-that-you-just-set-up-doing-this-will-pose-the-risk-of-invalidating-your-sandbox-user-test-account-this-is-explained-over-in-the-apple-developer-documentation&#34;&gt;2 – Sign out of your regular Apple ID, but &lt;em&gt;do not&lt;/em&gt; re-sign in using your new Sandbox Tester Apple ID that you just set up. Doing this will pose the risk of invalidating your Sandbox User test account. This is explained over in the &lt;a href=&#34;https://developer.apple.com/library/ios/documentation/LanguagesUtilities/Conceptual/iTunesConnect_Guide/Chapters/SettingUpUserAccounts.html#//apple_ref/doc/uid/TP40011225-CH25-SW9&#34;&gt;Apple Developer documentation&lt;/a&gt;:&lt;/h5&gt;
&lt;blockquote&gt;
&lt;p&gt;Important: If you mistakenly use a sandbox tester account to log in to a production environment on your test device instead of your test environment, the sandbox account becomes invalid and can’t be used again. If this happens, create a new sandbox tester account with a new email address.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Later on, when you run your app on your device from Xcode, you&amp;rsquo;ll get a prompt to sign in to the &lt;em&gt;fake&lt;/em&gt; App Store. You&amp;rsquo;ll enter the credentials you created for the Sandbox User test account into the prompt.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Ideally, this step is done on an iOS &lt;em&gt;test&lt;/em&gt; device, not on your own personal device. That being said, there are times (like in my own situation) where you might not have a test device and you&amp;rsquo;re forced to switch between Apple IDs to test.&lt;/p&gt;
&lt;h5 id=&#34;3--go-through-the-necessary-steps-to-set-up-a-new-itunes-store-account-youll-receive-a-welcome8221-e-mail-from-the-itunes-store-once-everything-is-completely-set-up&#34;&gt;3 – Go through the necessary steps to set up a new iTunes Store account. You&amp;rsquo;ll receive a &amp;ldquo;Welcome” e-mail from the iTunes Store once everything is completely set up.&lt;/h5&gt;
&lt;h3 id=&#34;write-some-receipt-verification-code&#34;&gt;Write Some Receipt Verification Code&lt;/h3&gt;
&lt;p&gt;As a quick example, consider the following class that will act as a &lt;code&gt;ReceiptFetcher&lt;/code&gt;. To test things out, you can simply create an instance of this class in your primary view controller, and call &lt;code&gt;fetchReceipt&lt;/code&gt; in the &lt;code&gt;viewDidLoad()&lt;/code&gt; method.&lt;/p&gt;
&lt;p&gt;You&amp;rsquo;ll be looking to make sure you&amp;rsquo;re prompted for iTunes Store credentials, and that you successfully receive a receipt (ie, &lt;code&gt;request(_:didFailWithError:)&lt;/code&gt; did not get called, but instead &lt;code&gt;requestDidFinish(_:)&lt;/code&gt; got called):&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Receipt Fetcher&lt;/strong&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;12
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;13
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;14
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;15
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;16
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;17
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;18
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;19
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;20
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;21
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;22
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;23
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;24
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;25
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;26
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;27
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;28
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;29
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;30
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;31
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;32
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;33
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;34
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;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;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;import&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;StoreKit&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;ReceiptFetcher&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;SKRequestDelegate&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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;receiptRefreshRequest&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;SKReceiptRefreshRequest&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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;init&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;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;kd&#34;&gt;init&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;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;receiptRefreshRequest&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&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;fetchReceipt&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;cl&#34;&gt;        &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;receiptUrl&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Bundle&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;main&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;appStoreReceiptURL&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;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;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;receiptFound&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;try&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;receiptUrl&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;?.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;checkResourceIsReachable&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;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;receiptFound&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;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;                    &lt;span class=&#34;n&#34;&gt;receiptRefreshRequest&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;start&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;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;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;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;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;Could not check for receipt presence for some reason... &lt;/span&gt;&lt;span class=&#34;si&#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;localizedDescription&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 class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;requestDidFinish&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;request&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;SKRequest&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;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;The request finished successfully&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;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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;request&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;request&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;SKRequest&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;didFailWithError&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;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;Something went wrong: &lt;/span&gt;&lt;span class=&#34;si&#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;localizedDescription&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 class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;View Controller&lt;/strong&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;12
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;public&lt;/span&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;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Note that an instance of ReceiptFetcher is kept at the class-level&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;receiptFetcher&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ReceiptFetcher&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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;public&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;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;cl&#34;&gt;        &lt;span class=&#34;c1&#34;&gt;// Do any additional setup after loading the view, typically from a nib.&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;receiptFetcher&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;fetchReceipt&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;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;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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Once again, note that the instance of &lt;code&gt;ReceiptFetcher&lt;/code&gt; is held at the view controller&amp;rsquo;s class-level scope. This is necessary to allow the delegate callback functions to be invoked. If you initialize the &lt;code&gt;ReceiptFetcher&lt;/code&gt; in &lt;code&gt;viewDidLoad&lt;/code&gt; without holding a reference at the class level, the instance will be deallocated prior to the delegate callback functions being invoked.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;build-run-on-device&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;debug-app-on-device&#34;&gt;Debug App on Device&lt;/h3&gt;
&lt;h5 id=&#34;1--run-your-app-from-xcode-on-the-device-youve-just-set-up&#34;&gt;1 – Run your app from Xcode on the device you&amp;rsquo;ve just set up.&lt;/h5&gt;
&lt;h5 id=&#34;2--executing-code-such-as-a-request-to-refresh-a-receipt-should-prompt-you-for-your-newly-created-sandbox-tester-credentials&#34;&gt;2 – Executing code, such as a request to refresh a receipt should prompt you for your newly created Sandbox Tester credentials.&lt;/h5&gt;
&lt;h5 id=&#34;3--enter-the-password-for-that-apple-id--you-should-receive-a-receipt&#34;&gt;3 – Enter the password for that Apple ID – you should receive a receipt!&lt;/h5&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/10/13/loading-a-receipt-for-validation-with-swift/&#34; title=&#34;Loading a Receipt for Validation with Swift&#34;&gt;Loading a Receipt for Validation with 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/2015/09/21/openssl-for-ios-swift-the-easy-way/&#34; title=&#34;OpenSSL for iOS &amp;#038; Swift the Easy Way&#34;&gt;OpenSSL for iOS &amp; Swift the Easy Way&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/06/09/extracting-a-pkcs7-container-for-receipt-validation-with-swift/&#34; title=&#34;Extracting a PKCS7 Container for Receipt Validation with Swift&#34;&gt;Extracting a PKCS7 Container for Receipt Validation with 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/2017/07/16/receipt-validation-verifying-a-receipt-signature-in-swift/&#34; title=&#34;Receipt Validation – Verifying a Receipt Signature in Swift&#34;&gt;Receipt Validation – Verifying a Receipt Signature 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/2017/07/27/receipt-validation-parsing-a-receipt-with-swift/&#34; title=&#34;Receipt Validation – Parse and Decode a Receipt with Swift&#34;&gt;Receipt Validation – Parse and Decode a Receipt with 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/2017/07/31/finalizing-receipt-validation-in-swift-computing-a-guid-hash/&#34; title=&#34;Finalizing Receipt Validation in Swift – Computing a GUID Hash&#34;&gt;Finalizing Receipt Validation in Swift – Computing a GUID Hash&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>OpenSSL for iOS and Swift the Easy Way</title>
      <link>https://www.andrewcbancroft.com/2015/09/21/openssl-for-ios-swift-the-easy-way/</link>
      <pubDate>Tue, 22 Sep 2015 04:39:28 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2015/09/21/openssl-for-ios-swift-the-easy-way/</guid>
      <description>&lt;p&gt;I&amp;rsquo;m currently working on outfitting an app I&amp;rsquo;m working on to be able to &lt;a href=&#34;https://developer.apple.com/library/ios/releasenotes/General/ValidateAppStoreReceipt/Chapters/ValidateLocally.html#//apple_ref/doc/uid/TP40010573-CH1-SW2&#34;&gt;validate receipts&lt;/a&gt; to verify purchases of the app.&lt;/p&gt;
&lt;p&gt;Little did I know, this adventure would introduce the need to understand how to use cryptography in order to work with the receipt.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;cryptography-library-needed&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;cryptography-library-needed&#34;&gt;Cryptography library needed&lt;/h2&gt;
&lt;p&gt;In order to even &amp;ldquo;open&amp;rdquo; the receipt itself, we&amp;rsquo;ve got to work with something called a &amp;ldquo;PKCS #7 container&amp;rdquo;. Once we get the container open, we need to be able to validate Apple&amp;rsquo;s certificate that they use to sign every purchase of every app.&lt;/p&gt;
&lt;p&gt;All of this requires the use of a cryptography library, and OpenSSL seems to be the common choice, due to its being open source.&lt;/p&gt;
&lt;p&gt;I found that figuring out what to do to get OpenSSL into my project was harder than I wanted it to be. It turns out that there are a few to do this, but I wanted the easiest to implement (and the easiest to maintain). The options I was able to identify are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Build the binaries for each platform yourself. To do this you&amp;rsquo;ve got to make sure you build for both the simulator and the device by running special config routines and using makefiles and what-not. It seemed error-prone, and I&amp;rsquo;m not confident enough in my ability to know whether or not I&amp;rsquo;ve done it right.&lt;/li&gt;
&lt;li&gt;Download a pre-built static library – however, this introduces a risk that I wasn&amp;rsquo;t really willing to take. How do I trust that pre-built library? If there&amp;rsquo;s a vulnerability there, how do I easily upgrade the library to the patched version? Every recommendation I&amp;rsquo;ve seen thus far says, &amp;ldquo;Always build your own static library, rather than download it already-built from somewhere&amp;rdquo;.&lt;/li&gt;
&lt;li&gt;Cocoapods. Simply put, this was the easiest route for me.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;a name=&#34;cocoapods&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;openssl-for-ios-with-cocoapods&#34;&gt;OpenSSL for iOS with Cocoapods&lt;/h2&gt;
&lt;p&gt;How easy is it to get started with OpenSSL for iOS with Cocoapods?&lt;/p&gt;
&lt;p&gt;Pretty easy.&lt;/p&gt;
&lt;p&gt;If you don&amp;rsquo;t have Cocoapods yet, you can head over to &lt;a href=&#34;http://cocoapods.org&#34;&gt;Cocoapods.org&lt;/a&gt; and follow their instructions for installing. It&amp;rsquo;s literally one command at the terminal:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;sudo gem install cocoapods&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Once Cocoapods is installed, open your project in Xcode and add a new empty file (File -&amp;gt; New File -&amp;gt; Other -&amp;gt; Empty) called Podfile.&lt;/p&gt;
&lt;p&gt;In its contents, you simply add a reference to the OpenSSL library:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-sh&#34; data-lang=&#34;sh&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;target &lt;span class=&#34;s1&#34;&gt;&amp;#39;NameOfYourApp&amp;#39;&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;do&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    pod &lt;span class=&#34;s1&#34;&gt;&amp;#39;OpenSSL&amp;#39;&lt;/span&gt;, &lt;span class=&#34;s1&#34;&gt;&amp;#39;~&amp;gt; 1.0&amp;#39;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Note that there are several ‘OpenSSL&amp;rsquo; Cocoapod specs out there to choose from. I originally tried one that looked like it was for iOS (OpenSSL-iOS), but I was never able to get Swift code to recognize the C functions and types.&lt;/p&gt;
&lt;p&gt;The one that seemed to still pull the source from openssl.org and build it when the pod is installed was simply named ‘OpenSSL&amp;rsquo; and I&amp;rsquo;ve verified that my Swift code can ‘see&amp;rsquo; the C code without running into ‘unresolved identifier&amp;rsquo; compiler errors.&lt;/p&gt;
&lt;p&gt;Once the Podfile is created and configured, save it and close Xcode. Then head to the Terminal.&lt;/p&gt;
&lt;p&gt;Navigate to your Xcode project folder where the Podfile is located and run &lt;code&gt;pod install&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Once it&amp;rsquo;s finished doing its thing (be patient… it took several minutes for me), you can open the new .xcworkspace file that Cocoapods created for you. You&amp;rsquo;ll have everything in your project configured to depend on the OpenSSL library through the new Pods project that&amp;rsquo;s been added to your Workspace.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;bridging-header&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;bridging-header&#34;&gt;Bridging header&lt;/h3&gt;
&lt;p&gt;In order to use OpenSSL with Swift, you&amp;rsquo;re going to need to create an Objective-C bridging header to access the functionality provided in the OpenSSL libraries.&lt;/p&gt;
&lt;p&gt;If you don&amp;rsquo;t already have an Objective-C bridging header, it&amp;rsquo;s simple to get one added automatically by Xcode for you:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Choose File -&amp;gt; New -&amp;gt; File&lt;/li&gt;
&lt;li&gt;Choose Source under iOS (on the left&lt;/li&gt;
&lt;li&gt;Choose Objective-C File&lt;/li&gt;
&lt;li&gt;Name it &amp;ldquo;bridge&amp;rdquo; (or anything, really – it&amp;rsquo;s a dummy file that you&amp;rsquo;ll delete after Xcode generates the bridging header)&lt;/li&gt;
&lt;li&gt;Choose Next, and then Create&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Xcode will prompt you to create the bridging header – you should let it. Once it&amp;rsquo;s created, you can delete &amp;ldquo;bridge.m&amp;rdquo; (the Objective-C .m file that you just created in the steps above).&lt;/p&gt;
&lt;p&gt;Within the bridging header, you can insert some &lt;code&gt;#import&lt;/code&gt; statements to make the OpenSSL library components visible to your Swift project. For example, to start off, you could flesh out the bridging header with a couple of OpenSSL header files:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;#&lt;/span&gt;&lt;span class=&#34;kd&#34;&gt;import&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nc&#34;&gt;openssl&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;/&lt;/span&gt;&lt;span class=&#34;nc&#34;&gt;pkcs7&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nc&#34;&gt;h&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;#&lt;/span&gt;&lt;span class=&#34;kd&#34;&gt;import&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nc&#34;&gt;openssl&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;/&lt;/span&gt;&lt;span class=&#34;nc&#34;&gt;objects&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nc&#34;&gt;h&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;// Others that you may need in your Swift project&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;a name=&#34;xcode-7-bitcode&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;xcode-7--bitcode&#34;&gt;Xcode 7 &amp;amp; Bitcode&lt;/h3&gt;
&lt;p&gt;When you build and run your newly configured project in the Simulator, things are going to work fine.&lt;/p&gt;
&lt;p&gt;When you build it for your device, however, it will fail.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;…Pods/OpenSSL/lib/libcrypto.a(bio_lib.o)&amp;rsquo; does not contain bitcode. You must rebuild it with bitcode enabled (Xcode setting ENABLE_BITCODE), obtain an updated library from the vendor, or disable bitcode for this target. for architecture arm64&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;At the time of this writing, there is somthing in OpenSSL that doesn&amp;rsquo;t support Xcode 7&amp;rsquo;s Bitcode feature. Eventually (hopefully) they&amp;rsquo;ll fix it, but for now, if you want to use OpenSSL in a Swift project using Xcode 7, you&amp;rsquo;ll have to turn off Bitcode.&lt;/p&gt;
&lt;p&gt;Click on your project, and then click on your app target under ‘Targets&amp;rsquo; in the project settings window.&lt;/p&gt;
&lt;p&gt;Find Build Options and set Enable Bitcode to ‘No&#39;&lt;/p&gt;
&lt;p&gt;If you try re-building against the device with this setting set to ‘No&amp;rsquo;, it should build and run without issue.&lt;/p&gt;
&lt;h3 id=&#34;wrapping-up&#34;&gt;Wrapping up&lt;/h3&gt;
&lt;p&gt;That&amp;rsquo;s all there is to getting OpenSSL built and added as a reference in your app. &lt;em&gt;Now&lt;/em&gt; will come the fun part of using it with Swift, but I will save that for another entry.&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/10/05/preparing-to-test-receipt-validation-for-ios/&#34; title=&#34;Preparing to Test Receipt Validation for iOS&#34;&gt;Preparing to Test Receipt Validation for iOS&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/10/13/loading-a-receipt-for-validation-with-swift/&#34; title=&#34;Loading a Receipt for Validation with Swift&#34;&gt;Loading a Receipt for Validation with 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/06/09/extracting-a-pkcs7-container-for-receipt-validation-with-swift/&#34; title=&#34;Extracting a PKCS7 Container for Receipt Validation with Swift&#34;&gt;Extracting a PKCS7 Container for Receipt Validation with 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/2017/07/16/receipt-validation-verifying-a-receipt-signature-in-swift/&#34; title=&#34;Receipt Validation – Verifying a Receipt Signature in Swift&#34;&gt;Receipt Validation – Verifying a Receipt Signature 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/2017/07/27/receipt-validation-parsing-a-receipt-with-swift/&#34; title=&#34;Receipt Validation – Parse and Decode a Receipt with Swift&#34;&gt;Receipt Validation – Parse and Decode a Receipt with 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/2017/07/31/finalizing-receipt-validation-in-swift-computing-a-guid-hash/&#34; title=&#34;Finalizing Receipt Validation in Swift – Computing a GUID Hash&#34;&gt;Finalizing Receipt Validation in Swift – Computing a GUID Hash&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>Testability Tip for Swift Developers – Parameterize and Push</title>
      <link>https://www.andrewcbancroft.com/2015/09/04/testability-tip-for-swift-developers-parameterize-and-push/</link>
      <pubDate>Fri, 04 Sep 2015 18:36:52 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2015/09/04/testability-tip-for-swift-developers-parameterize-and-push/</guid>
      <description>&lt;p&gt;In a previous &amp;ldquo;Testability Tip for Swift Developers”, &lt;a href=&#34;https://www.andrewcbancroft.com/2015/04/15/testability-tip-for-swift-developers-public-over-private/&#34;&gt;I discussed the principle of observability&lt;/a&gt;. &amp;ldquo;If it&amp;rsquo;s observable, it&amp;rsquo;s testable” was the primary conclusion of the article, and I pointed toward using the &lt;code&gt;public&lt;/code&gt; access control modifier for parts of your app that you intend to test.&lt;/p&gt;
&lt;p&gt;In this edition, I&amp;rsquo;d like to introduce a new principle that I try to adhere to when I&amp;rsquo;m unit testing, namely, &amp;ldquo;If it&amp;rsquo;s controllable, it&amp;rsquo;s testable”. Here&amp;rsquo;s what I mean by &amp;ldquo;controllable”…&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;controllability&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;controllability&#34;&gt;Controllability&lt;/h3&gt;
&lt;p&gt;In &lt;a href=&#34;https://www.andrewcbancroft.com/2015/04/29/testing-is-to-software-as-experiment-is-to-science/&#34;&gt;Testing is to Software as Experiment is to Science&lt;/a&gt;, I analyzed how testing software mirrors scientific experimentation. Good scientific experiments are controllable. That is, they are set up such that everything stays as constant as possible except the thing you&amp;rsquo;re poking at.&lt;/p&gt;
&lt;p&gt;Changing multiple things at a time in an experiment clouds the ability to verify that [tweak x] produced [y outcome]. So a scientist will do his/her best to &lt;em&gt;control&lt;/em&gt; the environment by holding as many variables constant as possible, so that he/she can make accurate conclusions about the outcome.&lt;/p&gt;
&lt;p&gt;The same goes for testing software. If I&amp;rsquo;m going to automate a test, I want to set up my &amp;ldquo;experiment” such that I control as much of the system as possible so that I can set up valid expectations and verify results coming from the system under test. Note that I&amp;rsquo;m using the term &amp;ldquo;system” in a very broad sense – it could be referring to an entire app, a single &amp;ldquo;object”, or a function.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;parameterize-push&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;parameterize-and-push&#34;&gt;Parameterize and push&lt;/h3&gt;
&lt;p&gt;So where does &amp;ldquo;parameterize and push” come into play?&lt;/p&gt;
&lt;p&gt;Parameterizing sets us up with the ability to provide &lt;em&gt;inputs&lt;/em&gt; into the system we&amp;rsquo;re testing. Anytime you have an input, you have the ability to supply a value of your choosing.&lt;/p&gt;
&lt;p&gt;Serving as inputs to the system, you can view parameters as entry points for &lt;em&gt;controls&lt;/em&gt;. They&amp;rsquo;re &amp;ldquo;controls” because &lt;em&gt;we&lt;/em&gt; decide what those values should be before we pass them off as arguments &lt;em&gt;to&lt;/em&gt; those parameters. So long as the system we&amp;rsquo;re testing only gets the data it uses from its inputs (its parameters), we can be guaranteed predictable, controlled outputs on the other end.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;forms&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;forms-of-parameterization&#34;&gt;Forms of parameterization&lt;/h3&gt;
&lt;p&gt;Three forms of parameterization are common:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;At the instance level through initializers&lt;/li&gt;
&lt;li&gt;At the instance level through property setters&lt;/li&gt;
&lt;li&gt;At the function level through function parameters&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Using an initializer, or a &lt;code&gt;public&lt;/code&gt; variable property, or by adding parameters to your functions and using only those parameters for the function&amp;rsquo;s computation and output production, you give yourself the ability to control the system in various ways that are appropriate on a test-by-test basis.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;usefulness&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;usefulness-and-examples-of-parameterization&#34;&gt;Usefulness and examples of parameterization&lt;/h3&gt;
&lt;p&gt;Setting up your instance definitions to use a set of inputs from the very start through initialization gives you the ability to provide real &amp;ldquo;production-ready” values in your app, but fake &amp;ldquo;test-customized” values for testing. &lt;a href=&#34;https://www.andrewcbancroft.com/2014/07/15/how-to-create-mocks-and-stubs-in-swift/&#34;&gt;Creating fake objects&lt;/a&gt; for testing is outside the scope of this article, but providing public initializers with parameters is a really great way to set yourself up for being able to test that particular 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;c1&#34;&gt;// Prefer&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;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;DatabaseCommunicator&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;database&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Database&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;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;init&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;database&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Database&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;database&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;database&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;// able to supply a controlled input via parameter, such as supplying a &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;// customized &amp;#34;fake&amp;#34; database to use for testing but still supply a &amp;#34;real&amp;#34; database 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; 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;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;// over&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;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;DatabaseCommunicator&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;database&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Database&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;c1&#34;&gt;// stuck with talking to a real database...&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;&lt;p&gt;Another viable option is to provide public variable properties that can be set after the instance is initialized. This is a little more round-about, but I would still call it a form of &amp;ldquo;parameterization” because the strategy still provides you with the same control point that an initializer with parameters does.&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;// prefer&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;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;DatabaseCommunicator&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;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;database&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Database&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;// able to supply a controlled input via property setter, such as supplying a &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;// customized &amp;#34;fake&amp;#34; database to use for testing but still supply a &amp;#34;real&amp;#34; database 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; 6&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;init&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;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;database&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Database&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;p&#34;&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;// over&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;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;DatabaseCommunicator&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;database&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Database&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;c1&#34;&gt;// stuck with talking to a real database&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;At the function level, the usefulness of parameters is that you can supply inputs and examine outputs with ease. If you pull in data from the encapsulating instance inside the function body, say by referencing &lt;code&gt;self.somePropertyValue&lt;/code&gt;, you&amp;rsquo;ve got a bit more setup to do to be able to accurately verify results. &lt;code&gt;somePropertyValue&lt;/code&gt; needs to actually have a value before the function will produce accurate results. If you&amp;rsquo;d opted to simply define parameters for everything the function needs in order to produce its output, you can test that function in isolation far more easily and be guaranteed that your results are correct and accurate.&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;// prefer&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;getNameFromDatabase&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;database&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Database&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;String&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 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;database&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;getName&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;// able to supply a controlled input via parameter, such as supplying a &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;// customized &amp;#34;fake&amp;#34; database to use for testing but still supply a &amp;#34;real&amp;#34; database 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; 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;p&#34;&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;// over&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;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;getNameFromDatabase&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;String&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;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;database&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Database&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;c1&#34;&gt;// stuck with talking to a real database&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;13&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;database&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;getName&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;14&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;15&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;16&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;// and over&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;17&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;getNameFromDatabase&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;String&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;18&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;k&#34;&gt;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;database&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;getName&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;c1&#34;&gt;// requires additional setting of the database property on &amp;#39;self&amp;#39;&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;c1&#34;&gt;// before you&amp;#39;re able to get results from this function&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;a name=&#34;di&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;parameterization-or-dependency-injection8221&#34;&gt;Parameterization or &amp;ldquo;Dependency Injection”?&lt;/h3&gt;
&lt;p&gt;Yes.&lt;/p&gt;
&lt;p&gt;What I&amp;rsquo;m calling &amp;ldquo;parameterization” is really just &amp;ldquo;dependency injection”. But the term &amp;ldquo;dependency injection” can sound really daunting, while we&amp;rsquo;re used to working with parameters. I intend for the meaning of each term for the purpose of this article to be equivalent.&lt;/p&gt;
&lt;h3 id=&#34;wrapping-up&#34;&gt;Wrapping up&lt;/h3&gt;
&lt;p&gt;Parameterizing, your instance definitions and functions provides you an immense amount of leverage when it comes to controlling your system under test. I encourage you to try this out and do your best to shift to a more parameterized approach to writing your code for improved testability. Remember, &amp;ldquo;Controllable is testable”!&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/04/15/testability-tip-for-swift-developers-public-over-private/&#34; title=&#34;Testability Tip for Swift Developers – Public Over Private&#34;&gt;Testability Tip for Swift Developers – Public Over Private&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>Sharing a Core Data Model with a Swift Framework</title>
      <link>https://www.andrewcbancroft.com/2015/08/25/sharing-a-core-data-model-with-a-swift-framework/</link>
      <pubDate>Wed, 26 Aug 2015 04:04:05 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2015/08/25/sharing-a-core-data-model-with-a-swift-framework/</guid>
      <description>&lt;p&gt;&lt;small&gt;Updated on June 13, 2017 – Swift 3.0, Xcode 8&lt;/small&gt;&lt;/p&gt;
&lt;p&gt;Code re-use is a powerful thing, but it&amp;rsquo;s not always easy to pull off. We strive for it though, because in the long run, it makes maintaining the code far, far easier than if we just settled for copying and pasting.&lt;/p&gt;
&lt;p&gt;With the introduction of dynamic frameworks in iOS 8, a world of possibility opened up for iOS developers to achieve some pretty cool re-use scenarios, one of which we&amp;rsquo;re going to dive into today.&lt;/p&gt;
&lt;p&gt;Not only can we share &lt;em&gt;code&lt;/em&gt; between projects, we can also share Core Data &lt;em&gt;models&lt;/em&gt; between projects by utilizing frameworks!&lt;/p&gt;
&lt;p&gt;Perhaps you&amp;rsquo;re working on an iOS + Mac app combination and the data model for each is identical. Maybe you&amp;rsquo;re building several iOS apps that have different user interfaces but share some underlying persistence-layer models. Whatever the case may be, wouldn&amp;rsquo;t it be awesome to design the Core Data model once and share it between your projects?&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;example-scenario&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;example-scenario&#34;&gt;Example scenario&lt;/h3&gt;
&lt;p&gt;Our working example for this walkthrough will be the following:&lt;/p&gt;
&lt;p&gt;Our team is building an &lt;strong&gt;iOS app&lt;/strong&gt; and a &lt;strong&gt;Mac app&lt;/strong&gt; and the &lt;strong&gt;underlying data model will be exactly the same&lt;/strong&gt; between them. The only difference between the apps will be the target platform.&lt;/p&gt;
&lt;p&gt;The app will target car enthusiasts everywhere – we will empower car fanatics to manage of a list of their favorite cars.&lt;/p&gt;
&lt;p&gt;We&amp;rsquo;ll be creating two Xcode projects during this walkthrough: One will be the framework and will be called &amp;ldquo;CarKit”. The other will be a single view iOS application. We won&amp;rsquo;t actually dive into the Mac project, but one could imagine the process being very similar for importing CarKit into the Mac application when it came time to build that one.&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;m providing a completed CarKit + Carz package for you to look at over at GitHub:&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;fab fa-github fa-lg&#34;&gt;&lt;/i&gt; &lt;a href=&#34;https://github.com/andrewcbancroft/CoreDataFrameworkExample&#34; title=&#34;Core Data Framework Example&#34;&gt;CarKit + Carz Core Data Framework Example&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;
&lt;p&gt;Let&amp;rsquo;s get started!&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;create-swift-framework-project&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;creating-a-swift-framework-project&#34;&gt;Creating a Swift framework project&lt;/h3&gt;
&lt;p&gt;To get started with creating a Swift framework, begin with Xcode&amp;rsquo;s New Project dialog (File -&amp;gt; New -&amp;gt; Project…). Typically we stay in the realm of the &amp;ldquo;iOS Application” project templates, but if you click &amp;ldquo;Framework &amp;amp; library”, you&amp;rsquo;ll see the option to create a new Cocoa Touch Framework:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/08/01-New-Project-Framework.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/08/01-New-Project-Framework.png&#34; alt=&#34;01 - New Project - Framework&#34; width=&#34;778&#34; height=&#34;490&#34; class=&#34;alignnone size-full wp-image-12239&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/08/01-New-Project-Framework.png 778w, https://www.andrewcbancroft.com/wp-content/uploads/2015/08/01-New-Project-Framework-300x189.png 300w&#34; sizes=&#34;(max-width: 778px) 100vw, 778px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;In keeping with Apple&amp;rsquo;s &amp;ldquo;___Kit” theme, we&amp;rsquo;ll name our framework &amp;ldquo;CarKit”:&lt;br&gt;
&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/08/02-Project-Name.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/08/02-Project-Name.png&#34; alt=&#34;02 - Project Name&#34; width=&#34;760&#34; height=&#34;467&#34; class=&#34;alignnone size-full wp-image-12240&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/08/02-Project-Name.png 760w, https://www.andrewcbancroft.com/wp-content/uploads/2015/08/02-Project-Name-300x184.png 300w&#34; sizes=&#34;(max-width: 760px) 100vw, 760px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;add-data-model-file&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;add-a-data-model-file&#34;&gt;Add a data model file&lt;/h4&gt;
&lt;p&gt;Once the framework project has been created, we&amp;rsquo;re set to drop in a new Data Model file from Xcode&amp;rsquo;s new file dialog (File -&amp;gt; New -&amp;gt; File…). Choose Core Data, and then Data Model:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/08/03-New-File-Core-Data-Data-Model.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/08/03-New-File-Core-Data-Data-Model.png&#34; alt=&#34;03 - New File - Core Data - Data Model&#34; width=&#34;750&#34; height=&#34;444&#34; class=&#34;alignnone size-full wp-image-12241&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/08/03-New-File-Core-Data-Data-Model.png 750w, https://www.andrewcbancroft.com/wp-content/uploads/2015/08/03-New-File-Core-Data-Data-Model-300x178.png 300w&#34; sizes=&#34;(max-width: 750px) 100vw, 750px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Give the data model file a name that seems to fit your situation. For our example, let&amp;rsquo;s name it &amp;ldquo;CarModel”:&lt;br&gt;
&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/08/04-Model-Name.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/08/04-Model-Name.png&#34; alt=&#34;04 - Model Name&#34; width=&#34;748&#34; height=&#34;740&#34; class=&#34;alignnone size-full wp-image-12242&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/08/04-Model-Name.png 748w, https://www.andrewcbancroft.com/wp-content/uploads/2015/08/04-Model-Name-300x297.png 300w&#34; sizes=&#34;(max-width: 748px) 100vw, 748px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;add-model-attributes&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;add-model-attributes&#34;&gt;Add model attributes&lt;/h4&gt;
&lt;p&gt;Next it&amp;rsquo;s time to actually add attributes to the model. Since our theme is cars here, we&amp;rsquo;ll stick with three simple attributes: &lt;code&gt;year&lt;/code&gt;, &lt;code&gt;make&lt;/code&gt;, and &lt;code&gt;model&lt;/code&gt;:&lt;br&gt;
&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/08/05-Complete-Model.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/08/05-Complete-Model-1024x656.png&#34; alt=&#34;05 - Complete Model&#34; width=&#34;1024&#34; height=&#34;656&#34; class=&#34;alignnone size-large wp-image-12243&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/08/05-Complete-Model-1024x656.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2015/08/05-Complete-Model-300x192.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2015/08/05-Complete-Model.png 1399w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#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;h4 id=&#34;create-nsmanagedobject-subclass&#34;&gt;Create NSManagedObject subclass&lt;/h4&gt;
&lt;p&gt;With the model attributes all configured, it&amp;rsquo;s time to create an &lt;code&gt;NSManagedObject&lt;/code&gt; subclass. This will make consuming the model much easier in client applications. I&amp;rsquo;ve actually &lt;a href=&#34;http://www.andrewcbancroft.com/2014/07/17/implement-nsmanagedobject-subclass-in-swift/&#34;&gt;written up a full walk through on creating an NSManagedObject subclass in Swift&lt;/a&gt; as there are some nuances. Feel free to read up on that if you so desire!&lt;br&gt;
&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/08/06-NSManagedObject-Subclass.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/08/06-NSManagedObject-Subclass.png&#34; alt=&#34;06 - NSManagedObject Subclass&#34; width=&#34;517&#34; height=&#34;490&#34; class=&#34;alignnone size-full wp-image-12244&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/08/06-NSManagedObject-Subclass.png 517w, https://www.andrewcbancroft.com/wp-content/uploads/2015/08/06-NSManagedObject-Subclass-300x284.png 300w&#34; sizes=&#34;(max-width: 517px) 100vw, 517px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Be absolutely sure to mark your &lt;code&gt;NSManagedObject&lt;/code&gt; subclass and its properties &lt;code&gt;public&lt;/code&gt; – otherwise, the client app won&amp;rsquo;t be able to see the class or its properties:&lt;br&gt;
&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/08/061-NSManagedObjectSubclass-Public.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/08/061-NSManagedObjectSubclass-Public.png&#34; alt=&#34;061 - NSManagedObjectSubclass Public&#34; width=&#34;671&#34; height=&#34;351&#34; class=&#34;alignnone size-full wp-image-12255&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/08/061-NSManagedObjectSubclass-Public.png 671w, https://www.andrewcbancroft.com/wp-content/uploads/2015/08/061-NSManagedObjectSubclass-Public-300x157.png 300w&#34; sizes=&#34;(max-width: 671px) 100vw, 671px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;As I point out in &lt;a href=&#34;http://www.andrewcbancroft.com/2014/07/17/implement-nsmanagedobject-subclass-in-swift/&#34;&gt;Implement NSManagedObject Subclass in Swift&lt;/a&gt;, you need to set the Module property in the Data Model Inspector to be CarKit (which is the same as the &amp;ldquo;Current Project Module” option for this example):&lt;br&gt;
&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/08/07-Change-Entity-Class.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/08/07-Change-Entity-Class-1024x213.png&#34; alt=&#34;07 - Change Entity Class&#34; width=&#34;1024&#34; height=&#34;213&#34; class=&#34;alignnone size-large wp-image-12245&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/08/07-Change-Entity-Class-1024x213.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2015/08/07-Change-Entity-Class-300x62.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2015/08/07-Change-Entity-Class-768x160.png 768w, https://www.andrewcbancroft.com/wp-content/uploads/2015/08/07-Change-Entity-Class.png 1404w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;build-inspect-outputs&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;build-and-inspect-outputs&#34;&gt;Build and inspect outputs&lt;/h4&gt;
&lt;p&gt;We&amp;rsquo;ve got a framework, and we&amp;rsquo;ve got a Data Model with attributes and an &lt;code&gt;NSManagedObject&lt;/code&gt; subclass all appropriately implemented. Now it&amp;rsquo;s time to build the project and inspect the output of the build!&lt;/p&gt;
&lt;p&gt;Command + B to build, and then head up to the File menu and choose Project Settings:&lt;br&gt;
&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/08/08-Window-Projects.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/08/08-Window-Projects.png&#34; alt=&#34;08 - Window - Projects&#34; width=&#34;292&#34; height=&#34;492&#34; class=&#34;alignnone size-full wp-image-12246&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/08/08-Window-Projects.png 292w, https://www.andrewcbancroft.com/wp-content/uploads/2015/08/08-Window-Projects-178x300.png 178w&#34; sizes=&#34;(max-width: 292px) 100vw, 292px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;In the Project Settings area that appears, click the small gray arrow under the &amp;ldquo;Derived Data” section:&lt;br&gt;
&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/08/09-CarKit-Projects-Window.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/08/09-CarKit-Projects-Window.png&#34; alt=&#34;09 - CarKit - Projects Window&#34; width=&#34;612&#34; height=&#34;374&#34; class=&#34;alignnone size-full wp-image-12247&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/08/09-CarKit-Projects-Window.png 612w, https://www.andrewcbancroft.com/wp-content/uploads/2015/08/09-CarKit-Projects-Window-300x183.png 300w&#34; sizes=&#34;(max-width: 612px) 100vw, 612px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Next, find the derived data folder for your project. It should be named the same as your project with a set of random characters after. Sometimes there will be multiple folders that could be your project. You can look at the last modified date to help figure you figure out which one was most recently built and choose that one.&lt;/p&gt;
&lt;p&gt;Expand the Build folder down to Build -&amp;gt; Products -&amp;gt; Debug-iphonesimulator. There you should see the CarKit.framework artifact, and within it, everything that&amp;rsquo;s needed to be able to utilize the data model in a client application. Awesome!&lt;br&gt;
&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/08/10-CarKit-Finder-Includes-momd.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/08/10-CarKit-Finder-Includes-momd-1024x733.png&#34; alt=&#34;10 - CarKit - Finder - Includes momd&#34; width=&#34;1024&#34; height=&#34;733&#34; class=&#34;alignnone size-large wp-image-12248&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/08/10-CarKit-Finder-Includes-momd-1024x733.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2015/08/10-CarKit-Finder-Includes-momd-300x215.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2015/08/10-CarKit-Finder-Includes-momd-768x550.png 768w, https://www.andrewcbancroft.com/wp-content/uploads/2015/08/10-CarKit-Finder-Includes-momd.png 1064w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; This framework is not production-ready. It&amp;rsquo;s a little more involved to create a framework that one can run on a device / pass validation when submitting to the app store. The development of the framework remains the same, but the &lt;em&gt;build&lt;/em&gt; phases and procedures must be modified to make it &amp;ldquo;universal”. Rather than overly complicate this walkthrough, I recommend reviewing &amp;ldquo;&lt;a href=&#34;https://kodmunki.wordpress.com/2015/03/04/cocoa-touch-frameworks-for-ios8-remix/&#34;&gt;Universal Cocoa Touch Frameworks for iOS 8 – (Remix)&lt;/a&gt; by &lt;a href=&#34;https://twitter.com/kodmunki&#34;&gt;@kodmunki&lt;/a&gt; to create a &amp;ldquo;universal” framework capable of being run in the simulator and on iOS devices.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;create-framework-dependent-app&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;creating-the-framework-dependent-app&#34;&gt;Creating the framework-dependent app&lt;/h3&gt;
&lt;p&gt;With the framework built, it&amp;rsquo;s time to create the iOS app that will utilize that framework and its packaged assets! Begin a new project from File -&amp;gt; New -&amp;gt; Project and select &amp;ldquo;Single View Application”. I&amp;rsquo;ll name our example app &amp;ldquo;Carz”. &lt;strong&gt;Ensure that &amp;ldquo;Use Core Data” is selected&lt;/strong&gt; so that you get the boilerplate Core Data code put into your project by Xcode:&lt;br&gt;
&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/08/11-New-Single-View-App-Carz.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/08/11-New-Single-View-App-Carz.png&#34; alt=&#34;11 - New Single View App - Carz&#34; width=&#34;755&#34; height=&#34;492&#34; class=&#34;alignnone size-full wp-image-12249&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/08/11-New-Single-View-App-Carz.png 755w, https://www.andrewcbancroft.com/wp-content/uploads/2015/08/11-New-Single-View-App-Carz-300x195.png 300w&#34; sizes=&#34;(max-width: 755px) 100vw, 755px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;remove-xcode-generated-xcdatamodeld&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;remove-xcode-generated-xcdatamodeld-file&#34;&gt;Remove Xcode-generated .xcdatamodeld file&lt;/h4&gt;
&lt;p&gt;When you select &amp;ldquo;Use Core Data” in the project creation window, Xcode automatically generates some boilerplate code, which we want. But it also gives us a &amp;ldquo;Carz.xcdatamodeld” file, which we will not need because we&amp;rsquo;ll use the model that&amp;rsquo;s found in CarKit. Remove the &amp;ldquo;Carz.xcdatamodeld” file that Xcode provides for you:&lt;br&gt;
&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/08/12-Remove-default-data-model-from-new-project.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/08/12-Remove-default-data-model-from-new-project-1024x475.png&#34; alt=&#34;12 - Remove default data model from new project&#34; width=&#34;1024&#34; height=&#34;475&#34; class=&#34;alignnone size-large wp-image-12250&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/08/12-Remove-default-data-model-from-new-project-1024x475.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2015/08/12-Remove-default-data-model-from-new-project-300x139.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2015/08/12-Remove-default-data-model-from-new-project.png 1394w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;obtain-framework-bundle-identifier&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;obtain-framework-bundle-identifier&#34;&gt;Obtain framework Bundle Identifier&lt;/h4&gt;
&lt;p&gt;Speaking of using the CarKit Core Data model, we&amp;rsquo;re now ready to configure that piece of the app. To do this part, you will need to know the Bundle Identifier from your framework project. To find out what it is, jump back over to the framework project, click the top-level node in the project navigator, click on the framework target name, and look under the General tab of the project configuration. There you&amp;rsquo;ll find the Bundle Identifier, which you can copy to your clipboard:&lt;br&gt;
&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/08/13-CarKit-Bundle-Identifier.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/08/13-CarKit-Bundle-Identifier-1024x301.png&#34; alt=&#34;13 - CarKit Bundle Identifier&#34; width=&#34;1024&#34; height=&#34;301&#34; class=&#34;alignnone size-large wp-image-12251&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/08/13-CarKit-Bundle-Identifier-1024x301.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2015/08/13-CarKit-Bundle-Identifier-300x88.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2015/08/13-CarKit-Bundle-Identifier.png 1137w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;replace-managed-object-model-property&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;replace-managedobjectmodel-property-initialization&#34;&gt;Replace managedObjectModel property initialization&lt;/h4&gt;
&lt;p&gt;Out of the box, Xcode generates some code to help locate your Core Data model file. The boilerplate &lt;code&gt;managedObjectModel&lt;/code&gt; property looks like this:&lt;/p&gt;
&lt;p&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;5
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;kr&#34;&gt;lazy&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;var&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;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// The managed object model for the application. This property is not optional. It is a fatal error for the application not to be able to find and load its model.&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;modelURL&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;n&#34;&gt;URLForResource&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;DataModelFileName&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;withExtension&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;momd&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&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;NSManagedObjectModel&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;contentsOfURL&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;modelURL&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;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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
However, this won&amp;rsquo;t work for us, because we&amp;rsquo;re going to use the data model from CarKit, and CarKit is not in the &lt;code&gt;mainBundle()&lt;/code&gt;. This is why we jumped over and copied the Bundle Identifier for CarKit in the previous step. To locate the data model file in &lt;em&gt;that&lt;/em&gt; bundle, you&amp;rsquo;ll replace the &lt;code&gt;managedObjectModel&lt;/code&gt; initialization step to the following (for CarKit):&lt;/p&gt;
&lt;p&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;7
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;kr&#34;&gt;lazy&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;var&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;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;c1&#34;&gt;// The managed object model for the application. This property is not optional. It is a fatal error for the application not to be able to find and load its model.&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;carKitBundle&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Bundle&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;s&#34;&gt;&amp;#34;com.andrewcbancroft.CarKit&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;cl&#34;&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;modelURL&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;carKitBundle&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;n&#34;&gt;forResource&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;CarModel&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;withExtension&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;momd&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&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;NSManagedObjectModel&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;contentsOf&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;modelURL&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;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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/08/16-AppDelegate.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/08/16-AppDelegate-1024x394.png&#34; alt=&#34;16 - AppDelegate&#34; width=&#34;1024&#34; height=&#34;394&#34; class=&#34;alignnone size-large wp-image-12253&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/08/16-AppDelegate-1024x394.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2015/08/16-AppDelegate-300x115.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2015/08/16-AppDelegate-768x296.png 768w, https://www.andrewcbancroft.com/wp-content/uploads/2015/08/16-AppDelegate.png 1213w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Recall that &amp;ldquo;CarModel” is the name of the Core Data model we created for the framework in CarKit. We simply look for that artifact by calling &lt;code&gt;URLForResource:withExtension:&lt;/code&gt; on the &lt;code&gt;carKitBundle&lt;/code&gt; to initialize an &lt;code&gt;NSManagedObjectModel&lt;/code&gt; instance.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;add-carkit-framework&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;add-carkit-framework-to-project-and-embed-binary&#34;&gt;Add CarKit framework to project and embed binary&lt;/h4&gt;
&lt;p&gt;Now it&amp;rsquo;s time to actually bring in the framework for use within our app. I typically open up a Finder window and drag over a copy of the framework (in this case, the CarKit.framework file) into my project. Feel free to organize it into a &amp;ldquo;lib” folder.&lt;/p&gt;
&lt;p&gt;Assuming that you go through all the necessary steps to make the framework production-ready, you&amp;rsquo;ll want to embed the binary and ensure that it&amp;rsquo;s referenced in the &amp;ldquo;Linked Frameworks and Libraries” portion of your project configuration:&lt;br&gt;
&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/08/15-Embed-CarKit-and-Link-CarKit.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/08/15-Embed-CarKit-and-Link-CarKit-1024x634.png&#34; alt=&#34;15 - Embed CarKit and Link CarKit&#34; width=&#34;1024&#34; height=&#34;634&#34; class=&#34;alignnone size-large wp-image-12252&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/08/15-Embed-CarKit-and-Link-CarKit-1024x634.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2015/08/15-Embed-CarKit-and-Link-CarKit-300x186.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2015/08/15-Embed-CarKit-and-Link-CarKit.png 1399w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;take-for-test-drive&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;taking-the-model-out-for-a-test-drive-pun-intended&#34;&gt;Taking the model out for a test drive (pun intended)&lt;/h4&gt;
&lt;p&gt;It&amp;rsquo;s simple enough to try things out by writing a simple little snippet of code in the &lt;code&gt;AppDelegate&lt;/code&gt;:&lt;/p&gt;
&lt;p&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;12
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;13
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;14
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;15
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;16
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;17
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;18
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;19
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;20
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;application&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;application&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIApplication&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;didFinishLaunchingWithOptions&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;launchOptions&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;UIApplicationLaunchOptionsKey&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;Any&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;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Override point for customization after application launch.&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;newCar&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;insertNewObject&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;forEntityName&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;Car&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;into&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;managedObjectContext&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;Car&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;n&#34;&gt;newCar&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;year&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;2015&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;n&#34;&gt;newCar&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;make&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;Tesla&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;n&#34;&gt;newCar&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;model&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;S&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;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;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;try&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;managedObjectContext&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;?.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;save&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;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;kc&#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;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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;&amp;lt;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;NSFetchRequestResult&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;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;Car&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;cars&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;k&#34;&gt;try&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;managedObjectContext&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;?.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;fetch&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;k&#34;&gt;as&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;Car&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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;n&#34;&gt;cars&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;terminator&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;&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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;true&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
This code simply obtains a new Car object, sets some properties, and saves it all with the &lt;code&gt;managedObjectContext&lt;/code&gt; instance that&amp;rsquo;s configured in the &lt;code&gt;AppDelegate&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Then it goes and performs a fetch request to grab all the &lt;code&gt;Car&lt;/code&gt; objects and prints them. The results? See for yourself:&lt;br&gt;
&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/08/17-Final-Result.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/08/17-Final-Result-1024x592.png&#34; alt=&#34;17 - Final Result&#34; width=&#34;1024&#34; height=&#34;592&#34; class=&#34;alignnone size-large wp-image-12254&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/08/17-Final-Result-1024x592.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2015/08/17-Final-Result-300x173.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2015/08/17-Final-Result-768x444.png 768w, https://www.andrewcbancroft.com/wp-content/uploads/2015/08/17-Final-Result.png 1470w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;wrapping-up&#34;&gt;Wrapping up&lt;/h3&gt;
&lt;p&gt;This walkthrough guided you through the process of creating a framework for the purpose of sharing a Core Data model with multiple projects. My hope is that you&amp;rsquo;re now empowered to make use of _re_use by utilizing Swift frameworks to share even portions of your persistence layer!&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/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/02/25/using-swift-to-seed-a-core-data-database/&#34; title=&#34;Using Swift to Seed a Core Data Database&#34;&gt;Using Swift to Seed a Core Data Database&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;li&gt;
      &lt;i class=&#34;fa fa-angle-right&#34;&gt;&lt;/i&gt; &lt;a href=&#34;https://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;
    &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>Diagnosing “Restore failed” with ASP.NET 5 on Mac</title>
      <link>https://www.andrewcbancroft.com/2015/08/09/diagnosing-restore-failed-with-asp-net-5-on-mac/</link>
      <pubDate>Sun, 09 Aug 2015 20:57:02 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2015/08/09/diagnosing-restore-failed-with-asp-net-5-on-mac/</guid>
      <description>&lt;p&gt;Following along with Steve Smith&amp;rsquo;s &lt;a href=&#34;http://docs.asp.net/en/latest/tutorials/your-first-mac-aspnet.html&#34;&gt;&amp;ldquo;Your First ASP.NET 5 Application on a Mac”&lt;/a&gt;, I found myself frustrated that, despite all my efforts, the simple little sample project I was trying to get running just wouldn&amp;rsquo;t work due to a problem with restoring the project&amp;rsquo;s dependencies.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;retracing-steps&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;retracing-my-steps&#34;&gt;Retracing my steps&lt;/h3&gt;
&lt;p&gt;I &lt;a href=&#34;http://docs.asp.net/en/latest/getting-started/installing-on-mac.html&#34;&gt;got ASP.NET installed on my Mac&lt;/a&gt;, and I even got a cool little template going using &lt;a href=&#34;http://yeoman.io/&#34;&gt;Yeoman&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;I opened up my scaffolded project using Visual Studio Code, and began the dependency restore process. But it failed. So I troubleshooted (troubleshot?), and it failed some more.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Restore failed Unknown header: 3208085783”.&lt;/p&gt;
&lt;p&gt;I found David Fowler&amp;rsquo;s &lt;a href=&#34;http://davidfowl.com/diagnosing-dependency-issues-with-asp-net-5/&#34;&gt;&amp;ldquo;Diagnosing Dependency Issues with ASP.NET 5”&lt;/a&gt;, but none of the things he mentioned helped resolve my exact issue. But something he said, caused me to think:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Make sure your DNX and packages are on the same version &amp;ldquo;train”&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;My issue wasn&amp;rsquo;t with DNX, but with &lt;strong&gt;Mono&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;mis-matched-mono&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;mis-matched-mono-versions&#34;&gt;Mis-matched Mono versions&lt;/h3&gt;
&lt;p&gt;When I followed the instructions for installing ASP.NET, I distinctly remember installing Mono using the installer package available at their website.&lt;/p&gt;
&lt;p&gt;What I &lt;em&gt;failed&lt;/em&gt; to realize, however, was that a while back (long enough ago to where it didn&amp;rsquo;t hit me until just a few minutes ago), I installed Mono using Homebrew.&lt;/p&gt;
&lt;p&gt;So despite my efforts to install Mono using the installer package, the Mono that everything in my system was using was the one installed by Homebrew, which was version 3.10.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;resolved&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;resolved&#34;&gt;Resolved&lt;/h3&gt;
&lt;p&gt;To resolve the issue, I went ahead and just ran &lt;code&gt;brew upgrade mono&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Verifying that Mono was successfully upgraded via a &lt;code&gt;mono --version&lt;/code&gt; command, I retried the dependency restoration in my scaffolded ASP.NET 5 application.&lt;/p&gt;
&lt;p&gt;The result?&lt;/p&gt;
&lt;p&gt;&lt;code&gt;dnu restore&lt;/code&gt; produced a wonderfully green, &amp;ldquo;Restore complete”!&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>Analyzing Swift Protocol Extensions and C# Abstract Classes</title>
      <link>https://www.andrewcbancroft.com/2015/08/06/analyzing-swift-protocol-extensions-and-c-abstract-classes/</link>
      <pubDate>Fri, 07 Aug 2015 03:52:25 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2015/08/06/analyzing-swift-protocol-extensions-and-c-abstract-classes/</guid>
      <description>&lt;p&gt;Being a C# developer by day and a Swift developer by night has me constantly thinking about the similarities and differences between these two languages. I genuinely enjoy programming with each, and I love it when I can take a strategy that works well in one language, and see where that might cross over to the other. One of the areas I&amp;rsquo;ve been pondering as of late is the idea of how Swift and C# compare in terms of protocol extensions and abstract classes.&lt;/p&gt;
&lt;p&gt;Swift doesn&amp;rsquo;t have the notion of &lt;a href=&#34;https://msdn.microsoft.com/en-us/library/sf985hc5.aspx&#34;&gt;abstract classes&lt;/a&gt; like C# does. However, it &lt;em&gt;does&lt;/em&gt; now have an amazingly powerful feature called protocol extensions, which were explained and demonstrated in the popular WWDC15 talk on &lt;a href=&#34;https://developer.apple.com/videos/wwdc/2015/?id=408&#34;&gt;Protocol Oriented Programming&lt;/a&gt;. Why am I comparing protocol extensions with C# abstract classes? What are the similarities? What are the differences? Which one do I like best? The analysis of and concluding answers to these questions is the goal of this article.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;why&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;why-the-comparison&#34;&gt;Why the comparison?&lt;/h3&gt;
&lt;p&gt;What got me thinking about this? Well, I was re-watching &lt;a href=&#34;https://developer.apple.com/videos/wwdc/2015/?id=408&#34;&gt;Protocol Oriented Programming&lt;/a&gt; the other day and was digesting some of the arguments for why protocols often serve as better abstractions than classes. When classes are used to model a generalized abstraction, the only way they do it is through inheritance. Subclasses of some other, more generalized, base class will automatically be able to behave the same and store the same state as that base class by virtue of inheritance.&lt;/p&gt;
&lt;p&gt;Protocols on the other hand, model abstraction through composable template-like descriptions: &amp;ldquo;Adopters of this protocol will do [x, y, and z] by implementing [function x, function y, and function z] and will have [property a, and property b]”, etc… But they define a template only – no implementations are defined within a protocol.&lt;/p&gt;
&lt;p&gt;In C#, we have a similar mechanism to protocols called interfaces. The same paradigm of defining a template with no implementations exists in C# when we use an interface to model some abstraction. C#, of course, also has classes and can pass along behavior and state to subclasses through inheritance.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;understanding-cs-abstract-classes&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;understanding-c-abstract-classes&#34;&gt;Understanding C# abstract classes&lt;/h4&gt;
&lt;p&gt;But C# has one &lt;em&gt;other&lt;/em&gt; mechanism for abstraction: &lt;strong&gt;abstract classes&lt;/strong&gt;. These special types of abstractions have the ability to behave like interfaces (or protocols) in that they can strictly define a template with no implementation, requiring all &lt;em&gt;subclasses&lt;/em&gt; to supply that implementation. But abstract classes are unique in that they aren&amp;rsquo;t &lt;em&gt;required&lt;/em&gt; to define a template &lt;em&gt;only&lt;/em&gt; – they can actually provide a default implementation that may or may not be overridden in a subclass, depending on what the implementer of the subclass wants to do.&lt;/p&gt;
&lt;p&gt;We still can&amp;rsquo;t make instances of an abstract class, just like we can&amp;rsquo;t make instances of an interface or protocol. But with abstract classes, we can provide some default implementation that can be a customization point for concrete subclasses, should the implementer of the subclass desire to override this default behavior.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;back-to-swift&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;bringing-it-back-to-swift&#34;&gt;Bringing it back to Swift&lt;/h4&gt;
&lt;p&gt;So… what does this have to do with Swift? Well, it seemed to me that there&amp;rsquo;s a &lt;em&gt;little&lt;/em&gt; overlap between Swift 2.0&amp;rsquo;s new protocol extensions, and C# abstract classes. How?&lt;/p&gt;
&lt;p&gt;With Swift protocol extensions, we can now provide default implementation for a protocol requirement, such that any adopter of the protocol &lt;em&gt;automatically&lt;/em&gt; uses that implementation and satisfies that particular requirement of the protocol.&lt;/p&gt;
&lt;p&gt;A given Type implementing the extended protocol could choose to provide its own implementation to customize the behavior as it needs. But if it chooses not to, it gets that default behavior for free.&lt;/p&gt;
&lt;p&gt;Therein lies the overlap I see between Swift protocol extensions and C# abstractions. But let&amp;rsquo;s explore a little more in terms of similarities and differences between the two by analyzing an example.&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;h3 id=&#34;working-example-modeling-athletes&#34;&gt;Working example: Modeling athletes&lt;/h3&gt;
&lt;p&gt;Suppose for a moment that we&amp;rsquo;re working on an app that models athletic competition (such as marathons, triathlons, and other sporting events). Now, athletic competition implies &lt;em&gt;athletes&lt;/em&gt;, does it not?&lt;/p&gt;
&lt;p&gt;How then, could we model an &lt;code&gt;Athlete&lt;/code&gt; in an abstract way? That is, how can we provide just the blueprint for what an &lt;code&gt;Athlete&lt;/code&gt; does, so that such a Type can be entered into one of the athletic competitions and perform in it?&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;model-with-cs&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;modeling-the-athlete-abstraction-with-c&#34;&gt;Modeling the Athlete abstraction with C#&lt;/h4&gt;
&lt;p&gt;In C#, we&amp;rsquo;ve got two possibilities: Create an interface, or create an abstract class.&lt;/p&gt;
&lt;p&gt;Using an interface may 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;public&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;interface&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Athlete&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&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;public&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;void&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Run&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;public&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;void&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Swim&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;public&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;void&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Cycle&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;// Other things that an Athlete may be able to do&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;p&gt;Using an abstract class may look very similar. The primary difference is in the declaration of each method, where we mark each of them &lt;code&gt;virtual&lt;/code&gt;, so that they can be overridden in a subclass to provide that customization point I talked about earlier:&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-csharp&#34; data-lang=&#34;csharp&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;abstract&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;Athlete&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&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;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;abstract&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;void&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Run&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;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;abstract&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;void&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Swim&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;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;abstract&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;void&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Cycle&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;// Other things that an Athlete may be able to do&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;p&gt;Right off, you might be asking, &amp;ldquo;Should a marathon runner have to be able to swim and cycle??”. It&amp;rsquo;s a great question, and I&amp;rsquo;ll address it further down in the article when I discuss &lt;a href=&#34;#refactoring&#34;&gt;&amp;ldquo;refactoring for enhanced composability with Swift protocol extensions”&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;model-with-swift&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;modeling-the-athlete-abstraction-with-swift&#34;&gt;Modeling the Athlete abstraction with Swift&lt;/h4&gt;
&lt;p&gt;In Swift, we essentially have one possibility that compares with C# for a pure abstraction, that is, just the blueprint describing an &lt;code&gt;Athlete&#39;s&lt;/code&gt; capabilities: Create a protocol.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;Athlete&lt;/code&gt; protocol 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;protocol&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;Athlete&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;run&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;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;swim&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;cycle&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;// Other things that an Athlete may be able to do&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;We still don&amp;rsquo;t avoid the necessity of a marathon runner being required to be able to swim and cycle. And having read &lt;a href=&#34;http://owensd.io/2015/08/06/protocols.html&#34;&gt;David Owens&amp;rsquo; recent recommendations on Protocols&lt;/a&gt;, I&amp;rsquo;m even more uncomfortable with modeling an &lt;code&gt;Athlete&lt;/code&gt; this way, because it feels like we&amp;rsquo;re treating a protocol as a Type here, which he identifies as a less powerful usage of protocols.&lt;/p&gt;
&lt;p&gt;I want to refactor this, but for this moment in time, we&amp;rsquo;ll stick with the code as-is, just to keep it as similar to the C# code as possible. I&amp;rsquo;ll discuss &lt;a href=&#34;#refactoring&#34;&gt;an option for refactoring this shortly&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;default-implementation&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;default-implementation-for-athletic-abilities&#34;&gt;Default implementation for athletic abilities&lt;/h4&gt;
&lt;p&gt;As the example stands right now in both C# and Swift, we&amp;rsquo;ve got a situation where any Type wishing to be an &lt;code&gt;Athlete&lt;/code&gt;, whether it be by implementing the C# interface, subclassing the C# abstract class, or adopting the Swift protocol, &lt;em&gt;must&lt;/em&gt; provide implementations of each of those athletic abilities (run, swim, and cycle). The Type can&amp;rsquo;t &lt;em&gt;not&lt;/em&gt; implement one of those requirements and have the code still compile. They&amp;rsquo;re requirements of what it means to be an &lt;code&gt;Athlete&lt;/code&gt;, so the Type must conform.&lt;/p&gt;
&lt;p&gt;Suppose that in our scenario, any given &lt;code&gt;Athlete&lt;/code&gt; has one &lt;em&gt;primary&lt;/em&gt; ability which he/she is amazing at, but when it comes to his/her non-primary abilities, the &lt;code&gt;Athlete&lt;/code&gt; is only able to perform at &amp;ldquo;average” skill.&lt;/p&gt;
&lt;p&gt;This sounds like a case where it might be nice to have overridable default implementation provided. Any &lt;em&gt;specific&lt;/em&gt; type of &lt;code&gt;Athlete&lt;/code&gt; could override that default implementation to perform the ability better or worse, depending on what kind of &lt;code&gt;Athlete&lt;/code&gt; he/she is. But if the specific &lt;code&gt;Athlete&lt;/code&gt; Type didn&amp;rsquo;t provide an customized override, the Type would get the &amp;ldquo;average” behavior for free.&lt;/p&gt;
&lt;p&gt;How could we make this happen?&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;default-implementation-with-cs&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h5 id=&#34;default-implementation-with-c&#34;&gt;Default implementation with C#&lt;/h5&gt;
&lt;p&gt;In C#, abstract classes allow us to do just that. Here&amp;rsquo;s how a default implementation might be written:&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-csharp&#34; data-lang=&#34;csharp&#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;kd&#34;&gt;public&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;virtual&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;void&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Run&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;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;// run with average speed and endurance&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 5&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 6&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 7&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;public&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;virtual&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;void&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Swim&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 class=&#34;c1&#34;&gt;// swim with average speed and endurance&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;10&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;11&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;12&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;public&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;virtual&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;void&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Cycle&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&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;// cycle with average speed and endurance&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;So now, when we want to model a &lt;code&gt;MarathonRunner&lt;/code&gt;, we can override his/her ability to run, swim, and cycle as 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-csharp&#34; data-lang=&#34;csharp&#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;kd&#34;&gt;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;override&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;void&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Run&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;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;// run with average speed and __insane__ endurance&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;It&amp;rsquo;s not terrible – At least here we can rely on the default implementation if we just want to give a &lt;code&gt;MarathonRunner&lt;/code&gt; &amp;ldquo;average” abilities in all areas but running.&lt;/p&gt;
&lt;p&gt;We might prefer that a &lt;code&gt;MarathonRunner&lt;/code&gt; not be required to have &lt;em&gt;any&lt;/em&gt; ability to swim or cycle, but that&amp;rsquo;s always the struggle with inheritance-based modeling. You only get to choose one base class to inherit from, and you&amp;rsquo;re bound to get some behavior that you don&amp;rsquo;t need, simply because it&amp;rsquo;s hard to model abstractions using inheritance that avoid giving you more than you need.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;default-implementation-with-swift&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h5 id=&#34;default-implementation-with-swift&#34;&gt;Default implementation with Swift&lt;/h5&gt;
&lt;p&gt;The default implementation story with Swift was non-existent until Swift 2.0 entered the scene. The approach is similar, but as we&amp;rsquo;ll see shortly, provides far more power in terms of composability. Take a look at the implementation that compares most closely with C# for now:&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;Athlete&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;run&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;c1&#34;&gt;// run with average speed and endurance &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;swim&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;c1&#34;&gt;// swim with average speed and endurance &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;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;cycle&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;c1&#34;&gt;// cycle with average speed and endurance &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 when we want to model a &lt;code&gt;MarathonRunner&lt;/code&gt; in Swift, we can adopt the &lt;code&gt;Athlete&lt;/code&gt; protocol, and provide &amp;ldquo;override” implementations for any of the protocol&amp;rsquo;s requirements that we&amp;rsquo;d like. Anything we don&amp;rsquo;t provide a custom implementation for falls back to the protocol extension&amp;rsquo;s implementation, just like in C#:&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;MarathonRunner&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Athlete&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&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;run&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;c1&#34;&gt;// run with average speed and __insane__ endurance&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;a name=&#34;similarities&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;similarities&#34;&gt;Similarities&lt;/h3&gt;
&lt;p&gt;Here&amp;rsquo;s a list of the similarities I see between C# abstract classes and Swift protocol extensions:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Both outline a set of requirements that either a subclass or a protocol adopter &lt;em&gt;must&lt;/em&gt; implement.&lt;/li&gt;
&lt;li&gt;Both provide a means to &lt;em&gt;automatically&lt;/em&gt; satisfy some (or &lt;em&gt;all&lt;/em&gt;) of the requirements by providing a default implementation. With C#, we simply mark the method &lt;code&gt;virtual&lt;/code&gt; to allow overriding in the subclass, and provide then an implementation. With Swift, we define a protocol extension that implements one or more of the protocol&amp;rsquo;s requirements.&lt;/li&gt;
&lt;li&gt;Both ease the burden of subclasses (C#) or protocol adopters (Swift) to implement all of the requirements when reasonable default implementations could suffice.&lt;/li&gt;
&lt;li&gt;Both are used to provide customization points in subclasses (C#) or protocol adopters (Swift), when the default implementation is inadequate.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;a name=&#34;differences&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;differences&#34;&gt;Differences&lt;/h3&gt;
&lt;p&gt;So there are some similarities that I hope you can see and appreciate between C# abstract classes and Swift protocol extensions. But there are some major differences that should also be recognized:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Fundamentally, C# abstract classes are a &amp;ldquo;behavior by inheritance” tool, while Swift protocol extension are a &amp;ldquo;behavior by composition” tool.&lt;/li&gt;
&lt;li&gt;Consequently, C# abstract classes impose a significant limitation: subclasses can inherit from one and only one base class. Swift protocols, on the other hand, can be decomposed into fine-grained, specific requirements that can later be re-combined and composed into more robust and dynamic Type specifications. While C# interfaces provide this same composability, they &lt;em&gt;don&amp;rsquo;t&lt;/em&gt; have the ability to provide default implementation, which is a significant difference between the Swift counterpart.&lt;/li&gt;
&lt;li&gt;As a consequence of &lt;em&gt;that&lt;/em&gt;, subclasses of a C# abstract class get &lt;em&gt;all&lt;/em&gt; of the behavior, whether they need (or want) it or not. Swift protocols, being composable, allow a Type to conform to &lt;em&gt;just&lt;/em&gt; the pieces it needs. The protocol extension can still exist to provide default behavior when it&amp;rsquo;s appropriate. But if a certain Type needs no ability to [do some thing], it simply drops conforming to that protocol and no superfluous behavior is imposed upon the Type.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;a name=&#34;preference&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;preferring-one-over-the-other&#34;&gt;Preferring one over the other&lt;/h3&gt;
&lt;p&gt;Needless to say, I prefer Swift protocol extensions over C# abstract classes (shocker). I love the composability they offer, while at the same time allowing me to provide default implementations where it&amp;rsquo;s appropriate. In my opinion, Swift protocol extensions are the perfect blend of interface and abstract class in C#. If only C# had &amp;ldquo;interface extensions”. :]&lt;/p&gt;
&lt;p&gt;Since we can apply multiple protocols to a Type to signify what the Type can do, and essentially compose its behavior, how might we diverge from the constraints we had previously when we tried to stick closely with the C# abstract class paradigm?&lt;/p&gt;
&lt;p&gt;Recall that I was uncomfortable with making a &lt;code&gt;MarathonRunner&lt;/code&gt; have ability to swim and cycle, however &amp;ldquo;average” that ability may be. What I really want is to break things out a bit more, but still be able to provide that default implementation when I want it.&lt;/p&gt;
&lt;p&gt;How might I refactor this by leveraging even more of the power of Swift protocol extensions?&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;refactoring&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;refactoring-for-enhanced-composability-with-swift-protocol-extensions&#34;&gt;Refactoring for enhanced composability with Swift protocol extensions&lt;/h3&gt;
&lt;p&gt;I think I might like to define 3 protocols instead of 1. Rather than modeling things as &lt;code&gt;Athletes&lt;/code&gt;, I&amp;rsquo;d much rather model some athletic &lt;em&gt;behavior&lt;/em&gt;, and let the specific &lt;em&gt;kinds&lt;/em&gt; of athletes adopt whatever behavior is most appropriate for each athlete type.&lt;/p&gt;
&lt;p&gt;So I&amp;rsquo;ll ditch the &lt;code&gt;Athlete&lt;/code&gt; protocol, and define these three instead:&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;protocol&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;Runnable&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;run&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;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;protocol&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;Swimmable&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;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;swim&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;p&#34;&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;protocol&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;Cycleable&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;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;cycle&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Alright… now… how about some default implementation?&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;Runnable&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;run&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;// run with average speed and endurance&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;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 6&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 7&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;extension&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;Swimmable&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 8&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;swim&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 class=&#34;c1&#34;&gt;// swim with average speed and endurance&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;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;kd&#34;&gt;extension&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;Cycleable&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;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;cycle&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;15&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;c1&#34;&gt;// cycle with average speed and endurance&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;Excellent. Now to cap things off, I&amp;rsquo;ll define some Types that adopt &lt;em&gt;just the protocols that are needed&lt;/em&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;struct&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;MarathonRunner&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Runnable&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;run&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;// run with average speed and __insane__ endurance&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;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;struct&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;Triathlete&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Runnable&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Swimmable&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Cycleable&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 8&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;swim&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 class=&#34;c1&#34;&gt;// swim with lightning speed and crazy endurance&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;// fall back to protocol extension&amp;#39;s average run speed &amp;amp; endurance&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;// fall back to protocol extension&amp;#39;s average cycle speed and endurance&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;14&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;15&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;16&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;struct&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;Andrew&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;c1&#34;&gt;// Let&amp;#39;s not impose the re-definition of any of the athletic terms, shall we?&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;Notice how the different types of athletes only pick up the behavior that&amp;rsquo;s relevant to their ability. Nothing more, and nothing less.&lt;/p&gt;
&lt;p&gt;I also enjoy being able to look at a Type declaration like &lt;code&gt;Triathlete&lt;/code&gt; and see that he/she is able to run, swim, and cycle. It feels right to compose abilities this way. And it&amp;rsquo;s even more convenient that some of the &lt;code&gt;Triathlete&#39;s&lt;/code&gt; behavior is already implemented for me by virtue of the protocol extension.&lt;/p&gt;
&lt;h3 id=&#34;wrapping-up&#34;&gt;Wrapping up&lt;/h3&gt;
&lt;p&gt;I hope this analysis helped you see some of the same things I saw when it comes to how abstractions can be modeled with Swift, and how that compares with others languages like C#.&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>Setting Up Carthage for the Terminal-Timid</title>
      <link>https://www.andrewcbancroft.com/2015/07/29/setting-up-carthage-for-the-terminal-timid/</link>
      <pubDate>Thu, 30 Jul 2015 04:05:05 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2015/07/29/setting-up-carthage-for-the-terminal-timid/</guid>
      <description>&lt;p&gt;The old adage &amp;ldquo;Don&amp;rsquo;t go reinventing the wheel” comes into play &lt;em&gt;often&lt;/em&gt; when developing software, doesn&amp;rsquo;t it? Why build this [thing] ourselves when someone else has invented it already?&lt;/p&gt;
&lt;p&gt;Fantastic libraries and frameworks exist out there to assist developers with common and sometimes even difficult problems. Whether it be an easier-to-use abstraction over the networking stack with &lt;a href=&#34;https://github.com/AFNetworking/AFNetworking&#34;&gt;AFNetworking&lt;/a&gt; or &lt;a href=&#34;https://github.com/Alamofire/Alamofire&#34;&gt;AlamoFire&lt;/a&gt;, or adding a popular behavior-driven development testing framework like &lt;a href=&#34;https://github.com/Quick/Quick&#34;&gt;Quick&lt;/a&gt; to our workflow, we find ourselves constantly wanting to be able to take advantage of the things that others are doing in the community so we don&amp;rsquo;t have to invent that wheel again ourselves.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;need-dependency-management&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;the-need-for-dependency-management&#34;&gt;The need for dependency management&lt;/h3&gt;
&lt;p&gt;With that comes the need for an easy way to manage external dependencies in our projects. We find the need to make sure that our projects have the right versions of whatever libraries or frameworks we&amp;rsquo;re bringing in. We want to ensure that if we move to another machine or add folks to our teams that they can go and restore all those same external dependencies without a lot of hassle. Therein lies at least partial justification for the usage of dependency managers.&lt;/p&gt;
&lt;p&gt;The long-running &amp;ldquo;king of the hill” of dependency management for iOS developers has been the ever-popular &lt;a href=&#34;https://cocoapods.org/&#34;&gt;CocoaPods&lt;/a&gt;. However, there&amp;rsquo;s a new kid on the block, and its picking up popularity. This &amp;ldquo;new” dependency manager is called &lt;a href=&#34;https://github.com/Carthage/Carthage&#34;&gt;Carthage&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;what-is-carthage&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;what-is-carthage&#34;&gt;What is Carthage?&lt;/h3&gt;
&lt;p&gt;From the GitHub repository&amp;rsquo;s main page for Carthage (emphasis mine):&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Carthage is intended to be the simplest way to add frameworks to your Cocoa application… Ultimately, we created Carthage because we wanted the &lt;strong&gt;simplest tool possible&lt;/strong&gt;—a dependency manager that gets the job done &lt;strong&gt;without taking over the responsibility of Xcode&lt;/strong&gt;, and &lt;strong&gt;without creating extra work for framework authors&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;I&amp;rsquo;ve used Carthage for simple tasks like bringing in Quick for unit testing. It delivers on the promise of being simple, but even in its simplicity, installing and using Carthage requires the use of the Terminal.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;terminal-tension&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;terminal-tension&#34;&gt;Terminal tension&lt;/h3&gt;
&lt;p&gt;If you&amp;rsquo;ll withhold judgement of this confession, I&amp;rsquo;ll openly share that I&amp;rsquo;m not a whiz on the command line. I &lt;em&gt;have&lt;/em&gt; found that the more I spend time in the Terminal, the less timid I become. It&amp;rsquo;s been a gradual transition into spending intentional time on the command line, but I share this to simply relate to those of you who, like me, find themselves thinking, &amp;ldquo;Really? All of this involves running commands in the Terminal? Ugh…”&lt;/p&gt;
&lt;p&gt;Hang in there. Becoming proficient at this will take practice, but it&amp;rsquo;s worth it. Carthage really does make your dependency management simpler and more shareable with your team. Even if you&amp;rsquo;re working alone, I recommend investing the time to overcoming any anxiety you may have around the Terminal.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;installing-carthage&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;installing-carthage&#34;&gt;Installing Carthage&lt;/h3&gt;
&lt;p&gt;So here&amp;rsquo;s the deal: The folks on the Carthage team have actually made it pretty easy to install Carthage &lt;em&gt;without&lt;/em&gt; the Terminal. If you go to their &lt;a href=&#34;https://github.com/Carthage/Carthage/releases&#34;&gt;releases page on GitHub&lt;/a&gt; and download + run the Carthage.pkg file for the latest release, you&amp;rsquo;ll be set up. BUT, &lt;strong&gt;Carthage is a command-line tool&lt;/strong&gt;, so to use it, you&amp;rsquo;ll still need the Terminal.&lt;/p&gt;
&lt;p&gt;With that in mind, I&amp;rsquo;ve decided to get used to working with this tool &lt;em&gt;totally&lt;/em&gt; from the command-line from the start, which means installing it from the Terminal window. There&amp;rsquo;s a couple more steps involved, but I&amp;rsquo;d argue that it&amp;rsquo;s good practice. If you&amp;rsquo;re wanting to overcome your anxiety around the Terminal, skip the .pkg installation method and continue on.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;terminal-installation&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;terminal-installation&#34;&gt;Terminal installation&lt;/h3&gt;
&lt;p&gt;Installing Carthage from the command line assumes that you have a package manager for OSX called &lt;a href=&#34;http://brew.sh/&#34;&gt;&amp;ldquo;Homebrew”&lt;/a&gt; installed on your Mac. If you don&amp;rsquo;t, you need it. And yes, &lt;em&gt;it&lt;/em&gt; involves the Terminal too, but don&amp;rsquo;t worry – I&amp;rsquo;ve got you covered in the steps to follow. We&amp;rsquo;ll use Homebrew to install Carthage which will be used to manage the external dependencies in our app.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;installing-homebrew&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;first-things-first--installing-homebrew&#34;&gt;First things first – installing Homebrew&lt;/h4&gt;
&lt;p&gt;Installing Homebrew isn&amp;rsquo;t complicated. It just involves knowing the command to run in the Terminal to get it downloaded and registered in your machine.&lt;/p&gt;
&lt;p&gt;From the &lt;a href=&#34;http://brew.sh/&#34;&gt;Homebrew homepage&lt;/a&gt; we learn that the installation simply requires us to type in the following in the Terminal:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;ruby -e &amp;quot;$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)&amp;quot;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Pasting in the command and pressing enter will immediately produce a &lt;em&gt;lot&lt;/em&gt; of stuff in the terminal window. It&amp;rsquo;s okay. Don&amp;rsquo;t panic.&lt;/p&gt;
&lt;p&gt;It&amp;rsquo;s simply letting you know what&amp;rsquo;s about to happen if you agree to continue:&lt;br&gt;
&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/07/homebrew_install_command.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/07/homebrew_install_command.png&#34; alt=&#34;Homebrew Install Command&#34; width=&#34;580&#34; height=&#34;364&#34; class=&#34;alignnone size-full wp-image-12168&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/07/homebrew_install_command.png 580w, https://www.andrewcbancroft.com/wp-content/uploads/2015/07/homebrew_install_command-300x188.png 300w&#34; sizes=&#34;(max-width: 580px) 100vw, 580px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;So it&amp;rsquo;s going to…&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Install some files into some directories on your computer.&lt;/li&gt;
&lt;li&gt;Change the read-write permissions on some directories to be &lt;em&gt;group&lt;/em&gt; writable. This allows Homebrew to do its thing when you tell it to.&lt;/li&gt;
&lt;li&gt;Change those same directories&amp;rsquo; group to &lt;strong&gt;admin&lt;/strong&gt;. So these last two bullet points go together.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You&amp;rsquo;re prompted to press ‘RETURN&amp;rsquo; to continue (or anything else to stop the installation). If you&amp;rsquo;re good with the changes it describes to you, press ‘RETURN&#39;.&lt;/p&gt;
&lt;p&gt;Yikes! What&amp;rsquo;s all this now?&lt;br&gt;
&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/07/sudo_permissions.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/07/sudo_permissions.png&#34; alt=&#34;Elevate permissions using sudo&#34; width=&#34;580&#34; height=&#34;365&#34; class=&#34;alignnone size-full wp-image-12169&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/07/sudo_permissions.png 580w, https://www.andrewcbancroft.com/wp-content/uploads/2015/07/sudo_permissions-300x189.png 300w&#34; sizes=&#34;(max-width: 580px) 100vw, 580px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Well, for the installation to continue, Homebrew needs to elevate the level of permissions it has in order write all the files it needs to write and to set all the permissions it needs to set. Under the hood, it&amp;rsquo;s run a &lt;code&gt;sudo&lt;/code&gt; command. It requires your password to authorize this elevation. Go ahead and type your password, and press ‘RETURN&amp;rsquo; to continue.&lt;/p&gt;
&lt;p&gt;The installation will continue by downloading all of the necessary files and performing all the changes it said it&amp;rsquo;d make. When it&amp;rsquo;s done, you&amp;rsquo;ll get an &amp;ldquo;Installation Successful” message:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/07/install_successful.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/07/install_successful.png&#34; alt=&#34;Homebrew Installation Successful&#34; width=&#34;586&#34; height=&#34;367&#34; class=&#34;alignnone size-full wp-image-12177&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/07/install_successful.png 586w, https://www.andrewcbancroft.com/wp-content/uploads/2015/07/install_successful-300x188.png 300w&#34; sizes=&#34;(max-width: 586px) 100vw, 586px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Sweet! To test it out, you can simply type &lt;code&gt;brew help&lt;/code&gt; like it suggests in the line below the &amp;ldquo;Installation successful!” message. It should print out a bunch of stuff to the Terminal window, explaining how to use Homebrew:&lt;br&gt;
&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/07/it_works.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/07/it_works.png&#34; alt=&#34;It works!&#34; width=&#34;581&#34; height=&#34;366&#34; class=&#34;alignnone size-full wp-image-12172&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/07/it_works.png 581w, https://www.andrewcbancroft.com/wp-content/uploads/2015/07/it_works-300x189.png 300w&#34; sizes=&#34;(max-width: 581px) 100vw, 581px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Well done! We can now use Homebrew to install Carthage. You can use it to install all kinds of other fantastic utilities as well, but for now we&amp;rsquo;ll focus on the Carthage installation…&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;now-install-carthage&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;now-to-install-carthage&#34;&gt;Now to install Carthage&lt;/h4&gt;
&lt;p&gt;With Homebrew installed, we&amp;rsquo;re almost there. From the &lt;a href=&#34;https://github.com/Carthage/Carthage&#34;&gt;Carthage GitHub repository page&lt;/a&gt;, we learn what commands to run on the command line to get Carthage installed.&lt;/p&gt;
&lt;p&gt;First, we run &lt;code&gt;brew update&lt;/code&gt; to make sure that Homebrew is totally up-to-date. If you just installed Homebrew for the first time, you&amp;rsquo;re probably good to go. Just to be safe though, go ahead and run it.&lt;br&gt;
&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/07/Brew_Update.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/07/Brew_Update.png&#34; alt=&#34;Brew Update&#34; width=&#34;582&#34; height=&#34;363&#34; class=&#34;alignnone size-full wp-image-12189&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/07/Brew_Update.png 582w, https://www.andrewcbancroft.com/wp-content/uploads/2015/07/Brew_Update-300x187.png 300w&#34; sizes=&#34;(max-width: 582px) 100vw, 582px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The last step is to run &lt;code&gt;brew install carthage&lt;/code&gt;. Yes. It&amp;rsquo;s that simple. Homebrew will take care of everything else for you! Here&amp;rsquo;s the final result:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/07/Brew_Install_Carthage.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/07/Brew_Install_Carthage.png&#34; alt=&#34;Brew Install Carthage&#34; width=&#34;580&#34; height=&#34;362&#34; class=&#34;alignnone size-full wp-image-12188&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/07/Brew_Install_Carthage.png 580w, https://www.andrewcbancroft.com/wp-content/uploads/2015/07/Brew_Install_Carthage-300x187.png 300w&#34; sizes=&#34;(max-width: 580px) 100vw, 580px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;cartfiles&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;cartfiles&#34;&gt;Cartfiles&lt;/h3&gt;
&lt;p&gt;Once Carthage is installed, it&amp;rsquo;s time to create your Cartfile file. After I read &lt;a href=&#34;https://github.com/Carthage/Carthage&#34;&gt;the documentation&lt;/a&gt; on how to set this up, I found myself doubting whether I&amp;rsquo;d done it right, so again, to alleviate any fears our there around the mystery of what&amp;rsquo;s going on I&amp;rsquo;ll share what I&amp;rsquo;ve discovered.&lt;/p&gt;
&lt;p&gt;First of all, the documentation merely says, &amp;ldquo;Create a Cartfile that lists the frameworks you’d like to use in your project.” I scratched my head and asked, &amp;ldquo;Okay – well, where? And should it have a file extension?”&lt;/p&gt;
&lt;p&gt;The answers that worked for me were, &amp;ldquo;Somewhere in your Xcode project (I put it under Supporting Files)” and &amp;ldquo;No file extension…just the name Cartfile works”.&lt;/p&gt;
&lt;p&gt;To demonstrate, here&amp;rsquo;s a couple of screenshots depicting how to add a Cartfile to your Xcode project…&lt;/p&gt;
&lt;p&gt;Simply add a new Empty file, found under iOS -&amp;gt; Other:&lt;br&gt;
&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/07/New-empty-file.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/07/New-empty-file-1024x409.png&#34; alt=&#34;New empty file&#34; width=&#34;1024&#34; height=&#34;409&#34; class=&#34;alignnone size-large wp-image-12191&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/07/New-empty-file-1024x409.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2015/07/New-empty-file-300x120.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2015/07/New-empty-file.png 1394w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;In the new file dialog, simply type in &amp;ldquo;Cartfile”, and press Create:&lt;br&gt;
&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/07/Cartfile-create.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/07/Cartfile-create.png&#34; alt=&#34;Cartfile -&gt; Create&#34; width=&#34;783&#34; height=&#34;298&#34; class=&#34;alignnone size-full wp-image-12192&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/07/Cartfile-create.png 783w, https://www.andrewcbancroft.com/wp-content/uploads/2015/07/Cartfile-create-300x114.png 300w&#34; sizes=&#34;(max-width: 783px) 100vw, 783px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;wrapping-up&#34;&gt;Wrapping up&lt;/h3&gt;
&lt;p&gt;This is where I&amp;rsquo;m going to stop for now and defer to the &lt;a href=&#34;https://github.com/Carthage/Carthage&#34;&gt;Carthage GitHub documentation&lt;/a&gt; to explain more about what exactly to put into the Cartfile and how to run the subsequent commands to grab the libraries and frameworks you list, and build them so that they can be added to your Xcode project. I may pick this back up again, but this guide was starting to get lengthy, and I felt that actually building the frameworks from a Cartfile could very well deserve isolated treatment for the sake of clarity. If you&amp;rsquo;re interested in reading more on Carthage, sound off in the comments or shoot me an e-mail! Thanks for reading.&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>Dissecting 10 Read 10 Said – My New Swift iOS App</title>
      <link>https://www.andrewcbancroft.com/2015/07/22/dissecting-10-read-10-said-my-new-swift-ios-app/</link>
      <pubDate>Thu, 23 Jul 2015 03:57:33 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2015/07/22/dissecting-10-read-10-said-my-new-swift-ios-app/</guid>
      <description>&lt;p&gt;I&amp;rsquo;m very excited to announce that my new app, 10 Read 10 Said, is now available for &lt;a href=&#34;https://itunes.apple.com/us/app/10-read-10-said/id1018662429?mt=8&#34; title=&#34;10 Read 10 Said iOS App Store&#34;&gt;iOS&lt;/a&gt; and &lt;a href=&#34;https://play.google.com/store/apps/details?id=com.gibcroft.tenreadtensaid&#34; title=&#34;10 Read 10 Said Google Play Store&#34;&gt;Android&lt;/a&gt;!&lt;/p&gt;
&lt;p&gt;While the goal of the app is to help you implement the &lt;a href=&#34;http://10read10said.andrewcbancroft.com/&#34; title=&#34;10 Read 10 Said Strategy&#34;&gt;&amp;ldquo;10 Read 10 Said” strategy for memorizing Bible verses&lt;/a&gt;, my goal in this &lt;em&gt;article&lt;/em&gt; is to dissect the app, and share with you how some of the major features were implemented.&lt;/p&gt;
&lt;p&gt;As it turns out, &lt;em&gt;most&lt;/em&gt; of the major features of the app are things that I&amp;rsquo;ve already written about over the &lt;a href=&#34;http://www.andrewcbancroft.com/category/software-development/ios-mac/swift/&#34; title=&#34;Swift Content at andrewcbancroft.com&#34;&gt;past year of producing Swift content&lt;/a&gt;! The approach I&amp;rsquo;ll take, then, is to walk through each screen and point out which articles I&amp;rsquo;ve written might pertain to the particular feature implementations that are visible from that screen.&lt;/p&gt;
&lt;p&gt;I hope this tour satisfies the curiosity of many like myself who ask, &amp;ldquo;I wonder how they did that!”&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;demo&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;app-demo&#34;&gt;App Demo&lt;/h3&gt;
&lt;p&gt;To demonstrate the major functionality of the app, I&amp;rsquo;ve created a short (50 second) YouTube video. Take a look, and then I&amp;rsquo;ll break down each screen:&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;verse-list&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;verse-list&#34;&gt;Verse List&lt;/h3&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/07/Verse-List.png&#34;&gt;&lt;img src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/07/Verse-List-576x1024.png&#34; alt=&#34;Verse List&#34; width=&#34;326&#34; height=&#34;774&#34; class=&#34;alignnone size-large wp-image-12126&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;navigation-controller&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;navigation-controller&#34;&gt;Navigation Controller&lt;/h4&gt;
&lt;p&gt;To begin, the Verse List scene has been embedded in a &lt;code&gt;UINavigationController&lt;/code&gt;. This caused me to have to make a few changes in &lt;code&gt;AppDelegate&lt;/code&gt; to properly dig in to access the verse list controller and set some properties on it.&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;ve covered the details of how to &amp;ldquo;unpack” a &lt;code&gt;UINavigationController&lt;/code&gt; and access its first &lt;em&gt;child&lt;/em&gt; view controller in my article titled &lt;a href=&#34;http://www.andrewcbancroft.com/2015/06/02/access-sub-controllers-from-a-uinavigationcontroller-in-swift/&#34;&gt;&amp;ldquo;Access Sub-Controllers from a UINavigationController in Swift”&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;table-view&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;table-view&#34;&gt;Table View&lt;/h4&gt;
&lt;p&gt;The next most obvious thing to notice about the verse list is that it&amp;rsquo;s been implemented with a &lt;code&gt;UITableView&lt;/code&gt;. Additionally, each cell is a &amp;ldquo;custom” cell, rather than one of the default cell styles that Xcode gives you out of the box. I&amp;rsquo;ve written an article or three on these:&lt;/p&gt;
&lt;div class=&#34;resources&#34;&gt;
  &lt;div class=&#34;resources-header&#34;&gt;
    UITableView Articles
  &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/05/18/swift-how-to-setting-up-a-table-view/&#34; title=&#34;Swift How-To: Setting up a Table View&#34;&gt;Swift How-To: Setting up a Table View&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/02/12/custom-uitableviewcell-text-input-swift/&#34; title=&#34;Custom UITableViewCell for Text Input in Swift&#34;&gt;Custom UITableViewCell for Text Input 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/2015/07/16/uitableview-swipe-to-delete-workflow-in-swift/&#34; title=&#34;UITableView Swipe to Delete Workflow in Swift&#34;&gt;UITableView Swipe to Delete Workflow in Swift&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;
&lt;p&gt;&lt;a name=&#34;data-source&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;data-soure&#34;&gt;Data Soure&lt;/h4&gt;
&lt;p&gt;The verse list&amp;rsquo;s data source is an &lt;code&gt;NSFetchedResultsController&lt;/code&gt;. This, of course, implies that my underlying data framework for the app is Core Data. Here are some of the articles I&amp;rsquo;ve written on these subjects:&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;core-data&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h5 id=&#34;core-data&#34;&gt;Core Data&lt;/h5&gt;
&lt;div class=&#34;resources&#34;&gt;
  &lt;div class=&#34;resources-header&#34;&gt;
    Core Data Articles
  &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/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;li&gt;
      &lt;i class=&#34;fa fa-angle-right&#34;&gt;&lt;/i&gt; &lt;a href=&#34;http://www.andrewcbancroft.com/2014/07/17/implement-nsmanagedobject-subclass-in-swift/&#34; title=&#34;Implement NSManagedObject Subclass in Swift&#34;&gt;Implement NSManagedObject Subclass 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/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;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;
&lt;p&gt;&lt;a name=&#34;ns-fetched-results-controller&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h5 id=&#34;nsfetchedresultscontroller&#34;&gt;NSFetchedResultsController&lt;/h5&gt;
&lt;div class=&#34;resources&#34;&gt;
  &lt;div class=&#34;resources-header&#34;&gt;
    NSFetchedResultsController Articles
  &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/05/displaying-data-with-nsfetchedresultscontroller-and-swift/&#34; title=&#34;Displaying Data With NSFetchedResultsController and Swift&#34;&gt;Displaying Data With NSFetchedResultsController and 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/2015/05/28/sync-table-view-data-nsfetchedresultscontroller-swift/&#34; title=&#34;Sync Table View Data: NSFetchedResultsController and Swift&#34;&gt;Sync Table View Data: NSFetchedResultsController and 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/2015/02/25/using-swift-to-seed-a-core-data-database/&#34; title=&#34;Using Swift to Seed a Core Data Database&#34;&gt;Using Swift to Seed a Core Data Database&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;
&lt;p&gt;&lt;a name=&#34;add-verse&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;add-verse&#34;&gt;Add Verse&lt;/h3&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/07/Add-Verse.png&#34;&gt;&lt;img src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/07/Add-Verse-576x1024.png&#34; alt=&#34;Add Verse&#34; width=&#34;326&#34; height=&#34;774&#34; class=&#34;alignnone size-large wp-image-12124&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Adding a verse and synchronizing things to the table view (and the persistent data store) involves all-things Core Data and &lt;code&gt;NSFetchedResultsController&lt;/code&gt;. Using the combination of these two made things super easy to keep up-to-date in the UI.&lt;/p&gt;
&lt;p&gt;When you press ‘Save&amp;rsquo;, your verse gets sent to the managed object context for the app. Once it&amp;rsquo;s saved using the context, the verse list gets automatically updated using its &lt;code&gt;NSFetchedResultsController&lt;/code&gt; data source. I really liked the results!&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;memorize&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;memorize&#34;&gt;Memorize&lt;/h3&gt;
&lt;p&gt;The memorization screen brings into play a few UI elements that I&amp;rsquo;ve experimented with and written on in the past. First, let&amp;rsquo;s take a look at these two screen layouts to have the visual before us:&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;read-it-said-it&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;read-it-said-it&#34;&gt;Read It, Said It&lt;/h4&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/07/Memorize-Read-It.png&#34;&gt;&lt;img src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/07/Memorize-Read-It-576x1024.png&#34; alt=&#34;Memorize - Read It&#34; width=&#34;326&#34; height=&#34;774&#34; class=&#34;alignnone size-large wp-image-12123&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/07/Memorize-Said-It.png&#34;&gt;&lt;img src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/07/Memorize-Said-It-576x1024.png&#34; alt=&#34;Memorize - Said It&#34; width=&#34;326&#34; height=&#34;774&#34; class=&#34;alignnone size-large wp-image-12122&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The most notable thing I can think of on this scene is the circular progress indicator to help visualize how close you are to completing the read / said goal.&lt;/p&gt;
&lt;p&gt;A few other subtle notes are the fade in/out animations that happen when you complete the read goal, the said goal, and the peek/hide action. Check out the related articles for those pieces:&lt;/p&gt;
&lt;div class=&#34;resources&#34;&gt;
  &lt;div class=&#34;resources-header&#34;&gt;
    UI Articles
  &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/07/09/circular-progress-indicator-in-swift/&#34; title=&#34;Circular Progress Indicator in Swift&#34;&gt;Circular Progress Indicator 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/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;i class=&#34;fa fa-angle-right&#34;&gt;&lt;/i&gt; &lt;a href=&#34;https://www.andrewcbancroft.com/2015/06/25/swift-uicolor-extension-create-using-rgb-values-not/&#34; title=&#34;Swift UIColor Extension – Create using RGB Values (Not %)&#34;&gt;Swift UIColor Extension – Create using RGB Values (Not %)&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;
&lt;h3 id=&#34;wrapping-up&#34;&gt;Wrapping up&lt;/h3&gt;
&lt;p&gt;While this isn&amp;rsquo;t the first app I&amp;rsquo;ve worked on and published to the App Store, it &lt;em&gt;is&lt;/em&gt; my first &lt;em&gt;personal&lt;/em&gt; project idea that&amp;rsquo;s out there. I had a lot of fun building it as a utility for myself in my own Scripture memorization efforts. My hope is that the tool (and more importantly, &lt;a href=&#34;http://10read10said.andrewcbancroft.com/&#34;&gt;the strategy&lt;/a&gt;) is helpful to others as well!&lt;/p&gt;
&lt;p&gt;My goal in this article was to give you a &amp;ldquo;behind the scenes” look at 10 Read 10 Said. I hope your curiosity has been satisfied by some of these inside looks into the app!&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>UITableView Swipe to Delete Workflow in Swift</title>
      <link>https://www.andrewcbancroft.com/2015/07/16/uitableview-swipe-to-delete-workflow-in-swift/</link>
      <pubDate>Fri, 17 Jul 2015 04:00:18 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2015/07/16/uitableview-swipe-to-delete-workflow-in-swift/</guid>
      <description>&lt;p&gt;Data management applications, by which I mean an app where you&amp;rsquo;re allowing users to add, edit, and delete bits of data as part of your app&amp;rsquo;s core function, very likely use a table view (or two) to visualize lists of information that users of the app can interact with.&lt;/p&gt;
&lt;p&gt;Making &lt;em&gt;changes&lt;/em&gt; to the information listed in the table view and signaling those changes in a fluent way becomes a top concern for these types of apps. How do we allow users to add or remove &amp;ldquo;records” to the system? Furthermore, how do we signal that those changes were effective and refresh the view of the data in the UI?&lt;/p&gt;
&lt;p&gt;The primary concern I want to focus in on in this article is the ever-common paradigm of &amp;ldquo;swipe to delete” when using a table view. What could the workflow of deleting a row from the table view (and its data source) look like? How could it be implemented in Swift? Let&amp;rsquo;s explore…&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;workflow&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;workflow&#34;&gt;Workflow&lt;/h3&gt;
&lt;p&gt;The workflow of &amp;ldquo;swipe to delete” that I typically use in my own applications is this:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Swipe&lt;/strong&gt; a table view row and have the Delete button appear.&lt;/li&gt;
&lt;li&gt;Press the delete button, which triggers a &lt;strong&gt;confirmation&lt;/strong&gt;: &amp;ldquo;Do you really want to delete this?”&lt;/li&gt;
&lt;li&gt;Based on the user&amp;rsquo;s response to the confirmation, &lt;strong&gt;delete the object&lt;/strong&gt; from the data source and remove it from the table view, &lt;strong&gt;or cancel&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The confirmation step is atypical for iOS it seems. Swiping to delete an e-mail or a reminder or just about anything else in Apple&amp;rsquo;s own apps simply deletes the item right away.&lt;/p&gt;
&lt;p&gt;There may be a good reason for this, but I like to give folks an out if they didn&amp;rsquo;t mean to do it. It&amp;rsquo;s fair enough to say, &amp;ldquo;Well, they went to the effort of swiping the row &lt;em&gt;and&lt;/em&gt; pressing the button… surely they mean to do it!”. I still feel more comfortable if I get the opportunity to cancel something like a delete operation. Feel free to disagree there – for this article, I&amp;rsquo;ll assume you want to include that into your delete workflow, and will demonstrate a simple way to implement all three steps of the strategy.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;implementation&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;implementation&#34;&gt;Implementation&lt;/h3&gt;
&lt;p&gt;To demonstrate the implementation of this workflow, suppose that we&amp;rsquo;ve got a table view listing out all of the planets in our solar system (all of the news on Pluto this week has me thinking in this direction, so I just went with it). Feel free to grab the example project over at GitHub:&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;fab fa-github fa-lg&#34;&gt;&lt;/i&gt; &lt;a href=&#34;https://github.com/andrewcbancroft/SwipeToDeleteExample&#34; title=&#34;Swipe to Delete Example&#34;&gt;Swipe to Delete Example&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;
&lt;p&gt;The table view looks like this once its data source is all configured:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/07/iOS-Simulator-Screen-Shot-Jul-16-2015-1.07.06-PM.png&#34;&gt;&lt;img src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/07/iOS-Simulator-Screen-Shot-Jul-16-2015-1.07.06-PM-576x1024.png&#34; alt=&#34;Table View with Planets&#34; width=&#34;576&#34; height=&#34;1024&#34; class=&#34;alignnone size-large wp-image-12096&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/07/iOS-Simulator-Screen-Shot-Jul-16-2015-1.07.06-PM-576x1024.png 576w, https://www.andrewcbancroft.com/wp-content/uploads/2015/07/iOS-Simulator-Screen-Shot-Jul-16-2015-1.07.06-PM-169x300.png 169w, https://www.andrewcbancroft.com/wp-content/uploads/2015/07/iOS-Simulator-Screen-Shot-Jul-16-2015-1.07.06-PM.png 750w&#34; sizes=&#34;(max-width: 576px) 100vw, 576px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;swipe&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;swipe&#34;&gt;Swipe&lt;/h4&gt;
&lt;p&gt;What we want is to be able to swipe one of the rows and delete the planet from the data source and, consequently, remove it from the table view in a nice, fluid way:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/07/iOS-Simulator-Screen-Shot-Jul-16-2015-1.07.11-PM.png&#34;&gt;&lt;img src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/07/iOS-Simulator-Screen-Shot-Jul-16-2015-1.07.11-PM-576x1024.png&#34; alt=&#34;Table View - Delete Button Revealed&#34; width=&#34;576&#34; height=&#34;1024&#34; class=&#34;alignnone size-large wp-image-12097&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/07/iOS-Simulator-Screen-Shot-Jul-16-2015-1.07.11-PM-576x1024.png 576w, https://www.andrewcbancroft.com/wp-content/uploads/2015/07/iOS-Simulator-Screen-Shot-Jul-16-2015-1.07.11-PM-169x300.png 169w, https://www.andrewcbancroft.com/wp-content/uploads/2015/07/iOS-Simulator-Screen-Shot-Jul-16-2015-1.07.11-PM.png 750w&#34; sizes=&#34;(max-width: 576px) 100vw, 576px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;To accomplish this, we need to do a couple of things.&lt;/p&gt;
&lt;p&gt;First, your view controller needs to conform to the &lt;code&gt;UITableViewDelegate&lt;/code&gt; protocol, because the swipe to delete functionality is encapsulated by one of the table view delegate methods. It&amp;rsquo;s a simple change at the class declaration:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { ... }&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;The delegate method that causes the buttons to appear on swipe is called &lt;code&gt;tableView(_:commitEditingStyle:forRowAtIndexPath&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Its implementation for the example I&amp;rsquo;m working through 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;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;UITableViewDataSource&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UITableViewDelegate&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 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&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;deletePlanetIndexPath&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;=&lt;/span&gt; &lt;span class=&#34;kc&#34;&gt;nil&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;// ...&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;commitEditingStyle&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;editingStyle&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UITableViewCellEditingStyle&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;forRowAtIndexPath&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;{&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;if&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;editingStyle&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;Delete&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;deletePlanetIndexPath&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;indexPath&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;planetToDelete&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;planets&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&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;row&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;confirmDelete&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;planetToDelete&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;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;// ...&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;So what&amp;rsquo;s happening here? The primary thing to notice is that we&amp;rsquo;re evaluating the editingStyle from the method&amp;rsquo;s parameter list. Comparing it to the &lt;code&gt;.Delete&lt;/code&gt; &lt;code&gt;UITableViewCellEditingStyle&lt;/code&gt; value is what allows us to know that the Delete button was tapped.&lt;/p&gt;
&lt;p&gt;Since I have a confirmation step to take care of, I&amp;rsquo;ve chosen to store the &lt;code&gt;indexPath&lt;/code&gt; of the row we&amp;rsquo;re wanting to delete in a class-viewable variable so that I can use it later on when we handle the deletion (if the user confirms it).&lt;/p&gt;
&lt;p&gt;The final step of this function is to call &lt;code&gt;confirmDelete()&lt;/code&gt;, which has its own explanation coming up…&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;confirm&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;confirm&#34;&gt;Confirm&lt;/h4&gt;
&lt;p&gt;The next step is confirming that the user really wants to delete the particular planet they&amp;rsquo;ve initiated the delete action on:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/07/iOS-Simulator-Screen-Shot-Jul-16-2015-1.07.15-PM.png&#34;&gt;&lt;img src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/07/iOS-Simulator-Screen-Shot-Jul-16-2015-1.07.15-PM-576x1024.png&#34; alt=&#34;Delete Confirmation&#34; width=&#34;576&#34; height=&#34;1024&#34; class=&#34;alignnone size-large wp-image-12098&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/07/iOS-Simulator-Screen-Shot-Jul-16-2015-1.07.15-PM-576x1024.png 576w, https://www.andrewcbancroft.com/wp-content/uploads/2015/07/iOS-Simulator-Screen-Shot-Jul-16-2015-1.07.15-PM-169x300.png 169w, https://www.andrewcbancroft.com/wp-content/uploads/2015/07/iOS-Simulator-Screen-Shot-Jul-16-2015-1.07.15-PM.png 750w&#34; sizes=&#34;(max-width: 576px) 100vw, 576px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;How is this implemented? It all boils down to using iOS 8&amp;rsquo;s new &lt;code&gt;UIAlertController&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;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;UITableViewDataSource&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UITableViewDelegate&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 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&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;confirmDelete&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;planet&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;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;alert&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIAlertController&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;Delete Planet&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;Are you sure you want to permanently delete &lt;/span&gt;&lt;span class=&#34;si&#34;&gt;\(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;planet&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 class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;preferredStyle&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;ActionSheet&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;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;DeleteAction&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIAlertAction&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;Delete&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;style&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;Destructive&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;handler&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;handleDeletePlanet&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;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;CancelAction&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIAlertAction&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;Cancel&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;style&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;Cancel&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;handler&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;cancelDeletePlanet&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;n&#34;&gt;alert&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;addAction&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;DeleteAction&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;alert&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;addAction&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;CancelAction&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;// Support display in iPad&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;alert&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;popoverPresentationController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;?.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;sourceView&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;view&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;alert&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;popoverPresentationController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;?.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;sourceRect&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;CGRectMake&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;view&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;bounds&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;size&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;width&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;/&lt;/span&gt; &lt;span class=&#34;mf&#34;&gt;2.0&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;view&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;bounds&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;size&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;height&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;/&lt;/span&gt; &lt;span class=&#34;mf&#34;&gt;2.0&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;mf&#34;&gt;1.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;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;kc&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&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;alert&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;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&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;We&amp;rsquo;re asking the user if they&amp;rsquo;re sure they want to permanently delete the planet using the &lt;code&gt;.ActionSheet&lt;/code&gt; style.&lt;/p&gt;
&lt;p&gt;The next step of the function is to create a couple of &lt;code&gt;UIAlertAction&lt;/code&gt; buttons: one for Delete and one for Cancel, with the appropriate style (&lt;code&gt;.Destructive&lt;/code&gt; and &lt;code&gt;.Cancel&lt;/code&gt;, respectively).&lt;/p&gt;
&lt;p&gt;Finally, we provide a function to each &lt;code&gt;UIAlertAction&lt;/code&gt; instance. The handlers, of course, could have been implemented with closures, but I chose to built out a couple of named functions, just to be able to step through it with you, and to separate out the logic of the steps a little more. Feel free to do whichever seems most natural to you.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;delete-cancel&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;delete-or-cancel&#34;&gt;Delete or cancel&lt;/h4&gt;
&lt;p&gt;This is the final step! The following code snippet is an implementation of the two handlers specified for the initialization step of the &lt;code&gt;UIAlertAction&lt;/code&gt; buttons, which were &lt;code&gt;handleDeletePlanet&lt;/code&gt; and &lt;code&gt;cancelDeletePlanet&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;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;UITableViewDataSource&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UITableViewDelegate&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 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&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;deletePlanetIndexPath&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;=&lt;/span&gt; &lt;span class=&#34;kc&#34;&gt;nil&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;// ...&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;handleDeletePlanet&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;alertAction&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIAlertAction&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&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;if&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;indexPath&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;deletePlanetIndexPath&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;tableView&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;beginUpdates&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;n&#34;&gt;planets&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;bp&#34;&gt;removeAtIndex&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&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;row&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;// Note that indexPath is wrapped in an array:  [indexPath]&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;tableView&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;deleteRowsAtIndexPaths&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;([&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;withRowAnimation&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;Automatic&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;deletePlanetIndexPath&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&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;n&#34;&gt;tableView&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;endUpdates&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&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;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;cancelDeletePlanet&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;alertAction&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIAlertAction&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;25&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;deletePlanetIndexPath&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&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;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;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;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;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Clearly, &lt;code&gt;handleDeletePlanet()&lt;/code&gt; is the most involved. The essential process is this:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Call &lt;code&gt;beginUpdates()&lt;/code&gt; on the table view instance to signal the start of UI updates to the table view.&lt;/li&gt;
&lt;li&gt;Remove the planet from the data source using the &lt;code&gt;deletePlanetIndexPath&lt;/code&gt; we set in the alert controller step.&lt;/li&gt;
&lt;li&gt;Call &lt;code&gt;deleteRowsAtIndexPaths()&lt;/code&gt; on the table view to remove the planet from the UI.&lt;/li&gt;
&lt;li&gt;Reset the class-viewable &lt;code&gt;deletePlanetIndexPath&lt;/code&gt; to nil.&lt;/li&gt;
&lt;li&gt;Call &lt;code&gt;endUpdates()&lt;/code&gt; on the table view instance to complete the UI updates.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;There&amp;rsquo;s one gotcha, that I&amp;rsquo;ve tried to highlight by way of comment in the code snippet: Notice that within the call to &lt;code&gt;deleteRowsAtIndexPaths&lt;/code&gt; (plural), I&amp;rsquo;ve wrapped the &lt;code&gt;indexPath&lt;/code&gt; we&amp;rsquo;re removing in an array. It&amp;rsquo;s subtle, but this delegate method expects an array of &lt;code&gt;indexPath&lt;/code&gt; instances, not a single index path instance. It&amp;rsquo;s simple enough, but it can catch you off guard if you&amp;rsquo;re in the mindset of removing a single row and come across this method, which is flexible enough to allow you to delete several at a time.&lt;/p&gt;
&lt;h3 id=&#34;wrapping-up&#34;&gt;Wrapping up&lt;/h3&gt;
&lt;p&gt;Swipe -&amp;gt; confirm -&amp;gt; delete or cancel. Those are the steps we analyzed in this commonly needed workflow in data management applications.&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/05/18/swift-how-to-setting-up-a-table-view/&#34; title=&#34;Swift How-To: Setting up a Table View&#34;&gt;Swift How-To: Setting up a Table View&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/11/24/swift-uitableviewdatasource-cheat-sheet/&#34; title=&#34;Swift UITableViewDataSource Cheat Sheet&#34;&gt;Swift UITableViewDataSource Cheat Sheet&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/05/28/sync-table-view-data-nsfetchedresultscontroller-swift/&#34; title=&#34;Sync Table View Data: NSFetchedResultsController and Swift&#34;&gt;Sync Table View Data: NSFetchedResultsController and 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/2015/02/12/custom-uitableviewcell-text-input-swift/&#34; title=&#34;Custom UITableViewCell for Text Input in Swift&#34;&gt;Custom UITableViewCell for Text Input 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>Circular Progress Indicator in Swift</title>
      <link>https://www.andrewcbancroft.com/2015/07/09/circular-progress-indicator-in-swift/</link>
      <pubDate>Fri, 10 Jul 2015 04:31:49 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2015/07/09/circular-progress-indicator-in-swift/</guid>
      <description>&lt;p&gt;&lt;small&gt;Updated on April 17, 2017 – Swift 3&lt;/small&gt;&lt;/p&gt;
&lt;p&gt;Circular progress indicators are a nice and compact way to visualize progress information for users of your iOS app. I was extremely grateful to come across &lt;a href=&#34;https://github.com/kaandedeoglu/KDCircularProgress&#34;&gt;Kaan Dedeoglu&amp;rsquo;s KDCircularProgress project on GitHub&lt;/a&gt;. It&amp;rsquo;s a versatile little UI component written in Swift that provides a great amount of flexibility and customization options. I love it!&lt;/p&gt;
&lt;p&gt;As a bonus to this amazing component, recent contributions by Kaan have opened up the ability for us to lay out and set up the circular progress indicator in the Storyboard!&lt;/p&gt;
&lt;p&gt;My goal in this article is to help get you up and running with this library in your own Storyboard-based Swift project.&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;h3 id=&#34;example-project&#34;&gt;Example project&lt;/h3&gt;
&lt;p&gt;Kaan&amp;rsquo;s GitHub repository has an example project, but I&amp;rsquo;ve also gone ahead and made one as well for showing how to use this indicator in a Storyboard. We&amp;rsquo;ll be using the example I created as a reference point for the forthcoming walk-through.&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;fab fa-github fa-lg&#34;&gt;&lt;/i&gt; &lt;a href=&#34;https://github.com/andrewcbancroft/CircularProgressExample&#34; title=&#34;Circular Progress Example&#34;&gt;Circular Progress Example&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/07/circprogexample1.gif&#34;&gt;&lt;img src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/07/circprogexample1.gif&#34; alt=&#34;Circular Progress Example&#34; width=&#34;372&#34; height=&#34;681&#34; class=&#34;alignnone size-full wp-image-12087&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;getting-started&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;getting-started&#34;&gt;Getting started&lt;/h3&gt;
&lt;p&gt;The most obvious starting place is to head over to the &lt;a href=&#34;https://github.com/kaandedeoglu/KDCircularProgress&#34;&gt;KDCircularProgress repository on GitHub&lt;/a&gt; to grab the &lt;a href=&#34;https://github.com/kaandedeoglu/KDCircularProgress/blob/master/KDCircularProgress/KDCircularProgress.swift&#34;&gt;KDCircularProgress.swift source file&lt;/a&gt; and add it to your own Swift project. Kaan has things set up with CocoaPods as well if you care to use that. I found adding the .swift file to my project to be the least intrusive way to add this indicator to my UI components arsenal.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;to-storyboard&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;to-the-storyboard&#34;&gt;To the Storyboard!&lt;/h3&gt;
&lt;p&gt;Since Kaan added &lt;code&gt;IBDesignable&lt;/code&gt; and &lt;code&gt;IBInspectable&lt;/code&gt; support, working with the circular progress indicator in the Storyboard is super easy.&lt;/p&gt;
&lt;p&gt;To get started, all you need to do is drag a plain View over to the Storyboard Scene. Set up your constraints to correctly position the view where you need it:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/07/UIView-with-Constraints.png&#34;&gt;&lt;img src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/07/UIView-with-Constraints.png&#34; alt=&#34;UIView with Constraints&#34; width=&#34;906&#34; height=&#34;855&#34; class=&#34;alignnone size-full wp-image-12080&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/07/UIView-with-Constraints.png 906w, https://www.andrewcbancroft.com/wp-content/uploads/2015/07/UIView-with-Constraints-300x283.png 300w&#34; sizes=&#34;(max-width: 906px) 100vw, 906px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Next, you need to set the View&amp;rsquo;s class to a &lt;em&gt;custom&lt;/em&gt; class, namely, &lt;code&gt;KDCircularProgress&lt;/code&gt;:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/07/Set-View-Class.png&#34;&gt;&lt;img src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/07/Set-View-Class.png&#34; alt=&#34;Set View Class&#34; width=&#34;916&#34; height=&#34;701&#34; class=&#34;alignnone size-full wp-image-12081&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/07/Set-View-Class.png 916w, https://www.andrewcbancroft.com/wp-content/uploads/2015/07/Set-View-Class-300x230.png 300w&#34; sizes=&#34;(max-width: 916px) 100vw, 916px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;With that in place, Xcode will process things and allow you to modify the properties of the progress indicator directly in the attributes section of the Utilities pane. Best of all, you&amp;rsquo;ll see those changes be reflected in the Storyboard scene in real-time as you adjust values!&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/07/Customized-in-storyboard.png&#34;&gt;&lt;img src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/07/Customized-in-storyboard.png&#34; alt=&#34;Customized in Storyboard&#34; width=&#34;841&#34; height=&#34;702&#34; class=&#34;alignnone size-full wp-image-12083&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/07/Customized-in-storyboard.png 841w, https://www.andrewcbancroft.com/wp-content/uploads/2015/07/Customized-in-storyboard-300x250.png 300w&#34; sizes=&#34;(max-width: 841px) 100vw, 841px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;view-did-load&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;viewdidload&#34;&gt;viewDidLoad()&lt;/h3&gt;
&lt;p&gt;While in the Storyboard, I set a few values so that I could actually see certain colors when progress had been made. However, to get things set to their true initial state, there may be some values you want to reset in &lt;code&gt;viewDidLoad()&lt;/code&gt;. For example, I set the &lt;code&gt;angle&lt;/code&gt; property to 90 degrees so that I could see the color of the progress track in the storyboard. But when I load the app, I want the angle to be 0 degrees (since no progress has been made when the app first loads).&lt;/p&gt;
&lt;p&gt;To accomplish this, you&amp;rsquo;d simply make sure there&amp;rsquo;s an outlet between your progress view in the Storyboard and the View Controller. Once they&amp;rsquo;re connected, you can write something as simple as &lt;code&gt;circularProgressView.angle = 0&lt;/code&gt; to start off with no progress.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;increasing-progress&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;increasing-progress&#34;&gt;Increasing progress&lt;/h3&gt;
&lt;p&gt;We&amp;rsquo;re dealing with a progress indicator here, so this implies that there&amp;rsquo;s a beginning, some incremental steps taken toward a completion goal, and of course, the fully completed &lt;em&gt;whatever it was you were doing&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;The example I&amp;rsquo;ve contrived is a simple counter with an upper limit of 5. So as you tap &amp;ldquo;Increase Progress”, the circular progress view should update to be some fraction of the way around the 360 degree circle based upon how close we are to completing the count to 5.&lt;/p&gt;
&lt;p&gt;There&amp;rsquo;s a function I&amp;rsquo;ve built to calculate the new angle:&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;newAngle&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;Double&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;360&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;*&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;currentCount&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;/&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;maxCount&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;The rest is simply a matter of updating the current count and animating to the new angle:&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;increaseProgressButtonTapped&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;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;currentCount&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;!=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;maxCount&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;currentCount&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;+=&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;1&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;newAngleValue&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;newAngle&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;n&#34;&gt;circularProgressView&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;toAngle&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;newAngleValue&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;mf&#34;&gt;0.5&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;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;&lt;a name=&#34;resetting&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;resetting-the-indicator&#34;&gt;Resetting the indicator&lt;/h3&gt;
&lt;p&gt;To reset everything, we&amp;rsquo;d want to update the state of our current count back to 0.&lt;/p&gt;
&lt;p&gt;The change to the circular progress indicator&amp;rsquo;s visualization of the progress state can be animated by calling the view&amp;rsquo;s &lt;code&gt;animate(fromAngle:toAngle:duration:completion:)&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;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;resetButtonTapped&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;n&#34;&gt;currentCount&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;0&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;circularProgressView&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;fromAngle&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;circularProgressView&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;angle&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;toAngle&lt;/span&gt;&lt;span class=&#34;p&#34;&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 class=&#34;n&#34;&gt;duration&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;mf&#34;&gt;0.5&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;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;h3 id=&#34;wrapping-up&#34;&gt;Wrapping up&lt;/h3&gt;
&lt;p&gt;There are a &lt;em&gt;ton&lt;/em&gt; of other customization options that you can play with. I highly recommend this UI component if you&amp;rsquo;re looking for an easy-to-use, versatile circular progress indicator!&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>Every Swift Value Type Should Be Equatable</title>
      <link>https://www.andrewcbancroft.com/2015/07/01/every-swift-value-type-should-be-equatable/</link>
      <pubDate>Wed, 01 Jul 2015 18:07:35 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2015/07/01/every-swift-value-type-should-be-equatable/</guid>
      <description>&lt;p&gt;As I listened to the WWDC15 talk on &lt;a href=&#34;https://developer.apple.com/videos/wwdc/2015/?id=414&#34;&gt;Building Better Apps with Value Types in Swift&lt;/a&gt; I was struck by a sentence that I had never dawned on me before:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Every Value Type should be Equatable.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;That is, every Value Type should conform to the &lt;code&gt;Equatable&lt;/code&gt; protocol.&lt;/p&gt;
&lt;p&gt;Talk about a sweeping statement! Wow – &lt;em&gt;every&lt;/em&gt; Value Type should be &lt;code&gt;Equatable&lt;/code&gt;? Hmm… Let&amp;rsquo;s unpack the &amp;ldquo;why&amp;rsquo;s” and &amp;ldquo;how&amp;rsquo;s” of this statement.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;why&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;why&#34;&gt;Why?&lt;/h3&gt;
&lt;p&gt;I&amp;rsquo;d never thought about why I might want my Value Types in Swift to be Equatable. Not that I thought it&amp;rsquo;d be a terrible idea to implement the &lt;code&gt;==&lt;/code&gt; operator for a Type… I just never realized that this was actually &lt;em&gt;expected&lt;/em&gt; behavior of Value Types!&lt;/p&gt;
&lt;p&gt;The reasoning in the talk was that Values are intuitively &lt;em&gt;meant&lt;/em&gt; to be compared for equality. &lt;em&gt;Because&lt;/em&gt; they&amp;rsquo;re &lt;em&gt;Values&lt;/em&gt;, there is inherent expectation from clients of the Type to be able to ask and know if one Value is equal to another Value of the same Type.&lt;/p&gt;
&lt;p&gt;We naturally expect to be able to ask two variables/constants, each holding &lt;code&gt;Int&lt;/code&gt; Values (because in Swift, &lt;code&gt;Int&lt;/code&gt; is a Value Type), if they equal each other. And we naturally expect the comparison to compare the actual &lt;em&gt;numbers&lt;/em&gt;… the &lt;em&gt;Values themlselves&lt;/em&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;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;a&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;10&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;b&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;5&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;+&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;2&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;+&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;3&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;a&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;==&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;b&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;// true&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;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;x&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;1&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;y&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;2&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;x&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;==&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;y&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;// false&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Likewise, we naturally expect to ask two Strings if &lt;em&gt;they&amp;rsquo;re&lt;/em&gt; equal:&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;str1&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;I love 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;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;str2&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;I love 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;3&lt;/span&gt;&lt;span class=&#34;cl&#34;&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;str2&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;// true&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&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;str3&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;i love 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;7&lt;/span&gt;&lt;span class=&#34;cl&#34;&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;str3&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;// false - case-sensitive comparison&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In fact, we naturally expect to ask these kinds of equality questions about &lt;em&gt;any&lt;/em&gt; of the Swift standard library Value Types, don&amp;rsquo;t we?&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;how&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;how&#34;&gt;How?&lt;/h3&gt;
&lt;p&gt;We &lt;em&gt;do&lt;/em&gt; expect to test for equality between two Value Types. It just makes sense.&lt;/p&gt;
&lt;p&gt;So now the question is, &amp;ldquo;&lt;em&gt;How&lt;/em&gt;?”&lt;/p&gt;
&lt;p&gt;The simple answer is that our Value Types need to implement an &lt;code&gt;==&lt;/code&gt; operator. But there&amp;rsquo;s something really important to consider:&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;equality-properties&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;properties-of-equality&#34;&gt;Properties of equality&lt;/h4&gt;
&lt;p&gt;To be truly equal, the &lt;code&gt;==&lt;/code&gt; operator not only needs to be implemented, but it needs to be implemented in such a way that it &lt;em&gt;behaves&lt;/em&gt; as we&amp;rsquo;d expect when doing our comparisons. During the talk, Doug mentioned three important properties of equality that need to hold for our Value Types:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The comparison must be &lt;strong&gt;reflexive&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;The comparison must be &lt;strong&gt;symmetric&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;The comparison must be &lt;strong&gt;transitive&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;That sounds awfully &amp;ldquo;math-y”. In fact, it&amp;rsquo;s the exact same terminology used in mathematics. But don&amp;rsquo;t worry, the terminology is simple and natural to understand.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;reflexive&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h5 id=&#34;reflexive&#34;&gt;Reflexive&lt;/h5&gt;
&lt;p&gt;To be reflexive, the Type&amp;rsquo;s &lt;code&gt;==&lt;/code&gt; operator needs to make sure that the expression &lt;code&gt;x == x&lt;/code&gt; returns &lt;code&gt;true&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;So if I have &lt;code&gt;let x = 1&lt;/code&gt; and I write the expression &lt;code&gt;x == x&lt;/code&gt;, I do in fact get &lt;code&gt;true&lt;/code&gt; because &lt;code&gt;Int&lt;/code&gt;‘s equality operator is reflexive (as expected).&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;symmetric&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h5 id=&#34;symmetric&#34;&gt;Symmetric&lt;/h5&gt;
&lt;p&gt;To be symmetric, the Type&amp;rsquo;s &lt;code&gt;==&lt;/code&gt; operator needs to compute things in such a way that the expression &lt;code&gt;x == y&lt;/code&gt; and &lt;code&gt;y == x&lt;/code&gt; return the same value.&lt;/p&gt;
&lt;p&gt;Here&amp;rsquo;s an example of symmetry:&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;x&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;1&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;y&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;1&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;n&#34;&gt;x&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;==&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;y&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;// true&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;y&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;==&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;x&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;// true&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;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;str1&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;Hi&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;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;str2&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&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;n&#34;&gt;x&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;==&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;y&lt;/span&gt; &lt;span class=&#34;c1&#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;11&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;n&#34;&gt;y&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;==&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;x&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;// false&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;transitive&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h5 id=&#34;transitive&#34;&gt;Transitive&lt;/h5&gt;
&lt;p&gt;Finally, to be transitive, the Type&amp;rsquo;s &lt;code&gt;==&lt;/code&gt; operator needs to compute things in such a way that when &lt;code&gt;x == y&lt;/code&gt; is &lt;code&gt;true&lt;/code&gt;, and &lt;code&gt;y == z&lt;/code&gt; is &lt;code&gt;true&lt;/code&gt;, then &lt;code&gt;x == z&lt;/code&gt; is also &lt;code&gt;true&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Here&amp;rsquo;s an example of transitivity:&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;x&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;100&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;y&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;50&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;+&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;50&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;z&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;50&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;*&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;2&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;n&#34;&gt;x&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;==&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;y&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;// true&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;y&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;==&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;z&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;// true&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;x&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;==&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;z&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;// true&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;implementation&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;implementation&#34;&gt;Implementation&lt;/h4&gt;
&lt;p&gt;&lt;em&gt;Most&lt;/em&gt; of the time, the implementation of &lt;code&gt;==&lt;/code&gt; is very simple. If your Value Type is comprised of other Value Types that have an &lt;code&gt;==&lt;/code&gt; operator that&amp;rsquo;s correctly implemented with the semantics I just described, then the implementation for your Type is straight-forward.&lt;/p&gt;
&lt;p&gt;An example might help to set things up for understanding. Suppose that we&amp;rsquo;re building a sight-seeing app for a local tourism company. We&amp;rsquo;ve got a struct called &lt;code&gt;Place&lt;/code&gt; to help us encapsulate the idea of… well… a &amp;ldquo;place” to visit. It looks something like this:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-swift&#34; data-lang=&#34;swift&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;struct&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;Place&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;name&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;String&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;3&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;latitude&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;Double&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;longitude&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;Double&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;c1&#34;&gt;// init is auto-generated by the compiler in this case&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;p&gt;Since &lt;code&gt;Place&lt;/code&gt; is a Value Type (Struct) which is comprised of other Value Types, you&amp;rsquo;d simply need to do something like the following to make it &lt;code&gt;Equatable&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;Place&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;Equatable&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 class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;==(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;lhs&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Place&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;rhs&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Place&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;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;areEqual&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;lhs&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;rhs&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;o&#34;&gt;&amp;amp;&amp;amp;&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;lhs&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;latitude&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;==&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;rhs&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;latitude&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;amp;&amp;amp;&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;lhs&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;longitude&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;==&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;rhs&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;longitude&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;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;areEqual&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;One of the first things to notice is that the &lt;code&gt;==&lt;/code&gt; operator has to be implemented as a stand-alone &lt;em&gt;global&lt;/em&gt; function, rather than as part of the Type definition.&lt;/p&gt;
&lt;p&gt;Notice also that even though we have the source for the Type that we want to make &lt;code&gt;Equatable&lt;/code&gt;, I chose to signal the &lt;code&gt;Equatable&lt;/code&gt; protocol adoption through an &lt;em&gt;extension&lt;/em&gt; on the Type, rather than at the Type declaration itself. Both are acceptable, but it&amp;rsquo;s become convention to use the extension strategy for this particular protocol.&lt;/p&gt;
&lt;p&gt;The implementation of &lt;code&gt;==&lt;/code&gt; uses the intuitive semantics that one &lt;code&gt;Place&lt;/code&gt; isn&amp;rsquo;t the same as another &lt;code&gt;Place&lt;/code&gt; unless the &lt;code&gt;name&lt;/code&gt;s, &lt;code&gt;latidude&lt;/code&gt;s, and &lt;code&gt;longitude&lt;/code&gt;s are all the same.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;lhs&lt;/code&gt; and &lt;code&gt;rhs&lt;/code&gt; simply mean &amp;ldquo;left-hand side” and &amp;ldquo;right-hand side”, respectively. Since there&amp;rsquo;s a &lt;code&gt;Place&lt;/code&gt; instance on the left-hand side of the &lt;code&gt;==&lt;/code&gt; operator, and a &lt;code&gt;Place&lt;/code&gt; instance on the right-hand side of the &lt;code&gt;==&lt;/code&gt; operator when we use it in practice, it makes sense to label these parameters according to that pattern.&lt;/p&gt;
&lt;p&gt;The implementation could literally be read as, &amp;ldquo;If the &lt;code&gt;Place&lt;/code&gt; on the left-hand side&amp;rsquo;s &lt;code&gt;name&lt;/code&gt; is equal to the &lt;code&gt;Place&lt;/code&gt; on the right-hand side&amp;rsquo;s &lt;code&gt;name&lt;/code&gt;, AND … the &lt;code&gt;latitude&lt;/code&gt; … AND … the &lt;code&gt;longitude&lt;/code&gt;, then the two &lt;code&gt;Place&lt;/code&gt; instances are equal.”&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;reference-types&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h5 id=&#34;dealing-with-reference-types&#34;&gt;Dealing with reference types&lt;/h5&gt;
&lt;p&gt;If Reference Types are involved with your Value Type implementation, things could get a little more complicated. &amp;ldquo;Complicated” probably isn&amp;rsquo;t the right word… but you do have to &lt;em&gt;think&lt;/em&gt; a little more about your Type&amp;rsquo;s equality semantics.&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s modify the example just a little bit:&lt;/p&gt;
&lt;p&gt;Supposing that &lt;code&gt;Place&lt;/code&gt; had an additional property called &lt;code&gt;featureImage&lt;/code&gt; which held a reference to a &lt;code&gt;UIImage&lt;/code&gt; instance (a Reference Type), we&amp;rsquo;d need to test for equality a little bit differently. And &lt;em&gt;how&lt;/em&gt; we test for equality depends on the particulars of our Type&amp;rsquo;s equality semantics:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Are the two &lt;code&gt;Place&lt;/code&gt;s equal if both of them point to the same &lt;code&gt;featureImage&lt;/code&gt; (ie, should we just use &lt;code&gt;===&lt;/code&gt; to check and see if the references are the same)?&lt;/li&gt;
&lt;li&gt;OR, are the two &lt;code&gt;Place&lt;/code&gt;s equal if both of their &lt;code&gt;featureImage&lt;/code&gt; instances contain the same underlying bitmap (ie, they&amp;rsquo;re the same picture in &lt;em&gt;essence&lt;/em&gt;)?&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As you can see, the phrase &amp;ldquo;it depends” applies here. Certainly we need to test for &lt;em&gt;some&lt;/em&gt; kind of equality on the &lt;code&gt;featureImage&lt;/code&gt; in order to have a complete &lt;code&gt;==&lt;/code&gt; implementation. But how we go about it really comes down to the semantics that you and others would expect from asking the question, &amp;ldquo;Is this &lt;code&gt;Place&lt;/code&gt; equivalent to that &lt;code&gt;Place&lt;/code&gt;?”&lt;/p&gt;
&lt;p&gt;For this example, I&amp;rsquo;m going to go with the latter statement: that two &lt;code&gt;Places&lt;/code&gt; are equal if both of their &lt;code&gt;featureImage&lt;/code&gt; instances contain the same underlying bitmap.&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;Place&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;Equatable&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 class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;==(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;lhs&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Place&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;rhs&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Place&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; 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;areEqual&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;lhs&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;rhs&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;o&#34;&gt;&amp;amp;&amp;amp;&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;lhs&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;latitude&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;==&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;rhs&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;latitude&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;amp;&amp;amp;&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;lhs&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;longitude&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;==&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;rhs&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;longitude&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;amp;&amp;amp;&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;lhs&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;featureImage&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;isEqual&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;rhs&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;featureImage&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;// depends on your Type&amp;#39;s equality semantics&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;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;areEqual&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;h3 id=&#34;wrapping-up&#34;&gt;Wrapping up&lt;/h3&gt;
&lt;p&gt;Every Value Type should conform to the &lt;code&gt;Equatable&lt;/code&gt; protocol. In this article, we unpacked the &amp;ldquo;why&amp;rsquo;s” and the &amp;ldquo;how&amp;rsquo;s” of this fundamental characteristic of Value Types. From here, we&amp;rsquo;ve all got to jump on board and ensure that we meet this expectation in our code!&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/12/02/send-a-type-to-obedience-school-using-swift-extensions-for-additional-protocol-conformance/&#34; title=&#34;Send a Type to Obedience School – Using Swift Extensions for Additional Protocol Conformance&#34;&gt;Send a Type to Obedience School – Using Swift Extensions for Additional Protocol Conformance&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;
&lt;p&gt;&lt;a name=&#34;share&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Swift UIColor Extension – Create using RGB Values (Not %)</title>
      <link>https://www.andrewcbancroft.com/2015/06/25/swift-uicolor-extension-create-using-rgb-values-not/</link>
      <pubDate>Fri, 26 Jun 2015 03:11:48 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2015/06/25/swift-uicolor-extension-create-using-rgb-values-not/</guid>
      <description>&lt;p&gt;I&amp;rsquo;ll say it up front – I&amp;rsquo;m not a great designer. What tends to happen with me and projects is that I end up saving all-things color until the &lt;em&gt;end&lt;/em&gt; of a project. You know… the old procrastinator&amp;rsquo;s motto: &amp;ldquo;If you don&amp;rsquo;t get it done today and tomorrow doesn&amp;rsquo;t come, then you ain&amp;rsquo;t gotta do it!”&lt;/p&gt;
&lt;p&gt;So the other day came and I had to &amp;ldquo;do color”…&lt;/p&gt;
&lt;p&gt;Thankfully, a buddy of mine has good skills in the area, and gave me a color palette to use. Score!&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;working-with-colors&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;working-with-colors&#34;&gt;Working with colors&lt;/h3&gt;
&lt;p&gt;I decided it might be nice to go ahead and encapsulate my color scheme in code and set the various tints and font colors and navigation bar backgrounds to their appropriate values in code.&lt;/p&gt;
&lt;p&gt;This seemed like a really nice way to keep all the color stuff in once place, so that if I ever needed to change things, I&amp;rsquo;d be able to do it in one spot, and the whole app would just magically take on the adjustments.&lt;/p&gt;
&lt;p&gt;Inevitably, I&amp;rsquo;d need to create myself some &lt;code&gt;UIColor&lt;/code&gt; instances.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;baffled&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;baffled&#34;&gt;Baffled&lt;/h3&gt;
&lt;p&gt;For those of us who spend a lot of time designing in the Storyboard, as opposed to creating views and layouts directly in code, coming up against the &lt;code&gt;UIColor&lt;/code&gt; API when you haven&amp;rsquo;t in a while can be…well…baffling. Not because it&amp;rsquo;s &amp;ldquo;hard” but because it&amp;rsquo;s not as intuitive as it looks on first-sight. Here&amp;rsquo;s what I mean…&lt;/p&gt;
&lt;p&gt;My color-adept friend had given me some hex color values. Looking at &lt;code&gt;UIColor&lt;/code&gt; of course, there&amp;rsquo;s no initializer or class function to create a color with such a value. No worries, though – there are plenty of converters out there!&lt;/p&gt;
&lt;p&gt;So I got my red, green, and blue values all in order and did the thing that seemed most obvious: I supplied &lt;code&gt;UIColor&lt;/code&gt;‘s init method with those values, verbatim:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;let primaryColor = UIColor(red: 39.0, green: 44.0, blue: 79.0, alpha: 1.0)&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;And then I realized that I hit the same &amp;ldquo;gotcha” that I had before. &lt;code&gt;UIColor&lt;/code&gt; takes a &lt;em&gt;percentage&lt;/em&gt; of red, green, and blue, not the &lt;em&gt;value&lt;/em&gt; of red, green, and blue. I needed to divide the values by 255.0 each in order to obtain a value between 0 and 1 as the initializer requires. Sure, this is in &lt;a href=&#34;https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIColor_Class/#//apple_ref/occ/instm/UIColor/initWithRed:green:blue:alpha:&#34;&gt;the documentation&lt;/a&gt;:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;The red component of the color object, specified as a value from 0.0 to 1.0.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Similar instructions appear for green, blue, and alpha.&lt;/p&gt;
&lt;p&gt;It&amp;rsquo;s easy enough to fix, but man – that sure seems like an easy-to-fall-for mistake if you&amp;rsquo;re coming to this API after spending time away from it, or if you&amp;rsquo;re brand new to iOS development.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;uicolor-extension&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;uicolor-extension&#34;&gt;UIColor extension&lt;/h3&gt;
&lt;p&gt;Having to divide the RGB values by 255.0 every time was just annoying enough that I created a &lt;code&gt;UIColor&lt;/code&gt; extension to help me be able to do the intuitive thing and just supply the RGB values verbatim, like I&amp;rsquo;d done to begin with. Here it is for your enjoyment:&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;UIColor&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;static&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;colorWithRedValue&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(#&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;redValue&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;CGFloat&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;greenValue&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;CGFloat&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;blueValue&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;CGFloat&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;n&#34;&gt;CGFloat&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;UIColor&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;3&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIColor&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;red&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;redValue&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;/&lt;/span&gt;&lt;span class=&#34;mf&#34;&gt;255.0&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;green&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;greenValue&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;/&lt;/span&gt;&lt;span class=&#34;mf&#34;&gt;255.0&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;blue&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;blueValue&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;/&lt;/span&gt;&lt;span class=&#34;mf&#34;&gt;255.0&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;n&#34;&gt;alpha&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;h3 id=&#34;wrapping-up&#34;&gt;Wrapping up&lt;/h3&gt;
&lt;p&gt;I feel a little silly for having made this mistake, but hey – making the mistake is a good way to remember what not to do in the future. And now that I&amp;rsquo;ve written an extension (and this blog post), maybe I&amp;rsquo;ll be set for next time I spend extended periods of time avoiding colors.&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/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;i class=&#34;fa fa-angle-right&#34;&gt;&lt;/i&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;i class=&#34;fa fa-angle-right&#34;&gt;&lt;/i&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;
&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>Creating Calendars with Event Kit and Swift</title>
      <link>https://www.andrewcbancroft.com/2015/06/17/creating-calendars-with-event-kit-and-swift/</link>
      <pubDate>Wed, 17 Jun 2015 17:46:29 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2015/06/17/creating-calendars-with-event-kit-and-swift/</guid>
      <description>&lt;p&gt;&lt;small&gt;Updated on April 19, 2016 – Swift 2.1 | Added example project&lt;/small&gt;&lt;/p&gt;
&lt;p&gt;Apple’s Event Kit framework empowers developers to interact with an iOS device’s calendar database. Not only can we read calendars and events from the database, we can also &lt;em&gt;create&lt;/em&gt; calendars.&lt;/p&gt;
&lt;p&gt;In a previous article, we looked at &lt;a href=&#34;http://www.andrewcbancroft.com/2015/05/14/beginners-guide-to-eventkit-in-swift-requesting-permission/&#34; title=&#34;Beginner’s Guide to EventKit in Swift – Requesting Permission&#34;&gt;how to handle asking the user for permission to access their calendars&lt;/a&gt;. Now my goal is to show you how to create local calendars on the user&amp;rsquo;s device programmatically with Swift using the Event Kit framework.&lt;/p&gt;
&lt;p&gt;Here&amp;rsquo;s a demo of what we&amp;rsquo;re going for by the time I&amp;rsquo;m finished with this article:&lt;br&gt;
&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/06/CreateCalendarDemo.gif&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/06/CreateCalendarDemo.gif&#34; alt=&#34;Create Calendar Demo&#34; width=&#34;473&#34; height=&#34;845&#34; class=&#34;alignnone size-full wp-image-12814&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;If you&amp;rsquo;d like to tinker with the code for yourself, you can download the example project:&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;fab fa-github fa-lg&#34;&gt;&lt;/i&gt; &lt;a href=&#34;https://github.com/andrewcbancroft/EventTracker/tree/create-calendar&#34; title=&#34;Event Tracker - Create Calendar&#34;&gt;Event Tracker – Create Calendar&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;
&lt;p&gt;&lt;a name=&#34;import-eventkit&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;import-eventkit&#34;&gt;Import EventKit&lt;/h3&gt;
&lt;p&gt;Step 1 in this whole process will be to import EventKit at the top of your Swift file:&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 class=&#34;kd&#34;&gt;import&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;EventKit&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;3&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;4&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;// ...&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Importing EventKit gives us access to everything we need to work with calendars.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;general-outline&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;general-outline-for-creating-a-local-calendar&#34;&gt;General outline for creating a local calendar&lt;/h3&gt;
&lt;p&gt;Now, we&amp;rsquo;ll proceed with creating a local calendar on the user&amp;rsquo;s device. It&amp;rsquo;s important to note that there are other kinds of calendars that you can create. For example, you can create calendars that sync with iCloud. For now though, we&amp;rsquo;re going to narrow the focus down to just creating the calendar on the user&amp;rsquo;s local device.&lt;/p&gt;
&lt;p&gt;Here&amp;rsquo;s the general outline (and then some code):&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Create an &lt;code&gt;EKEventStore&lt;/code&gt; instance&lt;/li&gt;
&lt;li&gt;Create a new &lt;code&gt;EKCalendar&lt;/code&gt; instance using that event store instance&lt;/li&gt;
&lt;li&gt;Configure the new calendar&amp;rsquo;s &lt;code&gt;title&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Wire up the new calendar&amp;rsquo;s source
&lt;ul&gt;
&lt;li&gt;Obtain a list of the available sources from the event store instance&lt;/li&gt;
&lt;li&gt;Filter that list down to the &lt;code&gt;EKSourceTypeLocal&lt;/code&gt; source type&lt;/li&gt;
&lt;li&gt;Assign it to the calendar&amp;rsquo;s &lt;code&gt;source&lt;/code&gt; property&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Save the calendar using the event store instance&lt;/li&gt;
&lt;li&gt;Handle any problems that might have occurred&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;a name=&#34;code-example&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;code-example&#34;&gt;Code example&lt;/h3&gt;
&lt;p&gt;That&amp;rsquo;s the general outline… Now for the 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;c1&#34;&gt;// Create an Event Store instance&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;eventStore&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;EKEventStore&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;// Use Event Store to create a new calendar instance&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;// Configure its title&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;newCalendar&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;EKCalendar&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;forEntityType&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;Event&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;eventStore&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;eventStore&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;// Probably want to prevent someone from saving a calendar&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;// if they don&amp;#39;t type in a name...&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;n&#34;&gt;newCalendar&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;Some Calendar Name&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;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;// Access list of available sources from the Event Store&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;sourcesInEventStore&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;eventStore&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;sources&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;// Filter the available sources and select the &amp;#34;Local&amp;#34; source to assign to the new calendar&amp;#39;s&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;c1&#34;&gt;// source property&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;newCalendar&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;source&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;sourcesInEventStore&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;bp&#34;&gt;filter&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 class=&#34;n&#34;&gt;source&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;EKSource&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;19&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;n&#34;&gt;source&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;sourceType&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;EKSourceType&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;Local&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&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 class=&#34;bp&#34;&gt;first&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;c1&#34;&gt;// Save the calendar using the Event Store instance&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;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;24&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;eventStore&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;saveCalendar&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;newCalendar&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;commit&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&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;NSUserDefaults&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;standardUserDefaults&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;().&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;setObject&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;newCalendar&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;calendarIdentifier&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;EventTrackerPrimaryCalendar&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;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;27&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;alert&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIAlertController&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;Calendar could not save&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;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;error&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;as&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;n&#34;&gt;localizedDescription&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;preferredStyle&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;Alert&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;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;OKAction&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIAlertAction&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;OK&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;style&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;Default&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;handler&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;29&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;n&#34;&gt;alert&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;addAction&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;OKAction&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;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;kc&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&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;alert&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;32&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 most confusing part of the code above for me was obtaining the right source to assign to the new calendar&amp;rsquo;s &lt;code&gt;source&lt;/code&gt; property. Let&amp;rsquo;s unpack that for a second…&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;assign-source&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;obtaining-and-assigning-the-calendars-source&#34;&gt;Obtaining and assigning the calendar&amp;rsquo;s source&lt;/h4&gt;
&lt;p&gt;The &lt;code&gt;eventStore&lt;/code&gt; instance gives us the ability to query for a listing of its relevant calendar source types. But why are we going to the event store just to get a list of all the sources so that we can filter it down to just the one we want? Well, because this is the only way to get &lt;code&gt;EKSource&lt;/code&gt; instances! Take a look at this quote from the &lt;a href=&#34;https://developer.apple.com/library/prerelease/mac/documentation/EventKit/Reference/EKSourceClassRef/index.html&#34;&gt;Apple Documentation&lt;/a&gt;:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;You do not create instances of &lt;code&gt;EKSource&lt;/code&gt;&lt;/strong&gt;. You &lt;em&gt;retrieve&lt;/em&gt; EKSource objects from an &lt;code&gt;EKEventStore&lt;/code&gt; object. Use the sources property to get all the &lt;code&gt;EKSource&lt;/code&gt; objects for an event store, and use the methods in this class to access properties of the source object. (emphasis added)&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;So that answers the question of why we query the event store for that list of source types. But now, how do we narrow that list down to the one we want? That&amp;rsquo;s where the call to &lt;code&gt;filter&lt;/code&gt; comes in…&lt;/p&gt;
&lt;p&gt;First let&amp;rsquo;s isolate that code segment from the rest so it&amp;rsquo;s clear what we&amp;rsquo;re analyzing:&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;newCalendar&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;source&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;sourcesInEventStore&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;bp&#34;&gt;filter&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;source&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;EKSource&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;n&#34;&gt;source&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;sourceType&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;EKSourceType&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;Local&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&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 class=&#34;bp&#34;&gt;first&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 goal of this code is to take the list of sources in the event store, and filter them so that only the one matching the value of &lt;code&gt;EKSourceTypeLocal&lt;/code&gt; is returned. This is easily accomplished using the &lt;code&gt;filter&lt;/code&gt; function on the array of &lt;code&gt;EKSources&lt;/code&gt; that&amp;rsquo;s returned by the event store.&lt;/p&gt;
&lt;p&gt;But &lt;code&gt;filter&lt;/code&gt; &lt;em&gt;also&lt;/em&gt; returns an array, so to get the single source we&amp;rsquo;re looking for, we&amp;rsquo;ll simply grab the &lt;code&gt;first&lt;/code&gt; element out of the array &lt;code&gt;filter&lt;/code&gt; returns, and assign it to the new calendar&amp;rsquo;s &lt;code&gt;source&lt;/code&gt; property. There are no duplicated &lt;code&gt;EKSourceTypes&lt;/code&gt; in the list returned by the event store, so our filter expression should only return one match wrapped in an array.&lt;/p&gt;
&lt;p&gt;That&amp;rsquo;s it for configuring the calendar. The remainder of the code uses the event store instance to save the calendar, and handle any errors that might occur with the save process.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;save-identifier&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;saving-the-calendar-identifier&#34;&gt;Saving the calendar identifier&lt;/h3&gt;
&lt;p&gt;One last thing to note is that if you&amp;rsquo;re creating a calendar for your app to store events in, you probably want to stash the calendar&amp;rsquo;s identifier value somewhere, so that you can query the event store for the calendar directly, at a later point in time.&lt;/p&gt;
&lt;p&gt;Using &lt;code&gt;NSUserDefaults.standardUserDefaults()&lt;/code&gt; is a convenient way to store this calendar identifier value. The code to pay attention to is highlighted in this snippet:&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;// ...&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;c1&#34;&gt;// Save the calendar using the Event Store instance&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;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; 5&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;eventStore&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;saveCalendar&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;newCalendar&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;commit&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&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;NSUserDefaults&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;standardUserDefaults&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;().&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;setObject&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;newCalendar&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;calendarIdentifier&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;EventTrackerPrimaryCalendar&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;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; 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;alert&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIAlertController&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;Calendar could not save&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;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;error&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;as&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;n&#34;&gt;localizedDescription&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;preferredStyle&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;Alert&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;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;OKAction&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIAlertAction&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;OK&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;style&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;Default&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;handler&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;n&#34;&gt;alert&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;addAction&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;OKAction&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;kc&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&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;alert&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;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;So assuming that the calendar was saved successfully without error, we&amp;rsquo;ll simply access the standard user defaults, and insert a new object (our calendar&amp;rsquo;s identifier) for a key that we&amp;rsquo;ll use to retrieve the identifier again later.&lt;/p&gt;
&lt;h3 id=&#34;wrapping-up&#34;&gt;Wrapping up&lt;/h3&gt;
&lt;p&gt;Having the ability to create a calendar for an iOS application using Event Kit is a powerful thing if you&amp;rsquo;re wanting to take advantage of some of the built-in event-related features of the iOS platform. In this article we saw how to create a calendar using Event Kit and Swift. Additionally we analyzed some of the less-than-intuitive pieces of accessing the event store for a list of sources. We concluded by saving the new calendar&amp;rsquo;s identifier to &lt;code&gt;NSUserDefaults&lt;/code&gt; so that we could easily retrieve the calendar from the event store at a later time.&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/05/14/beginners-guide-to-eventkit-in-swift-requesting-permission/&#34; title=&#34;Beginner’s Guide to EventKit in Swift – Requesting Permission&#34;&gt;Beginner’s Guide to EventKit in Swift – Requesting Permission&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/04/28/listing-calendar-events-with-event-kit-and-swift/&#34; title=&#34;Listing Calendar Events with Event Kit and Swift&#34;&gt;Listing Calendar Events with Event Kit and 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/06/02/creating-calendar-events-with-event-kit-and-swift/&#34; title=&#34;Creating Calendar Events with Event Kit and Swift&#34;&gt;Creating Calendar Events with Event Kit and 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>Improvements to Unit Testing in Swift with Xcode 7</title>
      <link>https://www.andrewcbancroft.com/2015/06/10/improvements-to-unit-testing-in-swift-with-xcode-7/</link>
      <pubDate>Wed, 10 Jun 2015 17:43:09 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2015/06/10/improvements-to-unit-testing-in-swift-with-xcode-7/</guid>
      <description>&lt;p&gt;One of the most exciting things that&amp;rsquo;s come out of WWDC 2015 is the fact that unit testing in Swift has been greatly simplified. The latest announcements have actually rendered a few of my articles on the subject obsolete, once Xcode 7 is in full circulation among the Swift developer community.&lt;/p&gt;
&lt;p&gt;If you&amp;rsquo;re using Xcode 6.x, you may still find use in a couple of my previous articles:&lt;/p&gt;
&lt;ul&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;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/2015/04/15/testability-tip-for-swift-developers-public-over-private/&#34;&gt;Testability Tip for Swift Developers – Public Over Private&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If you&amp;rsquo;re jumping headlong into Xcode 7 world, setting yourself up for unit testing is incredibly simple. Essentially, all &lt;code&gt;internal&lt;/code&gt; and &lt;code&gt;public&lt;/code&gt; Types (and members of those Types) are visible to your unit test project when you import the module using the &lt;code&gt;@testable&lt;/code&gt; keyword in front of the import. With Xcode 7, there&amp;rsquo;s no reason to change any of the access modifiers on your Types/members to support testing.&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://twitter.com/NatashaTheRobot&#34;&gt;@natashatherobot&lt;/a&gt; has an &lt;a href=&#34;http://natashatherobot.com/swift-2-xcode-7-unit-testing-access/&#34;&gt;excellent 3-step write-up&lt;/a&gt; that I won&amp;rsquo;t repeat here. To summarize though, you&amp;rsquo;ll simply…&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Code your Types (classes/structs/enums) using the default &lt;code&gt;internal&lt;/code&gt; access.&lt;/li&gt;
&lt;li&gt;Add an import statement to the file containing your &lt;code&gt;XCTestCases&lt;/code&gt;. There&amp;rsquo;s special syntax for this now: &lt;code&gt;@testable import AppName/ModuleName&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Begin unit testing with access to all &lt;code&gt;public&lt;/code&gt; and &lt;code&gt;internal&lt;/code&gt; Types and members!&lt;/li&gt;
&lt;/ul&gt;
</description>
    </item>
    
    <item>
      <title>Access Sub-Controllers from a UINavigationController in Swift</title>
      <link>https://www.andrewcbancroft.com/2015/06/02/access-sub-controllers-from-a-uinavigationcontroller-in-swift/</link>
      <pubDate>Tue, 02 Jun 2015 17:37:44 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2015/06/02/access-sub-controllers-from-a-uinavigationcontroller-in-swift/</guid>
      <description>&lt;p&gt;The sequence of accessing a &lt;code&gt;UINavigationController&#39;s&lt;/code&gt; first child from within the &lt;code&gt;AppDelegate&lt;/code&gt; or within &lt;code&gt;prepareForSegue(_:sender:)&lt;/code&gt; always gets me. Here are a few quick snippets to help you and I quickly get up and running when working with navigation controllers in our Swift applications:&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;app-delegate&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;appdelegate&#34;&gt;AppDelegate&lt;/h3&gt;
&lt;p&gt;Every iOS application has one root view controller that&amp;rsquo;s presented when the app finishes its launch process. Suppose we&amp;rsquo;re building a navigation controller-based app… that is, we&amp;rsquo;re building an app where the first (root) view controller is a UINavigationController. In our Storyboard, we&amp;rsquo;ve set up a Scene with some UI controls with a view controller and some properties, and we&amp;rsquo;ve embedded that view controller in a navigation controller.&lt;/p&gt;
&lt;p&gt;What if we want to set some of the view controller&amp;rsquo;s properties after the app launches? How could we go about doing that?&lt;/p&gt;
&lt;p&gt;I tend to always think of the &amp;ldquo;first view controller” as the first Scene in the Storyboard where I&amp;rsquo;ve set up UI components. To iOS, however, the &lt;em&gt;navigation controller&lt;/em&gt; is actually the first (or &lt;em&gt;root&lt;/em&gt;) view controller.&lt;/p&gt;
&lt;p&gt;When an app incorporates a navigation controller as its first (root) view controller, we end up needing to do a little digging into the view controller hierarchy to get access what we might perceive as the true &amp;ldquo;first view controller”.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;dig-first-view-controller&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;digging-for-the-first-view-controller&#34;&gt;Digging for the first view controller&lt;/h4&gt;
&lt;p&gt;Here is a snippet of how to dig into the &lt;code&gt;UINavigationController&#39;s&lt;/code&gt; view controller hierarchy to grab the first one and set some fictitious properties on it, all from within the &lt;code&gt;AppDelegate&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;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;AppDelegate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIResponder&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIApplicationDelegate&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 class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;window&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIWindow&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&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;application&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;application&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIApplication&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;didFinishLaunchingWithOptions&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;launchOptions&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;NSObject&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;-&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;c1&#34;&gt;// Override point for customization after application launch.&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;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;navigationController&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;window&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;?.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;rootViewController&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;UINavigationController&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;firstVC&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;navigationController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;viewControllers&lt;/span&gt;&lt;span class=&#34;p&#34;&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 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;NameOfFirstViewController&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;c1&#34;&gt;// set whatever properties you might want to set&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;// such as an NSmanagedObjectContext reference&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;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;kc&#34;&gt;true&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;// ...&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;So the workflow goes like this:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Get the window&amp;rsquo;s root view controller (which is the navigation controller in our case)&lt;/li&gt;
&lt;li&gt;Get the navigation controller&amp;rsquo;s first view controller from its array of view controllers (which is what I always think of as the &amp;ldquo;first” view controller)&lt;/li&gt;
&lt;li&gt;Set whatever properties you need to set&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; With iOS 13, the code to access the root view controller needs to go &lt;a href=&#34;https://www.andrewcbancroft.com/blog/ios-development/ui-work/accessing-root-view-controller-ios13-scenedelegate/&#34;&gt;in your app&amp;rsquo;s SceneDelegate&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;You may be worried about the usage of implicitly unwrapped optionals in this snippet. I tend to avoid them wherever possible too, but I used them here because I reasoned that my navigation controller-based app &lt;em&gt;hinges&lt;/em&gt; on the fact that the root view controller of the application is a &lt;code&gt;UINavigationController&lt;/code&gt;. Something so fundamental to the app warranted my usage of the implicitly unwrapped optionals, since changing the navigation paradigm of the app would probably break things anyway.&lt;/p&gt;
&lt;p&gt;If you&amp;rsquo;re not convinced by that line of reasoning, no worries – you can switch out some of the &lt;code&gt;!&lt;/code&gt; operators for &lt;code&gt;?&lt;/code&gt; operators and add in some &lt;code&gt;if-let&lt;/code&gt; syntax to protect against encountering nil. For 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;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;AppDelegate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIResponder&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIApplicationDelegate&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 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;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;navigationController&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;window&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;?.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;rootViewController&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;UINavigationController&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;if&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;firstVC&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;navigationController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;viewControllers&lt;/span&gt;&lt;span class=&#34;p&#34;&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 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;NameOfFirstViewController&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;firstVC&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;someProperty&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;someValue&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;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;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;a name=&#34;prepare-for-segue&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;prepare-for-segue&#34;&gt;Prepare for segue&lt;/h3&gt;
&lt;p&gt;What about in &lt;code&gt;prepareForSegue(_:sender:)&lt;/code&gt;? When would this even be necessary?&lt;/p&gt;
&lt;p&gt;Well, suppose that we have an app which segues &lt;em&gt;into&lt;/em&gt; a navigation controller. We may need to pass some data off the next view controller, but that &amp;ldquo;next view controller” is technically the navigation controller, not the controller where our properties are declared.&lt;/p&gt;
&lt;p&gt;In similar fashion to the &lt;code&gt;AppDelegate&lt;/code&gt; situation, we want to dig into the navigation controller&amp;rsquo;s view controller hierarchy to access the first child so that we can pass the data along. Here&amp;rsquo;s an example implementation:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-swift&#34; data-lang=&#34;swift&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 1&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;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&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;kr&#34;&gt;override&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;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; 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;destinationVC&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;destinationViewController&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;UINavigationController&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;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;nextViewController&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;destinationVC&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;viewControllers&lt;/span&gt;&lt;span class=&#34;p&#34;&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 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;SecondViewController&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;n&#34;&gt;nextViewController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;someProperty&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;someValue&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;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;The only thing that really changes between the &lt;code&gt;AppDelegate&lt;/code&gt; example and the &lt;code&gt;prepareForSegue&lt;/code&gt; example is where we obtain the &lt;code&gt;UINavigationController&lt;/code&gt; from. In &lt;code&gt;AppDelegate&lt;/code&gt;, the navigation controller comes from the window&amp;rsquo;s root view controller. In &lt;code&gt;prepareForSegue&lt;/code&gt; it comes from the segue&amp;rsquo;s destination view controller.&lt;/p&gt;
&lt;p&gt;After that, though, the process for grabbing the first child of the navigation controller is the same.&lt;/p&gt;
&lt;h3 id=&#34;wrapping-up&#34;&gt;Wrapping up&lt;/h3&gt;
&lt;p&gt;Accessing a navigation controller&amp;rsquo;s view controller hierarchy was just vague enough for me to write this little reference for myself, but I hope you benefited from it as well!&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>Sync Table View Data: NSFetchedResultsController and Swift</title>
      <link>https://www.andrewcbancroft.com/2015/05/28/sync-table-view-data-nsfetchedresultscontroller-swift/</link>
      <pubDate>Fri, 29 May 2015 03:20:54 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2015/05/28/sync-table-view-data-nsfetchedresultscontroller-swift/</guid>
      <description>&lt;p&gt;&lt;small&gt;Updated on September 23, 2015 – Swift 2.0&lt;/small&gt;&lt;/p&gt;
&lt;p&gt;My goal with this article is to help you utilize the full power of &lt;code&gt;NSFetchedResultsController&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;This is a continuation on a series of articles I&amp;rsquo;ve written on Core Data and &lt;code&gt;NSFetchedResultsController&lt;/code&gt;, so you may want to check out those previous posts to get an idea of where I&amp;rsquo;m picking up in this walk-through. Previously I touched on &lt;a href=&#34;http://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;&gt;how to seed a Core Data database&lt;/a&gt;, and &lt;a href=&#34;http://www.andrewcbancroft.com/2015/03/05/displaying-data-with-nsfetchedresultscontroller-and-swift/&#34; title=&#34;Displaying Data With NSFetchedResultsController and Swift&#34;&gt;how to take that data and display it in a table view with an NSFetchedResultsController&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;As with the previous posts, I&amp;rsquo;m providing an example Xcode project over at GitHub, so feel free to follow along with the live working example:&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;fab fa-github fa&#34;&gt;&lt;/i&gt; &lt;a href=&#34;https://github.com/andrewcbancroft/Zootastic/tree/NSFetchedResultsController_SyncTableView&#34; title=&#34;GitHub - Sync Table View Example Project&#34; target=&#34;_blank&#34;&gt;Sync Table View Example Project&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;
&lt;p&gt;In this installment to the series, I want to answer the question, &amp;ldquo;How do I update the rows in a table view when I add or remove objects from the Core Data database?” I will show how to implement the &lt;code&gt;NSFetchedResultsControllerDelegate&lt;/code&gt; protocol, which is the key to automatically synchronizing changes made to your Core Data persistent store with a table view.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;examine-delegate-protocol&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;examining-the-nsfetchedresultscontrollerdelegate-protocol&#34;&gt;Examining the NSFetchedResultsControllerDelegate protocol&lt;/h3&gt;
&lt;p&gt;The &lt;code&gt;NSFetchedResultsControllerDelegate&lt;/code&gt; protocol is the piece of the puzzle that helps us update a table view with changes made to the Core Data persistent store. There are five methods that we&amp;rsquo;ll be taking a look at:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;controllerWillChangeContent(_:)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;controller(_:didChangeObject:atIndexPath:forChangeType:newIndexPath:)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;controller(_:didChangeSection:atIndex:forChangeType:)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;controller(_:sectionIndexTitleForSectionName:)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;controllerDidChangeContent(_:)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The two methods that are responsible for doing the actual updates to the table view&amp;rsquo;s structure are &lt;code&gt;controller(_:didChangeSection:atIndex:forChangeType:)&lt;/code&gt; and &lt;code&gt;controller(_:didChangeObject:atIndexPath:forChangeType:newIndexPath:)&lt;/code&gt;. If some of the changes to the table view result in new sections being created, &lt;code&gt;controller(_:sectionIndexTitleForSectionName:)&lt;/code&gt; will help give it an appropriate title (and make sure the &lt;em&gt;other&lt;/em&gt; sections keep their appropriate titles as well).&lt;/p&gt;
&lt;p&gt;&lt;code&gt;controllerWillChangeContent(_:)&lt;/code&gt; and &lt;code&gt;controllerDidChangeContent(_:)&lt;/code&gt; help inform the table view that changes are about to happen / just finished happening. Sandwiching the primary &amp;ldquo;didChangeObject” and &amp;ldquo;didChangeSection” protocol methods with these two methods allows the table view to animate in all of the changes to its structure in one batch.&lt;/p&gt;
&lt;p&gt;So, the general structure of the &lt;code&gt;NSFetchedResultsControllerDelegate&lt;/code&gt; section of your source file might look 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;c1&#34;&gt;// &lt;/span&gt;&lt;span class=&#34;cs&#34;&gt;MARK:&lt;/span&gt;&lt;span class=&#34;c1&#34;&gt; NSFetchedResultsControllerDelegate methods&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;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;controllerWillChangeContent&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;NSFetchedResultsController&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;kc&#34;&gt;self&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;beginUpdates&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&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 6&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;controller&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;controller&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSFetchedResultsController&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;n&#34;&gt;didChangeObject&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;anObject&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&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;atIndexPath&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&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;n&#34;&gt;forChangeType&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;NSFetchedResultsChangeType&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;newIndexPath&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;{&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;// implementation to follow...&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;14&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;15&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;16&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;controller&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;n&#34;&gt;controller&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSFetchedResultsController&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;n&#34;&gt;didChangeSection&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;sectionInfo&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSFetchedResultsSectionInfo&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;n&#34;&gt;atIndex&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;sectionIndex&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&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;forChangeType&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;NSFetchedResultsChangeType&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&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;// implementation to follow...&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&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;kd&#34;&gt;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;controller&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;NSFetchedResultsController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;sectionIndexTitleForSectionName&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;sectionName&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;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;26&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;sectionName&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;27&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;p&#34;&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&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;kd&#34;&gt;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;controllerDidChangeContent&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;NSFetchedResultsController&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;30&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;tableView&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;endUpdates&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;31&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;did-change-object&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;controller_didchangeobjectatindexpathforchangetypenewindexpath&#34;&gt;controller(_:didChangeObject:atIndexPath:forChangeType:newIndexPath:)&lt;/h3&gt;
&lt;p&gt;This is the method that governs how we want to handle the rows in a table view when the synchronization would require inserting rows, updating existing ones, removing them, or reordering them.&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;ll give you the implementation and then point out a couple of &amp;ldquo;gotchas” and expound a little more. Recall that we&amp;rsquo;re working with a sample app named &amp;ldquo;Zootastic”, so if you see references to &lt;code&gt;Animals&lt;/code&gt; in the example, you&amp;rsquo;ll know why. :]&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-swift&#34; data-lang=&#34;swift&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 1&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;controller&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;controller&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSFetchedResultsController&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;didChangeObject&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;anObject&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&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;atIndexPath&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&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;forChangeType&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;NSFetchedResultsChangeType&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;newIndexPath&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;{&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;switch&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&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;case&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSFetchedResultsChangeType&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;Insert&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 that for Insert, we insert a row at the __newIndexPath__&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;insertIndexPath&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;newIndexPath&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;tableView&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;insertRowsAtIndexPaths&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;([&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;insertIndexPath&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;],&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;withRowAnimation&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UITableViewRowAnimation&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;Fade&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&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;k&#34;&gt;case&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSFetchedResultsChangeType&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;Delete&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;c1&#34;&gt;// Note that for Delete, we delete the row at __indexPath__&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;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;deleteIndexPath&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&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&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;tableView&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;deleteRowsAtIndexPaths&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;([&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;deleteIndexPath&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;],&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;withRowAnimation&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UITableViewRowAnimation&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;Fade&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;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;19&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;            &lt;span class=&#34;k&#34;&gt;case&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSFetchedResultsChangeType&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;Update&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;20&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;                &lt;span class=&#34;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;updateIndexPath&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&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&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;// Note that for Update, we update the row at __indexPath__&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;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;kc&#34;&gt;self&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;cellForRowAtIndexPath&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;updateIndexPath&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;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;animal&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;fetchedResultsController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;objectAtIndexPath&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;updateIndexPath&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;Animal&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&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;cell&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;?.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;textLabel&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;n&#34;&gt;animal&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&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;cell&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;?.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;detailTextLabel&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;n&#34;&gt;animal&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;?.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;habitat&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;27&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;                &lt;span class=&#34;p&#34;&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;k&#34;&gt;case&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSFetchedResultsChangeType&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;Move&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;29&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;                &lt;span class=&#34;c1&#34;&gt;// Note that for Move, we delete the row at __indexPath__&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;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;deleteIndexPath&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&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&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;kc&#34;&gt;self&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;deleteRowsAtIndexPaths&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;([&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;deleteIndexPath&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;],&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;withRowAnimation&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UITableViewRowAnimation&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;Fade&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;p&#34;&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&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;c1&#34;&gt;// Note that for Move, we insert a row at the __newIndexPath__&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 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;insertIndexPath&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;newIndexPath&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;36&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;tableView&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;insertRowsAtIndexPaths&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;([&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;insertIndexPath&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;],&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;withRowAnimation&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UITableViewRowAnimation&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;Fade&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;37&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;38&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;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;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Right away you&amp;rsquo;ll notice we enter a switch on the &lt;code&gt;type&lt;/code&gt; parameter of the method. There are four options possible in the &lt;code&gt;NSFetchedResultsChangeType&lt;/code&gt; enum: Insert, Delete, Update, and Move.&lt;/p&gt;
&lt;p&gt;Beware of a few common gotchas with each case of the switch:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;First of all, notice that first argument of the majority of the &lt;code&gt;tableView&lt;/code&gt; methods takes an &lt;em&gt;array&lt;/em&gt; of &lt;code&gt;NSIndexPaths&lt;/code&gt;. Be sure to wrap your argument in &lt;code&gt;[&lt;/code&gt; and &lt;code&gt;]&lt;/code&gt; to create an array.&lt;/li&gt;
&lt;li&gt;Pay extra attention to which index path parameter you&amp;rsquo;re referencing in each case. For insert, the goal is to add a row at the &lt;code&gt;newIndexPath&lt;/code&gt;. For Delete, the goal is to remove the row at &lt;code&gt;indexPath&lt;/code&gt;. Move will require a deletion of the &lt;code&gt;indexPath&lt;/code&gt; and an insertion at the &lt;code&gt;newIndexPath&lt;/code&gt;. Getting these mixed up will cause runtime errors, so pay close attention here!&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;a name=&#34;did-change-section&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;controller_didchangesectionatindexforchangetype&#34;&gt;controller(_:didChangeSection:atIndex:forChangeType:)&lt;/h3&gt;
&lt;p&gt;If your table view only has one section, you don&amp;rsquo;t need to worry with this one.&lt;/p&gt;
&lt;p&gt;If your table view has multiple sections, you want to make sure and implement this protocol method – if you fail to do so and the change to the persistent store results in adjustments to the table view that can&amp;rsquo;t be handled, runtime errors can occur. For example, deleting all rows in a section would result in the section needing to be deleted as well, but without this protocol method being implemented, the update to the table view can&amp;rsquo;t be made and the app crashes.&lt;/p&gt;
&lt;p&gt;Once again, I&amp;rsquo;ll throw the code your way and follow up with commentary:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-swift&#34; data-lang=&#34;swift&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 1&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;controller&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;controller&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSFetchedResultsController&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;didChangeSection&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;sectionInfo&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSFetchedResultsSectionInfo&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;atIndex&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;sectionIndex&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&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;forChangeType&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;NSFetchedResultsChangeType&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&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;switch&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&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;Insert&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;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;sectionIndexSet&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSIndexSet&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;index&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;sectionIndex&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;kc&#34;&gt;self&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;insertSections&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;sectionIndexSet&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;withRowAnimation&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UITableViewRowAnimation&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;Fade&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;case&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;Delete&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;sectionIndexSet&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSIndexSet&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;index&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;sectionIndex&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;kc&#34;&gt;self&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;deleteSections&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;sectionIndexSet&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;withRowAnimation&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UITableViewRowAnimation&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;Fade&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;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;15&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;            &lt;span class=&#34;s&#34;&gt;&amp;#34;&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;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&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;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;controller&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;NSFetchedResultsController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;sectionIndexTitleForSectionName&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;sectionName&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;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;20&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;sectionName&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;For this one, we&amp;rsquo;re only implementing code for Insert and Delete. The necessary information to insert a section or remove a section (ie, the &lt;code&gt;sectionIndex&lt;/code&gt;) comes as a parameter to the method.&lt;/p&gt;
&lt;p&gt;We utilize an &lt;code&gt;NSIndexSet&lt;/code&gt; to wrap up the section that needs to be inserted or deleted and pass it to the table view&amp;rsquo;s &lt;code&gt;insertSections()&lt;/code&gt; and &lt;code&gt;deleteSections()&lt;/code&gt; methods, respectively.&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/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;http://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;&gt;Using Swift to Seed a Core Data Database&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/03/05/displaying-data-with-nsfetchedresultscontroller-and-swift/&#34; title=&#34;Displaying Data With NSFetchedResultsController and Swift&#34;&gt;Displaying Data With NSFetchedResultsController and 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 How-To:  Setting up a Table View</title>
      <link>https://www.andrewcbancroft.com/2015/05/18/swift-how-to-setting-up-a-table-view/</link>
      <pubDate>Tue, 19 May 2015 02:31:38 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2015/05/18/swift-how-to-setting-up-a-table-view/</guid>
      <description>&lt;p&gt;Think of this screencast as the &amp;ldquo;Hello World” of creating a view controller with a table view. I cover organizing things in the Storyboard, Auto Layout constraints, and the implementation of the data source protocol methods for a table view.&lt;/p&gt;
&lt;h3 id=&#34;screencast&#34;&gt;Screencast&lt;/h3&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.&lt;/p&gt;
&lt;p&gt;I use table views fairly often in my examples, so I&amp;rsquo;ve decided to create a short walkthrough of how to set up a table view in Swift.&lt;/p&gt;
&lt;p&gt;Think of this as the &amp;ldquo;Hello World” of creating a view controller with a table view.&lt;/p&gt;
&lt;h4 id=&#34;016&#34;&gt;[0:16]&lt;/h4&gt;
&lt;p&gt;In this walkthrough, I&amp;rsquo;m using Xcode 6.3.1, which has me writing Swift 1.2 syntax in a couple of spots.&lt;/p&gt;
&lt;h4 id=&#34;024&#34;&gt;[0:24]&lt;/h4&gt;
&lt;p&gt;To get started, I&amp;rsquo;m going to drop into the search bar of the Object Library, which is in the Utilities Pane to hunt for a table view.&lt;/p&gt;
&lt;p&gt;Once I&amp;rsquo;ve narrowed the results and located the Table View control, I&amp;rsquo;ll click and drag one onto my Storyboard scene.&lt;/p&gt;
&lt;h4 id=&#34;041&#34;&gt;[0:41]&lt;/h4&gt;
&lt;p&gt;With the table view in place, filling the entire scene, I&amp;rsquo;ll control + click and drag up to the yellow View Controller icon to wire up my the table view&amp;rsquo;s dataSource… and delegate properties.&lt;/p&gt;
&lt;h4 id=&#34;056&#34;&gt;[0:56]&lt;/h4&gt;
&lt;p&gt;Now I need to connect the table view to my View Controller source file. To bring up the assistant editor, I&amp;rsquo;ll option+click the ViewController.swift file in the Project Navigator.&lt;/p&gt;
&lt;h4 id=&#34;106&#34;&gt;[1:06]&lt;/h4&gt;
&lt;p&gt;With the table view and the source side by side, once again, I&amp;rsquo;ll control + click the table view, and drag over to the ViewController source file to create an IBOutlet property for my table view.&lt;/p&gt;
&lt;h4 id=&#34;121&#34;&gt;[1:21]&lt;/h4&gt;
&lt;p&gt;Next up is to specify that my ViewController class will conform to the UITableViewDataSource protocol… and the UITableViewDelegate protocols.&lt;/p&gt;
&lt;h4 id=&#34;131&#34;&gt;[1:31]&lt;/h4&gt;
&lt;p&gt;Of course, at this point, I haven&amp;rsquo;t actually implemented any of the required protocol methods, so I get a compiler error saying so.&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;ve written up a handy UITableViewDataSource cheat sheet over at my website (andrewcbancroft.com). I&amp;rsquo;ll go grab that code and paste it into my View Controller.&lt;/p&gt;
&lt;h4 id=&#34;151&#34;&gt;[1:51]&lt;/h4&gt;
&lt;p&gt;Here I am at andrewcbancroft.com – I&amp;rsquo;ve searched for &amp;ldquo;UITableViewDataSource” and located my cheet sheet.&lt;/p&gt;
&lt;h4 id=&#34;202&#34;&gt;[2:02]&lt;/h4&gt;
&lt;p&gt;Scrolling down, I find the code I need, switch the code snippet plugin to &amp;ldquo;raw” mode, select everything, and copy.&lt;/p&gt;
&lt;h4 id=&#34;212&#34;&gt;[2:12]&lt;/h4&gt;
&lt;p&gt;Switching back over to my Xcode window, I paste the code snippet in.&lt;/p&gt;
&lt;h4 id=&#34;216&#34;&gt;[2:16]&lt;/h4&gt;
&lt;p&gt;As I mentioned in the beginning, this walkthrough can be thought of as the &amp;ldquo;Hello World” of table view setup. With that in mind, for now, I&amp;rsquo;ll create a dataSourceArray with some dummy data in it, just to get things compiling and displaying data.&lt;/p&gt;
&lt;h4 id=&#34;231&#34;&gt;[2:31]&lt;/h4&gt;
&lt;p&gt;Now that I&amp;rsquo;ve got the data source geared to go, I need some cells to display data in.&lt;/p&gt;
&lt;p&gt;For this task, I&amp;rsquo;ll close the assistant editor…&lt;/p&gt;
&lt;p&gt;Next, I&amp;rsquo;ll click to open the document outline. I find that working with the view hierarchy is &lt;em&gt;much&lt;/em&gt; easier to manipulate using this outline, rather than clicking to try and select the right thing in the Storyboard Scene.&lt;/p&gt;
&lt;h4 id=&#34;255&#34;&gt;[2:55]&lt;/h4&gt;
&lt;p&gt;I&amp;rsquo;ll select the table view, and open the Utilities pane to get access to the Attributes inspector.&lt;/p&gt;
&lt;h4 id=&#34;301&#34;&gt;[3:01]&lt;/h4&gt;
&lt;p&gt;I&amp;rsquo;m going to configure this table view to have one kind of prototype cell.&lt;/p&gt;
&lt;h4 id=&#34;306&#34;&gt;[3:06]&lt;/h4&gt;
&lt;p&gt;Increasing the Prototype Cells count added a new node to the document outline&amp;rsquo;s view hierarchy.&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;ll expand the Table View and click on the Table View Cell to do a bit more configuration.&lt;/p&gt;
&lt;h4 id=&#34;319&#34;&gt;[3:19]&lt;/h4&gt;
&lt;p&gt;I&amp;rsquo;ll set the Style to Basic…&lt;/p&gt;
&lt;p&gt;Then I&amp;rsquo;ll give it an identitifer of &amp;ldquo;BasicCell”.&lt;/p&gt;
&lt;p&gt;I need this &amp;ldquo;BasicCell” string for one of my other data source proocol methods, so I&amp;rsquo;ll select it, and copy it.&lt;/p&gt;
&lt;h4 id=&#34;337&#34;&gt;[3:37]&lt;/h4&gt;
&lt;p&gt;Back in my ViewController.swift source file, I&amp;rsquo;ll paste that &amp;ldquo;BasicCell” string as the argument to dequeReusableCellWithIdentifier.&lt;/p&gt;
&lt;h4 id=&#34;348&#34;&gt;[3:48]&lt;/h4&gt;
&lt;p&gt;The last step to get things wired up is to set the cell&amp;rsquo;s textLabel.text property.&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;ll use the dataSourceArray&amp;rsquo;s values here.&lt;/p&gt;
&lt;p&gt;Simply indexing into the array at the indexPath.row value that&amp;rsquo;s currently being rendered in the table view will grab the string out of the array and assign it to the textLabel.text property of the cell.&lt;/p&gt;
&lt;h4 id=&#34;413&#34;&gt;[4:13]&lt;/h4&gt;
&lt;p&gt;When I run the app and observe the table view&amp;rsquo;s behavior, I&amp;rsquo;m not quite satisfied with it. The status bar overlaps the rows and makes the text hard to read.&lt;/p&gt;
&lt;p&gt;Additionally, the table view doesn&amp;rsquo;t extend all the way to the bottom when I run it in the iPhone 6 sized simulator.&lt;/p&gt;
&lt;h4 id=&#34;430&#34;&gt;[4:30]&lt;/h4&gt;
&lt;p&gt;I&amp;rsquo;ll stop the app and make an adjustment in the Storyboard.&lt;/p&gt;
&lt;h4 id=&#34;434&#34;&gt;[4:34]&lt;/h4&gt;
&lt;p&gt;Selecting the table view from the document outline will allow me to grab the top edge and drag the hight downward slowly until it &amp;ldquo;snaps” into place where the bottom of the status bar would appear to be.&lt;/p&gt;
&lt;h4 id=&#34;448&#34;&gt;[4:48]&lt;/h4&gt;
&lt;p&gt;Next, I&amp;rsquo;ll set some constraints. Within the document outline, I&amp;rsquo;ll control + click from the Table View to the View and set constraints for &amp;ldquo;Top Space to Top Layout Guide”, Center Horizontally, Center Vertically, and Equal Widths.&lt;/p&gt;
&lt;h4 id=&#34;517&#34;&gt;[5:17]&lt;/h4&gt;
&lt;p&gt;Running the app one more time gives me the behavior I want!&lt;/p&gt;
&lt;h4 id=&#34;531&#34;&gt;[5:31]&lt;/h4&gt;
&lt;p&gt;Thanks for watching – I have other resources r elated to Swift and iOS development at andrewcbancroft.com, and you can find me on Twitter: &lt;a href=&#34;https://twitter.com/andrewcbancroft&#34;&gt;@andrewcbancroft&lt;/a&gt;&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Beginner’s Guide to Event Kit in Swift – Requesting Permission</title>
      <link>https://www.andrewcbancroft.com/2015/05/14/beginners-guide-to-eventkit-in-swift-requesting-permission/</link>
      <pubDate>Thu, 14 May 2015 20:37:52 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2015/05/14/beginners-guide-to-eventkit-in-swift-requesting-permission/</guid>
      <description>&lt;p&gt;&lt;small&gt;Updated on October 26, 2016 – Swift 3.0&lt;/small&gt;&lt;/p&gt;
&lt;p&gt;EventKit provides a set of classes for accessing and manipulating a user&amp;rsquo;s calendar events and reminders. In the tutorial that follows, my goal is to walk you through the first steps of setting up an app that utilizes EventKit. I will demonstrate how to request permission to the user&amp;rsquo;s calendar and show a couple of examples for how to handle the user&amp;rsquo;s response (for when they grant access, or &lt;em&gt;deny&lt;/em&gt; it).&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/EventTracker/tree/swift-2.3&#34;&gt;example project&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;example-scenario&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;example-scenario&#34;&gt;Example scenario&lt;/h3&gt;
&lt;p&gt;Let&amp;rsquo;s start by proposing a basic scenario to serve as this tutorial&amp;rsquo;s example.&lt;/p&gt;
&lt;p&gt;Suppose that we&amp;rsquo;re building an app that, for now, has a single View Controller. We&amp;rsquo;d like this View Controller to display a list of calendars if the user grants us permission to do so. If they &lt;em&gt;deny&lt;/em&gt; permission, we&amp;rsquo;d like to show a message to them that indicates that our app can&amp;rsquo;t function without this permission, and we&amp;rsquo;ll allow them to click a button to grant us permission in the Settings of their device.&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;ve created such an app as an example – &lt;a href=&#34;https://github.com/andrewcbancroft/EventTracker/tree/ask-for-permission&#34;&gt;jump over to GitHub&lt;/a&gt; to grab the code and explore. Read on for an explanation of some of the finer points of the setup and code.&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;fab fa-github fa-lg&#34;&gt;&lt;/i&gt; Example Xcode Project (&lt;a href=&#34;https://github.com/andrewcbancroft/EventTracker/tree/ask-for-permission-swift-2.3&#34; title=&#34;EventTracker - Swift 2.3&#34;&gt;Swift 2.3&lt;/a&gt; | &lt;a href=&#34;https://github.com/andrewcbancroft/EventTracker/tree/ask-for-permission&#34; title=&#34;EventTracker - Swift 3.0&#34;&gt;Swift 3.0&lt;/a&gt;)&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;
&lt;p&gt;&lt;a name=&#34;storyboard-setup&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;storyboard-setup&#34;&gt;Storyboard setup&lt;/h3&gt;
&lt;p&gt;One of the first things you&amp;rsquo;ll deal with in EventKit is the need to set yourself up with a UI to handle the different responses that the user can give you on that first application launch when you ask, &amp;ldquo;Can we access your calendar?”. We&amp;rsquo;ll get to the particulars of &lt;a href=&#34;#request-access-to-calendars&#34;&gt;how request that permission&lt;/a&gt; shortly. But first, let&amp;rsquo;s dissect how we might arrange a Storyboard with some views that do the right thing for a given response to that permission prompt.&lt;/p&gt;
&lt;p&gt;The user can either grant permission, or deny permission to interact with their calendar or reminders. We need to be prepared for either scenario.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;table-view-setup&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;tableview-for-the-calendar-list-for-when-access-is-granted&#34;&gt;Tableview for the calendar list for when access is granted&lt;/h4&gt;
&lt;p&gt;I&amp;rsquo;m feeling optimistic today, so let&amp;rsquo;s begin with the case where the user grants us permission to their calendar from the get-go.&lt;/p&gt;
&lt;p&gt;When the user grants us permission, we&amp;rsquo;d like to list out their calendars inside a table view. We&amp;rsquo;ll worry with setting up the data source later in the tutorial. For now, we&amp;rsquo;ll drag over a table view from the Utilities pane.&lt;/p&gt;
&lt;p&gt;To get the table view to fill the whole screen, I do a couple of things. Usually, when you drag one out from the Utilities pane, the table view will fill the whole scene in the Storyboard. From that layout, I drag the top edge down until it &amp;ldquo;snaps” to the line where I&amp;rsquo;d expect the bottom of the status bar to be positioned. Then I set the following constraints:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Center X&lt;/li&gt;
&lt;li&gt;Center Y&lt;/li&gt;
&lt;li&gt;Equal width to Superview&lt;/li&gt;
&lt;li&gt;Top space to Top Layout Guide for height.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I&amp;rsquo;ve created a short screencast on setting up a table view if you&amp;rsquo;d like a complete walkthrough:&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 fa&#34;&gt;&lt;/i&gt; &lt;a href=&#34;http://bit.ly/WatchSetUpTVFromACB&#34; title=&#34;Setting Up a Table View&#34; target=&#34;_blank&#34;&gt;Screencast: Setting Up a Table View&lt;/a&gt;
    &lt;/li&gt;
    &lt;li&gt;
      &lt;i class=&#34;fab fa-file-text-o fa&#34;&gt;&lt;/i&gt; &lt;a href=&#34;http://www.andrewcbancroft.com/2015/05/18/swift-how-to-setting-up-a-table-view/&#34; title=&#34;Setting Up a Table View - Transcript&#34;&gt;Full transcript&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;
&lt;p&gt;Here&amp;rsquo;s a detailed view of the constraints, along with a visual of what the Storyboard Scene looks like with the table view installed:&lt;br&gt;
&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/05/Detailed_Constraints.png&#34;&gt;&lt;img src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/05/Detailed_Constraints.png&#34; alt=&#34;Detailed view of constraints&#34; width=&#34;293&#34; height=&#34;450&#34; class=&#34;alignnone size-full wp-image-11872&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/05/Detailed_Constraints.png 293w, https://www.andrewcbancroft.com/wp-content/uploads/2015/05/Detailed_Constraints-195x300.png 195w&#34; sizes=&#34;(max-width: 293px) 100vw, 293px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/05/TableView.png&#34;&gt;&lt;img src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/05/TableView-1024x657.png&#34; alt=&#34;Table view in Storyboard&#34; width=&#34;1024&#34; height=&#34;657&#34; class=&#34;alignnone size-large wp-image-11873&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/05/TableView-1024x657.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2015/05/TableView-300x192.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2015/05/TableView.png 1398w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;As a final note, I&amp;rsquo;ve set the &lt;code&gt;hidden&lt;/code&gt; property of the table view to &lt;code&gt;true&lt;/code&gt; in the Storyboard. I&amp;rsquo;ll toggle the table&amp;rsquo;s visibility based on the user&amp;rsquo;s granting or denying of the calendar access later, but I thought it was worth pointing out that the initial state of my table view in the example is hidden.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;needs-permission-view-setup&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;need-permission8221-view-for-when-access-is-denied&#34;&gt;&amp;ldquo;Need permission” view for when access is denied&lt;/h4&gt;
&lt;p&gt;There &lt;em&gt;will&lt;/em&gt; be times when a user denies access to the calendar before realizing that doing so essentially stops all the functionality provided by your app. If your entire app, or even just a portion of it requires access to function, you need a way to inform the user of this, and provide them a way to navigate to settings and grant access manually if possible.&lt;/p&gt;
&lt;p&gt;The way I did this in the sample project was to organize a new View onto the Storyboard Scene which contains a label with some instructions, and a button that takes the user to the Settings page for our app when they tap on it.&lt;/p&gt;
&lt;p&gt;Once again, some constraints are involved in getting things to appear correctly at run-time. I won&amp;rsquo;t go into the details of this here, since it&amp;rsquo;s likely that every implementation of this will be slightly different.&lt;/p&gt;
&lt;p&gt;One thing I &lt;em&gt;will&lt;/em&gt; point out though, is that the View&amp;rsquo;s alpha has been set to 0 so that I can perform a nice &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 transition&lt;/a&gt; if the user denies access. Here&amp;rsquo;s a look at the Scene with the invisible &amp;ldquo;NeedPermissionsView” installed:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/05/NeedPermissionView.png&#34;&gt;&lt;img src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/05/NeedPermissionView-1024x625.png&#34; alt=&#34;Need permission view&#34; width=&#34;1024&#34; height=&#34;625&#34; class=&#34;alignnone size-large wp-image-11874&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/05/NeedPermissionView-1024x625.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2015/05/NeedPermissionView-300x183.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2015/05/NeedPermissionView.png 1465w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;role-of-event-store&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;the-role-of-the-event-store&#34;&gt;The role of the Event Store&lt;/h3&gt;
&lt;p&gt;At the heart of EventKit is the &lt;code&gt;EKEventStore&lt;/code&gt;. &lt;code&gt;EKEventStore&lt;/code&gt; is the central &amp;ldquo;thing”. Creating an instance of &lt;code&gt;EKEventStore&lt;/code&gt; provides developers with an API for performing various read/write operations on the user&amp;rsquo;s calendars and reminder lists.&lt;/p&gt;
&lt;p&gt;A View Controller that interacts with the calendar should hold a reference to an &lt;code&gt;EKEventStore&lt;/code&gt; instance. It&amp;rsquo;s easy to create one – here&amp;rsquo;s an 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;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;UITableViewDataSource&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UITableViewDelegate&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 class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;eventStore&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;EKEventStore&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;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;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;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;a name=&#34;checking-calendar-authorization&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;checking-for-calendar-authorization&#34;&gt;Checking for calendar authorization&lt;/h3&gt;
&lt;p&gt;Once we have a reference to an &lt;code&gt;EKEventStore&lt;/code&gt; instance, we can use it to do things like check whether or not the user has granted us permission to use their calendar. From there, we can make decisions about whether or not we need to request permission, and subsequently figure out which view to show (the table view or the need permission view).&lt;/p&gt;
&lt;p&gt;Where we check for calendar authorization is important. My recommendation is to check each time the view appears (ie, in &lt;code&gt;viewWillAppear()&lt;/code&gt;, because it&amp;rsquo;s completely possible that the user could grant access at first, switch to settings, and deny access. Our app would need to respond appropriately.&lt;/p&gt;
&lt;p&gt;In the example project provided with this article, I&amp;rsquo;ve created a function named &lt;code&gt;checkCalendarAuthorizationStatus()&lt;/code&gt;. Here a peek at what it does:&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 class=&#34;n&#34;&gt;UITableViewDataSource&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UITableViewDelegate&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 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&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;override&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;viewWillAppear&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;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; 6&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;checkCalendarAuthorizationStatus&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;p&#34;&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;checkCalendarAuthorizationStatus&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;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;status&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;EKEventStore&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;authorizationStatus&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;k&#34;&gt;for&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;EKEntityType&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;event&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;k&#34;&gt;switch&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;status&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;k&#34;&gt;case&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;EKAuthorizationStatus&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;notDetermined&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;c1&#34;&gt;// This happens on first-run&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;requestAccessToCalendar&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;k&#34;&gt;case&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;EKAuthorizationStatus&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;authorized&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;c1&#34;&gt;// Things are in line with being able to show the calendars in the table view&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;loadCalendars&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;n&#34;&gt;refreshTableView&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;20&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;case&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;EKAuthorizationStatus&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;restricted&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;EKAuthorizationStatus&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;denied&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;c1&#34;&gt;// We need to help them give us permission&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;needPermissionView&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;23&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;24&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;25&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;26&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;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;27&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 key function here is &lt;code&gt;EKEventStore&#39;s&lt;/code&gt; &lt;code&gt;authorizationStatus(for:)&lt;/code&gt; function. Passing in &lt;code&gt;EKEntityType.event&lt;/code&gt; is what corresponds to the user&amp;rsquo;s &lt;em&gt;calendar&lt;/em&gt;. If we wanted to check for access to their reminders, we&amp;rsquo;d use &lt;code&gt;EKEntityTypeReminder&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The possible &lt;code&gt;EKAuthorizationStatus&lt;/code&gt; enumeration values are simply switched over – the logic to be performed is encapsulated in separate functions for ease of readability.&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s step through each of those functions one by one…&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;info-plist&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;update-infoplist-for-ios-10-support&#34;&gt;Update Info.plist for iOS 10 support&lt;/h4&gt;
&lt;p&gt;Apple now requires us to have a key/value pair in Info.plist that provides a description to the user as to why our apps need access to their calendar.&lt;/p&gt;
&lt;p&gt;To set this value, open your Info.plist file, and add a new key for &amp;ldquo;Privacy – Calendars Usage Description”:&lt;br&gt;
&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/05/info-plist-calendar-usage-description.png&#34;&gt;&lt;img src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/05/info-plist-calendar-usage-description-1024x346.png&#34; alt=&#34;Info.plist Calendar Usage Description&#34; width=&#34;1024&#34; height=&#34;346&#34; class=&#34;alignnone size-large wp-image-13055&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/05/info-plist-calendar-usage-description-1024x346.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2015/05/info-plist-calendar-usage-description-300x101.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2015/05/info-plist-calendar-usage-description.png 1100w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The value that you provide for this plist key ends up being displayed in the alert that&amp;rsquo;s displayed when you request access.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;request-access-to-calendars&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;requesting-access-to-calendars&#34;&gt;Requesting access to calendars&lt;/h4&gt;
&lt;p&gt;As the title of this tutorial suggests, all things start here. Whenever our application loads and we call &lt;code&gt;authorizationStatus(for:)&lt;/code&gt;, the status that will be returned is &lt;code&gt;notDetermined&lt;/code&gt;. It&amp;rsquo;s at this point that we&amp;rsquo;d like to request access to the calendar.&lt;/p&gt;
&lt;p&gt;To do so, let&amp;rsquo;s dissect the &lt;code&gt;requestAccessToCalendar&lt;/code&gt; 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;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;UITableViewDataSource&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UITableViewDelegate&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 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&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;requestAccessToCalendar&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;eventStore&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;requestAccess&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;to&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;EKEntityType&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;event&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; 7&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;accessGranted&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;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;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; 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;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;accessGranted&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&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;n&#34;&gt;DispatchQueue&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;main&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;async&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;execute&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;kc&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;loadCalendars&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;refreshTableView&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&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;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;15&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;                &lt;span class=&#34;n&#34;&gt;DispatchQueue&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;main&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;async&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;execute&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;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;needPermissionView&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;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 class=&#34;p&#34;&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;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;// ...&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;p&gt;Our &lt;code&gt;EKEventStore&lt;/code&gt; instance provides a function called &lt;code&gt;requestAccess(to:)&lt;/code&gt;. Once again, passing in &lt;code&gt;EKEntityType.event&lt;/code&gt; is what signals that we&amp;rsquo;re requesting access to the &lt;em&gt;calendar&lt;/em&gt;. The rest of the interesting parts are found in the completion closure that we provide.&lt;/p&gt;
&lt;p&gt;There are three main things to note with this portion of the implementation:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The two parameters that are passed in to the closure is a &lt;code&gt;Bool&lt;/code&gt; indicating access was granted (&lt;code&gt;true&lt;/code&gt;) or denied (&lt;code&gt;false&lt;/code&gt;). The second is an &lt;code&gt;NSError&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;We need to call &lt;code&gt;dispatch_async()&lt;/code&gt; and indicate that we want to jump back over to the main queue to execute our UI refreshes.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;self.needPermissionView.fadeIn()&lt;/code&gt; utilizes a &lt;code&gt;UIView&lt;/code&gt; extension from my post, &lt;a href=&#34;http://www.andrewcbancroft.com/2014/07/27/fade-in-out-animations-as-class-extensions-with-swift/&#34;&gt;Fade In / Out Animations as Class Extensions in Swift&lt;/a&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;a name=&#34;access-granted&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h5 id=&#34;access-granted-load-calendars-and-refresh-table-view&#34;&gt;Access granted! Load calendars and refresh table view&lt;/h5&gt;
&lt;p&gt;When access is granted, we can call the &lt;code&gt;eventStore&lt;/code&gt; instance&amp;rsquo;s &lt;code&gt;calendarsForEntityType&lt;/code&gt; function and pass it &lt;code&gt;EKEntityType.event&lt;/code&gt; to grab an array of the user&amp;rsquo;s calendars to display in our table view. Here&amp;rsquo;s 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;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;UITableViewDataSource&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UITableViewDelegate&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 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&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;calendars&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;EKCalendar&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;// ...&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;loadCalendars&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;kc&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;calendars&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;eventStore&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;calendars&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;k&#34;&gt;for&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;EKEntityType&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;event&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;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;refreshTableView&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;calendarsTableView&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;isHidden&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;15&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;calendarsTableView&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;reloadData&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&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;// ...&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;&lt;a name=&#34;access-denied&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h5 id=&#34;access-denied--show-needs-permission-view&#34;&gt;Access denied – Show needs permission view&lt;/h5&gt;
&lt;p&gt;When access is &lt;em&gt;denied&lt;/em&gt;, we need to unveil the &amp;ldquo;Needs Permission View” we created in our Storyboard Scene.&lt;/p&gt;
&lt;p&gt;Recall that in that view, there&amp;rsquo;s a button to direct the user to the Settings page for our app so that they can easily grant access to the calendar from there. That button is wired up to an IBAction. Here&amp;rsquo;s an example implementation of that IBAction:&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 class=&#34;n&#34;&gt;UITableViewDataSource&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UITableViewDelegate&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 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&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;goToSettingsButtonTapped&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; 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;openSettingsUrl&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;n&#34;&gt;string&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIApplicationOpenSettingsURLString&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;UIApplication&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;shared&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;openURL&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;openSettingsUrl&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;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;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;&lt;a name=&#34;wrapping-up&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;wrapping-up&#34;&gt;Wrapping up&lt;/h3&gt;
&lt;p&gt;That pretty much completes the setup process for working with Event Kit! The remaining cases for the &lt;code&gt;checkCalendarAuthorizationStatus()&lt;/code&gt; function simply re-use the functions I just dissected when exploring the requesting permission process.&lt;/p&gt;
&lt;p&gt;I encourage you to &lt;a href=&#34;https://github.com/andrewcbancroft/EventTracker/tree/ask-for-permission&#34;&gt;head over to GitHub&lt;/a&gt; and dive into the code for yourself as you get started with utilizing Event Kit in your app!&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/06/17/creating-calendars-with-event-kit-and-swift/&#34; title=&#34;Creating Calendars with Event Kit and Swift&#34;&gt;Creating Calendars with Event Kit and 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/04/28/listing-calendar-events-with-event-kit-and-swift/&#34; title=&#34;Listing Calendar Events with Event Kit and Swift&#34;&gt;Listing Calendar Events with Event Kit and 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/06/02/creating-calendar-events-with-event-kit-and-swift/&#34; title=&#34;Creating Calendar Events with Event Kit and Swift&#34;&gt;Creating Calendar Events with Event Kit and 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>Strong, Weak, and Unowned – Sorting out ARC and Swift</title>
      <link>https://www.andrewcbancroft.com/2015/05/08/strong-weak-and-unowned-sorting-out-arc-and-swift/</link>
      <pubDate>Fri, 08 May 2015 18:01:09 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2015/05/08/strong-weak-and-unowned-sorting-out-arc-and-swift/</guid>
      <description>&lt;p&gt;I&amp;rsquo;m willing to bet that a good number of Swift developers struggle with the particulars of how &lt;code&gt;strong&lt;/code&gt;, &lt;code&gt;weak&lt;/code&gt;, and &lt;code&gt;unowned&lt;/code&gt; affect the run-time behavior of their code. I, myself, wouldn&amp;rsquo;t want to have to give an explanation of the finer points of Automatic Reference Counting (ARC) if my life depended on it.&lt;/p&gt;
&lt;p&gt;I wanted to stop being unsure about the implications of typing one of those three words before variable / constant declarations, so I finally pulled up the &lt;a href=&#34;https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/AutomaticReferenceCounting.html&#34;&gt;Apple Documentation on ARC&lt;/a&gt; and began trying to digest the semantics. This article is an attempt to share what got sorted out in my brain as it regards ARC and Swift.&lt;/p&gt;
&lt;p&gt;This article is long enough that I thought, &amp;ldquo;Why don&amp;rsquo;t I summarize my conclusions up front and then let folks read about how I got there if they so-desire”. So here you go: Conclusions first!&lt;/p&gt;
&lt;h3 id=&#34;conclusions&#34;&gt;Conclusions&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;To determine if you even need to worry about &lt;code&gt;strong&lt;/code&gt;, &lt;code&gt;weak&lt;/code&gt;, or &lt;code&gt;unowned&lt;/code&gt;, ask, &amp;ldquo;Am I dealing with &lt;em&gt;reference&lt;/em&gt; types”. If you&amp;rsquo;re working with Structs or Enums, ARC isn&amp;rsquo;t managing the memory for those Types and you don&amp;rsquo;t even need to worry about specifying &lt;code&gt;weak&lt;/code&gt; or &lt;code&gt;unowned&lt;/code&gt; for those constants or variables.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Strong&lt;/code&gt; references are fine in hierarchical relationships where the parent references the child, but not vice-versa. In fact, &lt;code&gt;strong&lt;/code&gt; references are the most appropraite kind of reference most of the time.&lt;/li&gt;
&lt;li&gt;When two instances are &lt;em&gt;optionally&lt;/em&gt; related to one another, make sure that one of those instances holds a &lt;code&gt;weak&lt;/code&gt; reference to the other.&lt;/li&gt;
&lt;li&gt;When two instances are related in such a way that one of the instances can&amp;rsquo;t exist without the other, the instance with the mandatory dependency needs to hold an &lt;code&gt;unowned&lt;/code&gt; reference to the other instance.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;To see how I arrived at this set of conclusions, read on or jump around!&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;arc-and-memory-management&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;arc-and-memory-management&#34;&gt;ARC and memory management&lt;/h3&gt;
&lt;p&gt;For many, much of &amp;ldquo;memory management” feels like a huge black box.&lt;/p&gt;
&lt;p&gt;The mystical nature of memory management is exaggerated by the fact that many of the languages we&amp;rsquo;re familiar with abstract it away. For the most part, we can simply write code and not think too hard about the number of bytes our object instances are using or how they get cleaned up when they&amp;rsquo;re done being used. It just works.&lt;/p&gt;
&lt;p&gt;Automatic Reference Counting (ARC) is one of those abstractions over managing memory. It&amp;rsquo;s the methodology Apple employs to free up memory when class instances are finished using it.&lt;/p&gt;
&lt;p&gt;ARC &lt;em&gt;only&lt;/em&gt; applies to classes (so not structs or enums), because Automatic &lt;em&gt;Reference&lt;/em&gt; Counting applies only to &lt;em&gt;reference Types&lt;/em&gt;. Structs &amp;amp; enums are &lt;em&gt;value&lt;/em&gt; Types, so ARC does not manage the memory associated with instances of those Types.&lt;/p&gt;
&lt;p&gt;The question that begins the discussion of our featured keywords is, &amp;ldquo;How does ARC know when an instance is &amp;ldquo;finished using” the memory it borrowed?”&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;strong-references&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;strong-references&#34;&gt;Strong references&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;Strong&lt;/code&gt;, &lt;code&gt;weak&lt;/code&gt;, and &lt;code&gt;unowned&lt;/code&gt; are keywords that describe the nature of a reference in the ARC paradigm.&lt;/p&gt;
&lt;p&gt;All references are &lt;code&gt;strong&lt;/code&gt; references by default unless otherwise specified. &lt;em&gt;Most&lt;/em&gt; of the time, this is the right thing to do. &lt;code&gt;Strong&lt;/code&gt; is always implied when you declare a variable or constant. You don&amp;rsquo;t need to type &lt;code&gt;strong&lt;/code&gt; in Swift.&lt;/p&gt;
&lt;p&gt;So what does the &amp;ldquo;strength” of a reference have to do with how ARC manages memory?&lt;/p&gt;
&lt;p&gt;ARC doesn&amp;rsquo;t free up the memory being used by a class instance until &lt;em&gt;all&lt;/em&gt; &lt;code&gt;strong&lt;/code&gt; references to that instance are broken. How are &lt;code&gt;strong&lt;/code&gt; references broken?&lt;/p&gt;
&lt;p&gt;&lt;code&gt;Strong&lt;/code&gt; references can be broken…&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;When a variable that references an instance of something is set to &lt;code&gt;nil&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;When a parent variable that holds a reference to a child class instance is to &lt;code&gt;nil&lt;/code&gt; it will break the reference to the parent &lt;em&gt;and&lt;/em&gt; the child&lt;/li&gt;
&lt;li&gt;When a variable or constant goes out of scope – for example, if something gets initialized inside a control-flow code segment like an &lt;code&gt;if&lt;/code&gt;/&lt;code&gt;else&lt;/code&gt; or inside a &lt;code&gt;for&lt;/code&gt; loop, when execution moves past that code segment, the reference is broken and the memory is freed by ARC&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Everything in ARC&amp;rsquo;s memory management model hinges on the number of &lt;code&gt;strong&lt;/code&gt; references are connected to an instance. The moment that the number of &lt;code&gt;strong&lt;/code&gt; references counts down to zero, the memory where that instance was stored is freed.&lt;/p&gt;
&lt;p&gt;So where does &lt;code&gt;weak&lt;/code&gt; and &lt;code&gt;unowned&lt;/code&gt; come in?&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;weak-unowned-references&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;weak-and-unowned-references&#34;&gt;Weak and unowned references&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;Weak&lt;/code&gt; and &lt;code&gt;unowned&lt;/code&gt; references come into the picture when we start talking about the relationships that emerge between class instances.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;relationships-between-instances&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;relationships-between-instances&#34;&gt;Relationships between instances&lt;/h4&gt;
&lt;p&gt;Relationships between classes are core to the Object-Oriented paradigm. Whether you plan them or not, these relationships exist, and they affect the ARC memory management model.&lt;/p&gt;
&lt;p&gt;So which kinds of relationships exist? Which ones warrant the use of &lt;code&gt;weak&lt;/code&gt; and &lt;code&gt;unowned&lt;/code&gt;?&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;hierarchical&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h5 id=&#34;hierarchical-relationships&#34;&gt;Hierarchical relationships&lt;/h5&gt;
&lt;p&gt;Much of our programming in Swift involves hierarchical relationships. Things are often modeled in such a way that one class instance holds &lt;code&gt;strong&lt;/code&gt; references to one or more child class instances, and those child class instances hold one or more &lt;code&gt;strong&lt;/code&gt; references to &lt;em&gt;other&lt;/em&gt; child class instances, and so on. The &lt;code&gt;strong&lt;/code&gt; reference relationship in this type of situation flows in one direction: from parent to child.&lt;/p&gt;
&lt;p&gt;In this arrangement, &lt;code&gt;strong&lt;/code&gt; references are normal and fine. But what happens if, say, a &lt;em&gt;child&lt;/em&gt; class were to hold a reference back to its &lt;em&gt;parent&lt;/em&gt;? This is where we can get into trouble with ARC.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;optional-mutually-dependent&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h5 id=&#34;optional-mutually-dependent-relationships&#34;&gt;Optional, mutually dependent relationships&lt;/h5&gt;
&lt;p&gt;By the end of this section, my goal is to demonstrate the role of &lt;code&gt;weak&lt;/code&gt; references.&lt;/p&gt;
&lt;p&gt;When class instances depend on each other, that is, they both hold a reference to one another, it can be said that the instances are mutually dependent. Sometimes, the dependence isn&amp;rsquo;t required – in these cases, it makes sense for one instance to exist without a reference to the other, and vice-versa.&lt;/p&gt;
&lt;p&gt;If we were talking database cardinality, we might say that the relationship is 0:1 both ways.&lt;/p&gt;
&lt;p&gt;An example is always nice to have before us – This past week, a tornado hit a zoo here in Oklahoma, and a few exotic animals escaped, so the theme is on my mind.&lt;/p&gt;
&lt;p&gt;In a zoo, an &lt;code&gt;Animal&lt;/code&gt; lives in an &lt;code&gt;Exhibit&lt;/code&gt;. Sometimes &lt;code&gt;Exhibits&lt;/code&gt; may be vacant. They could be cleaning it, or the &lt;code&gt;Animal&lt;/code&gt; that lived there is sick or being moved, or a tornado hits it, etc.&lt;/p&gt;
&lt;p&gt;Likewise, an &lt;code&gt;Animal&lt;/code&gt; may not live in an &lt;code&gt;Exhibit&lt;/code&gt;. It could be held in a temporary location while its &lt;code&gt;Exhibit&lt;/code&gt; is being cleaned, or be in the process of being treated or transferred to another location, or it could have escaped the zoo due to a tornado, etc.&lt;/p&gt;
&lt;p&gt;We could model this example 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;kd&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;Animal&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;name&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;String&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 3&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;species&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 class=&#34;kd&#34;&gt;init&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;name&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;String&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;species&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; 5&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;kc&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;name&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;name&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 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;species&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;species&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;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;exhibit&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Exhibit&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;?&lt;/span&gt;  &lt;span class=&#34;c1&#34;&gt;// notice the optional nature of exhibit&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;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;Exhibit&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;title&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;14&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;title&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;kc&#34;&gt;self&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;n&#34;&gt;title&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&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;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;animal&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Animal&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;?&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;// notice the optional nature of animal&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;Let&amp;rsquo;s consider the situation where an &lt;code&gt;Animal&lt;/code&gt; &lt;em&gt;is&lt;/em&gt; living in an &lt;code&gt;Exhibit&lt;/code&gt;. It might be nice to traverse a relationship between them to get information about one or the other. So as we instantiate an &lt;code&gt;Animal&lt;/code&gt; and an &lt;code&gt;Exhibit&lt;/code&gt;, the next immediate step would be to assign the instances to each other&amp;rsquo;s corresponding property.&lt;/p&gt;
&lt;p&gt;Now consider the situation that happened this week – a tornado hits the zoo and the &lt;code&gt;Animal&lt;/code&gt; escapes from its &lt;code&gt;Exhibit&lt;/code&gt;. Say that &lt;em&gt;unlike&lt;/em&gt; this week, the &lt;code&gt;Animal&lt;/code&gt; isn&amp;rsquo;t recovered (they actually got the animals that escaped back). Suppose that it roams the plains of Oklahoma in freedom. The zoo, in turn, has to close the &lt;code&gt;Exhibit&lt;/code&gt;. To model this, we may simply set the &lt;code&gt;Animal&lt;/code&gt; instance to nil and the &lt;code&gt;Exhibit&lt;/code&gt; instance to nil.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;memory-leak-warning&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h6 id=&#34;memory-leak-warning&#34;&gt;Memory leak warning&lt;/h6&gt;
&lt;p&gt;The example just given is one that shows how it is possible to create a memory leak in your application. How?&lt;/p&gt;
&lt;p&gt;It all has to do with ARC – remember that it &lt;em&gt;only&lt;/em&gt; frees up memory for a class instance when there are &lt;em&gt;zero&lt;/em&gt; references to it.&lt;/p&gt;
&lt;p&gt;In our &lt;code&gt;Animal&lt;/code&gt; – &lt;code&gt;Exhibit&lt;/code&gt; example, both instances referenced &lt;em&gt;each other&lt;/em&gt;. When we set the &lt;code&gt;Animal&lt;/code&gt; instance to nil, the &lt;code&gt;Exhibit&lt;/code&gt; instance still held a reference to it through its &lt;code&gt;animal&lt;/code&gt; property.&lt;/p&gt;
&lt;p&gt;In turn, since that &lt;code&gt;Animal&lt;/code&gt; instance is still around, it holds a reference to the &lt;code&gt;Exhibit&lt;/code&gt; instance through its &lt;code&gt;exhibit&lt;/code&gt; property. So when we set the &lt;code&gt;Exhibit&lt;/code&gt; instance to nil, the &lt;code&gt;Animal&lt;/code&gt; still holds on to it.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Now&lt;/em&gt; we have a dilemma. While the &lt;code&gt;Animal&lt;/code&gt; and the &lt;code&gt;Exhibit&lt;/code&gt; continue to reference each other (so neither instance&amp;rsquo;s memory can be freed), &lt;em&gt;nothing else does&lt;/em&gt; – there&amp;rsquo;s no way to access either instance any more. Thus, a memory leak is created.&lt;/p&gt;
&lt;p&gt;But there&amp;rsquo;s hope! In this &amp;ldquo;Optional, mutually dependent relationship” scenario, this is where the keyword &lt;code&gt;weak&lt;/code&gt; comes into play.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;breaking-strong-reference-cycle&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h6 id=&#34;breaking-strong-reference-cycle&#34;&gt;Breaking strong reference cycle&lt;/h6&gt;
&lt;p&gt;The situation just described is what&amp;rsquo;s known as a &amp;ldquo;strong reference cycle”.&lt;/p&gt;
&lt;p&gt;Thankfully, there&amp;rsquo;s a way to break those cycles and avoid memory leaks.&lt;/p&gt;
&lt;p&gt;In the situation where that optional mutually dependent relationship exists, such as between an &lt;code&gt;Animal&lt;/code&gt; and an &lt;code&gt;Exhibit&lt;/code&gt;, changing one of those instances reference to the other from &lt;code&gt;strong&lt;/code&gt; to &lt;code&gt;weak&lt;/code&gt; will break the cycle.&lt;/p&gt;
&lt;p&gt;It doesn&amp;rsquo;t really matter which class holds the weak reference, just as long as one of them does.&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;Animal&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;name&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;String&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 3&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;species&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 class=&#34;kd&#34;&gt;init&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;name&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;String&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;species&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; 5&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;kc&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;name&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;name&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 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;species&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;species&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;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;exhibit&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Exhibit&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;?&lt;/span&gt;  &lt;span class=&#34;c1&#34;&gt;// we&amp;#39;ll hold a strong reference to the exhibit&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;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;Exhibit&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;title&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;14&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;title&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;kc&#34;&gt;self&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;n&#34;&gt;title&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&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;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;animal&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Animal&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;?&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;// but notice - we&amp;#39;re holding a _weak_ reference to the animal&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;Making the &lt;code&gt;Exhibit&#39;s&lt;/code&gt; &lt;code&gt;animal&lt;/code&gt; property hold a &lt;code&gt;weak&lt;/code&gt; reference to an &lt;code&gt;Animal&lt;/code&gt; instance eliminates the possibility of a strong reference cycle.&lt;/p&gt;
&lt;p&gt;So what about &lt;code&gt;unowned&lt;/code&gt;?&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;mandatory-one-way-dependent&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h5 id=&#34;mandatory-one-way-dependent-relationships&#34;&gt;Mandatory, one-way dependent relationships&lt;/h5&gt;
&lt;p&gt;By the end of this section, my goal is to demonstrate the role of &lt;code&gt;unowned&lt;/code&gt; references.&lt;/p&gt;
&lt;p&gt;There is one final category of relationship that pertains most immediately to the topic of ARC. There are times when two class instances are related to one another, but &lt;em&gt;one&lt;/em&gt; of those instance &lt;em&gt;cannot&lt;/em&gt; exist without the other.&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s continue with the zoo theme. Suppose that our zoo issues &lt;code&gt;AnnualPasses&lt;/code&gt; to &lt;code&gt;Visitors&lt;/code&gt;. A &lt;code&gt;Visitor&lt;/code&gt; can come to the zoo &lt;em&gt;without&lt;/em&gt; an &lt;code&gt;AnnualPass&lt;/code&gt;, of course, and can thus exist on its own and happily enjoy the exhibits. An &lt;code&gt;AnnualPass&lt;/code&gt;, however, is &lt;em&gt;always&lt;/em&gt; issued to a &lt;code&gt;Visitor&lt;/code&gt; and cannot exist without being associated with one.&lt;/p&gt;
&lt;p&gt;To model this kind of relationship, we may define some classes 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;kd&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;Visitor&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;name&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;String&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 3&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;annualPass&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;AnnualPass&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;init&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;name&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;String&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;p&#34;&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;self&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;name&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;name&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 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;kd&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;AnnualPass&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;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;number&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;UInt64&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;kr&#34;&gt;unowned&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;passholder&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Visitor&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;// notice the use of an _unowned_ reference&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;init&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;number&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;UInt64&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;passholder&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Visitor&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;kc&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;number&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;number&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;passholder&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;passholder&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Notice a couple of things:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The &lt;code&gt;Visitor&lt;/code&gt; holds an optional &lt;code&gt;strong&lt;/code&gt; reference to an &lt;code&gt;AnnualPass&lt;/code&gt; instance&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;AnnualPass&lt;/code&gt; holds a &lt;em&gt;non&lt;/em&gt;-optional &lt;code&gt;unowned&lt;/code&gt; reference to a &lt;code&gt;Visitor&lt;/code&gt; instance&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Modeling things this way also eliminates the possibility of a strong reference cycle by allowing the &lt;code&gt;strong&lt;/code&gt; reference count to reach zero when a &lt;code&gt;Visitor&lt;/code&gt; variable is set to nil.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;conclusions-2&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;conclusions-1&#34;&gt;Conclusions&lt;/h3&gt;
&lt;p&gt;I know I stated these at the beginning, but I&amp;rsquo;ll restate them here just for completeness…&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;To determine if you even need to worry about &lt;code&gt;strong&lt;/code&gt;, &lt;code&gt;weak&lt;/code&gt;, or &lt;code&gt;unowned&lt;/code&gt;, ask, &amp;ldquo;Am I dealing with &lt;em&gt;reference&lt;/em&gt; types”. If you&amp;rsquo;re working with Structs or Enums, ARC isn&amp;rsquo;t managing the memory for those Types and you don&amp;rsquo;t even need to worry about specifying &lt;code&gt;weak&lt;/code&gt; or &lt;code&gt;unowned&lt;/code&gt; for those constants or variables.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Strong&lt;/code&gt; references are fine in hierarchical relationships where the parent references the child, but not vice-versa. In fact, &lt;code&gt;strong&lt;/code&gt; references are the most appropraite kind of reference most of the time.&lt;/li&gt;
&lt;li&gt;When two instances are &lt;em&gt;optionally&lt;/em&gt; related to one another, make sure that one of those instances holds a &lt;code&gt;weak&lt;/code&gt; reference to the other.&lt;/li&gt;
&lt;li&gt;When two instances are related in such a way that one of the instances can&amp;rsquo;t exist without the other, the instance with the mandatory dependency needs to hold an &lt;code&gt;unowned&lt;/code&gt; reference to the other instance.&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>SQL Sever Synonyms, Entity Framework, and Integrated Security</title>
      <link>https://www.andrewcbancroft.com/2015/05/01/using-sql-sever-synonyms-entity-framework-integrated-security/</link>
      <pubDate>Fri, 01 May 2015 17:44:31 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2015/05/01/using-sql-sever-synonyms-entity-framework-integrated-security/</guid>
      <description>&lt;p&gt;&lt;a name=&#34;scenario&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;scenario&#34;&gt;Scenario&lt;/h3&gt;
&lt;p&gt;This may be an edge case, but I spent enough time (a couple of &lt;em&gt;days&lt;/em&gt;) spinning on this that I thought it warranted a signpost on the Internet.&lt;/p&gt;
&lt;p&gt;Suppose you have the following scenario before you:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Basics&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;You&amp;rsquo;re building an ASP.Net web application&lt;/li&gt;
&lt;li&gt;Entity Framework has been chosen as your ORM&lt;/li&gt;
&lt;li&gt;SQL Server (any version supporting &lt;a href=&#34;https://msdn.microsoft.com/en-us/library/ms187552.aspx&#34;&gt;Synonyms&lt;/a&gt;) is your backend database server of choice&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Details&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;You&amp;rsquo;re using &amp;ldquo;Integrated Security=True” in your connection string to the database (in web.config)&lt;/li&gt;
&lt;li&gt;The Application Pool in which your app resides is running under a domain service account (ie, not the default identity)&lt;/li&gt;
&lt;li&gt;Your application primarily talks to one database, but there is information in &lt;em&gt;another&lt;/em&gt; database that you need to pull into your app&lt;/li&gt;
&lt;li&gt;To access the data in that other database, you&amp;rsquo;ve chosen to create a &lt;a href=&#34;https://msdn.microsoft.com/en-us/library/ms187552.aspx&#34;&gt;Synonym&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Every bullet point in this scenario is normal and valid, until you get to the part about using &amp;ldquo;Integrated Security=True” in your connection string…&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;login-failed&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;login-failed-for-user-8216nt-authorityanonymous-logon&#34;&gt;Login failed for user ‘NT AUTHORITY\ANONYMOUS LOGON&#39;&lt;/h3&gt;
&lt;p&gt;If you had chosen to use SQL Server authentication in your conenction string, and passed in a username and password for a SQL Server login that had permissions to the database, things would have worked out perfectly. I know this because that&amp;rsquo;s what I had been doing in my own application. I started running into the &amp;ldquo;Login failed…” error message once I switched to Integrated Security.&lt;/p&gt;
&lt;p&gt;For some reason &amp;ldquo;Integrated Security=True” that throws Entity Framework for a loop. It accesses the database used in the &lt;code&gt;DbContext&lt;/code&gt; instance just fine, but the second a line of code needs to use the object mapped to the &lt;em&gt;Synonym&lt;/em&gt;, &lt;strong&gt;boom&lt;/strong&gt;… crash… error…&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;reolving-error&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;resolving-the-error&#34;&gt;Resolving the error&lt;/h3&gt;
&lt;p&gt;It turns out that the resolution is really, really simple. Instead of using a Synonym, use a View. You don&amp;rsquo;t have to change a single line of .Net code. Just make sure the view is named the same as the Synonym was (and drop the Synonym from the database, of course).&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;creating-view&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;creating-the-view&#34;&gt;Creating the view&lt;/h4&gt;
&lt;p&gt;A Synonym essentially maps to an entire table, so the View&amp;rsquo;s structure could literally be a simple &lt;code&gt;select * from [fully.qualified.database.objectName]&lt;/code&gt;. If your DBA hates &lt;code&gt;select *&lt;/code&gt;, you&amp;rsquo;ll end up listing out every column that your Entity Framework object lists as one of its Properties.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;granting-permissions&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;granting-permissions&#34;&gt;Granting permissions&lt;/h4&gt;
&lt;p&gt;If the service account that&amp;rsquo;s running your Application Pool doesn&amp;rsquo;t already have permission to the database objects you created the View for, you need to go in and grant appropriate permissions to that account. Otherwise, you&amp;rsquo;re likely to get an error stating&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;The server principal &amp;ldquo;Domain\PrincipalName” is not able to access the database &amp;ldquo;DatabaseName” under the current security context.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Be specific and granular with the permission you grant to the account. If you&amp;rsquo;re just pulling in data for your app to display, only give the account SELECT permission on the database object your View references.&lt;/p&gt;
&lt;h3 id=&#34;wrapping-up&#34;&gt;Wrapping up&lt;/h3&gt;
&lt;p&gt;In essence, using a Synonym that references a database object in &lt;em&gt;another database&lt;/em&gt; with Entity Framework and Integrated Security just doesn&amp;rsquo;t work out in the end. Somehow, the security context loses the identity of the account running the application pool, and the dreated Login failed for user ‘NT AUTHORITY\ANONYMOUS LOGON&amp;rsquo; rears its ugly head. Thankfully, the solution is simple: Create a View instead of a Synonym, grant approprate permissions, and you&amp;rsquo;ll be off and running (and you&amp;rsquo;ll avoid usernames and passwords in your web.config)!&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>Testing is to Software as Experiment is to Science</title>
      <link>https://www.andrewcbancroft.com/2015/04/29/testing-is-to-software-as-experiment-is-to-science/</link>
      <pubDate>Thu, 30 Apr 2015 04:01:24 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2015/04/29/testing-is-to-software-as-experiment-is-to-science/</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;The principle of science, the definition, almost, is the following: The test of all knowledge is experiment.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;blockquote&gt;
&lt;p&gt;The sole test of the validity of any idea is experiment.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;These quote came from &lt;a href=&#34;http://www.feynmanlectures.caltech.edu/I_01.html#Ch1-S1&#34;&gt;Chapter 1&lt;/a&gt; and &lt;a href=&#34;http://www.feynmanlectures.caltech.edu/I_02.html&#34;&gt;Chapter 2&lt;/a&gt; of the &lt;a href=&#34;http://www.feynmanlectures.caltech.edu/&#34;&gt;Feynman Lectures on Physics&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;m not a physicist, but the software developer in me resonated with the connection between &lt;em&gt;experimentation&lt;/em&gt; and &lt;em&gt;validation&lt;/em&gt; of what we know about the world around us. Almost immediately, I related it to the world of software.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;validity-confirmed-by-experiments&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;validity-is-confirmed-by-experiments-tests&#34;&gt;Validity is confirmed by experiments (tests)&lt;/h3&gt;
&lt;p&gt;The universe around us is an immense mystery. We have many ideas about it, but the ideas that are validated by experiment are the ones we cling to more tightly than the ones that remain in the realm of guess and theory.&lt;/p&gt;
&lt;p&gt;How do we know that things are valid within a software application? The answer, of course, is by &lt;em&gt;testing&lt;/em&gt; that software… experimenting with it, if you will, and observing the outcomes of those experiments.&lt;/p&gt;
&lt;p&gt;Your software is a universe of mystery until it&amp;rsquo;s tested.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;repeated-experiments-most-valuable&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;repeated-experiments-tests-are-most-valuable&#34;&gt;Repeated experiments (tests) are most valuable&lt;/h3&gt;
&lt;p&gt;Experiments that are conducted &lt;em&gt;multiple times&lt;/em&gt; are significantly more valuable than a single observation made from a single experiment. Repeated experiments increase the probability that what was observed was not an accident, but was truly the result of correct procedure validating a correct hypothesis. Performing the experiments as identically as possible to one other is the key to trusting the results.&lt;/p&gt;
&lt;p&gt;Likewise, repeated testing of software is far more valuable than testing it once and walking away. Repeated tests done identically over time provide the reassurance we need to know &lt;em&gt;for sure&lt;/em&gt; that our software behaves as we expect as it morphs to accommodate new ideas and new needs.&lt;/p&gt;
&lt;p&gt;Sure, we could spin up the app in a simulator and click/tap through it to see if pressing [Button X] does what its label implies it will do. But I&amp;rsquo;ve heard it said that if we have to do it more than once, it should be &lt;em&gt;automated&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;Testing is one of those things that needs to be done often. Pretty much any time the app&amp;rsquo;s code changes (even just a little bit!), there is warrant to check and make sure the universe as we knew it still obeys all the laws we previously discovered.&lt;/p&gt;
&lt;p&gt;Repeated tests are just as valuable to software as repeated experiment is to science. The more we can automate them, the greater the consistency in our repeated observations of how the software behaves.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;experimentation-creative-effort&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;experimentation-testing-is-a-creative-effort&#34;&gt;Experimentation (testing) is a creative effort&lt;/h3&gt;
&lt;p&gt;Designing the right experiment to elicit the right conditions to make that &lt;em&gt;one critical&lt;/em&gt; observation is a creative process. Sometimes, scientists have to go to great lengths to make their observations. But they all find it worth it when their experiment yields results!&lt;/p&gt;
&lt;p&gt;I work with some amazing scientists on a day-to-day basis. Watching them work and hearing about some of the things they have to do in order to test their ideas is so fascinating to me. For example, one of our Principal Investigators is making observations about how microgravity affects a plant&amp;rsquo;s cell walls. To do that, he had to literally &lt;a href=&#34;http://www.noble.org/blog/plants-in-space/archive/&#34;&gt;launch his plant&amp;rsquo;s seeds into space&lt;/a&gt;! &lt;a href=&#34;http://www.nasa.gov/mission_pages/station/research/experiments/1062.html&#34;&gt;They&amp;rsquo;re aboard the International Space Station&lt;/a&gt;. Growing plants! In &lt;em&gt;space&lt;/em&gt;! I&amp;rsquo;m blown away by these guys!&lt;/p&gt;
&lt;p&gt;While we needn&amp;rsquo;t launch our code into space just to experiment with it, sometimes we have to get creative in order to make the observations we need to make from our code. Automated testing isn&amp;rsquo;t easy. Running the app and clicking a button and watching what happens with our own eyes is easy, but it&amp;rsquo;s tedious and I&amp;rsquo;m lazy!&lt;/p&gt;
&lt;p&gt;Creating the right environment (in code) for a test to verify what it needs can take effort, but in the end, when the test yields results time after time after time, I&amp;rsquo;ve always found that it&amp;rsquo;s worth it!&lt;/p&gt;
&lt;h3 id=&#34;wrapping-up&#34;&gt;Wrapping up&lt;/h3&gt;
&lt;p&gt;My hope is that seeing automated unit testing from a slightly different perspective has sparked some thoughts and ideas in your mind. How can you begin experiment with your code with automated tests? How can you begin to make repeatable observations about your code so that you know, &lt;em&gt;for sure&lt;/em&gt;, that it works, even &lt;em&gt;after&lt;/em&gt; you go about making changes to it as time goes along?&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;ve devoted a bit time to writing about unit testing in Swift since the language was released. I hope this article adds something unique to the collection and that it gets you motivated to validate your software universe with tests!&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/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;i class=&#34;fa fa-angle-right&#34;&gt;&lt;/i&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;/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>Clarifying Swift Access Control (Hint:  Swift Isn’t C#)</title>
      <link>https://www.andrewcbancroft.com/2015/04/24/clarifying-swift-access-control-hint-swift-isnt-c-sharp/</link>
      <pubDate>Fri, 24 Apr 2015 18:02:34 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2015/04/24/clarifying-swift-access-control-hint-swift-isnt-c-sharp/</guid>
      <description>&lt;p&gt;As it turns out, Swift isn&amp;rsquo;t C# (or Java or VB.Net or…)! My day job keeps me busy writing C#, so I&amp;rsquo;ll reference it as my go-to comparison language for this article. The conclusion, however, carries over to other languages that have access control modifiers.&lt;/p&gt;
&lt;p&gt;I just spent the greater part of a week experimenting with Swift extensions. I was trying to figure out some seemingly strange behavior that they were exhibiting.&lt;/p&gt;
&lt;p&gt;After publishing &lt;a href=&#34;http://www.andrewcbancroft.com/2015/04/22/3-nuances-of-swift-extensions/&#34; title=&#34;3 Nuances of Swift Extensions&#34;&gt;&amp;ldquo;3 Nuances of Swift Extensions”&lt;/a&gt;, I quickly learned that I had a fundamental misunderstanding of Swift access control, thanks to some observant folks in the Swift community.&lt;/p&gt;
&lt;p&gt;What was the hiccup? Read on to find out where I went wrong…&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;how-private-is-private&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;how-private-is-private&#34;&gt;How private is private?&lt;/h3&gt;
&lt;p&gt;One of the most surprising &amp;ldquo;nuances” from &lt;a href=&#34;http://www.andrewcbancroft.com/2015/04/22/3-nuances-of-swift-extensions/&#34; title=&#34;3 Nuances of Swift Extensions&#34;&gt;&amp;ldquo;3 Nuances of Swift Extensions”&lt;/a&gt; that I discovered was that if you define an extension within the same source file as another Type, the extension&amp;rsquo;s members can see the &lt;em&gt;other&lt;/em&gt; Type&amp;rsquo;s &lt;code&gt;private&lt;/code&gt; properties and functions! &amp;ldquo;Whaaat?? How is this possible?!”, I reacted.&lt;/p&gt;
&lt;p&gt;Well… To restate the obvious, Swift isn&amp;rsquo;t C#… and it isn&amp;rsquo;t C# in more ways than just syntax.&lt;/p&gt;
&lt;p&gt;Ever since access control modifiers were introduced in Xcode 6 Beta 4, I had it in my mind that &lt;code&gt;public&lt;/code&gt;, &lt;code&gt;private&lt;/code&gt;, and &lt;code&gt;internal&lt;/code&gt; worked just like they do in C#. Sure, I read &lt;a href=&#34;https://developer.apple.com/swift/blog/?id=5&#34;&gt;the blog article on access control published by the Swift team&lt;/a&gt;, but it was a mere cursory look. I basically saw the three key words and thought, &amp;ldquo;Ah, I got this… moving on!”&lt;/p&gt;
&lt;p&gt;This was a fundamental mistake for me to make, and it goes to show that just because there are &lt;em&gt;similarities&lt;/em&gt; between languages, it doesn&amp;rsquo;t mean the &lt;em&gt;semantics&lt;/em&gt; of those similarities carry over.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;semantics-of-private&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;semantics-of-private&#34;&gt;Semantics of private&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;Private&lt;/code&gt; is the access modifier that got me all confused. The concept of &lt;code&gt;public&lt;/code&gt; and &lt;code&gt;internal&lt;/code&gt; carry over fairly nicely, but &lt;code&gt;private&lt;/code&gt; is the one that&amp;rsquo;s fundamentally different between C# and Swift, so that&amp;rsquo;s where I&amp;rsquo;ll focus.&lt;/p&gt;
&lt;p&gt;In addition to this article, recommend giving &lt;a href=&#34;https://developer.apple.com/swift/blog/?id=5&#34;&gt;the Swift team&amp;rsquo;s original article on access control&lt;/a&gt; a very close read, just to make sure all the semantics of &lt;code&gt;public&lt;/code&gt; and &lt;code&gt;internal&lt;/code&gt; in Swift are clear as well.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;private-and-c-sharp&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;private-and-c&#34;&gt;Private and C#&lt;/h4&gt;
&lt;p&gt;In C#, &lt;code&gt;private&lt;/code&gt; members of a Type are &amp;ldquo;truly” private. They are only visible &lt;em&gt;within that Type&lt;/em&gt;. The member&amp;rsquo;s visibility is limited to the curly braces enclosing the Type&amp;rsquo;s implementation. That&amp;rsquo;s it. No subclass can see &lt;code&gt;private&lt;/code&gt; members. No other Types can see those members, no matter which file those Types are defined in. &lt;code&gt;Private&lt;/code&gt; is private.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;private-and-swift&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;private-and-swift&#34;&gt;Private and Swift&lt;/h4&gt;
&lt;p&gt;And then there&amp;rsquo;s Swift. &lt;code&gt;Private&lt;/code&gt; takes on &lt;em&gt;entirely different&lt;/em&gt; semantics in Swift, and herein lay my fundamental misunderstanding. It was obvious that I just didn&amp;rsquo;t &amp;ldquo;get it” if you read through the &lt;a href=&#34;http://www.andrewcbancroft.com/2015/04/22/3-nuances-of-swift-extensions/&#34; title=&#34;3 Nuances of Swift Extensions&#34;&gt;Nuances Article&lt;/a&gt;. [sigh]&lt;/p&gt;
&lt;p&gt;In Swift, a &lt;code&gt;private&lt;/code&gt; Type, or a &lt;code&gt;public&lt;/code&gt;/&lt;code&gt;internal&lt;/code&gt; Type&amp;rsquo;s &lt;code&gt;private&lt;/code&gt; &lt;em&gt;members&lt;/em&gt; are visible to &lt;em&gt;anything else&lt;/em&gt; defined within the same &lt;strong&gt;file&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;This isn&amp;rsquo;t a characteristic that&amp;rsquo;s limited to extensions. &lt;em&gt;Anything&lt;/em&gt; defined in the same source file as something else can see everything. This is a shocker if you&amp;rsquo;re coming from C# or a similar language where the semantics of access control are used for encapsulation purposes.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;proof-by-example&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;proof-by-example&#34;&gt;Proof by example&lt;/h4&gt;
&lt;p&gt;So suppose you have a file named Types.swift, and within you have the following:&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;private&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;struct&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;Person&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;2&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;private&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;name&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;String&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;3&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;p&#34;&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;private&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;struct&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;Greeter&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;private&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;greet&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;person&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Person&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;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;Hi, I&amp;#39;m &lt;/span&gt;&lt;span class=&#34;si&#34;&gt;\(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;person&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;si&#34;&gt;)&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&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;p&#34;&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;If you&amp;rsquo;re a C# developer, you look at that code and immediately go, &amp;ldquo;Yeah, that&amp;rsquo;s not gonna work… &lt;code&gt;name&lt;/code&gt; is &lt;code&gt;private&lt;/code&gt; to &lt;code&gt;Person&lt;/code&gt; and can&amp;rsquo;t be referenced outside that Type”.&lt;/p&gt;
&lt;p&gt;But in Swift, this is totally legitimate. Even though &lt;code&gt;Person&lt;/code&gt; is &lt;code&gt;private&lt;/code&gt;, &lt;code&gt;Greeter&lt;/code&gt; can see &lt;code&gt;Person&lt;/code&gt; and initialize one, &lt;em&gt;and&lt;/em&gt; it can see &lt;code&gt;Person&lt;/code&gt;‘s &lt;code&gt;private&lt;/code&gt; property, &lt;code&gt;name&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;Private&lt;/code&gt; in Swift simply limits visibility to Types and members within the same &lt;em&gt;source file&lt;/em&gt;. Multiple Types defined in the same source file can see other &lt;code&gt;private&lt;/code&gt; Types, as well as other Types&amp;rsquo; &lt;code&gt;private&lt;/code&gt; properties and functions. In other words, &amp;ldquo;&lt;code&gt;private&lt;/code&gt; isn&amp;rsquo;t &lt;code&gt;private&lt;/code&gt;&amp;rdquo;, if you&amp;rsquo;re thinking of private like a C# developer (or a developer familiar with other languages with access control modifiers similar to C#).&lt;/p&gt;
&lt;h3 id=&#34;wrapping-up&#34;&gt;Wrapping up&lt;/h3&gt;
&lt;p&gt;There is a fundamental difference in the semantics of access control between C# and Swift.&lt;/p&gt;
&lt;p&gt;In C#, we typically think of access control in terms of inheritance characteristics. The modifiers affect the Type and revolve around the Type and its interaction with other Types.&lt;/p&gt;
&lt;p&gt;Swift, controls access to members in terms of &lt;em&gt;source file&lt;/em&gt; and &lt;em&gt;module&lt;/em&gt;. Types defined within the same source file see everything about each other, including &lt;code&gt;private&lt;/code&gt; members. &lt;code&gt;Internal&lt;/code&gt; Types and members expand visibility to anywhere within the same module. Finally, &lt;code&gt;public&lt;/code&gt; access makes Types and their members visible &lt;em&gt;everywhere&lt;/em&gt;, even to Types defined in other modules.&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/04/22/3-nuances-of-swift-extensions/&#34; title=&#34;3 Nuances of Swift Extensions&#34;&gt;3 Nuances of Swift Extensions&lt;/a&gt;
    &lt;/li&gt;
    &lt;li&gt;
      &lt;i class=&#34;fa fa-angle-right&#34;&gt;&lt;/i&gt; &lt;a href=&#34;http://www.andrewcbancroft.com/2014/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;
&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>3 Nuances of Swift Extensions</title>
      <link>https://www.andrewcbancroft.com/2015/04/22/3-nuances-of-swift-extensions/</link>
      <pubDate>Wed, 22 Apr 2015 17:52:36 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2015/04/22/3-nuances-of-swift-extensions/</guid>
      <description>&lt;p&gt;How often do we take an initial cursory look at some documentation, shake our heads and say, &amp;ldquo;Ok, sure! Got it!”, and then some time later get to the actual usage of that perceived understanding only to find out, &amp;ldquo;Woah – this is behaving differently than I expected! I wonder if the documentation says anything about this?!”&lt;/p&gt;
&lt;p&gt;A few discussions I&amp;rsquo;ve had recently have prompted me to question what I thought I knew about Swift extensions. I have read documentation about extensions and I &lt;em&gt;thought&lt;/em&gt; I understood them pretty thoroughly. However, these conversations, along with some experimentation done on my own revealed a few nuances that I didn&amp;rsquo;t pick up on before.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Update:&lt;/strong&gt; Almost immediately after publishing this article, the Swift community chimed in and helped me figure out my fundamental hiccup which prompted the aforementioned experimentation in the first place. I&amp;rsquo;ve written a follow-up article called &lt;a href=&#34;http://www.andrewcbancroft.com/2015/04/24/clarifying-swift-access-control-hint-swift-isnt-c-sharp/&#34; title=&#34;Clarifying Swift Access Control (Hint:  Swift Isn’t C#)&#34;&gt;&amp;ldquo;Clarifying Swift Access Control”&lt;/a&gt;, describing that misunderstanding. I recommend giving that one a read to avoid making the same mistake I did!&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;3-nuances&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;three-nuances-of-extensions&#34;&gt;Three nuances of extensions&lt;/h3&gt;
&lt;p&gt;In particular, the following three nuances challenged what I thought I knew about Swift extensions:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The visibility Swift extensions have into the Type they&amp;rsquo;re extending. Can they see things marked &lt;code&gt;private&lt;/code&gt;, for example?&lt;/li&gt;
&lt;li&gt;How that visibility is affected by where the extension is defined. If I have the source for a Type that I&amp;rsquo;m writing an extension for, does defining it within that same source file vs defining it in a separate file affect what it can &amp;ldquo;see”?&lt;/li&gt;
&lt;li&gt;The default access modifiers of the extension&amp;rsquo;s &amp;ldquo;members” and how specifying them or &lt;em&gt;not&lt;/em&gt; specifying them affect what an extension exposes as public API for the Type it&amp;rsquo;s extending.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Before I begin, suppose that I have a public struct called &lt;code&gt;Person&lt;/code&gt;. It has some private properties, &lt;code&gt;name&lt;/code&gt;, &lt;code&gt;gender&lt;/code&gt;, and &lt;code&gt;age&lt;/code&gt;. An enum encapsulates the idea of &lt;code&gt;Gender&lt;/code&gt;. The struct looks something like this:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-swift&#34; data-lang=&#34;swift&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 1&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;struct&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;Person&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 2&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;private&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;name&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;String&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 3&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;private&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;gender&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Gender&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 4&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;private&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;age&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;Int&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 5&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 6&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;init&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;name&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;String&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;gender&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Gender&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;age&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;Int&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 7&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;kc&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;name&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;name&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 8&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;kc&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;gender&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;gender&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 9&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;kc&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;age&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;age&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;10&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;11&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;12&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;howOldAreYou&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;String&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;13&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;formattedAge&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;14&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;15&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;16&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// private func, simply to show extension visibility traits in the analysis below...&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;17&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;private&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;formattedAge&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;String&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;18&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;switch&lt;/span&gt; &lt;span class=&#34;kc&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;gender&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;19&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;case&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;Male&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;20&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;            &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;I&amp;#39;m &lt;/span&gt;&lt;span class=&#34;si&#34;&gt;\(&lt;/span&gt;&lt;span class=&#34;kc&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;age&lt;/span&gt;&lt;span class=&#34;si&#34;&gt;)&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;.&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;21&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;case&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;Female&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;22&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;            &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;Not telling.&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;23&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;24&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;25&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;26&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;enum&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;Gender&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;27&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;case&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Male&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;28&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;case&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Female&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;29&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;30&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Now, suppose that I wanted to extend &lt;code&gt;Person&lt;/code&gt; and inspect the three nuances about the extension&amp;rsquo;s capabilities and behaviors that I introduced at the beginning of this article…&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;extension-visibility&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;extensions-and-visibility-into-extended-type&#34;&gt;Extensions and visibility into extended Type&lt;/h3&gt;
&lt;p&gt;When I introduced that first nuance about visibility into the extended Type, I asked the question, &amp;ldquo;Can they see things marked &lt;code&gt;private&lt;/code&gt;?” The answer surprised me at first: &lt;em&gt;Yes&lt;/em&gt;…they &lt;em&gt;can&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;However, here&amp;rsquo;s where the second nuance comes in: It absolutely does matter &lt;em&gt;where&lt;/em&gt; the extension is defined.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;defined-same-file&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;defined-within-same-file&#34;&gt;Defined within same file&lt;/h4&gt;
&lt;p&gt;Extensions defined within the same file as the Type they&amp;rsquo;re extending have access to &lt;code&gt;private&lt;/code&gt; members of that Type.&lt;/p&gt;
&lt;p&gt;For example, defining an extension to &lt;code&gt;Person&lt;/code&gt; &lt;em&gt;within&lt;/em&gt; Person.swift allows the extension to access &lt;code&gt;private&lt;/code&gt; properties and functions! Who knew?!&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-swift&#34; data-lang=&#34;swift&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;extension&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;Person&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;2&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;getAge&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;Int&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;3&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;age&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;// compiles, even though age is --private--&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;4&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;5&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;6&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;getFormattedAge&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;String&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;7&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;formattedAge&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;// compiles, even though formattedAge is --private--&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;8&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;9&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&amp;ldquo;What?? Why?”, I thought to myself…&lt;/p&gt;
&lt;p&gt;My reasoning as to why extensions defined within the same file behave this way is because when it comes down to it, I could have just written the extension&amp;rsquo;s implementation as part of the Type itself and it would have had the same effect.&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;m &lt;em&gt;in the source file&lt;/em&gt; of the Type I&amp;rsquo;m &amp;ldquo;extending”. So whether I write the additional functionality as an extension for the Type, or just define what would have been in the extension &lt;em&gt;inside the Type, itself&lt;/em&gt;, the net effect is the same.&lt;/p&gt;
&lt;p&gt;Therefore, the compiler essentially says, &amp;ldquo;I see this extension being defined, but there&amp;rsquo;s really no point. It&amp;rsquo;s in the same file that the Type is defined in… so the developer &lt;em&gt;could have&lt;/em&gt; just written all this code within the Type itself… so I&amp;rsquo;ll let him/her refer to &lt;code&gt;private&lt;/code&gt; code blocks.”&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Update:&lt;/strong&gt; My reasoning above reveals that I truly didn&amp;rsquo;t have an understanding of Swift access control. I recommend giving my followup article titled &lt;a href=&#34;http://www.andrewcbancroft.com/2015/04/24/clarifying-swift-access-control-hint-swift-isnt-c-sharp/&#34; title=&#34;Clarifying Swift Access Control (Hint:  Swift Isn’t C#)&#34;&gt;&amp;ldquo;Clarifying Swift Access Control”&lt;/a&gt; a read for more details!&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;defined-separate-file&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;defined-in-a-separate-file&#34;&gt;Defined in a separate file&lt;/h4&gt;
&lt;p&gt;Moving the extension definition into a separate file, however, causes the extension to lose that visibility into the Type it&amp;rsquo;s extending.&lt;/p&gt;
&lt;p&gt;Following the inverse of my previous reasoning about &lt;code&gt;private&lt;/code&gt; visibility when the extension is defined within the same file, this behavior actually makes sense to me.&lt;/p&gt;
&lt;p&gt;Most of the time, you&amp;rsquo;d be writing an extension for Types that you &lt;em&gt;don&amp;rsquo;t&lt;/em&gt; have the source to. In that scenario, extensions would have the same visibility that any client of the Type&amp;rsquo;s exposed API would have, namely, the things marked &lt;code&gt;public&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/04/PersonExtensions_swift.png&#34;&gt;&lt;img src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/04/PersonExtensions_swift.png&#34; alt=&#34;PersonExtensions.swift&#34; width=&#34;506&#34; height=&#34;159&#34; class=&#34;alignnone size-full wp-image-11731&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/04/PersonExtensions_swift.png 506w, https://www.andrewcbancroft.com/wp-content/uploads/2015/04/PersonExtensions_swift-300x94.png 300w&#34; sizes=&#34;(max-width: 506px) 100vw, 506px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;default-acces-control&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;default-extension-access-control&#34;&gt;Default extension access control&lt;/h3&gt;
&lt;p&gt;The final nuance also yielded some semi-surprising results for me. &lt;a href=&#34;https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/AccessControl.html#//apple_ref/doc/uid/TP40014097-CH41-ID25&#34;&gt;Apple&amp;rsquo;s documentation&lt;/a&gt; says it, but until I experimented and saw it in action, I didn&amp;rsquo;t catch the nuance around the default access control modifiers applied to extensions.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;no-explicit-access-control&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;default-access-when-no-explicit-access-modifiers-specified&#34;&gt;Default access when no explicit access modifiers specified&lt;/h4&gt;
&lt;p&gt;In short, when you declare an extension but specify no explicit access modifiers (ie, you just use the default), the extension&amp;rsquo;s default access level depends on the access level of the Type it&amp;rsquo;s extending.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If the Type is &lt;code&gt;public&lt;/code&gt; or &lt;code&gt;internal&lt;/code&gt;, the extension&amp;rsquo;s implementation &amp;ldquo;members” will be &lt;code&gt;internal&lt;/code&gt; by default. The &amp;ldquo;surprise” for me I think is that extensions for &lt;code&gt;public&lt;/code&gt; Types have &lt;code&gt;internal&lt;/code&gt; members by default, unless you specify otherwise.&lt;/li&gt;
&lt;li&gt;If the Type is &lt;code&gt;private&lt;/code&gt;, the extension&amp;rsquo;s implementation &amp;ldquo;members” will be &lt;code&gt;private&lt;/code&gt; by default.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Here&amp;rsquo;s what the extension looks like if we analyze it from the perspective of using no explicitly declared access modifiers (note that to gain access to private properties and functions, I&amp;rsquo;m declaring the extension within Person.swift):&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-swift&#34; data-lang=&#34;swift&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 1&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;struct&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;Person&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 2&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// ...&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 3&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 4&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// ...&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 5&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 6&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 7&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;extension&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;Person&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 8&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;getAge&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;Int&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 9&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;age&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;10&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;11&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;12&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;getFormattedAge&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;String&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;13&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;formattedAge&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;14&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;15&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Using the default access modifiers as shown in the code snippet above exposes access to the extension&amp;rsquo;s new API to instances within the same module. However, it does &lt;em&gt;not&lt;/em&gt; expose additional public API for the Type it&amp;rsquo;s extending to a client of that Type that&amp;rsquo;s in another &lt;em&gt;module&lt;/em&gt; (for example, the unit test target, which is another Swift module).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Same Module&lt;/strong&gt;&lt;br&gt;
&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/04/SameModule.png&#34;&gt;&lt;img src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/04/SameModule.png&#34; alt=&#34;Extended API within the same module&#34; width=&#34;505&#34; height=&#34;264&#34; class=&#34;alignnone size-full wp-image-11733&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/04/SameModule.png 505w, https://www.andrewcbancroft.com/wp-content/uploads/2015/04/SameModule-300x157.png 300w&#34; sizes=&#34;(max-width: 505px) 100vw, 505px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Different Module (test target)&lt;/strong&gt;&lt;br&gt;
&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/04/DifferentModule.png&#34;&gt;&lt;img src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/04/DifferentModule.png&#34; alt=&#34;Extended API in a different module&#34; width=&#34;507&#34; height=&#34;171&#34; class=&#34;alignnone size-full wp-image-11734&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/04/DifferentModule.png 507w, https://www.andrewcbancroft.com/wp-content/uploads/2015/04/DifferentModule-300x101.png 300w&#34; sizes=&#34;(max-width: 507px) 100vw, 507px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;For some reason, I had it in my head that if a Type that I&amp;rsquo;m extending is &lt;code&gt;public&lt;/code&gt;, the extension&amp;rsquo;s members would default to &lt;code&gt;public&lt;/code&gt;. I don&amp;rsquo;t know &lt;em&gt;why&lt;/em&gt; I thought that, but thankfully my experimentation cleared up!&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;default-declaration-public-implementation&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;default-access-when-using-default-extension-declaration-but-specify-public-for-implementation&#34;&gt;Default access when using default extension declaration, but specify public for implementation&lt;/h4&gt;
&lt;p&gt;Adding &lt;code&gt;public&lt;/code&gt; access control modifiers to the extension implementation&amp;rsquo;s members makes those members visible everywhere (that is, both within the same module, and within the test target).&lt;/p&gt;
&lt;p&gt;The location of the extension&amp;rsquo;s declaration, be it within the same source file as the Type it&amp;rsquo;s extending, or in a separate source file, does not matter in terms of what the extension exposes when adding &lt;code&gt;public&lt;/code&gt; members… But only extensions declared within the same source file as the Type it&amp;rsquo;s extending can see &lt;code&gt;private&lt;/code&gt; members of that Type, as we discovered previously.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Extensions declared in same &lt;em&gt;and&lt;/em&gt; separate source files&lt;/strong&gt;&lt;br&gt;
&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/04/public_extension_members.png&#34;&gt;&lt;img src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/04/public_extension_members-1024x645.png&#34; alt=&#34;Public extension members defined within the same source file, and in separate source files...&#34; width=&#34;1024&#34; height=&#34;645&#34; class=&#34;alignnone size-large wp-image-11752&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/04/public_extension_members-1024x645.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2015/04/public_extension_members-300x189.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2015/04/public_extension_members.png 1061w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Public extension members visible in different module (test target)&lt;/strong&gt;&lt;br&gt;
&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/04/public_member_visibility.png&#34;&gt;&lt;img src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/04/public_member_visibility.png&#34; alt=&#34;Regardless of where the extension is declared, all public members are visible to  other modules (such as the test target).&#34; width=&#34;533&#34; height=&#34;353&#34; class=&#34;alignnone size-full wp-image-11753&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/04/public_member_visibility.png 533w, https://www.andrewcbancroft.com/wp-content/uploads/2015/04/public_member_visibility-300x199.png 300w&#34; sizes=&#34;(max-width: 533px) 100vw, 533px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;But notice that on the line where I&amp;rsquo;ve written &lt;code&gt;extension Person { ... }&lt;/code&gt; that I haven&amp;rsquo;t specified an access control modifier for the extension, itself. I&amp;rsquo;ve only added &lt;code&gt;public&lt;/code&gt; to its &lt;em&gt;members&lt;/em&gt;. Even still, the new functions are visible to the test target which is a different module.&lt;/p&gt;
&lt;p&gt;In other words, there&amp;rsquo;s no need to write &lt;code&gt;public extension Person { ... }&lt;/code&gt;. Since &lt;code&gt;Person&lt;/code&gt; is &lt;code&gt;public&lt;/code&gt;, the extension just uses the Type&amp;rsquo;s access level for its own declaration.&lt;/p&gt;
&lt;h3 id=&#34;wrapping-up&#34;&gt;Wrapping up&lt;/h3&gt;
&lt;p&gt;The three nuances about Swift extensions that were analyzed here were &amp;ldquo;surprising” enough to me to warrant some experimentation. My hope is that the analysis that was done will help clear up these subtleties for others who are struggling with understanding how Swift extensions behave!&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;related&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;div class=&#34;resources&#34;&gt;
  &lt;div class=&#34;resources-header&#34;&gt;
    You might also enjoy&amp;#8230;
  &lt;/div&gt;
  &lt;ul class=&#34;resources-content&#34;&gt;
    &lt;li&gt;
      &lt;i class=&#34;fa fa-angle-right&#34;&gt;&lt;/i&gt; &lt;a href=&#34;http://www.andrewcbancroft.com/2014/11/03/the-5-ws-of-swift-extensions/&#34; title=&#34;The 5 W’s of Swift Extensions&#34;&gt;The 5 W’s of Swift Extensions&lt;/a&gt;
    &lt;/li&gt;
    &lt;li&gt;
      &lt;i class=&#34;fa fa-angle-right&#34;&gt;&lt;/i&gt; &lt;a href=&#34;http://www.andrewcbancroft.com/2014/12/02/send-a-type-to-obedience-school-using-swift-extensions-for-additional-protocol-conformance/&#34; title=&#34;Send a Type to Obedience School – Using Swift Extensions for Additional Protocol Conformance&#34;&gt;Send a Type to Obedience School – Using Swift Extensions for Additional Protocol Conformance&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;
&lt;p&gt;&lt;a name=&#34;share&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Testability Tip for Swift Developers – Public Over Private</title>
      <link>https://www.andrewcbancroft.com/2015/04/15/testability-tip-for-swift-developers-public-over-private/</link>
      <pubDate>Wed, 15 Apr 2015 18:52:11 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2015/04/15/testability-tip-for-swift-developers-public-over-private/</guid>
      <description>&lt;p&gt;Quite often in unit testing, especially when practicing Test-Driven Development, I find myself wanting to test every single line of code. When I run up against a &lt;code&gt;private&lt;/code&gt; function, however, I often scratch my head and ask, &amp;ldquo;How am I supposed to test this??”.&lt;/p&gt;
&lt;p&gt;Most experienced testers will tell you, &amp;ldquo;Don&amp;rsquo;t test private implementation – only public API”.&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Okay… But how, does that private function get tested?”, I always asked.&lt;/p&gt;
&lt;p&gt;In this article, I intend to share a testability tip catered to the Swift developer community that helps alleviate this issue with testing private functions.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;testing-and-observability&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;testing-and-observability&#34;&gt;Testing and observability&lt;/h3&gt;
&lt;p&gt;&lt;a name=&#34;developers-as-clients-of-apis&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;developers-as-clients-of-apis&#34;&gt;Developers as clients of APIs&lt;/h4&gt;
&lt;p&gt;As developers, we are &amp;ldquo;clients” of APIs on a daily basis. We interact with other developers&amp;rsquo; frameworks and libraries through the visible, observable, Application Programming Interface that they&amp;rsquo;ve exposed to us. It&amp;rsquo;s the way they&amp;rsquo;ve designed for us to interact with their code.&lt;/p&gt;
&lt;p&gt;Notice three words that I chose in that introductory paragraph:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Visible&lt;/li&gt;
&lt;li&gt;Observable&lt;/li&gt;
&lt;li&gt;Exposed&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If we are going to use another developer&amp;rsquo;s library, &lt;em&gt;all we have&lt;/em&gt; as developers is the public interface that they&amp;rsquo;ve made visible to us… It&amp;rsquo;s the only observable thing we can look at and go, &amp;ldquo;Oh! Okay, to do this, I call &lt;em&gt;that&lt;/em&gt; function”. The only thing exposed are the functions and properties that the developer intends for us to see.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;tests-as-clients-of-apis&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;tests-as-clients-of-apis&#34;&gt;Tests as clients of APIs&lt;/h4&gt;
&lt;p&gt;Have you ever viewed your unit test suite as a &amp;ldquo;client” of your code? It is!&lt;/p&gt;
&lt;p&gt;And just like a developer, the unit tests in your test target interacts with &lt;em&gt;your&lt;/em&gt; app&amp;rsquo;s API through the same visible, observable, interface that you&amp;rsquo;ve exposed to them.&lt;/p&gt;
&lt;p&gt;If you start to personify your test target and think of it in terms of &amp;ldquo;just another client of your code”, you begin to see that it really doesn&amp;rsquo;t have any more visibility of your code than another developer would if he or she were consuming it.&lt;/p&gt;
&lt;p&gt;All of this boils down to say, if it&amp;rsquo;s observable, it&amp;rsquo;s testable. Which means, the easiest and most natural code to test is &lt;code&gt;public&lt;/code&gt; code.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;public-over-private&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;public-over-private-and-internal&#34;&gt;Public over private (and internal)&lt;/h3&gt;
&lt;p&gt;So should &lt;em&gt;everything&lt;/em&gt; be &lt;code&gt;public&lt;/code&gt; instead of &lt;code&gt;private&lt;/code&gt; or &lt;code&gt;internal&lt;/code&gt;? Certainly not.&lt;/p&gt;
&lt;p&gt;Object-oriented programming strives for data-hiding and encapsulation, so there &lt;em&gt;are&lt;/em&gt; reasons to keep some things non-public.&lt;/p&gt;
&lt;p&gt;Additionally, there are &lt;a href=&#34;https://developer.apple.com/swift/blog/?id=27&#34;&gt;certain Swift compiler optimizations&lt;/a&gt; (which lead to run-time optimizations) that can be gained when you mark things in your Type as &lt;code&gt;final&lt;/code&gt;. Using the &lt;code&gt;private&lt;/code&gt; access modifier allows the compiler to &lt;em&gt;infer&lt;/em&gt; that it is &lt;code&gt;final&lt;/code&gt; because it&amp;rsquo;s narrowly scoped to the current Type &lt;em&gt;only&lt;/em&gt;. These are handy things to know, but as is always the case with optimization, avoid &lt;em&gt;premature&lt;/em&gt; optimization by &lt;em&gt;measuring first&lt;/em&gt; to decide if you really need them.&lt;/p&gt;
&lt;p&gt;Whenever possible, I prefer &lt;code&gt;public&lt;/code&gt; over the other access modifiers to help enable testing for my apps.&lt;/p&gt;
&lt;p&gt;So when is &amp;ldquo;whenever possible”?&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;reason-for-existence&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;public-when-part-of-a-types-reason-for-existence&#34;&gt;Public when part of a Type&amp;rsquo;s reason for existence&lt;/h4&gt;
&lt;p&gt;Obviously, properties and functions that are part of a Type&amp;rsquo;s core purpose can be marked &lt;code&gt;public&lt;/code&gt;. My practice is to decide, &amp;ldquo;Is this function why this Type exists?”. If so, I mark it as &lt;code&gt;public&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Note also that the Type itself needs to be marked public if it&amp;rsquo;s going to be visible to your unit tests.&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;// Instead of this (default --internal-- access)...&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;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; 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;myFunc&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;c1&#34;&gt;// Performs something essential to why MyClass exists&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;// Make things public...&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;public&lt;/span&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;10&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;myFunc&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;c1&#34;&gt;// Performs something essential to why MyClass exists&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;&lt;a name=&#34;new-type&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;transition-many-private-components-to-new-type&#34;&gt;Transition many private components to new Type&lt;/h4&gt;
&lt;p&gt;Having &lt;em&gt;many&lt;/em&gt; &lt;code&gt;private&lt;/code&gt; properties and functions can be an indication that there needs to be a new Type created to encapsulate those components. I&amp;rsquo;ve heard developers talk about this situation as one that &amp;ldquo;screams, ‘New Type!&amp;rsquo;”.&lt;/p&gt;
&lt;p&gt;If we extract out sets of related &lt;code&gt;private&lt;/code&gt; properties and functions into a new Type, those pieces of code &lt;em&gt;are the reason that Type exists&lt;/em&gt;, and thus should be marked &lt;code&gt;public&lt;/code&gt;. Testing, then, becomes a matter of writing unit tests for the new Type and its public API!&lt;/p&gt;
&lt;p&gt;Even if you just have a few &lt;code&gt;private&lt;/code&gt; code blocks in the Type you&amp;rsquo;re trying to test, it can sometimes make your testing life easier to transition them to a new Type as &lt;code&gt;public&lt;/code&gt; components.&lt;/p&gt;
&lt;p&gt;If I&amp;rsquo;m really uncomfortable marking functions &lt;code&gt;public&lt;/code&gt; in the Type where they currently exist, creating a new Type and marking them &lt;code&gt;public&lt;/code&gt; there is usually the better alternative, and it immediately enables testing for that set of code.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;testing-non-public&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;testing-non-public-code&#34;&gt;Testing non-public code&lt;/h3&gt;
&lt;p&gt;As I mentioned in the beginning, it&amp;rsquo;s not good to just haphazardly go through your code and &amp;ldquo;&lt;code&gt;public&lt;/code&gt; all the things!!”. After &lt;em&gt;appropriately&lt;/em&gt; marking fundamental functions &lt;code&gt;public&lt;/code&gt; and transitioning sets of &lt;code&gt;private&lt;/code&gt; functions to new Types where they can happily live as part of &lt;em&gt;that&lt;/em&gt; Type&amp;rsquo;s public API, there will likely be a few &lt;code&gt;private&lt;/code&gt; or &lt;code&gt;internal&lt;/code&gt; things left over.&lt;/p&gt;
&lt;p&gt;How do these get tested?&lt;/p&gt;
&lt;p&gt;Well, ideally, these are small, simple, helper functions that are only useful when called within the Type you&amp;rsquo;re working on.&lt;/p&gt;
&lt;p&gt;If these functions produce observable results in the places where they&amp;rsquo;re called, you end up testing these non-public components &lt;em&gt;implicitly&lt;/em&gt;, that is, by testing the things that &lt;em&gt;are&lt;/em&gt; &lt;code&gt;public&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;During the course of testing some &lt;code&gt;public&lt;/code&gt; function which calls another non-public function, that non-public function&amp;rsquo;s logic will be executed. If the outcome of that function&amp;rsquo;s execution affects the Type&amp;rsquo;s state, or the output of its &lt;code&gt;public&lt;/code&gt; parent function, those are the things that you&amp;rsquo;d be able to write unit tests for, because those are the &amp;ldquo;observation points”, so to speak.&lt;/p&gt;
&lt;h3 id=&#34;wrapping-up&#34;&gt;Wrapping up&lt;/h3&gt;
&lt;p&gt;The bottom line is: observable == testable. Just like another developer, the suite of unit tests in your test target interacts with your app&amp;rsquo;s API through the visible, observable, interface that you&amp;rsquo;ve exposed to them. The more observable your API components are, the more testable your code becomes. Which is why I prefer &lt;code&gt;public&lt;/code&gt; over &lt;code&gt;private&lt;/code&gt; whenever possible!&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/tag/unit-testing/&#34; title=&#34;Browse All Unit Testing Articles @ andrewcbancroft.com&#34;&gt;Browse All Unit Testing Articles @ andrewcbancroft.com&lt;/a&gt;
    &lt;/li&gt;
    &lt;li&gt;
      &lt;i class=&#34;fa fa-angle-right&#34;&gt;&lt;/i&gt; &lt;a href=&#34;http://www.andrewcbancroft.com/2014/12/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;i class=&#34;fa fa-angle-right&#34;&gt;&lt;/i&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;
&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 Delegation Works – A Swift Developer’s Guide</title>
      <link>https://www.andrewcbancroft.com/2015/04/08/how-delegation-works-a-swift-developer-guide/</link>
      <pubDate>Thu, 09 Apr 2015 01:43:55 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2015/04/08/how-delegation-works-a-swift-developer-guide/</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;Delegation can be a difficult topic to wrap your head around. I found it easiest to break up posts on the topic to help readers who are new to the pattern grasp the concepts a little better. First, I analyzed what delegation &lt;em&gt;is&lt;/em&gt; in &lt;a href=&#34;http://www.andrewcbancroft.com/2015/03/26/what-is-delegation-a-swift-developers-guide/&#34;&gt;&amp;ldquo;What is Delegation – A Swift Developer&amp;rsquo;s Guide”&lt;/a&gt;. If you&amp;rsquo;re looking for the &amp;ldquo;what is it?” &lt;em&gt;behind&lt;/em&gt; the &amp;ldquo;how does it work?”, I recommend giving that first article a read.&lt;/p&gt;
&lt;p&gt;Once the terminology is unpacked and a high-level overview of delegation as a design pattern is understood, the next logical place to turn is to the question, &amp;ldquo;How does delegation work?”. That is the focus of this article.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;intro&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;introducing-the-players&#34;&gt;Introducing the players&lt;/h3&gt;
&lt;p&gt;For delegation to occur in software, you’d have a situation where one class (a delegat_or_ class) would give control or responsibility for some behavioral logic to &lt;em&gt;another&lt;/em&gt; class called a delegate.&lt;/p&gt;
&lt;p&gt;So how does one class delegate behavioral logic to another class? With iOS and Swift, the delegation design pattern is achieved by utilizing an abstraction layer called a &lt;a href=&#34;https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Protocols.html&#34;&gt;protocol&lt;/a&gt;.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;A protocol defines a blueprint of methods, properties, and other requirements that suit a particular task or piece of functionality.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;a name=&#34;abstractions&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;protocols-as-abstractions&#34;&gt;Protocols as abstractions&lt;/h4&gt;
&lt;p&gt;I used the fancy term &amp;ldquo;abstraction layer” prior to the quote. What is that all about?&lt;/p&gt;
&lt;p&gt;Protocols are an &amp;ldquo;abstraction”, because they do not provide implementation details in their declaration… Only function and property &lt;em&gt;names&lt;/em&gt;. Like an outline, or as Apple puts it, a blueprint.&lt;/p&gt;
&lt;h5 id=&#34;protocols-as-blueprints&#34;&gt;Protocols as blueprints&lt;/h5&gt;
&lt;p&gt;With a single blueprint, there can be many homes constructed. The fine details of their construction may differ, but in the end, houses of some similarity that satisfy the blueprint&amp;rsquo;s specifications are built.&lt;/p&gt;
&lt;p&gt;So, too with a protocol: Many classes can be built that follow the protocol&amp;rsquo;s specifications. At the end of the day, the fine details of each class&amp;rsquo; implementation (the stuff between the curly braces &lt;code&gt;{ ... }&lt;/code&gt;) may differ, but if they adopt the protocol, they&amp;rsquo;ll be similar in at least the fact that they provide the named behavior it specified.&lt;/p&gt;
&lt;h5 id=&#34;protocols-as-contracts&#34;&gt;Protocols as contracts&lt;/h5&gt;
&lt;p&gt;Another analogy from the legal world is popular for describing protocols: Protocols are similar to &lt;em&gt;contracts&lt;/em&gt;. It&amp;rsquo;s this contractual idea that actually makes the most sense to me when it comes to delegation.&lt;/p&gt;
&lt;p&gt;A contract is the &amp;ldquo;thing” in the middle of two parties who are trying to negotiate a deal. To one party, the contract is a &lt;em&gt;guarantee&lt;/em&gt; of some terms that will be satisfied. To the &lt;em&gt;other&lt;/em&gt; party, the contract is a set of &lt;em&gt;obligations&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;In the delegation design pattern, protocols serve the same kind middle-man role as a contract. To the delegat_or_ class, the protocol is a guarantee that some behavior will be supplied by the other party (the delegate). To the delegate class, the protocol is a set of obligations – things it must implement when it &amp;ldquo;signs the contract”, or in Swift terms, &amp;ldquo;adopts the protocol”.&lt;/p&gt;
&lt;p&gt;While the person signing the contract probably gets something out of the deal, the focus in the analogy we&amp;rsquo;re making to protocols and the delegation pattern is on the person on the guarantee end.&lt;/p&gt;
&lt;p&gt;That person, being guaranteed by the contract that certain terms will be executed by the person who signs the deal, is free to make decisions and take action based on that promise.&lt;/p&gt;
&lt;p&gt;So, too with the class delegating to another class. The delegat_or_ class can make perform actions (call methods defined by the protocol) or make decisions (access properties defined by the protocol to use in its logic).&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;listing-the-players&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;listing-the-players&#34;&gt;Listing the players&lt;/h4&gt;
&lt;p&gt;Stepping back from this description, we see three players involved:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A protocol defining the responsibilities that will be delegated&lt;/li&gt;
&lt;li&gt;A delegat_or_, which depends on an instance of something conforming to that protocol&lt;/li&gt;
&lt;li&gt;A delegate, which adopts the protocol and implements its requirements&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;a name=&#34;visualize-players&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;visualize-the-players&#34;&gt;Visualize the players&lt;/h3&gt;
&lt;p&gt;As you can see, there are a few moving parts to delegation. Sometimes it helps to visualize the players involved in the strategy. I created the following diagram for an analysis I wrote on &lt;a href=&#34;http://www.andrewcbancroft.com/2015/02/05/nsnotificationcenter-vs-delegation-analysis/&#34;&gt;NSNotificationCenter vs Delegation&lt;/a&gt;, but I think it gets the point across for this blog entry as well:&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;p&gt;&lt;a name=&#34;code&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;an-example-in-code&#34;&gt;An example in code&lt;/h3&gt;
&lt;p&gt;Hopefully the explanation so far has provided some good groundwork to sort out how to accomplish the delegation design pattern in code. So how &lt;em&gt;does&lt;/em&gt; it all get wired up?&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;set-up-delegator&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;setting-up-the-delegator&#34;&gt;Setting up the delegator&lt;/h4&gt;
&lt;p&gt;A delegat_or_ class typically defines a variable property with the word &amp;ldquo;delegate” somewhere in the name (oftentimes the property is simply named &lt;code&gt;delegate&lt;/code&gt; if that&amp;rsquo;s explanatory enough). The Type of the variable property is the key to it all. The variable will be of Type &lt;code&gt;whatever-you-named-your-delegate-protocol&lt;/code&gt;. So if I named my protocol &lt;code&gt;MySpecialDelegate&lt;/code&gt;, I&amp;rsquo;d specify the Type of the delegate property to be &lt;code&gt;MySpecialDelegate&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;set-up-delegate&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;setting-up-the-delegate&#34;&gt;Setting up the delegate&lt;/h4&gt;
&lt;p&gt;The delegate class is what &lt;em&gt;adopts&lt;/em&gt; the protocol and implements its requirements. In the class declaration, the name of the protocol(s) that the class intends to adopt are listed separated by commas after the name of the superclass (if the class inherits from a superclass):&lt;/p&gt;
&lt;p&gt;&lt;code&gt;class MyClass: SuperClass, Protocol1, Protocol2 { ... }&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;When the delegat_or_ class gets initialized, a second step is often to immediately assign an instance of the class that&amp;rsquo;s adopted the delegate protocol to its &lt;code&gt;delegate&lt;/code&gt; property so that everything is &amp;ldquo;wired up”.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;in-practice&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;delegation-in-practice&#34;&gt;Delegation in practice&lt;/h4&gt;
&lt;p&gt;Working with the delegation pattern in practice usually involves interacting with the protocol from the &lt;em&gt;delegate&lt;/em&gt; end of things. Most of the time, we&amp;rsquo;re working with Apple&amp;rsquo;s APIs (such as a &lt;code&gt;UITableView&lt;/code&gt; or just about any other UI control they provide). We typically only require knowledge of the protocol&amp;rsquo;s definition so that the class we choose as our delegate can implement the right functions.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;complete-example&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;complete-example&#34;&gt;Complete example&lt;/h4&gt;
&lt;p&gt;There may be some situations where you may decide to follow Apple&amp;rsquo;s lead and use the delegation design pattern for your own code. Maybe you&amp;rsquo;re making a special &lt;code&gt;UIView&lt;/code&gt; subclass or a special picker control (much like &lt;code&gt;UIImagePickerController&lt;/code&gt;). Or maybe you&amp;rsquo;re into game development and would like to communicate from your &lt;code&gt;SKScene&lt;/code&gt; back to the View Controller. These are just a few that came to mind, but they all present possibilities for utilizing the delegation strategy.&lt;/p&gt;
&lt;p&gt;To give a simple example, suppose that we&amp;rsquo;ve decided to create a class to encapsulate all of the logic for a special rating picker control. We&amp;rsquo;d like to offer the ability to customize the picker some by allowing the user of our control to specify a preferred rating symbol. We&amp;rsquo;d also like to provide a feedback loop to notify the user of our control when certain events have occurred. Delegation is a great tool to provide both customization options and communication between classes. What would this example look like in code?&lt;/p&gt;
&lt;h5 id=&#34;create-the-protocol&#34;&gt;Create the protocol&lt;/h5&gt;
&lt;p&gt;First, the protocol:&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;protocol&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;RatingPickerDelegate&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;preferredRatingSymbol&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;picker&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;RatingPicker&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;UIImage&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;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;didSelectRating&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;picker&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;RatingPicker&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;rating&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&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;didCancel&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;picker&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;RatingPicker&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;Notice how this protocol definition allows both the customization point and the feedback loop we were hoping for. It&amp;rsquo;s always nice for the delegate to have access to the public API of the instance calling its methods, so the &lt;code&gt;RatingPicker&lt;/code&gt; (or &lt;code&gt;UITableView&lt;/code&gt; or &lt;code&gt;UIScrollView&lt;/code&gt; or whatever) is often passed along as an argument.&lt;/p&gt;
&lt;h5 id=&#34;create-the-delegator&#34;&gt;Create the delegator&lt;/h5&gt;
&lt;p&gt;With the protocol defined, our &lt;code&gt;RatingPicker&lt;/code&gt; (the delegat_or_ in this case) can now set itself up to use that protocol:&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;// Disclaimer:  There is much more logic that would go into a real UIView subclass or a picker control 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; 2&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;// This example is contrived and is only meant to serve as a &amp;#34;shell&amp;#34; of what code could look like&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;// that uses a delegate within its 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&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;RatingPicker&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;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;delegate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;RatingPickerDelegate&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;kd&#34;&gt;init&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;withDelegate&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;RatingPickerDelegate&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 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;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;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;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;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;preferredRatingSymbol&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;preferredRatingSymbol&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;picker&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;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;// Set up the picker with the preferred rating symbol if it was specified&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 class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;selectRating&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;selectedRating&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;Int&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;19&lt;/span&gt;&lt;span class=&#34;cl&#34;&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;didSelectRating&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;picker&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;rating&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;selectedRating&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;c1&#34;&gt;// Other logic related to selecting a rating&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&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;cancel&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;delegate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;?.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;didCancel&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;picker&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;25&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;c1&#34;&gt;// Other logic related to canceling&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;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;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 &lt;code&gt;delegate&lt;/code&gt; property is strongly typed to be a &lt;code&gt;RatingPickerDelegate&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Since it&amp;rsquo;s &lt;em&gt;optional&lt;/em&gt; here in this implementation, the &lt;code&gt;delegate&lt;/code&gt; is not absolutely essential for the &lt;code&gt;RatingPicker&lt;/code&gt; to work. If it were essential, we&amp;rsquo;d parameterize &lt;code&gt;init&lt;/code&gt; and assign it during initialization.&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;ve used optional chaining to get at the &lt;code&gt;delegate&#39;s&lt;/code&gt; methods if the &lt;code&gt;delegate&lt;/code&gt; isn&amp;rsquo;t nil.&lt;/p&gt;
&lt;h5 id=&#34;choosing-the-delegate&#34;&gt;Choosing the delegate&lt;/h5&gt;
&lt;p&gt;Choosing the delegate class is the final decision to make. It&amp;rsquo;s not uncommon for a View Controller to take up the responsibility of being a delegate. In &lt;a href=&#34;http://www.andrewcbancroft.com/2014/08/26/pick-a-delegate-clean-view-controllers-in-swift/&#34;&gt;&amp;ldquo;Pick a Delegate, Any Delegate”&lt;/a&gt;, I attempted to show how it&amp;rsquo;s &lt;em&gt;not&lt;/em&gt; necessary to use the View Controller as your one stop delegate shop. For this example, I&amp;rsquo;ll avoid giving the View Controller more responsibility than it needs and I&amp;rsquo;ll create a simple handler class to assume the delegated responsibilities:&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;RatingPickerHandler&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;RatingPickerDelegate&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;preferredRatingSymbol&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;picker&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;RatingPicker&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;UIImage&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;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIImage&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;contentsOfFile&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;Star.png&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&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;didSelectRating&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;picker&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;RatingPicker&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;rating&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;Int&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 7&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;c1&#34;&gt;// do something in response to a rating being selected&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;didCancel&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;picker&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;RatingPicker&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;c1&#34;&gt;// do something in response to the rating picker canceling&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;h3 id=&#34;wrapping-up&#34;&gt;Wrapping up&lt;/h3&gt;
&lt;p&gt;Once the terminology of delegation is unpacked, understanding how it works is the next logical step for grasping the design pattern as a whole. Here we explored all the players involved in the strategy and related protocols, which are integral to the strategy, to some real-world analogies. Finally, we took a look at how delegation works in practice, and demonstrated each role in delegation with code.&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/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;li&gt;
      &lt;i class=&#34;fa fa-angle-right&#34;&gt;&lt;/i&gt; &lt;a href=&#34;http://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>Enums Instead of Booleans – An Example Implementation</title>
      <link>https://www.andrewcbancroft.com/2015/04/01/enums-instead-of-booleans-an-example-implementation/</link>
      <pubDate>Wed, 01 Apr 2015 12:00:52 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2015/04/01/enums-instead-of-booleans-an-example-implementation/</guid>
      <description>&lt;p&gt;This blog entry was inspired by &lt;a href=&#34;http://www.objc.io/snippets/12.html&#34;&gt;Objc.io&amp;rsquo;s snippet titled &amp;ldquo;Enums instead of Booleans”&lt;/a&gt;. I really loved the clarity that using enumerations brought to even the simplest of examples that was presented in that snippet. If you haven&amp;rsquo;t seen it, check it out before continuing on!&lt;/p&gt;
&lt;p&gt;Objc.io&amp;rsquo;s snippet focused primarily on the definition and &lt;em&gt;consumption&lt;/em&gt; of the enumeration values. What I&amp;rsquo;d like to focus on here is the implementation of the thing that &lt;em&gt;produces&lt;/em&gt; those values for the switch-case that will consume them later on.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;example-scenario&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;example-scenario&#34;&gt;Example scenario&lt;/h3&gt;
&lt;p&gt;To put an example before us, consider the following scenario:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Suppose that we&amp;rsquo;re building an app that utilizes a subscription-style service to deliver premium content.&lt;/li&gt;
&lt;li&gt;Special things, such as providing access to the premium content, need to happen in the app if the user is a Subscriber in good standing.&lt;/li&gt;
&lt;li&gt;Other things need to happen, such as denial of access to the premium content, if the user &lt;em&gt;had&lt;/em&gt; a subscription, but it&amp;rsquo;s expired.&lt;/li&gt;
&lt;li&gt;Finally, if the user has never had a subscription, we&amp;rsquo;d still like to deny access to the content, but perhaps offer them the opportunity to subscribe.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Using Objc.io&amp;rsquo;s idea as a springboard, we could program this in a couple of ways:&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;booleans&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;1--we-could-define-a-few-boolean-properties-throughout-that-would-indicate-the-users-subscription-status&#34;&gt;1 – We could define a few boolean properties throughout that would indicate the user&amp;rsquo;s subscription status:&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;isSubscriber&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;isSubscriptionExpired&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;isNonSubscriber&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;What I find interesting about this approach is that it&amp;rsquo;s not clear, from looking at the properties, that they&amp;rsquo;re all mutually exclusive. Can I be a &lt;strong&gt;subscriber&lt;/strong&gt; whose &lt;strong&gt;subscription has expired&lt;/strong&gt;? If my &lt;strong&gt;subscription expired&lt;/strong&gt;, am I a &lt;strong&gt;non-subscriber&lt;/strong&gt;?&lt;/p&gt;
&lt;p&gt;It could be that I&amp;rsquo;ve chosen poor names for the properties, but if I wanted to make clear that the statuses are mutually exclusive, there&amp;rsquo;s a better way to encapsulate them. Objc.io helpfully pointed us in the direction of that better encapsulation method. Yep… you got it: enumerations!&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;enums-instead&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;2--as-the-title-of-the-blog-entry-signals-we-could-replace-the-booleans-with-an-enumeration&#34;&gt;2 – As the title of the blog entry signals, we could replace the booleans with an enumeration:&lt;/h3&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-swift&#34; data-lang=&#34;swift&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;enum&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;SubscriberStatus&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;CurrentSubscriber&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;ExpiredSubscriber&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;NonSubscriber&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;Immediately, this clarifies the mutually exclusive part. Enumerations, by definition, expose mutually exclusive values. By defining the &lt;code&gt;SubscriberStatus&lt;/code&gt; enumeration, I&amp;rsquo;ve made clear that my intent is to have someone be &lt;em&gt;either&lt;/em&gt; a &lt;code&gt;CurrentSubscriber&lt;/code&gt;, an &lt;code&gt;ExpiredSubscriber&lt;/code&gt;, or a &lt;code&gt;NonSubscriber&lt;/code&gt;, but not combinations of each.&lt;/p&gt;
&lt;p&gt;Not only that, but rather than including complicated branching logic when I need to determine someone&amp;rsquo;s subscription status, I can simply switch-case over the enumeration possibilities and perform the appropriate behavior.&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;checkSubscriberStatusForRegistrant&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;registrant&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Registrant&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;// We&amp;#39;re coming to the definition of Registrant...&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;// In fact, Registrant&amp;#39;s implementation is the goal of this blog entry!&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;switch&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;registrant&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;subscriberStatus&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;k&#34;&gt;case&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;CurrentSubscriber&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;showPremiumContent&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;ExpiredSubscriber&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;alertExpiredSubscription&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;case&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;NonSubscriber&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;offerSubscription&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;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;produce-enum-values&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;producing-the-enumeration-values&#34;&gt;Producing the enumeration values&lt;/h3&gt;
&lt;p&gt;The one thing that left me scratching my head on at first was the part of the whole scheme that would &lt;em&gt;produce&lt;/em&gt; the enumeration values. In my example above, I&amp;rsquo;ve left out what the implementation of &lt;code&gt;Registrant&lt;/code&gt; looks like.&lt;/p&gt;
&lt;p&gt;When I&amp;rsquo;m consuming the enumeration values, I&amp;rsquo;m riding on the fact that &lt;code&gt;registrant.subscriberStatus&lt;/code&gt; has some way of producing a correct &lt;code&gt;SubscriberStatus&lt;/code&gt; for the registrant. But what does that look like? Well… here&amp;rsquo;s one possibility:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-swift&#34; data-lang=&#34;swift&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 1&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;struct&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;Registrant&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;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;subscriberStatus&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;SubscriberStatus&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;get&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;if&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;noSubscriptionOnFile&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;n&#34;&gt;NonSubscriber&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;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;subscriptionOverdueForPayment&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 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;ExpiredSubscriber&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;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;CurrentSubscriber&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;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;kd&#34;&gt;private&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;noSubscriptionOnFile&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;17&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;c1&#34;&gt;// Do what needs to be done to check if the registrant has a subscription on file or not&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;private&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;subscriptionOverdueForPayment&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;21&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;c1&#34;&gt;// Do what needs to be done to check if the registrant&amp;#39;s subscription is overdue for payment&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;p&gt;So as you can see, the thing that &lt;em&gt;produces&lt;/em&gt; the enumeration values &lt;em&gt;does&lt;/em&gt; have some &lt;code&gt;if&lt;/code&gt;s and &lt;code&gt;Bool&lt;/code&gt;s in it. I couldn&amp;rsquo;t really think of another way to do this. To simplify the readability of the &lt;code&gt;subscriberStatus&lt;/code&gt; property&amp;rsquo;s implementation, I&amp;rsquo;ve abstracted the more complicated computational logic for figuring out whether or not a subscription is on file or if the registrant is overdue for payment into functions.&lt;/p&gt;
&lt;p&gt;Even though we do still have some &lt;code&gt;Bool&lt;/code&gt;s to deal with, what we&amp;rsquo;ve gained (in my opinion) is clarity on the consumption end. To me, it&amp;rsquo;s worth it to have the &lt;code&gt;Bool&lt;/code&gt; logic buried in one spot so that throughout the rest of my app, I can deal with clear, understandable, enumeration values.&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>Announcing the Birth of……</title>
      <link>https://www.andrewcbancroft.com/2015/03/27/announcing-the-birth-of/</link>
      <pubDate>Fri, 27 Mar 2015 13:00:27 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2015/03/27/announcing-the-birth-of/</guid>
      <description>&lt;p&gt;My daughter, whom we codenamed Firefly, made her debut at 2:59 AM this morning! Being the nerd that I am, I built a small iOS app to reveal her name.&lt;/p&gt;
&lt;p&gt;Introducing….&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>What is Delegation? – A Swift Developer’s Guide</title>
      <link>https://www.andrewcbancroft.com/2015/03/26/what-is-delegation-a-swift-developers-guide/</link>
      <pubDate>Fri, 27 Mar 2015 03:29:01 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2015/03/26/what-is-delegation-a-swift-developers-guide/</guid>
      <description>&lt;p&gt;Of the major design patterns that are prevalent in iOS development, delegation is one that appears quite often. For many developers entering app development for the iOS platform, dealing with delegates is a new concept. It certainly was for me.&lt;/p&gt;
&lt;p&gt;In my analysis of &lt;a href=&#34;http://www.andrewcbancroft.com/2015/02/05/nsnotificationcenter-vs-delegation-analysis/&#34; title=&#34;NSNotificationCenter vs Delegation – An Analysis&#34;&gt;NSNotificationCenter vs Delegation&lt;/a&gt;, I wrote some about delegation, but only in comparison to how &lt;code&gt;NSNotificationCenter&lt;/code&gt; works. I haven&amp;rsquo;t yet given delegation dedicated time and analysis, but I intend to do so now.&lt;/p&gt;
&lt;p&gt;My aim in this blog entry is to try and make some sense out of the question, &amp;ldquo;What is delegation?”. Explore with me…&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;what-is-delegation&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;what-is-the-delegation-design-pattern&#34;&gt;What is the delegation design pattern?&lt;/h3&gt;
&lt;p&gt;To read that &amp;ldquo;delegation is a design pattern that [insert explanation here]” never really clicked with me. Many who are new to programming have not dealt extensively with &amp;ldquo;design patterns”, so it doesn&amp;rsquo;t always suffice to define delegation in those terms.&lt;/p&gt;
&lt;p&gt;I suspect that since Swift has lowered the barrier to entry for iOS development, many who are new to the platform are also new to coding in general, so here&amp;rsquo;s my attempt to explain what clicked for me regarding what delegation &lt;em&gt;is&lt;/em&gt;:&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;design-pattern&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;design-pattern&#34;&gt;Design pattern&lt;/h4&gt;
&lt;p&gt;First, take the phrase &amp;ldquo;design pattern”.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Design&lt;/strong&gt; implies architecture. It connotes a strategy for organizing something. Design conveys the method by which components will work together to accomplish an end.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Pattern&lt;/strong&gt; implies that there is some repeatable process that has honed in around a common thread… a common practice… a predictable method for doing something. &amp;ldquo;Pattern” gives the impression that such a practice has converged over time into something that is now well-known, well-understood, and commonly used. I imagine a sort of &amp;ldquo;survival of the fittest” approach to process and practice. Things that didn&amp;rsquo;t converge or that were weaker in the real world fell away and the strongest survived into this thing we call &amp;ldquo;pattern”.&lt;/p&gt;
&lt;p&gt;A &lt;strong&gt;design pattern&lt;/strong&gt; in software terms, then, is a method for architecting, strategizing about, and organizing an application&amp;rsquo;s code in such a way that has been found to be commonplace, repeatable, and practically sound over time.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;delegation&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;delegation&#34;&gt;Delegation&lt;/h4&gt;
&lt;p&gt;Now take the word delegation. Three things come to my mind:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The verb, &amp;ldquo;to delegate”, meaning &amp;ldquo;to give control”&lt;/li&gt;
&lt;li&gt;The noun, &amp;ldquo;a delegate”, meaning &amp;ldquo;a person acting for another”&lt;/li&gt;
&lt;li&gt;The made-up noun, &amp;ldquo;a delegat_or_&amp;rdquo;, or more properly, a &lt;em&gt;principal&lt;/em&gt;, meaning &amp;ldquo;a person who delegates to another”&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;In the real world, the word delegation encapsulates relationship and responsibility. A delegator/principal (noun) would delegate (verb) control or responsibility to another person called a delegate.&lt;/p&gt;
&lt;p&gt;How could we map this to software? Well, it actually falls in line quite nicely.&lt;/p&gt;
&lt;p&gt;Rather than &lt;em&gt;people&lt;/em&gt;, we&amp;rsquo;re dealing with instances of &lt;em&gt;classes&lt;/em&gt; (or other data structures like structs, but I&amp;rsquo;m just going to keep it simple and say &amp;ldquo;class” to encapsulate the idea). For delegation to occur in software, you&amp;rsquo;d have a situation where one class (a delegator/principal class) would delegate control or responsibility, here meaning behavioral logic, to another class called a delegate.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;how-used&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;how-is-delegation-used&#34;&gt;How is delegation used?&lt;/h3&gt;
&lt;p&gt;So yes… delegation is a design pattern. It&amp;rsquo;s a design pattern that exists on other platforms, but it is one that has been heavily adopted by Apple and is ubiquitous throughout the iOS APIs. It&amp;rsquo;s a design pattern that shifts responsibility from one class to another, thereby creating a separation of responsibilities and concerns. But what &lt;em&gt;kinds&lt;/em&gt; of responsibilities and concerns? How is delegation used in practice?&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;communication&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;communication&#34;&gt;Communication&lt;/h4&gt;
&lt;p&gt;In practice, delegation is most often used as a way for one class to &lt;em&gt;communicate&lt;/em&gt; to another class. Observing some of the actions that delegates perform from Apple&amp;rsquo;s own APIs give us clues about this. Take the following samples from &lt;code&gt;UITableViewDelegate&lt;/code&gt; as an example:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;tableView(_:&lt;strong&gt;_will_SelectRow&lt;/strong&gt;AtIndexPath:)&lt;/li&gt;
&lt;li&gt;tableView(_:&lt;strong&gt;_did_SelectRow&lt;/strong&gt;AtIndexPath:)&lt;/li&gt;
&lt;li&gt;tableView(_:&lt;strong&gt;_did_HighlightRow&lt;/strong&gt;AtIndexPath:)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Or how about a sampling from &lt;code&gt;UIScrollViewDelegate&lt;/code&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;scrollView**&lt;em&gt;Did_Scroll**(&lt;/em&gt;:)&lt;/li&gt;
&lt;li&gt;scrollView**&lt;em&gt;WillBegin_Dragging**(&lt;/em&gt;:)&lt;/li&gt;
&lt;li&gt;scrollView**&lt;em&gt;WillEnd_Dragging**(&lt;/em&gt;:withVelocity:targetContentOffset:)&lt;/li&gt;
&lt;li&gt;scrollView**&lt;em&gt;DidEnd_Dragging**(&lt;/em&gt;:willDecelerate:)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;My observation of Apple&amp;rsquo;s APIs indicates to me that one of the intended uses for delegation is to allow one instance to communicate back to some &lt;em&gt;other&lt;/em&gt; instance that something will/did happen. The table view or scroll view &lt;em&gt;delegates&lt;/em&gt; the opportunity to perform some action in response to some lifecycle event to another class, namely, its delegate.&lt;/p&gt;
&lt;p&gt;It&amp;rsquo;s also worth noting the scope of the communication that delegation is intended to be used for. Whereas &lt;code&gt;NSNotificationCenter&lt;/code&gt; fits the need for one instance to broadcast a message intended for more than one listening instance, delegation fits the scenario where an instance only needs to send a message to a single listener (the delegate).&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;customization&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;customization&#34;&gt;Customization&lt;/h4&gt;
&lt;p&gt;Another common usage for delegation is to provide a delegate instance the opportunity to customize certain aspects of the delegat_ing_ instance&amp;rsquo;s internal state. Once again, clues from a few of Apple&amp;rsquo;s APIs shed some light on this usage scenario. Let&amp;rsquo;s look at &lt;code&gt;UITableViewDelegate&lt;/code&gt; first:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;tableView(_:&lt;strong&gt;height&lt;/strong&gt;ForRowAtIndexPath:)&lt;/li&gt;
&lt;li&gt;tableView(_:&lt;strong&gt;editActions&lt;/strong&gt;ForRowAtIndexPath:)&lt;/li&gt;
&lt;li&gt;tableView(_:&lt;strong&gt;indentationLevel&lt;/strong&gt;ForRowAtIndexPath:)&lt;/li&gt;
&lt;li&gt;tableView(_:&lt;strong&gt;shouldHighlight&lt;/strong&gt;RowAtIndexPath:)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;These are all customization-points that a &lt;code&gt;UITableView&lt;/code&gt; allows its delegate to have a say in. &lt;em&gt;Some&lt;/em&gt; of the methods are so important that the table view can&amp;rsquo;t display itself unless it gets this information from its delegate. The point here is that the table view is shifting responsibility for the implementation of that logic off to its delegate, allowing for greater controlled flexibility and customization.&lt;/p&gt;
&lt;h3 id=&#34;wrapping-up&#34;&gt;Wrapping up&lt;/h3&gt;
&lt;p&gt;With delegation being such a heavily utilized strategy for organizing an iOS application&amp;rsquo;s logic, understanding what it is becomes key concern. In this article we unpacked the terms &amp;ldquo;design pattern” and &amp;ldquo;delegation” to get a grasp of why those words were chosen to describe the strategy. Finally, we looked at how the delegation pattern is used in practice, observing that two common use-cases for delegation: class-to-class communication and customization.&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/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;http://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;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>Basics of Pull to Refresh for Swift Developers</title>
      <link>https://www.andrewcbancroft.com/2015/03/17/basics-of-pull-to-refresh-for-swift-developers/</link>
      <pubDate>Tue, 17 Mar 2015 17:55:52 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2015/03/17/basics-of-pull-to-refresh-for-swift-developers/</guid>
      <description>&lt;p&gt;&lt;small&gt;Updated on September 21, 2016 – Swift 3.0&lt;/small&gt;&lt;/p&gt;
&lt;p&gt;Implementing &amp;ldquo;pull to refresh” is a common need that arises when working with table views. There are typically two scenarios that folks find themselves in when attempting to implement this feature:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;They&amp;rsquo;re working with a UITableViewController&lt;/li&gt;
&lt;li&gt;They&amp;rsquo;re working with a non-UITableViewController, but their view incorporates a regular UITableView, either taking up the whole screen, or a smaller portion of it&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;This entry will explore both scenarios to help you get up and running quickly with implementing pull to refresh for your Swift iOS app.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;example-scenario&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;example-scenario&#34;&gt;Example scenario&lt;/h3&gt;
&lt;p&gt;For this guide, suppose that we have a list of movies that we&amp;rsquo;d like to display in a table view. Pulling to refresh will fetch more movies and update the table view to show the new ones.&lt;/p&gt;
&lt;p&gt;If you&amp;rsquo;re the type that likes to simply dive into a working example, both implementations are available to download from GitHub:&lt;/p&gt;
&lt;div class=&#34;resources&#34;&gt;
  &lt;div class=&#34;resources-header&#34;&gt;
    Example projects
  &lt;/div&gt;
  &lt;ul class=&#34;resources-content&#34;&gt;
    &lt;li&gt;
      &lt;i class=&#34;fab fa-github fa-lg&#34;&gt;&lt;/i&gt; Example implemented with UITableViewController &lt;div&gt;
        (&lt;a href=&#34;https://github.com/andrewcbancroft/PullToRefreshExample_UITableViewController/tree/swift-2.3&#34;&gt;Swift 2.3&lt;/a&gt; | &lt;a href=&#34;https://github.com/andrewcbancroft/PullToRefreshExample_UITableViewController/tree/master&#34;&gt;Swift 3.0&lt;/a&gt;)
      &lt;/div&gt;
    &lt;/li&gt;
&lt;pre&gt;&lt;code&gt;&amp;lt;li&amp;gt;
  &amp;lt;i class=&amp;quot;fab fa-github fa-lg&amp;quot;&amp;gt;&amp;lt;/i&amp;gt; Example implemented with regular view controller + UITableView &amp;lt;div&amp;gt;
    (&amp;lt;a href=&amp;quot;https://github.com/andrewcbancroft/PullToRefreshExample_RegularViewController/tree/swift-2.3&amp;quot;&amp;gt;Swift 2.3&amp;lt;/a&amp;gt; | &amp;lt;a href=&amp;quot;https://github.com/andrewcbancroft/PullToRefreshExample_RegularViewController/tree/master&amp;quot;&amp;gt;Swift 3.0&amp;lt;/a&amp;gt;)
  &amp;lt;/div&amp;gt;
&amp;lt;/li&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
  &lt;/ul&gt;
&lt;/div&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 example projects above.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;Movies&lt;/code&gt; are represented by a simple struct:&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;struct&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;Movie&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;title&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;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;genre&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 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 table view (regardless of whether we use a &lt;code&gt;UITableViewController&lt;/code&gt; or a regular &lt;code&gt;UIViewController&lt;/code&gt;) has the following setup…&lt;/p&gt;
&lt;p&gt;Initial data source values:&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;movies&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;Movie&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;Lion King&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;genre&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;Animation&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;n&#34;&gt;Movie&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;Star Wars&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;genre&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;Sci-fi&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Setting up the table view&amp;rsquo;s data source protocol methods depends on whether you&amp;rsquo;re using a &lt;a href=&#34;#table-view-controller&#34;&gt;UITableViewController&lt;/a&gt; or a &lt;a href=&#34;#regular-view-controller&#34;&gt;regular UIViewController&lt;/a&gt; with a table view as one of its content views, so we&amp;rsquo;ll cover those in the individual examples.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;table-view-controller&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;implementing-with-uitableviewcontroller&#34;&gt;Implementing with UITableViewController&lt;/h3&gt;
&lt;p&gt;&lt;a name=&#34;tvc-example-setup&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;finish-example-setup&#34;&gt;Finish example setup&lt;/h4&gt;
&lt;p&gt;When working with a &lt;code&gt;UITableViewController&lt;/code&gt;, we simply override the data source method implementations. The following is how I&amp;rsquo;ve chosen to do it 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;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;tableView&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;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; 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;n&#34;&gt;movies&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&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;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;tableView&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;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;cellForRowAt&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;IndexPath&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; 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;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;dequeueReusableCell&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;withIdentifier&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;cell&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&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;n&#34;&gt;cell&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;textLabel&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;n&#34;&gt;movies&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;indexPath&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;as&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;n&#34;&gt;row&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&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;cell&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;detailTextLabel&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;n&#34;&gt;movies&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;indexPath&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;as&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;n&#34;&gt;row&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;].&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;genre&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;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;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;&lt;a name=&#34;enable-refreshing-storyboard&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;enable-refreshing-in-storyboard&#34;&gt;Enable refreshing in Storyboard&lt;/h4&gt;
&lt;p&gt;When you&amp;rsquo;re working with a &lt;code&gt;UITableViewController&lt;/code&gt;, the solution is fairly simple: First, Select the table view controller in your storyboard, open the attributes inspector, and enable refreshing:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/03/TableView_EnableRefreshing.png&#34;&gt;&lt;img src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/03/TableView_EnableRefreshing-1024x740.png&#34; alt=&#34;Table View Controller - Enable Refreshing&#34; width=&#34;1024&#34; height=&#34;740&#34; class=&#34;alignnone size-large wp-image-11511&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/03/TableView_EnableRefreshing-1024x740.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2015/03/TableView_EnableRefreshing-300x217.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2015/03/TableView_EnableRefreshing.png 1083w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;A &lt;code&gt;UITableViewController&lt;/code&gt; comes outfitted with a reference to a &lt;code&gt;UIRefreshControl&lt;/code&gt; out of the box. You simply need to wire up a few things to initiate and complete the refresh when the user pulls down.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;override-viewdidload&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;override-viewdidload&#34;&gt;Override viewDidLoad()&lt;/h4&gt;
&lt;p&gt;In your override of &lt;code&gt;viewDidLoad()&lt;/code&gt;, add a target to handle the refresh 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;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;2&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;3&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Do any additional setup after loading the view, typically from a nib.&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;kc&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;refreshControl&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;?.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;addTarget&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;action&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;ViewController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;handleRefresh&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;p&#34;&gt;:)),&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;for&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIControlEvents&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;valueChanged&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Here are a couple of things to observe about the code above:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Swift&amp;rsquo;s new #selector feature helps with specifying which action will handle the refresh. Since I&amp;rsquo;ve specified &lt;code&gt;ViewController.handleRefresh(_:)&lt;/code&gt; (note the underscore and the colon!) as the action argument, I need to define a function in this &lt;code&gt;UITableViewController&lt;/code&gt; class with the same name. Additionally, the function should take one argument.&lt;/li&gt;
&lt;li&gt;We&amp;rsquo;d like this action to be called for the &lt;code&gt;UIControlEvent&lt;/code&gt; called &lt;code&gt;ValueChanged&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;a name=&#34;tvc-handle-refresh-function&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;implement-handlerefresh-function&#34;&gt;Implement handleRefresh function&lt;/h4&gt;
&lt;p&gt;The &lt;code&gt;handleRefresh:&lt;/code&gt; function may look something like the following:&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;handleRefresh&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;refreshControl&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIRefreshControl&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;// Do some reloading of data and update the table view&amp;#39;s 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; 3&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Fetch more objects from a web service, for example...&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;c1&#34;&gt;// Simply adding an object to the data source for this example&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;newMovie&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Movie&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;Serenity&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;genre&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;Sci-fi&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;n&#34;&gt;movies&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;append&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;newMovie&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&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;movies&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;bp&#34;&gt;sort&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;nv&#34;&gt;$0&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;o&#34;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;$1&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&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;kc&#34;&gt;self&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;reloadData&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;refreshControl&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;endRefreshing&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&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;That should complete the pull to refresh implementation when you&amp;rsquo;re working with a &lt;code&gt;UITableViewController&lt;/code&gt;!&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;regular-view-controller&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;implementing-with-regular-view-controller--uitableview&#34;&gt;Implementing with regular view controller + UITableView&lt;/h3&gt;
&lt;p&gt;&lt;a name=&#34;reg-tv-example-setup&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;finish-example-setup-1&#34;&gt;Finish example setup&lt;/h4&gt;
&lt;p&gt;When working with a &lt;em&gt;regular&lt;/em&gt; &lt;code&gt;UIViewController&lt;/code&gt;, there are a few extra steps involved in getting things set up:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Create an IBOutlet from the storyboard to the view controller&lt;/li&gt;
&lt;li&gt;Wire up the table view&amp;rsquo;s data source and delegate from the storyboard&lt;/li&gt;
&lt;li&gt;Implement the &lt;a href=&#34;http://www.andrewcbancroft.com/2014/11/24/swift-uitableviewdatasource-cheat-sheet/&#34;&gt;required table view data source methods&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Creating the IBOutlet is a matter of control+clicking and dragging from the table view in the Storyboard to the view controller code to create the outlet.&lt;/p&gt;
&lt;p&gt;To wire up the table view&amp;rsquo;s data source and delegate in the Storyboard, control+click the table view and drag up to the yellow view controller icon:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/04/WireTableViewDataSource.png&#34;&gt;&lt;img src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/04/WireTableViewDataSource.png&#34; alt=&#34;Wire up table view&amp;#039;s data source and delegate&#34; width=&#34;789&#34; height=&#34;746&#34; class=&#34;alignnone size-full wp-image-11681&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/04/WireTableViewDataSource.png 789w, https://www.andrewcbancroft.com/wp-content/uploads/2015/04/WireTableViewDataSource-300x284.png 300w&#34; sizes=&#34;(max-width: 789px) 100vw, 789px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The data source protocol method implementations may 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;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;n&#34;&gt;movies&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&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;kc&#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;cellForRowAt&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;IndexPath&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; 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;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;dequeueReusableCell&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;withIdentifier&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;cell&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&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;n&#34;&gt;cell&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;textLabel&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;n&#34;&gt;movies&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;indexPath&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;as&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;n&#34;&gt;row&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&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;cell&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;detailTextLabel&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;n&#34;&gt;movies&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;indexPath&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;as&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;n&#34;&gt;row&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;].&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;genre&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;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;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;&lt;a name=&#34;setup-refresh-control&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;set-up-uirefreshcontrol&#34;&gt;Set up UIRefreshControl&lt;/h4&gt;
&lt;p&gt;Whereas a &lt;code&gt;UITableViewController&lt;/code&gt; comes pre-fit with a &lt;code&gt;UIRefreshControl&lt;/code&gt;, a regular &lt;code&gt;UIViewController&lt;/code&gt; does not. It&amp;rsquo;s simple enough to set one up though. Here is a snippet defining a lazily instantiated variable which creates and configures a &lt;code&gt;UIRefreshControl&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;kr&#34;&gt;lazy&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;refreshControl&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIRefreshControl&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;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;refreshControl&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIRefreshControl&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;refreshControl&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;addTarget&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;action&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;ViewController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;handleRefresh&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;p&#34;&gt;:)),&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;for&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIControlEvents&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;valueChanged&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;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;refreshControl&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The most complicated thing about the code I just proposed is how the &lt;code&gt;UIRefreshControl&lt;/code&gt; instance is assigned lazily by means of the closure expression denoted by &lt;code&gt;= { // ...closure body with setup code... }()&lt;/code&gt; in the above snippet. Using this approach allows me to complete the setup all in one spot without the use of optionals. You may prefer doing this another way. The bottom line goal is to have a &lt;code&gt;UIRefreshControl&lt;/code&gt; instance that we can add to the table view (coming up).&lt;/p&gt;
&lt;p&gt;As for the body of the closure expression, we&amp;rsquo;re adding a target-action to the &lt;code&gt;UIRefreshControl&lt;/code&gt; instance, &lt;a href=&#34;#override-viewdidload&#34;&gt;just like we did when we were dealing with a UITableViewController&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;As with the &lt;code&gt;UITableViewController&lt;/code&gt; example, note:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Since I&amp;rsquo;ve specified &amp;ldquo;handleRefresh:” (note the colon!) as the action argument, I need to define a function in this &lt;code&gt;UITableViewController&lt;/code&gt; class with the same name. Additionally, the function should take one argument.&lt;/li&gt;
&lt;li&gt;We&amp;rsquo;d like this action to be called for the &lt;code&gt;UIControlEvent&lt;/code&gt; called &lt;code&gt;ValueChanged&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;a name=&#34;regvc-override-viewdidload&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;override-viewdidload-1&#34;&gt;Override viewDidLoad&lt;/h4&gt;
&lt;p&gt;Assuming that &lt;a href=&#34;#reg-tv-example-setup&#34;&gt;there is an outlet to the table view in the Storyboard&lt;/a&gt;, the next step is to add the &lt;code&gt;UIRefreshControl&lt;/code&gt; as a subview to the table view:&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;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;2&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;3&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Do any additional setup after loading the view, typically from a nib.&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;kc&#34;&gt;self&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;addSubview&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;refreshControl&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;a name=&#34;regvc-handle-refresh-function&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;implement-handlerefresh-function-1&#34;&gt;Implement handleRefresh function&lt;/h4&gt;
&lt;p&gt;The &lt;code&gt;handleRefresh&lt;/code&gt; function is implemented exactly as it was when we were dealing with a &lt;code&gt;UITableViewController&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;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;handleRefresh&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;refreshControl&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIRefreshControl&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;// Do some reloading of data and update the table view&amp;#39;s 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; 3&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Fetch more objects from a web service, for example...&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;c1&#34;&gt;// Simply adding an object to the data source for this example&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;newMovie&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Movie&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;Serenity&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;genre&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;Sci-fi&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;n&#34;&gt;movies&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;append&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;newMovie&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&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;movies&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;bp&#34;&gt;sort&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;nv&#34;&gt;$0&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;o&#34;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;$1&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&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;kc&#34;&gt;self&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;reloadData&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;refreshControl&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;endRefreshing&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&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id=&#34;wrapping-up&#34;&gt;Wrapping up&lt;/h3&gt;
&lt;p&gt;Implementing &amp;ldquo;pull to refresh” is a common need that arises when working with table views. Here we&amp;rsquo;ve explored how to implement this feature using both a &lt;code&gt;UITableViewController&lt;/code&gt; &lt;em&gt;and&lt;/em&gt; with a regular view controller and a table view.&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>An (Almost) TDD Workflow in Swift</title>
      <link>https://www.andrewcbancroft.com/2015/03/10/an-almost-tdd-workflow-in-swift/</link>
      <pubDate>Wed, 11 Mar 2015 01:46:59 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2015/03/10/an-almost-tdd-workflow-in-swift/</guid>
      <description>&lt;p&gt;There are times when it feels paralyzing to write tests &lt;em&gt;first&lt;/em&gt; before any production code is written. Even with good requirements documentation, I often find myself asking, &amp;ldquo;How am I supposed to write a test to verify &lt;strong&gt;&lt;em&gt;x&lt;/em&gt;&lt;/strong&gt; about some &lt;strong&gt;&lt;em&gt;thing&lt;/em&gt;&lt;/strong&gt; that doesn&amp;rsquo;t exist in actual code yet?” It can be crippling.&lt;/p&gt;
&lt;p&gt;The following is a workflow that&amp;rsquo;s helped me grow in my test-first development skills. When I find myself staring at the screen, paralyzed because I&amp;rsquo;m &amp;ldquo;not supposed to write actual production code until the test is written”, I often turn to the workflow that I&amp;rsquo;m about to describe to help me break through to being productive. With practice and experience, I find myself needing this strategy less and less, but I&amp;rsquo;ve found it helpful to use this (almost) TDD workflow as a gateway into full test-first development.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;setup&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;set-up-side-by-side-view-test-on-left--code-on-right&#34;&gt;Set up side-by-side view: Test on left | Code on right&lt;/h3&gt;
&lt;p&gt;My starting place is to always have a test file open on the left, and the actual production code file that I want to write tests for on the right. This does a couple of things for me:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;It helps me avoid a lot of switching back and forth between tests and production code.&lt;/li&gt;
&lt;li&gt;It helps me keep tests at the forefront of my mind. Without seeing them in front of me, I could more easily forget about them. Having the split IDE keeps me conscious of the need to prioritize testing.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;I recently &lt;a href=&#34;http://www.andrewcbancroft.com/2015/02/25/using-swift-to-seed-a-core-data-database/&#34;&gt;wrote about a technique to seed a Core Data database&lt;/a&gt;, and with that post, I &lt;a href=&#34;https://github.com/andrewcbancroft/Zootastic&#34;&gt;included a project called &amp;ldquo;Zootastic”&lt;/a&gt; – a contrived app that modeled the storage and display of &lt;code&gt;Zoos&lt;/code&gt; (along with &lt;code&gt;Animals&lt;/code&gt; and their &lt;code&gt;Classifications&lt;/code&gt;). I created a class called &lt;code&gt;DataHelper&lt;/code&gt; which had several &lt;code&gt;seed()&lt;/code&gt; methods. For the purposes of having an example before us, suppose that I wanted to test &lt;code&gt;DataHelper&lt;/code&gt;. My screen might look something like this, with my tests on the left, and my &lt;code&gt;DataHelper&lt;/code&gt; class on the right:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2014/12/TestsLeft_CodeRight.png&#34;&gt;&lt;img src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2014/12/TestsLeft_CodeRight-1024x208.png&#34; alt=&#34;Test on the left | Code on the right&#34; width=&#34;1024&#34; height=&#34;208&#34; class=&#34;alignnone size-large wp-image-11495&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2014/12/TestsLeft_CodeRight-1024x208.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2014/12/TestsLeft_CodeRight-300x61.png 300w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;write-code&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;write-the-actual-production-code&#34;&gt;Write the actual production code&lt;/h3&gt;
&lt;p&gt;What I want is to insert 3 Zoo objects into the Core Data data store. But without the actual code before me, it&amp;rsquo;s hard to imagine what the test(s) for that code might look like.&lt;/p&gt;
&lt;p&gt;When I get stuck in this way, I&amp;rsquo;ll go ahead and write the actual production code:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2014/12/WriteProductionCode.png&#34;&gt;&lt;img src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2014/12/WriteProductionCode-1024x264.png&#34; alt=&#34;Write the production code&#34; width=&#34;1024&#34; height=&#34;264&#34; class=&#34;alignnone size-large wp-image-11497&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2014/12/WriteProductionCode-1024x264.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2014/12/WriteProductionCode-300x77.png 300w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;One important thing to remember is that you don&amp;rsquo;t want to write a &lt;em&gt;ton&lt;/em&gt; of code in this step… just enough to spark your brain into figuring out what kinds of tests you can write. Write small increments of code. The more you write, the harder it will be to ensure you&amp;rsquo;ve covered the code and the various edge cases that may exist. Your goal is not to write the &lt;em&gt;app&lt;/em&gt;. Your goal is to write a function, or a &lt;em&gt;part&lt;/em&gt; of the function – just enough to get you going with tests.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;write-test&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;write-a-test-that-will-exercise-the-code&#34;&gt;Write a test that will exercise the code&lt;/h3&gt;
&lt;p&gt;Having some real code with real class names and real function names usually helps me see what I need to do in terms of testing.&lt;/p&gt;
&lt;p&gt;In the example I have going, I&amp;rsquo;d like my &lt;code&gt;seedZoos()&lt;/code&gt; function to insert 3 &lt;code&gt;Zoo&lt;/code&gt; objects into my CoreData data store.&lt;/p&gt;
&lt;p&gt;At this point, it&amp;rsquo;s pretty easy for me to think of the name of my first test. How about, &lt;code&gt;testSeedZoosInserts3ZooObjectsIntoDataStore()&lt;/code&gt;:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2014/12/WriteTest.png&#34;&gt;&lt;img src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2014/12/WriteTest-1024x385.png&#34; alt=&#34;Write a test&#34; width=&#34;1024&#34; height=&#34;385&#34; class=&#34;alignnone size-large wp-image-11498&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2014/12/WriteTest-1024x385.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2014/12/WriteTest-300x113.png 300w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;comment-code&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;comment-out-the-production-code-so-that-the-test-will-fail&#34;&gt;Comment out the production code so that the test will fail&lt;/h3&gt;
&lt;p&gt;Running the tests right now would produce a passing test. &amp;ldquo;Great!”, you say – but here&amp;rsquo;s my issue with simply running the test, seeing it pass, and moving on &lt;em&gt;without&lt;/em&gt; ever having seen it fail:&lt;/p&gt;
&lt;p&gt;There are &lt;em&gt;many&lt;/em&gt; ways to produce passing tests without actually verifying the results of executing the app&amp;rsquo;s code.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;I could write a test with no assert. That&amp;rsquo;d be silly, but forgetting that at the end would produce a green test – and it&amp;rsquo;s easier to do than you think as you get rolling with these things. &lt;em&gt;Expecting&lt;/em&gt; the first time you run the test to produce a &lt;em&gt;failing&lt;/em&gt; test would alert you if you ran it the first time and saw a passing one.&lt;/li&gt;
&lt;li&gt;I could write a test that asserts the wrong thing and produces a false positive. Again, expecting &amp;ldquo;fail” at first would alert me if I saw &amp;ldquo;pass” at first.&lt;/li&gt;
&lt;li&gt;Suppose I copied and pasted a test and intended to replace the implementation to test my new code. But I get distracted between when I pasted it and when I ran it for the first time. If I ran it, saw &amp;ldquo;pass” and moved on, the test wouldn&amp;rsquo;t be doing its job – it&amp;rsquo;d be testing something that I already tested, and not these new lines of code I just produced!&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The point is this: There are too many ways to write a test that doesn&amp;rsquo;t truly test your code. Suffice it to say, you should &lt;em&gt;always&lt;/em&gt; make the test fail so that you know it&amp;rsquo;s wired up to the right production code. Thus, this crucial step: &lt;strong&gt;comment out the production code&lt;/strong&gt;. It&amp;rsquo;ll ensure you get a failing test on the first run (if you&amp;rsquo;re truly testing the right thing).&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2014/12/CommentProductionCode.png&#34;&gt;&lt;img src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2014/12/CommentProductionCode-1024x379.png&#34; alt=&#34;Comment out the production code&#34; width=&#34;1024&#34; height=&#34;379&#34; class=&#34;alignnone size-large wp-image-11492&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2014/12/CommentProductionCode-1024x379.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2014/12/CommentProductionCode-300x111.png 300w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;run-test-fail&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;run-the-test-and-verify-that-it-fails&#34;&gt;Run the test and verify that it fails&lt;/h3&gt;
&lt;p&gt;With the production code I just wrote commented out, I run the test. My expectation at this point is that it will &lt;em&gt;fail&lt;/em&gt;, because the &lt;code&gt;seedZoos()&lt;/code&gt; function does &lt;em&gt;not&lt;/em&gt; currently insert any &lt;code&gt;Zoo&lt;/code&gt; objects into the data store.&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2014/12/RunTest_Fail.png&#34;&gt;&lt;img src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2014/12/RunTest_Fail-1024x380.png&#34; alt=&#34;Run test - the test should fail&#34; width=&#34;1024&#34; height=&#34;380&#34; class=&#34;alignnone size-large wp-image-11493&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2014/12/RunTest_Fail-1024x380.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2014/12/RunTest_Fail-300x111.png 300w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;If the test doesn&amp;rsquo;t fail, something is wrong.&lt;/strong&gt; Check the basics: Did you include an assert at the end of the test? Are you exercising the right production code? Continue troubleshooting and re-running the test until it fails.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;uncomment-code&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;uncomment-the-production-code-so-that-the-test-will-pass&#34;&gt;Uncomment the production code so that the test will pass&lt;/h3&gt;
&lt;p&gt;Once I&amp;rsquo;ve been able to make the test fail, I uncomment the production code.&lt;/p&gt;
&lt;p&gt;The idea here is that once the production code is now &amp;ldquo;live”, the test that&amp;rsquo;s currently failing should &lt;em&gt;pass&lt;/em&gt;, now that production code is performing appropriate logic to meet the test&amp;rsquo;s assertion requirements. We know that the test currently fails, so if it passes &lt;em&gt;after&lt;/em&gt; we uncomment the production code, the only reason it could pass is because the production code is doing the right thing for that particular test&amp;rsquo;s assertion. Nothing else about our work environment changed, so nothing else except the uncommented production code could have been the cause of the passing test.&lt;/p&gt;
&lt;p&gt;Here&amp;rsquo;s a view of the IDE in the state right before I run the test again to watch it pass:&lt;br&gt;
&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2014/12/UncommentProductionCode.png&#34;&gt;&lt;img src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2014/12/UncommentProductionCode-1024x380.png&#34; alt=&#34;Uncomment the production code&#34; width=&#34;1024&#34; height=&#34;380&#34; class=&#34;alignnone size-large wp-image-11496&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2014/12/UncommentProductionCode-1024x380.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2014/12/UncommentProductionCode-300x111.png 300w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;run-test-pass&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;run-the-test-and-verify-that-it-passes&#34;&gt;Run the test and verify that it passes&lt;/h3&gt;
&lt;p&gt;The last step in this (almost) TDD workflow is to run the test one more time. This time it should pass:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2014/12/RunTest_Pass.png&#34;&gt;&lt;img src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2014/12/RunTest_Pass-1024x382.png&#34; alt=&#34;Run test - the test should pass&#34; width=&#34;1024&#34; height=&#34;382&#34; class=&#34;alignnone size-large wp-image-11494&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2014/12/RunTest_Pass-1024x382.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2014/12/RunTest_Pass-300x112.png 300w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;If the test &lt;em&gt;doesn&amp;rsquo;t&lt;/em&gt; pass, then something is wrong.&lt;/strong&gt; Check the basics: Does the test assert the right thing? Does the production code perform correct logic that would satisfy the test&amp;rsquo;s assertion? Continue troubleshooting and revise the necessary code until you have a passing test.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;rinse-repeat&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;rinse-and-repeat&#34;&gt;Rinse and repeat&lt;/h3&gt;
&lt;p&gt;You can perform this workflow as my times as you need. This is a stepping stone, so the hope is that eventually you&amp;rsquo;ll be able to write the tests &lt;em&gt;first&lt;/em&gt;. It takes a little practice, but using this technique has, in my experience, been a gateway to true Test Driven Development.&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>Displaying Data With NSFetchedResultsController and Swift</title>
      <link>https://www.andrewcbancroft.com/2015/03/05/displaying-data-with-nsfetchedresultscontroller-and-swift/</link>
      <pubDate>Thu, 05 Mar 2015 15:14:53 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2015/03/05/displaying-data-with-nsfetchedresultscontroller-and-swift/</guid>
      <description>&lt;p&gt;The combination of an &lt;code&gt;NSFetchedResultsController&lt;/code&gt; and a &lt;code&gt;UITableView&lt;/code&gt; provides a powerful way to integrate Core Data with a user interface. The greatest benefits of using &lt;code&gt;NSFetchedResultsController&lt;/code&gt; come when we use it to automatically update a table view when objects are added, updated, or removed from a Core Data data store. First things first, though…&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/2015/02/25/using-swift-to-seed-a-core-data-database/&#34;&gt;With a Core Data data store seeded with data&lt;/a&gt;, the next logical step is to &lt;em&gt;display&lt;/em&gt; that data somewhere other than the console. This post will be devoted to figuring out how to set up an &lt;code&gt;NSFetchedResultsController&lt;/code&gt; to display data inside a &lt;code&gt;UITableView&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;A follow-up post has been published to help you &lt;a href=&#34;http://www.andrewcbancroft.com/2015/05/28/sync-table-view-data-nsfetchedresultscontroller-swift/&#34;&gt;keep the table view in sync&lt;/a&gt; with the data as it changes in your persistent store, so once you&amp;rsquo;re finished here, you might check out that next step!&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;final-goal&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;final-goal&#34;&gt;Final goal&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;NSFetchedResultsController&lt;/code&gt; will help us accomplish two things:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;It will fetch data from the Core Data data store&lt;/li&gt;
&lt;li&gt;It will use some of the data we fetch to populate various pieces of the UI (table view section headers, cell &amp;ldquo;title” and &amp;ldquo;subtitle” text. Here&amp;rsquo;s what we&amp;rsquo;re going for:&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/03/Zootastic_TableView_FinalDisplay.png&#34;&gt;&lt;img src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/03/Zootastic_TableView_FinalDisplay.png&#34; alt=&#34;Zootastic TableView FinalDisplay&#34; width=&#34;479&#34; height=&#34;871&#34; class=&#34;alignnone size-full wp-image-11450&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/03/Zootastic_TableView_FinalDisplay.png 479w, https://www.andrewcbancroft.com/wp-content/uploads/2015/03/Zootastic_TableView_FinalDisplay-165x300.png 165w&#34; sizes=&#34;(max-width: 479px) 100vw, 479px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;scenario-setup&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;setup-and-resources&#34;&gt;Setup and resources&lt;/h3&gt;
&lt;p&gt;I&amp;rsquo;m continuing my &amp;ldquo;Zootastic” example that I used to write about &lt;a href=&#34;http://www.andrewcbancroft.com/2015/02/25/using-swift-to-seed-a-core-data-database/&#34;&gt;using Swift to seed a Core Data database&lt;/a&gt;. In fact, I&amp;rsquo;ve simply branched the project on GibHub and added the things we&amp;rsquo;re exploring in this post.&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;fab fa-github fa-lg&#34;&gt;&lt;/i&gt; &lt;a href=&#34;https://github.com/andrewcbancroft/Zootastic/tree/NSFetchedResultsController_DisplayInTableView&#34; title=&#34;Zootastic&#34;&gt;Example Xcode Project&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;
&lt;p&gt;For this entry, we&amp;rsquo;ll still be dealing with our three primary &lt;code&gt;NSManagedObject&lt;/code&gt; subclasses: Zoo, Animal, And Classification:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/03/Zootastic_xcdatamodel.png&#34;&gt;&lt;img src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/03/Zootastic_xcdatamodel.png&#34; alt=&#34;Zootastic XCdatamodel&#34; width=&#34;361&#34; height=&#34;310&#34; class=&#34;alignnone size-full wp-image-11463&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/03/Zootastic_xcdatamodel.png 361w, https://www.andrewcbancroft.com/wp-content/uploads/2015/03/Zootastic_xcdatamodel-300x258.png 300w&#34; sizes=&#34;(max-width: 361px) 100vw, 361px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;storyboard&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;storyboard&#34;&gt;Storyboard&lt;/h3&gt;
&lt;p&gt;Zootastic is a single view application. The Storyboard contains one view controller with a table view filling the Scene.&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/03/Zootastic_Storyboard_TableView.png&#34;&gt;&lt;img src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/03/Zootastic_Storyboard_TableView-1024x773.png&#34; alt=&#34;Zootastic Storyboard TableView&#34; width=&#34;1024&#34; height=&#34;773&#34; class=&#34;alignnone size-large wp-image-11445&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/03/Zootastic_Storyboard_TableView-1024x773.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2015/03/Zootastic_Storyboard_TableView-300x226.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2015/03/Zootastic_Storyboard_TableView.png 1379w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The table view is using one prototype cell with an &lt;strong&gt;Identifier&lt;/strong&gt; of &lt;strong&gt;&amp;ldquo;Cell”&lt;/strong&gt; (for simplicity). The &lt;strong&gt;Style&lt;/strong&gt; of the prototype cell is set to &lt;strong&gt;Subtitle&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/03/Zootastic_Storyboard_TableViewCell.png&#34;&gt;&lt;img src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/03/Zootastic_Storyboard_TableViewCell-1024x770.png&#34; alt=&#34;Zootastic Storyboard TableViewCell&#34; width=&#34;1024&#34; height=&#34;770&#34; class=&#34;alignnone size-large wp-image-11446&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/03/Zootastic_Storyboard_TableViewCell-1024x770.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2015/03/Zootastic_Storyboard_TableViewCell-300x225.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2015/03/Zootastic_Storyboard_TableViewCell.png 1381w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;main-view-controller&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;mainviewcontrollerswift&#34;&gt;MainViewController.swift&lt;/h3&gt;
&lt;p&gt;MainViewController.swift is where the action is happening. Here&amp;rsquo;s a quick outline of what we need to accomplish in this class:&lt;/p&gt;
&lt;p&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;8
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;MainViewController&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;UITableViewDataSource&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UITableViewDelegate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSFetchedResultsControllerDelegate&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;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// maintain a reference to NSManagedObjectContext instance&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// create and configure an NSFetchedResultsController instance&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Implement UITableViewDataSource methods&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
As you can see, &lt;code&gt;MainViewController&lt;/code&gt; is a class concerned with being the table view&amp;rsquo;s data source and delegate. Additionally, it will serve as the &lt;code&gt;NSFetchedResultsControllerDelegate&lt;/code&gt;. For this post, we won&amp;rsquo;t actually need the fetched results controller delegate functionality to display data. Those methods are particularly useful for synchronizing things when data &lt;em&gt;changes&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;With the class declaration out of the way, we&amp;rsquo;ll investigate the class implementation one section at a time.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;nsmanagedobjectcontext-reference&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;maintain-nsmanagedobjectcontext-instance-reference&#34;&gt;Maintain NSManagedObjectContext instance reference&lt;/h4&gt;
&lt;p&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;6
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;MainViewController&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;UITableViewDataSource&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UITableViewDelegate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSFetchedResultsControllerDelegate&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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;var&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 class=&#34;p&#34;&gt;!&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;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;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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
You may be asking, &amp;ldquo;Where will you set this NSManagedObjectContext reference?”. I&amp;rsquo;m employing a pattern that I&amp;rsquo;ve found successful in the past: I assign it when the finishes launching through the AppDelegate&amp;rsquo;s &lt;code&gt;application:didFinishLaunchingWithOptions&lt;/code&gt; method. &lt;a href=&#34;#app-delegate&#34;&gt;More on this, shortly&lt;/a&gt;…&lt;/p&gt;
&lt;p&gt;For now, know that we&amp;rsquo;re counting on that &lt;a href=&#34;#app-delegate&#34;&gt;later step&lt;/a&gt; to take place, since &lt;code&gt;context&lt;/code&gt; is defined as an implicitly unwrapped optional.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;create-nsfetchedresultscontroller&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;create-and-configure-nsfetchedresultscontroller-instance&#34;&gt;Create and configure NSFetchedResultsController instance&lt;/h4&gt;
&lt;p&gt;Next, we need to create and configure an &lt;code&gt;NSFetchedResultsController&lt;/code&gt; instance. Here&amp;rsquo;s a bit of code with comments to follow:&lt;/p&gt;
&lt;p&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;12
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;13
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;14
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;15
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;16
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;17
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;18
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;19
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;20
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;21
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;22
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;23
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;24
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;MainViewController&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;UITableViewDataSource&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UITableViewDelegate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSFetchedResultsControllerDelegate&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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kr&#34;&gt;lazy&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;fetchedResultsController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSFetchedResultsController&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;cl&#34;&gt;        &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;animalsFetchRequest&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;Animal&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;cl&#34;&gt;        &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;primarySortDescriptor&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSSortDescriptor&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;key&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;classification.order&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ascending&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&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;secondarySortDescriptor&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSSortDescriptor&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;key&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;commonName&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ascending&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&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;animalsFetchRequest&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;sortDescriptors&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;primarySortDescriptor&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;secondarySortDescriptor&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;cl&#34;&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;frc&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSFetchedResultsController&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;cl&#34;&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;animalsFetchRequest&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;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;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&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;            &lt;span class=&#34;n&#34;&gt;sectionNameKeyPath&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;classification.order&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;cl&#34;&gt;            &lt;span class=&#34;n&#34;&gt;cacheName&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;cl&#34;&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;frc&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&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;frc&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
If you&amp;rsquo;ve not read &lt;a href=&#34;https://twitter.com/ColinEberhardt&#34;&gt;Colin Eberhardt&amp;rsquo;s&lt;/a&gt; &lt;a href=&#34;http://blog.scottlogic.com/2014/11/20/swift-initialisation.html&#34;&gt;Swift Initialization and the Pain of Optionals&lt;/a&gt; post, I highly recommend it. His post is a fantastic analysis, and the final option of using lazy stored properties initialized by a closure expression is what I&amp;rsquo;ve chosen to do here. I won&amp;rsquo;t repeat his analysis here, so feel free to jump over to his post to figure out what&amp;rsquo;s going on there.&lt;/p&gt;
&lt;p&gt;Within the closure expression, I&amp;rsquo;m setting up a fetch request with some sorting applied. All that&amp;rsquo;s left is to initialize the &lt;code&gt;NSFetchedResultsController&lt;/code&gt;, set its delegate and return it.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;view-did-load&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;viewdidload&#34;&gt;viewDidLoad()&lt;/h4&gt;
&lt;p&gt;Once the view has loaded, the idea is to perform the &lt;code&gt;NSFetchedResultsController&lt;/code&gt; instance&amp;rsquo;s fetch request so that it has data to use in our &lt;code&gt;UITableViewDataSource&lt;/code&gt; methods. This is how to do it:&lt;/p&gt;
&lt;p&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;12
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;13
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;14
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;15
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;16
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;17
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;18
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;MainViewController&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;UITableViewDataSource&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UITableViewDelegate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSFetchedResultsControllerDelegate&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;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;public&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;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;cl&#34;&gt;        &lt;span class=&#34;c1&#34;&gt;// Do any additional setup after loading the view, typically from a nib.&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;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;cl&#34;&gt;                &lt;span class=&#34;k&#34;&gt;try&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;fetchedResultsController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;performFetch&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;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;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;An error occurred&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;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;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;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;a name=&#34;uitableviewdatasource-methods&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;uitableviewdatasource-methods&#34;&gt;UITableViewDataSource methods&lt;/h4&gt;
&lt;p&gt;The final step in implementing &lt;code&gt;MainViewController&lt;/code&gt; is to set up the table view so that it pulls data from &lt;code&gt;fetchedResultsController&lt;/code&gt;. I&amp;rsquo;m implementing the &lt;a href=&#34;http://www.andrewcbancroft.com/2014/11/24/swift-uitableviewdatasource-cheat-sheet/&#34;&gt;standard UITableViewDataSource methods&lt;/a&gt; here, along with &lt;code&gt;tableView:titleForHeaderInSection&lt;/code&gt;. Take a look:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;hl&#34;&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;12
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;13
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;14
&lt;/span&gt;&lt;span class=&#34;hl&#34;&gt;&lt;span class=&#34;lnt&#34;&gt;15
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;16
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;17
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;18
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;19
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;20
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;21
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;22
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;23
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;24
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;25
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;26
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;27
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;28
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;29
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;30
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;31
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;32
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;33
&lt;/span&gt;&lt;span class=&#34;hl&#34;&gt;&lt;span class=&#34;lnt&#34;&gt;34
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;35
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;36
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;37
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;38
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;39
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;40
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;41
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;42
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;43
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;44
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;MainViewController&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;UITableViewDataSource&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UITableViewDelegate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSFetchedResultsControllerDelegate&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;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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; TableView Data Source&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;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 hl&#34;&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;sections&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;fetchedResultsController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;sections&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;cl&#34;&gt;            &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;sections&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&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;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 hl&#34;&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;sections&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;fetchedResultsController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;sections&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;cl&#34;&gt;            &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;currentSection&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;sections&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&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&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;currentSection&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;numberOfObjects&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;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;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;Cell&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;forIndexPath&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&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&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;animal&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;fetchedResultsController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;objectAtIndexPath&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&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;k&#34;&gt;as&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;!&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Animal&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;cell&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;textLabel&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;n&#34;&gt;animal&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&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;cell&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;detailTextLabel&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;n&#34;&gt;animal&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;habitat&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;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;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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;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;titleForHeaderInSection&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;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 hl&#34;&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;sections&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;fetchedResultsController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;sections&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;cl&#34;&gt;            &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;currentSection&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;sections&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&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&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;currentSection&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;name&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;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;cl&#34;&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;nil&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Apart from a bit of &lt;code&gt;if let ___ = ___&lt;/code&gt; syntax, there&amp;rsquo;s not an awful lot of surprising code here if you&amp;rsquo;re familiar with working with table views. I&amp;rsquo;ve highlighted the relevant code related to &lt;code&gt;fetchedResultsController&lt;/code&gt;. Without using &lt;code&gt;NSFetchedResultsController&lt;/code&gt;, you&amp;rsquo;d probably supply data to the table view from an array or a dictionary or both. The &lt;code&gt;fetchedResultsController&lt;/code&gt; code simplifies the data display dilemma when you&amp;rsquo;re using Core Data.&lt;/p&gt;
&lt;p&gt;Once the &lt;code&gt;UITableViewDataSource&lt;/code&gt; methods are implemented, the implementation of &lt;code&gt;MainViewController&lt;/code&gt; is complete for this example.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;app-delegate&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;appdelegateswift&#34;&gt;AppDelegate.swift&lt;/h3&gt;
&lt;p&gt;There&amp;rsquo;s one final thing we need to do in order to get things rolling. In the &lt;a href=&#34;#nsmanagedobjectcontext-reference&#34;&gt;&amp;ldquo;maintain NSManagedObjectContext instance reference”&lt;/a&gt; section of this post, I mentioned the strategy for assigning the &lt;code&gt;NSManagedObjectContext&lt;/code&gt; instance in the &lt;code&gt;MainViewController&lt;/code&gt;. Here&amp;rsquo;s how I do it:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;12
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;13
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;14
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;15
&lt;/span&gt;&lt;span class=&#34;hl&#34;&gt;&lt;span class=&#34;lnt&#34;&gt;16
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;hl&#34;&gt;&lt;span class=&#34;lnt&#34;&gt;17
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;18
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;19
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;20
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;21
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;22
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;23
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;24
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;AppDelegate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIResponder&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIApplicationDelegate&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;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;application&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;application&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIApplication&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;didFinishLaunchingWithOptions&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;launchOptions&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;NSObject&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;-&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;cl&#34;&gt;        &lt;span class=&#34;c1&#34;&gt;// Override point for customization after application launch.&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;        &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;dataHelper&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;DataHelper&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;kc&#34;&gt;self&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;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;dataHelper&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;seedDataStore&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;cl&#34;&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;dataHelper&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;printAllZoos&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;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;dataHelper&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;printAllClassifications&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;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;dataHelper&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;printAllAnimals&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;cl&#34;&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line hl&#34;&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;rootViewController&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;window&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;!.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;rootViewController&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;MainViewController&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line hl&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;rootViewController&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;kc&#34;&gt;self&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;cl&#34;&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;true&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; With iOS 13, the code to assign the &lt;code&gt;managedObjectContext&lt;/code&gt; to the root view controller needs to go &lt;a href=&#34;https://www.andrewcbancroft.com/blog/ios-development/ui-work/accessing-root-view-controller-ios13-scenedelegate/&#34;&gt;in your app&amp;rsquo;s SceneDelegate&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The portion new to &amp;ldquo;injecting” the &lt;code&gt;managedObjectContext&lt;/code&gt; into &lt;code&gt;MainViewController&lt;/code&gt; is highlighted. I simply grab a reference to the &lt;code&gt;rootViewController&lt;/code&gt; (which in our example is the &lt;code&gt;MainViewController&lt;/code&gt;) and cast it to the appropriate type. Then I set the &lt;code&gt;context&lt;/code&gt; property to the &lt;code&gt;managedObjectContext&lt;/code&gt; that&amp;rsquo;s created in the &lt;code&gt;AppDelegate&lt;/code&gt; via Xcode&amp;rsquo;s auto-generated Core Data stack setup.&lt;/p&gt;
&lt;h3 id=&#34;wrapping-up&#34;&gt;Wrapping up&lt;/h3&gt;
&lt;p&gt;If you&amp;rsquo;re using Core Data in your iOS application, the combination of an &lt;code&gt;NSFetchedResultsController&lt;/code&gt; and a &lt;code&gt;UITableView&lt;/code&gt; provides a powerful way to integrate data from your data store into your UI. We&amp;rsquo;ve explored how to display data in a table view using &lt;code&gt;NSFetchedResultsController&lt;/code&gt;. Feel free to &lt;a href=&#34;https://github.com/andrewcbancroft/Zootastic/tree/NSFetchedResultsController_DisplayInTableView&#34;&gt;grab the GibHub project&lt;/a&gt; for further investigation and to see Zootastic in action.&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/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;http://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;&gt;Using Swift to Seed a Core Data Database&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/05/28/sync-table-view-data-nsfetchedresultscontroller-swift/&#34; title=&#34;Sync Table View Data: NSFetchedResultsController and Swift&#34;&gt;Sync Table View Data: NSFetchedResultsController and Swift&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;
&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>Using Swift to Seed a Core Data Database</title>
      <link>https://www.andrewcbancroft.com/2015/02/25/using-swift-to-seed-a-core-data-database/</link>
      <pubDate>Thu, 26 Feb 2015 03:44:53 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2015/02/25/using-swift-to-seed-a-core-data-database/</guid>
      <description>&lt;p&gt;Designing an application&amp;rsquo;s UI can be difficult without actual data to present. Early on in the design process, data sourced from something like an array can suffice. Later on, however, our data sources become more dependent on actual data stores, such as what Core Data provides us.&lt;/p&gt;
&lt;p&gt;During development, I&amp;rsquo;ve found that it&amp;rsquo;s often convenient to seed a Core Data database with sample data so that I can preview how it&amp;rsquo;ll look in my application. Along with that, it&amp;rsquo;s nice to start with a fresh copy of the data each time I run the app. Let&amp;rsquo;s explore how to accomplish this task in Swift!&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;scenario-setup&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;scenario-setup&#34;&gt;Scenario setup&lt;/h3&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;fab fa-github fa-lg&#34;&gt;&lt;/i&gt; &lt;a href=&#34;https://github.com/andrewcbancroft/Zootastic/tree/BaseProject_SeedDataStore&#34; title=&#34;Zootastic&#34;&gt;Example Xcode Project&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;
&lt;p&gt;I&amp;rsquo;ve created a fictitious Core Data app called &amp;ldquo;Zootastic”, which is intended to model zoos, and the animals that each zoo cares for. (I&amp;rsquo;ll give you one guess as to what kinds of shows and activities my two year old and I have been in to lately). :]&lt;/p&gt;
&lt;p&gt;Specifically, I have the following Entities:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Zoo
&lt;ul&gt;
&lt;li&gt;name&lt;/li&gt;
&lt;li&gt;location&lt;/li&gt;
&lt;li&gt;animals (relationship)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Animal
&lt;ul&gt;
&lt;li&gt;commonName&lt;/li&gt;
&lt;li&gt;habitat&lt;/li&gt;
&lt;li&gt;classification (relationship)&lt;/li&gt;
&lt;li&gt;zoos (relationship)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Classification
&lt;ul&gt;
&lt;li&gt;scientificClassification&lt;/li&gt;
&lt;li&gt;family&lt;/li&gt;
&lt;li&gt;order&lt;/li&gt;
&lt;li&gt;animals (relationship)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I&amp;rsquo;ve &lt;a href=&#34;http://www.andrewcbancroft.com/2014/07/17/implement-nsmanagedobject-subclass-in-swift/&#34;&gt;created NSManagedObject subclasses&lt;/a&gt; for each of my entities, to make it easier to set properties.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;data-helper&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;datahelperswift&#34;&gt;DataHelper.swift&lt;/h3&gt;
&lt;p&gt;Once a Core Data data model is set up, we&amp;rsquo;re ready to create what I called &lt;code&gt;DataHelper&lt;/code&gt;. It serves the purpose of seeding the data store, and logging the data store&amp;rsquo;s contents back out to the console. It violates the single-responsibility principle, but wait! Don&amp;rsquo;t lynch me!&lt;/p&gt;
&lt;p&gt;Knowing that this class is intended to be used &lt;em&gt;solely&lt;/em&gt; for development, I didn&amp;rsquo;t put a lot of effort into separating concerns. I opted for a &amp;ldquo;here&amp;rsquo;s where I go to do all my seeding for manual testing purposes” approach.&lt;/p&gt;
&lt;p&gt;Here are a few snippets from the class (&lt;a href=&#34;https://github.com/andrewcbancroft/Zootastic/tree/BaseProject_SeedDataStore&#34;&gt;grab the full Xcode project&lt;/a&gt; over at GitHub):&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;data-helper-initialization&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;initialization&#34;&gt;Initialization&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;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;DataHelper&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;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; 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;init&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;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; 5&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;context&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;// ...&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;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 primary thing to take away from the initialization routine is that instead of calling out to the AppDelegate to get an instance of the &lt;code&gt;NSManagedObjectContext&lt;/code&gt;, I&amp;rsquo;m choosing to require it to be passed in during the initialization of &lt;code&gt;DataHelper&lt;/code&gt;. It&amp;rsquo;s a pattern I like to practice because it allows me to do unit tests in real-world applications that use Core Data.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;data-helper-seed-zoos&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;seedzoos&#34;&gt;seedZoos()&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;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;DataHelper&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 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&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;private&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;seedZoos&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;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;zoos&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;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;s&#34;&gt;&amp;#34;Oklahoma City Zoo&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;location&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;Oklahoma City, 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; 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;name&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;Lowry Park Zoo&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;location&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;Tampa, FL&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 class=&#34;n&#34;&gt;name&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;San Diego Zoo&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;location&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;San Diego, CA&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;p&#34;&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;k&#34;&gt;for&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;zoo&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;in&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;zoos&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;newZoo&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;Zoo&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;context&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;Zoo&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;newZoo&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;zoo&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;name&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;15&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;            &lt;span class=&#34;n&#34;&gt;newZoo&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;location&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;zoo&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;location&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 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;19&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;context&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;save&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;p&#34;&gt;}&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;catch&lt;/span&gt; &lt;span class=&#34;kc&#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;p&#34;&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&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;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;25&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;26&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;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 few observations on the code above:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;This function may look a bit interesting. I&amp;rsquo;ve chosen to create an array of &lt;em&gt;tuples&lt;/em&gt; that I loop over and use to extract actual &lt;code&gt;Zoo&lt;/code&gt; information when I call &lt;code&gt;NSEntityDescription.insertNewObjectForEntityForName()&lt;/code&gt;. It just looked nice to me to have two chunks of code to analyze inside the function: One (the array of tuples) to see all of the zoo information I plan to insert, and another (the for-loop) to do the actual inserting into the data store. Adding more &lt;code&gt;Zoo&lt;/code&gt;s in the future would mean simply adding another tuple to the array. Convenient!&lt;/li&gt;
&lt;li&gt;Since I&amp;rsquo;ve &lt;a href=&#34;http://www.andrewcbancroft.com/2014/07/17/implement-nsmanagedobject-subclass-in-swift/&#34;&gt;created NSManagedObject subclasses&lt;/a&gt; for my entities, I can cast the result of &lt;code&gt;NSEntityDescription.insertNewObjectForEntityForName()&lt;/code&gt; to an actual &lt;code&gt;Zoo&lt;/code&gt;, to work with the properties directly.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;a name=&#34;data-helper-print-all-zoos&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;printallzoos&#34;&gt;printAllZoos()&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;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;DataHelper&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 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&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;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;printAllZoos&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;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;zooFetchRequest&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;Zoo&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;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;primarySortDescriptor&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSSortDescriptor&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;key&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;name&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ascending&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&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;n&#34;&gt;zooFetchRequest&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;sortDescriptors&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;primarySortDescriptor&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;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;allZoos&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;k&#34;&gt;try&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;zooFetchRequest&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;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;Zoo&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;k&#34;&gt;for&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;zoo&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;in&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;allZoos&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;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;Zoo Name: &lt;/span&gt;&lt;span class=&#34;si&#34;&gt;\(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;zoo&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;si&#34;&gt;)&lt;/span&gt;&lt;span class=&#34;se&#34;&gt;\n&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;Location: &lt;/span&gt;&lt;span class=&#34;si&#34;&gt;\(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;zoo&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;location&lt;/span&gt;&lt;span class=&#34;si&#34;&gt;)&lt;/span&gt;&lt;span class=&#34;s&#34;&gt; &lt;/span&gt;&lt;span class=&#34;se&#34;&gt;\n&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;-------&lt;/span&gt;&lt;span class=&#34;se&#34;&gt;\n&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;terminator&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;&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;p&#34;&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 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;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;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;code&gt;printAllZoos()&lt;/code&gt; function utilizes a standard &lt;code&gt;NSFetchRequest&lt;/code&gt;, along with an &lt;code&gt;NSSortDescriptor&lt;/code&gt;. Check out my &lt;a href=&#34;http://www.andrewcbancroft.com/2015/02/18/core-data-cheat-sheet-for-swift-ios-developers/&#34;&gt;cheat sheet&lt;/a&gt; for more examples of common Core Data operations.&lt;/p&gt;
&lt;p&gt;Once again, having the &lt;code&gt;NSManagedObject&lt;/code&gt; subclass in place allows me to cast the result of the fetch request to a &lt;code&gt;[Zoo]&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;data-helper-seed-animals&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;seedanimals&#34;&gt;seedAnimals()&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;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;DataHelper&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 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&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;private&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;seedAnimals&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;// Grab Classifications&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;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;classificationFetchRequest&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;Classification&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;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;allClassifications&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;k&#34;&gt;try&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;classificationFetchRequest&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;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;Classification&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;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;manatee&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;allClassifications&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;bp&#34;&gt;filter&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;({(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;c&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Classification&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;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;n&#34;&gt;c&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;Trichechidae&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;12&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;p&#34;&gt;}).&lt;/span&gt;&lt;span class=&#34;bp&#34;&gt;first&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;// Same pattern for monkey and bat&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;// Grab Zoos&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;17&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;zooFetchRequest&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;Zoo&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;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;allZoos&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;k&#34;&gt;try&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;zooFetchRequest&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;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;Zoo&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&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;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;oklahomaCityZoo&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;allZoos&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;bp&#34;&gt;filter&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;z&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Zoo&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;21&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;z&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;s&#34;&gt;&amp;#34;Oklahoma City Zoo&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;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;bp&#34;&gt;first&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;c1&#34;&gt;// Same pattern for San Diego Zoo and Lowry Park Zoo&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;25&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;26&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;c1&#34;&gt;// Create an array of &amp;#34;animal&amp;#34; tuples, assigning&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;27&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;c1&#34;&gt;// whatever Classification and Zoo make sense&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;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;animals&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;29&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;commonName&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;Pygmy Fruit-eating Bat&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;habitat&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;Flying Mamals Exhibit&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;classification&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;bat&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;!,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;zoos&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSSet&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;array&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;lowryParkZoo&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;!,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;oklahomaCityZoo&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;!,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;sanDiegoZoo&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;30&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;commonName&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;Mantled Howler&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;habitat&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;Primate Exhibit&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;classification&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;monkey&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;!,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;zoos&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSSet&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;array&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;sanDiegoZoo&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;!,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;lowryParkZoo&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;31&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;commonName&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;Geoffroy’s Spider Monkey&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;habitat&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;Primate Exhibit&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;classification&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;monkey&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;!,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;zoos&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSSet&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;array&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;sanDiegoZoo&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;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;s&#34;&gt;&amp;#34;West Indian Manatee&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;habitat&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;Aquatic Mamals Exhibit&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;classification&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;manatee&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;!,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;zoos&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSSet&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;array&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;lowryParkZoo&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;p&#34;&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&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 class=&#34;c1&#34;&gt;// Create -actual- Animal instances and insert them&lt;/span&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;k&#34;&gt;for&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;animal&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;in&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;animals&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;37&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;newAnimal&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;Animal&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;context&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;Animal&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;newAnimal&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;n&#34;&gt;animal&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&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;n&#34;&gt;newAnimal&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;habitat&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;animal&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;habitat&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;n&#34;&gt;newAnimal&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;classification&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;animal&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;classification&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;41&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;            &lt;span class=&#34;n&#34;&gt;newAnimal&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;zoos&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;animal&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;zoos&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;42&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;43&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;44&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;45&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;context&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;save&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;46&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;kc&#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;47&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;48&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;49&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;50&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;51&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;52&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;code&gt;seedAnimals()&lt;/code&gt; is the most complicated piece of the whole scenario because it depends on entities that have been previously inserted into the data store. An &lt;code&gt;Animal&lt;/code&gt;, which is a member of some &lt;code&gt;Classification&lt;/code&gt; lives in some habitat at a &lt;code&gt;Zoo&lt;/code&gt;. So in order to get a complete &lt;code&gt;Animal&lt;/code&gt; into the data store, we need to have a &lt;code&gt;Classification&lt;/code&gt; and one or more &lt;code&gt;Zoo&lt;/code&gt; to assign it.&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;ve chosen to go ahead and execute fetch requests for the entities I made in previous &lt;code&gt;seed___()&lt;/code&gt; functions. Additionally, rather than deal with &lt;code&gt;NSPredicate&lt;/code&gt;, I just grab &lt;em&gt;all&lt;/em&gt; &lt;code&gt;Classifications&lt;/code&gt; and &lt;em&gt;all&lt;/em&gt; &lt;code&gt;Zoos&lt;/code&gt;, and use array&amp;rsquo;s &lt;code&gt;filter&lt;/code&gt; function to get the exact Entity I want.&lt;/p&gt;
&lt;p&gt;The last thing I&amp;rsquo;ll mention / warn against, is that I &lt;em&gt;am&lt;/em&gt; using explicitly unwrapped optionals in this function. I&amp;rsquo;ve gone ahead and &amp;ldquo;risked it”, trusting that I&amp;rsquo;m only using this technique during development time to help me see how things will look in my UI. In other words, this isn&amp;rsquo;t code that will end up in Production. It&amp;rsquo;s simply meant to help me while I&amp;rsquo;m developing, so I&amp;rsquo;ve gone ahead and done things the quick way here.&lt;/p&gt;
&lt;p&gt;The rest of the function follows the same patterns that have already been used in this example.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;app-delegate&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;appdelegateswift&#34;&gt;AppDelegate.swift&lt;/h3&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;application&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;application&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIApplication&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;didFinishLaunchingWithOptions&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;launchOptions&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;NSObject&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;-&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; 2&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;c1&#34;&gt;// Override point for customization after application launch.&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;dataHelper&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;DataHelper&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;kc&#34;&gt;self&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; 5&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;dataHelper&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;seedDataStore&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;n&#34;&gt;dataHelper&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;printAllZoos&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;n&#34;&gt;dataHelper&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;printAllClassifications&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;dataHelper&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;printAllAnimals&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;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;kc&#34;&gt;true&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;Above is a peek at what my AppDelegate.swift file&amp;rsquo;s &lt;code&gt;application:didFinishLaunchingWithOptions:&lt;/code&gt; function looks like. Nothing fancy going on here. I&amp;rsquo;m simply initializing a &lt;code&gt;DataHelper&lt;/code&gt; instance with the &lt;code&gt;NSManagedObjectContext&lt;/code&gt; instance that&amp;rsquo;s created in this class, and calling the seed and print functions I defined earlier.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;starting-fresh&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;starting-fresh-every-time&#34;&gt;Starting fresh every time&lt;/h3&gt;
&lt;p&gt;I&amp;rsquo;ve found that sometimes it helps to have a freshly seeded data store every time I the app while I&amp;rsquo;m in development mode. When I&amp;rsquo;m testing the UI, I may create new entities during my manual testing, but one of the convenient things about seeding the data store is that I don&amp;rsquo;t &lt;em&gt;have&lt;/em&gt; to. And even if I did, it&amp;rsquo;s often quite nice to start fresh each run. To do this we&amp;rsquo;ll dive into some of the boilerplate code that Xcode generates for us when we choose to use Core Data when we create the project. Specifically, we&amp;rsquo;ll target the &lt;code&gt;persistentStoreCoordinator&lt;/code&gt; closure:&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;lazy&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;var&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;p&#34;&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;// The persistent store coordinator for the application. This implementation creates and returns a coordinator, having added the store for the application to it. This property is optional since there are legitimate error conditions that could cause the creation of the store to fail.&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;// Create the coordinator and store&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;coordinator&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;kc&#34;&gt;self&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 hl&#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;url&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;applicationDocumentsDirectory&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;URLByAppendingPathComponent&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;Zootastic.sqlite&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 hl&#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 hl&#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;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 hl&#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;try&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSFileManager&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;defaultManager&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;().&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;removeItemAtURL&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&gt;&lt;/span&gt;&lt;span class=&#34;line hl&#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;k&#34;&gt;catch&lt;/span&gt; &lt;span class=&#34;kc&#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 hl&#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&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;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;failureReason&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;There was an error creating or loading the application&amp;#39;s saved data.&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;14&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;15&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;coordinator&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;NSSQLiteStoreType&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;n&#34;&gt;url&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;16&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;17&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;            &lt;span class=&#34;c1&#34;&gt;// Report any error we got.&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;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;dict&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;nb&#34;&gt;String&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&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;dict&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;NSLocalizedDescriptionKey&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;Failed to initialize the application&amp;#39;s saved data&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;20&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;            &lt;span class=&#34;n&#34;&gt;dict&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;NSLocalizedFailureReasonErrorKey&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;failureReason&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;n&#34;&gt;dict&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;NSUnderlyingErrorKey&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;error&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;as&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSError&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;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;wrappedError&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;n&#34;&gt;domain&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;YOUR_ERROR_DOMAIN&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;code&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;9999&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;userInfo&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;dict&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;c1&#34;&gt;// Replace this with code to handle the error appropriately.&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;c1&#34;&gt;// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.&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;NSLog&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;Unresolved error &lt;/span&gt;&lt;span class=&#34;si&#34;&gt;\(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;wrappedError&lt;/span&gt;&lt;span class=&#34;si&#34;&gt;)&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;, &lt;/span&gt;&lt;span class=&#34;si&#34;&gt;\(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;wrappedError&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;userInfo&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 class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;27&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;            &lt;span class=&#34;n&#34;&gt;abort&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;p&#34;&gt;}&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&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;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;coordinator&lt;/span&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;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&amp;rsquo;ve highlighted the key line that I added (everything else was already in place, generated for me by Xcode). Adding that line removes the sqlite database. The lines that follow add it back in a fresh state.&lt;/p&gt;
&lt;h3 id=&#34;wrapping-up&#34;&gt;Wrapping up&lt;/h3&gt;
&lt;p&gt;Designing an application&amp;rsquo;s UI can be difficult without actual data to present. During development, it&amp;rsquo;s often convenient to seed a Core Data database with sample data so that we can preview how it&amp;rsquo;ll look in our application. Along with that, it&amp;rsquo;s nice to start with a fresh copy of the data each time we run the app. We explored how to accomplish this task in Swift!&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/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;http://www.andrewcbancroft.com/2015/03/05/displaying-data-with-nsfetchedresultscontroller-and-swift/&#34; title=&#34;Displaying Data With NSFetchedResultsController and Swift&#34;&gt;Displaying Data With NSFetchedResultsController and 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/2015/05/28/sync-table-view-data-nsfetchedresultscontroller-swift/&#34; title=&#34;Sync Table View Data: NSFetchedResultsController and Swift&#34;&gt;Sync Table View Data: NSFetchedResultsController and Swift&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;
&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>Core Data Cheat Sheet for Swift iOS Developers</title>
      <link>https://www.andrewcbancroft.com/2015/02/18/core-data-cheat-sheet-for-swift-ios-developers/</link>
      <pubDate>Wed, 18 Feb 2015 18:20:59 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2015/02/18/core-data-cheat-sheet-for-swift-ios-developers/</guid>
      <description>&lt;p&gt;&lt;small&gt;Updated on July 27, 2016 – Additional Cheats&lt;/small&gt;&lt;/p&gt;
&lt;p&gt;Having trouble recalling how to perform basic Core Data operations? This cheat sheet is a handy reference to keep you productive with Core Data and Swift!&lt;/p&gt;
&lt;p&gt;The code snippets below are here to help jog your memory when it&amp;rsquo;s been a while since you&amp;rsquo;ve worked in Core Data. They could also be helpful for newcomers to iOS development, Core Data, and Swift.&lt;/p&gt;
&lt;p&gt;One assumption I&amp;rsquo;m making in this post is that you&amp;rsquo;ve created NSManagedObject subclasses for your entities to make them easier to work with in a type-safe way. If you need help getting started with that, I&amp;rsquo;ve &lt;a href=&#34;http://www.andrewcbancroft.com/2014/07/17/implement-nsmanagedobject-subclass-in-swift/&#34;&gt;written a walk-through&lt;/a&gt; to guide you through that process.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;querying&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;querying&#34;&gt;Querying&lt;/h2&gt;
&lt;p&gt;&lt;a name=&#34;fetch-all&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;fetch-all-entities&#34;&gt;Fetch all entities&lt;/h4&gt;
&lt;p&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;9
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;// Assuming type has a reference to managed object context&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;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;MyEntity&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;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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;fetchedEntities&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;try&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;managedObjectContext&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;k&#34;&gt;as&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;MyEntity&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;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Do something with fetchedEntities&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Do something in response to error condition&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;a name=&#34;fetch-max-n&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;fetch-maximum-of-n-entities&#34;&gt;Fetch maximum of N entities&lt;/h4&gt;
&lt;p&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;// Assuming type has a reference to managed object context&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;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;MyEntity&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;cl&#34;&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;fetchLimit&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;10&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;fetchedEntities&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;try&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;managedObjectContext&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;k&#34;&gt;as&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;MyEntity&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;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Do something with fetchedEntities&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Do something in response to error condition&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;a name=&#34;insert&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;insert-a-new-entity&#34;&gt;Insert a new entity&lt;/h2&gt;
&lt;p&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;// Assuming encapsulating Type has a reference to managed object context&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;newEntity&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;MyEntity&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;kc&#34;&gt;self&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;k&#34;&gt;as&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;!&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;MyEntity&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;// Set properties&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;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;cl&#34;&gt;    &lt;span class=&#34;k&#34;&gt;try&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;managedObjectContext&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;save&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;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;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Do something in response to error condition&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;a name=&#34;update-single-entity&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;update-a-single-entity&#34;&gt;Update a single entity&lt;/h2&gt;
&lt;p&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;12
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;13
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;14
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;15
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;16
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;17
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;18
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;19
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;20
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;21
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;22
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;23
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;24
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;// Assuming type has a reference to managed object context&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;// Assuming that a specific NSManagedObject&amp;#39;s objectID property is accessible&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;// Alternatively, could supply a predicate expression that&amp;#39;s precise enough&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;// to select only a _single_ entity&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;predicate&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSPredicate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;format&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;objectID == %@&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;objectIDFromNSManagedObject&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;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;MyEntity&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;cl&#34;&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;predicate&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;predicate&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;fetchedEntities&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;try&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;managedObjectContext&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;k&#34;&gt;as&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;MyEntity&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;cl&#34;&gt;    &lt;span class=&#34;n&#34;&gt;fetchedEntities&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;bp&#34;&gt;first&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;?.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;FirstPropertyToUpdate&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NewValue&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;n&#34;&gt;fetchedEntities&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;bp&#34;&gt;first&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;?.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;SecondPropertyToUpdate&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NewValue&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// ... Update additional properties with new values&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Do something in response to error condition&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;    &lt;span class=&#34;k&#34;&gt;try&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;managedObjectContext&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;save&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;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;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Do something in response to error condition&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;a name=&#34;update-multiple-entities&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;update-multiple-entities&#34;&gt;Update multiple-entities&lt;/h2&gt;
&lt;p&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;12
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;13
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;14
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;15
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;16
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;17
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;18
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;19
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;20
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;21
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;22
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;23
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;24
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;// Assuming type has a reference to managed object context&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;predicate&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSPredicate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;format&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;MyEntityAttribute == %@&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;Matching Value&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;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;MyEntity&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;cl&#34;&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;predicate&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;predicate&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;fetchedEntities&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;try&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;managedObjectContext&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;k&#34;&gt;as&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;MyEntity&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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;k&#34;&gt;for&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;entity&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;in&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;fetchedEntities&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;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;entity&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;FirstPropertyToUpdate&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NewValue&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;entity&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;SecondPropertyToUpdate&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NewValue&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;c1&#34;&gt;// ... Update additional properties with new values&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;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;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Do something in response to error condition&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;    &lt;span class=&#34;k&#34;&gt;try&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;managedObjectContext&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;save&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;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;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Do something in response to error condition&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;a name=&#34;delete-single-entity&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;delete-a-single-entity&#34;&gt;Delete a single entity&lt;/h2&gt;
&lt;p&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;12
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;13
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;14
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;15
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;16
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;17
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;18
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;19
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;20
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;21
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;22
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;23
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;24
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;// Assuming type has a reference to managed object context&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;// Assuming that a specific NSManagedObject&amp;#39;s objectID property is accessible&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;// Alternatively, could supply a predicate expression that&amp;#39;s precise enough&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;// to select only a _single_ entity&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;predicate&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSPredicate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;format&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;objectID == %@&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;objectIDFromNSManagedObject&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;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;MyEntity&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;cl&#34;&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;predicate&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;predicate&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;fetchedEntities&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;try&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;managedObjectContext&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;k&#34;&gt;as&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;MyEntity&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;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;entityToDelete&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;fetchedEntities&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;bp&#34;&gt;first&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;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;managedObjectContext&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;deleteObject&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;entityToDelete&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;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;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;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Do something in response to error condition&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;    &lt;span class=&#34;k&#34;&gt;try&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;managedObjectContext&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;save&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;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;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Do something in response to error condition&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;a name=&#34;delete-multiple-entities&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;delete-multiple-entities&#34;&gt;Delete multiple-entities&lt;/h2&gt;
&lt;p&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;12
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;13
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;14
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;15
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;16
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;17
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;18
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;19
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;20
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;21
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;22
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;// Assuming type has a reference to managed object context&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;predicate&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSPredicate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;format&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;MyEntityAttribute == %@&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;Matching Value&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;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;MyEntity&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;cl&#34;&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;predicate&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;predicate&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;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;cl&#34;&gt;    &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;fetchedEntities&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;try&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;managedObjectContext&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;k&#34;&gt;as&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;MyEntity&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;cl&#34;&gt;    
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;k&#34;&gt;for&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;entity&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;in&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;fetchedEntities&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;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;managedObjectContext&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;deleteObject&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;entity&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;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;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;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Do something in response to error condition&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;    &lt;span class=&#34;k&#34;&gt;try&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;managedObjectContext&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;save&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;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;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// Do something in response to error condition&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;a name=&#34;migrate-automatically&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;migrate-core-data-model-with-automatic-migrations&#34;&gt;Migrate Core Data Model with Automatic Migrations&lt;/h2&gt;
&lt;p&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;6
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#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;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;model&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;// set up model&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;pscOptions&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;NSMigratePersistentStoresAutomaticallyOption&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;NSInferMappingModelAutomaticallyOption&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&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&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;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;psc&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;model&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;cl&#34;&gt;&lt;span class=&#34;k&#34;&gt;try&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;!&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;psc&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;addPersistentStore&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;ofType&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NSSQLiteStoreType&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;configurationName&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;at&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;storeURL&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;pscOptions&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;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&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/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;/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>Custom UITableViewCell for Text Input in Swift</title>
      <link>https://www.andrewcbancroft.com/2015/02/12/custom-uitableviewcell-text-input-swift/</link>
      <pubDate>Thu, 12 Feb 2015 13:00:50 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2015/02/12/custom-uitableviewcell-text-input-swift/</guid>
      <description>&lt;p&gt;The need to collect data via text input is a common in many applications. This walk-through showcases how to create a custom &lt;code&gt;UITableViewCell&lt;/code&gt; for accepting text input within a &lt;code&gt;UITableView&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;tableview-data-entry&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;table-views-for-data-entry&#34;&gt;Table Views for Data Entry?&lt;/h3&gt;
&lt;p&gt;Table views provide nice, built-in styles that present a form-like view for collecting data from your users. They also have inherent scrolling capabilities. Finally, there is some handy keyboard handling, such as auto-scrolling to avoid covering up a data entry cell, or hiding the keyboard when the user scrolls the Table View. Both of those are challenging to get right if you were to do implement a data entry form some other way.&lt;/p&gt;
&lt;p&gt;Many of Apple&amp;rsquo;s own UIs utilize table views to collect data from the user (think Settings, Calendar, Reminders).&lt;/p&gt;
&lt;p&gt;For those reasons, I think a table view is a really convenient choice for collecting data from a user.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;step-by-step&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;step-by-step-walkthrough&#34;&gt;Step by Step Walkthrough&lt;/h3&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;fab fa-github fa-lg&#34;&gt;&lt;/i&gt; &lt;a href=&#34;https://github.com/andrewcbancroft/CustomTextInputTableViewCell&#34;title=&#34;Custom Text Input TableView Cell Example&#34;&gt;Example Xcode Project&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;
&lt;p&gt;&lt;a name=&#34;drag-tableview&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;drag-a-table-view-to-storyboard&#34;&gt;Drag a table view to Storyboard&lt;/h4&gt;
&lt;p&gt;Assuming that you have a View Controller already on the Storyboard design surface, drag a table view onto the Storyboard scene.&lt;br&gt;
&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/02/Main_storyboard_%E2%80%94_Edited_and_Edit_Post_%E2%80%B9_Andrew_Bancroft_%E2%80%94_WordPress_and_AddVerseControllerTests_swift.png&#34;&gt;&lt;img src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/02/Main_storyboard_—_Edited_and_Edit_Post_‹_Andrew_Bancroft_—_WordPress_and_AddVerseControllerTests_swift-1024x569.png&#34; alt=&#34;Drag Table View to Storyboard&#34; width=&#34;1024&#34; height=&#34;569&#34; class=&#34;alignnone size-large wp-image-11324&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/02/Main_storyboard_—_Edited_and_Edit_Post_‹_Andrew_Bancroft_—_WordPress_and_AddVerseControllerTests_swift-1024x569.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2015/02/Main_storyboard_—_Edited_and_Edit_Post_‹_Andrew_Bancroft_—_WordPress_and_AddVerseControllerTests_swift-300x167.png 300w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;data-source-delegate&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;wire-up-table-view-data-source-and-delegate&#34;&gt;Wire up table view data source and delegate&lt;/h4&gt;
&lt;p&gt;For this step it&amp;rsquo;s helpful to have the document outline pane visible. Selecting the Table View from the document outline and subsequently Control + Click + Dragging to the View Controller icon on the Storyboard scene will allow you to link the table view&amp;rsquo;s dataSource and delegate properties to the View Controller.&lt;br&gt;
&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/02/Ctrl-Click-Drag-to-set-DataSource-and-Delegate.png&#34;&gt;&lt;img src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/02/Ctrl-Click-Drag-to-set-DataSource-and-Delegate-1024x507.png&#34; alt=&#34;Ctrl+Click-Drag to set DataSource and Delegate&#34; width=&#34;1024&#34; height=&#34;507&#34; class=&#34;alignnone size-large wp-image-11683&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/02/Ctrl-Click-Drag-to-set-DataSource-and-Delegate-1024x507.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2015/02/Ctrl-Click-Drag-to-set-DataSource-and-Delegate-300x148.png 300w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/02/Set-DataSource-and-Delegate.png&#34;&gt;&lt;img src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/02/Set-DataSource-and-Delegate-1024x508.png&#34; alt=&#34;Set DataSource and Delegate&#34; width=&#34;1024&#34; height=&#34;508&#34; class=&#34;alignnone size-large wp-image-11354&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/02/Set-DataSource-and-Delegate-1024x508.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2015/02/Set-DataSource-and-Delegate-300x149.png 300w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;&lt;/a&gt;&lt;br&gt;
Note that you&amp;rsquo;ll need to perform the Control + Click + Drag maneuver twice to set both the dataSource and the delegate.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;dismiss-keyboard&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;configure-keyboard-dismissal-for-table-view&#34;&gt;Configure keyboard dismissal for table view&lt;/h4&gt;
&lt;p&gt;With the attributes inspector selected in the utilities pane, scroll down to the Keyboard option and set it to &amp;ldquo;Dismiss on drag”.&lt;br&gt;
&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/02/Configure-Keyboard-Dismissal1.png&#34;&gt;&lt;img src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/02/Configure-Keyboard-Dismissal1-1024x509.png&#34; alt=&#34;Configure Keyboard Dismissal&#34; width=&#34;1024&#34; height=&#34;509&#34; class=&#34;alignnone size-large wp-image-11357&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/02/Configure-Keyboard-Dismissal1-1024x509.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2015/02/Configure-Keyboard-Dismissal1-300x149.png 300w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;&lt;/a&gt;&lt;br&gt;
This can be helpful if you&amp;rsquo;d like the keyboard to automatically hide itself when the user scrolls the table view.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;constraints&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;set-constraints-increase-prototype-cells-count&#34;&gt;Set constraints, increase prototype cells count&lt;/h4&gt;
&lt;p&gt;Use the document outline to set constraints. With the attributes inspector selected in the utilities pane, select &amp;ldquo;Dynamic Prototypes” for the Content property, and set the Prototype Count property to 1 (or more, depending on your needs).&lt;br&gt;
&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/02/Set-Constraints-and-Prototype-Cells1.png&#34;&gt;&lt;img src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/02/Set-Constraints-and-Prototype-Cells1-1024x508.png&#34; alt=&#34;Set Constraints and Prototype Cells&#34; width=&#34;1024&#34; height=&#34;508&#34; class=&#34;alignnone size-large wp-image-11352&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/02/Set-Constraints-and-Prototype-Cells1-1024x508.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2015/02/Set-Constraints-and-Prototype-Cells1-300x149.png 300w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;&lt;/a&gt;&lt;br&gt;
Configuring constraints on the table view will ensure that it displays properly on all device sizes and orientations.&lt;/p&gt;
&lt;p&gt;In this particular example, there&amp;rsquo;s only one kind of prototype cell that I want the table view to display. If I had multiple prototypes, I&amp;rsquo;d increase the prototype cells count to match the number of prototypes I had.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;drag-text-field&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;drag-text-field-to-cell-increase-text-field-width&#34;&gt;Drag text field to cell, increase text field width&lt;/h4&gt;
&lt;p&gt;Drag a text field into the cell&amp;rsquo;s contents and use Xcode&amp;rsquo;s blue guides to adjust the width so that it takes up the whole cell.&lt;br&gt;
&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/02/Main_storyboard_%E2%80%94_Edited_and_Main_storyboard_1.png&#34;&gt;&lt;img src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/02/Main_storyboard_—_Edited_and_Main_storyboard_1-1024x622.png&#34; alt=&#34;Drag Text Field to Cell&#34; width=&#34;1024&#34; height=&#34;622&#34; class=&#34;alignnone size-large wp-image-11325&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/02/Main_storyboard_—_Edited_and_Main_storyboard_1-1024x622.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2015/02/Main_storyboard_—_Edited_and_Main_storyboard_1-300x182.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2015/02/Main_storyboard_—_Edited_and_Main_storyboard_1.png 1488w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;&lt;/a&gt;&lt;br&gt;
Now comes the customization of the prototype cell itself. Since we&amp;rsquo;re designing it for text input, we&amp;rsquo;ll use a text field as the contents of the table view cell prototype.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;configure-text-field-constraints&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;configure-text-field-constraints-and-border&#34;&gt;Configure Text Field Constraints and Border&lt;/h4&gt;
&lt;p&gt;Use the document outline to set constraints for the text field. With the text field selected, and the attributes inspector selected in the utilities pane, set the Border Style property to the style you prefer.&lt;br&gt;
&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/02/Configure-Text-Field-Constraints-and-Border.png&#34;&gt;&lt;img src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/02/Configure-Text-Field-Constraints-and-Border-1024x508.png&#34; alt=&#34;Configure Text Field Constraints and Border&#34; width=&#34;1024&#34; height=&#34;508&#34; class=&#34;alignnone size-large wp-image-11350&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/02/Configure-Text-Field-Constraints-and-Border-1024x508.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2015/02/Configure-Text-Field-Constraints-and-Border-300x149.png 300w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;&lt;/a&gt;&lt;br&gt;
Configuring constraints on the text field will ensure that it displays properly on all device sizes and orientations.&lt;/p&gt;
&lt;p&gt;Depending on your taste / needs, you can change the border style from rounded to none, or whichever other border style is most appealing for your app.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;text-input-cell-class&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;create-new-textinputtableviewcell-class&#34;&gt;Create new TextInputTableViewCell class&lt;/h4&gt;
&lt;p&gt;Add a new .swift file named TextInputTableViewCell to your project. Create a new class in that file called &lt;code&gt;TextInputTableViewCell&lt;/code&gt; which inherits from &lt;code&gt;UITableViewCell&lt;/code&gt;.&lt;br&gt;
&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/02/Main_storyboard_and_Main_storyboard.png&#34;&gt;&lt;img src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/02/Main_storyboard_and_Main_storyboard-1024x567.png&#34; alt=&#34;Create new class - TextInputTableViewCell&#34; width=&#34;1024&#34; height=&#34;567&#34; class=&#34;alignnone size-large wp-image-11330&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/02/Main_storyboard_and_Main_storyboard-1024x567.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2015/02/Main_storyboard_and_Main_storyboard-300x166.png 300w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;ib-outlet&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;create-an-iboutlet-between-the-text-field-and-the-textinputtableviewcell-class&#34;&gt;Create an IBOutlet between the text field and the TextInputTableViewCell class&lt;/h4&gt;
&lt;p&gt;Use the document outline to Control + Click + Drag and IBOutlet to the &lt;code&gt;TextInputTableViewCell&lt;/code&gt; class.&lt;br&gt;
&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/02/Create-IBOutlet1.png&#34;&gt;&lt;img src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/02/Create-IBOutlet1-1024x512.png&#34; alt=&#34;Create IBOutlet&#34; width=&#34;1024&#34; height=&#34;512&#34; class=&#34;alignnone size-large wp-image-11684&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/02/Create-IBOutlet1-1024x512.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2015/02/Create-IBOutlet1-300x150.png 300w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;implement-class&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;implement-textinputtableviewcell-class&#34;&gt;Implement TextInputTableViewCell class&lt;/h4&gt;
&lt;p&gt;An example implementation with a function for configuring the text field&amp;rsquo;s properties 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;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;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;TextInputTableViewCell&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 class=&#34;p&#34;&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;textField&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UITextField&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;kd&#34;&gt;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;configure&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;nb&#34;&gt;String&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;?,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;placeholder&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; 7&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;textField&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;n&#34;&gt;text&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;textField&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;placeholder&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;placeholder&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;n&#34;&gt;textField&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;accessibilityValue&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&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;textField&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;accessibilityLabel&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;placeholder&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;&lt;a name=&#34;storyboard-setup&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;change-cell-class-in-storyboard-to-textinputtableviewcell&#34;&gt;Change cell class in Storyboard to TextInputTableViewCell&lt;/h4&gt;
&lt;p&gt;Use the document outline to select the Table View Cell. With the identity inspector selected in the utilities pane, change the Class property to your TextInputTableViewCell class.&lt;br&gt;
&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/02/Main_storyboard_%E2%80%94_Edited_and_Main_storyboard.png&#34;&gt;&lt;img src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/02/Main_storyboard_—_Edited_and_Main_storyboard-1024x513.png&#34; alt=&#34;Change Cell Class to TextInputCell&#34; width=&#34;1024&#34; height=&#34;513&#34; class=&#34;alignnone size-large wp-image-11328&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/02/Main_storyboard_—_Edited_and_Main_storyboard-1024x513.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2015/02/Main_storyboard_—_Edited_and_Main_storyboard-300x150.png 300w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;reuse-identifier&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;set-reuse-identifier&#34;&gt;Set reuse identifier&lt;/h4&gt;
&lt;p&gt;With the Table View Cell still selected in the document outline, select the attribute inspector in the utilities pane and change the Identifier property to &amp;ldquo;TextInputCell” (or some other string that&amp;rsquo;s easily remembered).&lt;br&gt;
&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/02/Main_storyboard_%E2%80%94_Edited_2.png&#34;&gt;&lt;img src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/02/Main_storyboard_—_Edited_2-1024x511.png&#34; alt=&#34;Set Reuse Identifier&#34; width=&#34;1024&#34; height=&#34;511&#34; class=&#34;alignnone size-large wp-image-11323&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/02/Main_storyboard_—_Edited_2-1024x511.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2015/02/Main_storyboard_—_Edited_2-300x150.png 300w&#34; sizes=&#34;(max-width: 1024px) 100vw, 1024px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;implement-datasource-delegate&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;implement-table-view-data-source-and-delegate-methods&#34;&gt;Implement table view data source and delegate methods&lt;/h4&gt;
&lt;p&gt;In the View Controller, specify that it adopts the &lt;code&gt;UITableViewDataSource&lt;/code&gt; and &lt;code&gt;UITableViewDelegate&lt;/code&gt; protocols. Implement the appropriate protocol methods.&lt;br&gt;
I&amp;rsquo;ve written a &lt;a href=&#34;http://www.andrewcbancroft.com/2014/11/24/swift-uitableviewdatasource-cheat-sheet/&#34;&gt;cheat sheet&lt;/a&gt; for this, but I&amp;rsquo;ll also provide the following sample implementation of the mai &lt;code&gt;ViewController&lt;/code&gt; class:&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;public&lt;/span&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;UITableViewDataSource&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UITableViewDelegate&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;public&lt;/span&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; 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;mi&#34;&gt;1&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;// Create 1 row as an example&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;kd&#34;&gt;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;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; 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;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;TextInputCell&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;n&#34;&gt;TextInputTableViewCell&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;n&#34;&gt;cell&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;configure&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;&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;placeholder&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;Enter some text!&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;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;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;Lines 9 and 11 are of most importance. Notice that I&amp;rsquo;m dequeueing a TextInputCell, which is what I set the reuse identifier of my cell to be. I&amp;rsquo;m also casting the dequeued cell as a TextInputTableViewCell so that it can be configured (line 11)&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;run-in-simulator&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;run-in-simulator&#34;&gt;Run in simulator&lt;/h4&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/02/Simulator-Output.png&#34;&gt;&lt;img src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/02/Simulator-Output.png&#34; alt=&#34;Simulator Output&#34; width=&#34;473&#34; height=&#34;869&#34; class=&#34;alignnone size-full wp-image-11365&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/02/Simulator-Output.png 473w, https://www.andrewcbancroft.com/wp-content/uploads/2015/02/Simulator-Output-163x300.png 163w&#34; sizes=&#34;(max-width: 473px) 100vw, 473px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;wrapping-up&#34;&gt;Wrapping up&lt;/h3&gt;
&lt;p&gt;In this walkthrough, my goal was to show how to take advantage of a table view&amp;rsquo;s inherent styles, scrolling capabilities, and keyboard handling to make form-like text input easier. We created a custom subclass of UITableViewCell to accomplish this task. Take a look at the &lt;a href=&#34;https://github.com/andrewcbancroft/CustomTextInputTableViewCell&#34;&gt;example published to GitHub&lt;/a&gt; to dive in further and explore the walkthrough in more depth.&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>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>Converting Complex Objective-C Macros to Swift Functions</title>
      <link>https://www.andrewcbancroft.com/2015/01/29/converting-complex-objective-c-macros-swift-functions/</link>
      <pubDate>Thu, 29 Jan 2015 13:00:51 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2015/01/29/converting-complex-objective-c-macros-swift-functions/</guid>
      <description>&lt;p&gt;The question of how to convert &lt;code&gt;#define&lt;/code&gt; macros from Objective-C to Swift is explained fairly simply in the [Apple developer documentation on the subject][1]. For &lt;em&gt;simple&lt;/em&gt; macros, it&amp;rsquo;s a matter of rewriting them as global constants. In fact, if you&amp;rsquo;re using the hybrid Objective-C — Swift approach to writing your app, Swift sees those simple macros and automatically makes them available to your Swift code. I also gave some tips on the [alternative to Objective-C macros][2] a while back.&lt;/p&gt;
&lt;p&gt;Where we run into trouble is when we need to port &lt;em&gt;complex&lt;/em&gt; Objective-C macros to Swift. According to the [same documentation from Apple][1],&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Complex macros are used in C and Objective-C but have no counterpart in Swift.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Yikes!&lt;/p&gt;
&lt;p&gt;Thankfully there &lt;em&gt;is&lt;/em&gt; a silver lining after that scary first sentence:&lt;/p&gt;
&lt;p&gt;[&lt;img src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/01/complex_macros.png&#34; alt=&#34;Complex Macros Explanation&#34; width=&#34;670&#34; height=&#34;168&#34; class=&#34;alignnone size-full wp-image-11214&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/01/complex_macros.png 670w, https://www.andrewcbancroft.com/wp-content/uploads/2015/01/complex_macros-300x75.png 300w&#34; sizes=&#34;(max-width: 670px) 100vw, 670px&#34; /&gt;][3]&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;In Swift, you can use functions and generics to achieve the same results without any compromises.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;That makes sense, actually! Complex Objective-C macros tend to look a &lt;em&gt;lot&lt;/em&gt; like functions, so the transition to Swift was straightforward in a case I ran across recently.&lt;/p&gt;
&lt;h2 id=&#34;two-examples&#34;&gt;Two Examples:&lt;/h2&gt;
&lt;h3 id=&#34;a-simple-example&#34;&gt;A simple example&lt;/h3&gt;
&lt;p&gt;What could we do in Swift to convert an Objective-C macro that looks something like this?&lt;/p&gt;
&lt;pre class=&#34;lang:objc decode:true &#34; &gt;#define SQUARE_NUMBER(n) n * n
```

One thing we could do is write a function that produces the same thing:

```swift
func squareNumber(n: Int) -&gt; Int {
    return n * n
}
```

### A little more complicated

An example situation that came to me on Twitter took the form of converting a macro that was a little more complicated than the simple example just presented. The input to the complex macro was a color, represented as a hexadecimal value, along with an alpha, represented as a float. The output? A `UIColor` instance based on some bitwise manipulations to that hex value.

I&#39;ve created a GitHub example if you&#39;d like to play around with everything. The relevant code is reproduced below&amp;#8230;

&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;fab fa-github fa-lg&#34;&gt;&lt;/i&gt; &lt;a href=&#34;https://github.com/andrewcbancroft/ConvertComplexMacroExample&#34; onclick=&#34;_gaq.push([&#39;_trackEvent&#39;, &#39;outbound-article&#39;, &#39;https://github.com/andrewcbancroft/ConvertComplexMacroExample&#39;, &#39;Example Xcode Project&#39;]);&#34; title=&#34;Convert Complex Macro Example Project&#34;&gt;Example Xcode Project&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;

The macro form looked like this:

&lt;pre class=&#34;lang:objc decode:true &#34; &gt;#define UIColorFromRGB(rgbValue, alphaValue) \
[UIColor colorWithRed:((float)((rgbValue &gt;&gt; 16) &amp; 0xFF))/255.0 \
green:((float)((rgbValue &gt;&gt; 8) &amp; 0xFF))/255.0 \
blue:((float)((rgbValue &gt;&gt; 0) &amp; 0xFF))/255.0 \
alpha:alphaValue]
```

Rewriting it as a Swift function:

```swift
func UIColorFromRGB(rgb: Int, alpha: Float) -&gt; UIColor {
    let red = CGFloat(Float(((rgb&gt;&gt;16) &amp; 0xFF)) / 255.0)
    let green = CGFloat(Float(((rgb&gt;&gt;8) &amp; 0xFF)) / 255.0)
    let blue = CGFloat(Float(((rgb&gt;&gt;0) &amp; 0xFF)) / 255.0)
    let alpha = CGFloat(alpha)
    
    return UIColor(red: red, green: green, blue: blue, alpha: alpha)
}
```

The main thing to keep in mind is that the output of the macro/function is the focus. The internals could change to better-adapt to Swift&#39;s features if you desire. If the macro was ugly inside, make it nice in Swift!

### Where should the function go?

  * For organization&#39;s sake, you could create a new .swift file and place the function inside it at the global level. This would provide the most convenient transition for your Objective-C to Swift conversion, because `#defines` were available wherever you imported the Objective-C header file.
  * Alternatively, you could encapsulate the function in a class/struct/enum.

### Wrapping up

With the power of Swift functions and the ability to even declare and use them globally, converting complex macros to a better Swift alternative is much less daunting than you might expect.

 [1]: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/InteractingWithCAPIs.html#//apple_ref/doc/uid/TP40014216-CH8-XID_20
 [2]: http://www.andrewcbancroft.com/2014/10/01/swift-alternative-to-objective-c-macros/
 [3]: http://www.andrewcbancroft.com/wp-content/uploads/2015/01/complex_macros.png</description>
    </item>
    
    <item>
      <title>Conveniently Transforming Immutable Types in Swift</title>
      <link>https://www.andrewcbancroft.com/2015/01/20/conveniently-transforming-immutable-types-swift/</link>
      <pubDate>Tue, 20 Jan 2015 12:30:51 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2015/01/20/conveniently-transforming-immutable-types-swift/</guid>
      <description>&lt;p&gt;A few weeks ago I wrote about &lt;a href=&#34;http://www.andrewcbancroft.com/2015/01/06/immutable-types-changing-state-swift/&#34;&gt;Immutable Types and Changing State in Swift&lt;/a&gt;, where I hoped to convey an &amp;ldquo;aha!-moment” that happened for me.&lt;/p&gt;
&lt;p&gt;Since then, I&amp;rsquo;ve learned a bit more. For example, the technique I presented for transforming instances of a Type immutably actually come for free when you use a value Type, such as a Struct! Check out &lt;a href=&#34;https://twitter.com/NatashaTheRobot&#34;&gt;@NatashaTheRobot&amp;rsquo;s&lt;/a&gt; writeup on the subject, titled &lt;a href=&#34;http://natashatherobot.com/mutating-functions-swift-structs/&#34;&gt;&amp;ldquo;Mutating Functions in Swift Structs”&lt;/a&gt;, for more information.&lt;/p&gt;
&lt;p&gt;But let&amp;rsquo;s say, for whatever reason, you&amp;rsquo;d like to use a &lt;em&gt;reference&lt;/em&gt; Type, such as a Class. In that case, the technique I presented in the aforementioned blog entry works out quite nicely. &lt;em&gt;Until….&lt;/em&gt;&lt;/p&gt;
&lt;h3 id=&#34;many-init-parameters--pain&#34;&gt;Many init parameters == Pain&lt;/h3&gt;
&lt;p&gt;… It works great right up until you have more than a few immutable properties that you need to transform.&lt;/p&gt;
&lt;p&gt;I want to thank &lt;a href=&#34;https://twitter.com/Jarsen&#34;&gt;@Jarsen&lt;/a&gt; for his &lt;a href=&#34;http://www.andrewcbancroft.com/2015/01/06/immutable-types-changing-state-swift/#comment-1788688298&#34;&gt;comment&lt;/a&gt;. He pointed out the exact pain point I was feeling, since I was actually using my own advice in a personal project. Not only that, he offers a solution in the form of a &lt;a href=&#34;https://gist.github.com/jarsen/41de7401d49cd2348e5f&#34;&gt;GitHub gist&lt;/a&gt;!&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;m bringing in his example so that we have it before us with a few minor modifications to make it relevant for this blog entry. However, I want to give 100% credit to Jarsen for his insight.&lt;/p&gt;
&lt;h3 id=&#34;its-all-about-convenience&#34;&gt;It&amp;rsquo;s all about convenience&lt;/h3&gt;
&lt;p&gt;The gist of Jarsen&amp;rsquo;s solution was to create a second helper initializer which would help setting the values for all the properties easier. 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;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;Scorekeeper&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;runningScore&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;Int&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 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;climbingScore&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;Int&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 4&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// potentially more properties&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;init&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;runningScore&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;mi&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;climbingScore&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;mi&#34;&gt;0&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;runningScore&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;runningScore&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;climbingScore&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;climbingScore&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;// second helper initializer&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;init&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;scoreKeeper&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Scorekeeper&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;runningScore&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;Int&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;?&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span 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;climbingScore&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;Int&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;?&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span 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;13&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;runningScore&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;runningScore&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;??&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;scoreKeeper&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;runningScore&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;climbingScore&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;climbingScore&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;??&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;scoreKeeper&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;climbingScore&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;incrementRunningScoreBy&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;points&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;n&#34;&gt;Scorekeeper&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;18&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Scorekeeper&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;scoreKeeper&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;runningScore&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;runningScore&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;+&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;points&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;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;// other functions to transform Scorekeeper by incrementing other score properties&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Note the use of optionals, and the corresponding nil-coalescing operator (&lt;code&gt;??&lt;/code&gt;) in the helper initializer&amp;rsquo;s implementation. It&amp;rsquo;s simple, &lt;em&gt;and&lt;/em&gt; it&amp;rsquo;s concise. I like it!&lt;/p&gt;
&lt;p&gt;The bottom line is that I couldn&amp;rsquo;t help but share Jarsen&amp;rsquo;s tip. I thought it deserved a little more attention than to be stuck down in the comment section where folks may or may not find it and be helped.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Fix MAMP, MySQL Workbench “Failed to Connect to MySQL”</title>
      <link>https://www.andrewcbancroft.com/2015/01/19/fix-mamp-mysql-workbench-failed-connect-mysql/</link>
      <pubDate>Mon, 19 Jan 2015 18:40:12 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2015/01/19/fix-mamp-mysql-workbench-failed-connect-mysql/</guid>
      <description>&lt;p&gt;Installing &lt;a href=&#34;http://www.mamp.info/&#34;&gt;MAMP&lt;/a&gt; was easy. I did, however, spend at least 45 minutes trying to get MySQL Workbench to connect to my local MySQL database server. The solution was simple, but getting there led me around the Internet and back with little help. Hopefully this signpost will help others having connection issues with MySQL Workbench to MAMP MySQL servers as well.&lt;/p&gt;
&lt;p&gt;Once MAMP is installed and the MySQL server has started, you&amp;rsquo;ll be taken to a start screen that looks something like this:&lt;br&gt;
&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/01/MAMP_StartScreen.png&#34;&gt;&lt;img src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/01/MAMP_StartScreen.png&#34; alt=&#34;MAMP Start Screen Port Number&#34; width=&#34;601&#34; height=&#34;411&#34; class=&#34;alignnone size-full wp-image-11187&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/01/MAMP_StartScreen.png 601w, https://www.andrewcbancroft.com/wp-content/uploads/2015/01/MAMP_StartScreen-300x205.png 300w&#34; sizes=&#34;(max-width: 601px) 100vw, 601px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The information presented there is misleading.&lt;/strong&gt; The start page instructs you to use port 3306. So I did:&lt;br&gt;
&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/01/SettingsBasedOnStartScreen.png&#34;&gt;&lt;img src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/01/SettingsBasedOnStartScreen.png&#34; alt=&#34;Settings based on MAMP Start Screen&#34; width=&#34;897&#34; height=&#34;515&#34; class=&#34;alignnone size-full wp-image-11185&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/01/SettingsBasedOnStartScreen.png 897w, https://www.andrewcbancroft.com/wp-content/uploads/2015/01/SettingsBasedOnStartScreen-300x172.png 300w&#34; sizes=&#34;(max-width: 897px) 100vw, 897px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;When testing the connection, however, I got the dreaded &amp;ldquo;Failed to Connect to MySQL at localhost”:&lt;br&gt;
&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/01/ErrorOnTestConnection.png&#34;&gt;&lt;img src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/01/ErrorOnTestConnection.png&#34; alt=&#34;Error on Test Connection&#34; width=&#34;412&#34; height=&#34;146&#34; class=&#34;alignnone size-full wp-image-11184&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/01/ErrorOnTestConnection.png 412w, https://www.andrewcbancroft.com/wp-content/uploads/2015/01/ErrorOnTestConnection-300x106.png 300w&#34; sizes=&#34;(max-width: 412px) 100vw, 412px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;It turns out that MAMP has a preferences panel wherein you can configure the ports for your Apache and MySQL servers. And of course, MySQL was &lt;em&gt;not&lt;/em&gt; set to use port 3306, as the start page led me to believe. Rather, it was set to use port 8889:&lt;br&gt;
&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/01/MAMP_Preferences_Port.png&#34;&gt;&lt;img src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/01/MAMP_Preferences_Port.png&#34; alt=&#34;MAMP Preferences Port Number&#34; width=&#34;529&#34; height=&#34;400&#34; class=&#34;alignnone size-full wp-image-11186&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/01/MAMP_Preferences_Port.png 529w, https://www.andrewcbancroft.com/wp-content/uploads/2015/01/MAMP_Preferences_Port-300x227.png 300w&#34; sizes=&#34;(max-width: 529px) 100vw, 529px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Adjusting the settings to use the port listed in the MAMP preferences pane led to a successful connection!&lt;br&gt;
&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/01/SettingsBasedOnMAMPPreferences1.png&#34;&gt;&lt;img src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/01/SettingsBasedOnMAMPPreferences1.png&#34; alt=&#34;Settings based on MAMP Preferences&#34; width=&#34;897&#34; height=&#34;515&#34; class=&#34;alignnone size-full wp-image-11198&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/01/SettingsBasedOnMAMPPreferences1.png 897w, https://www.andrewcbancroft.com/wp-content/uploads/2015/01/SettingsBasedOnMAMPPreferences1-300x172.png 300w&#34; sizes=&#34;(max-width: 897px) 100vw, 897px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Good to go!&lt;br&gt;
&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/01/GoodToGo.png&#34;&gt;&lt;img src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2015/01/GoodToGo.png&#34; alt=&#34;Good to go!&#34; width=&#34;414&#34; height=&#34;144&#34; class=&#34;alignnone size-full wp-image-11190&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2015/01/GoodToGo.png 414w, https://www.andrewcbancroft.com/wp-content/uploads/2015/01/GoodToGo-300x104.png 300w&#34; sizes=&#34;(max-width: 414px) 100vw, 414px&#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>Agile Blogging</title>
      <link>https://www.andrewcbancroft.com/2014/11/20/agile-blogging/</link>
      <pubDate>Fri, 21 Nov 2014 01:58:04 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2014/11/20/agile-blogging/</guid>
      <description>&lt;p&gt;After I wrote my post on &lt;a href=&#34;http://www.andrewcbancroft.com/2014/10/23/7-reasons-you-should-write-about-swift/&#34; title=&#34;7 Reasons You Should Write About Swift&#34;&gt;7 Reasons You Should Write About Swift&lt;/a&gt;, I began to think about some of the roadblocks that hinder blogging.&lt;/p&gt;
&lt;h3 id=&#34;blog-blockers&#34;&gt;Blog Blockers&lt;/h3&gt;
&lt;p&gt;I started to think about some of my &lt;em&gt;own&lt;/em&gt; reservations about blogging, and boiled it down to a couple of big blockers:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Time&lt;/li&gt;
&lt;li&gt;Perfectionism&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;My problems with blogging in the past often came down to the fact that &lt;strong&gt;it takes time&lt;/strong&gt;, and that if was going to publish something, &lt;strong&gt;it had to be perfect&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Of course, every one of us is time-bound, and every one of us has the potential to improve, no matter how &amp;ldquo;perfect” we think we&amp;rsquo;ve made something.&lt;/p&gt;
&lt;p&gt;It&amp;rsquo;s interesting to me, because these issues come up in software development a &lt;em&gt;lot&lt;/em&gt;, and one of the tools that much of the software development community has embraced are &amp;ldquo;Agile” practices for software development.&lt;/p&gt;
&lt;p&gt;I really enjoy many of aspects of Agile (&lt;a href=&#34;http://www.scrum.org&#34;&gt;Scrum&lt;/a&gt; being the specific flavor of Agile practices that I&amp;rsquo;ve had experience with), particularly as it relates to enabling the rapid delivery of a useful product that satisfies my customers, and the ability to adapt to change and make iterative improvements on that product.&lt;/p&gt;
&lt;h3 id=&#34;agile-blogging&#34;&gt;Agile &lt;em&gt;Blogging&lt;/em&gt;&lt;/h3&gt;
&lt;p&gt;I began to think, &amp;ldquo;What if some of the things that I enjoy about Agile could be applied to my blogging routine?” How could that positively impact my experience, so as to motivate me to blog more and remove those road blocks that hindered me before?&lt;/p&gt;
&lt;p&gt;If I had to pick two key phrases from what I like about Scrum that could apply to blogging, they&amp;rsquo;d be &amp;ldquo;&lt;strong&gt;rapid delivery of a useful product&lt;/strong&gt;” and &amp;ldquo;&lt;strong&gt;iterative improvements&lt;/strong&gt;&amp;rdquo;.&lt;/p&gt;
&lt;p&gt;Rapid, because what I really want is to produce &lt;em&gt;useful&lt;/em&gt; content &lt;em&gt;quickly&lt;/em&gt;. (Notice I said &amp;ldquo;useful”, not &amp;ldquo;perfect”.) And that&amp;rsquo;s where &amp;ldquo;iterative improvements” comes into play. Since anything I publish will &lt;em&gt;always&lt;/em&gt; have the potential to be improved, it makes better sense for me to prioritize publishing something that has potential to be &lt;em&gt;useful&lt;/em&gt;, with the mindset of cycling back around to improve that publication later over time.&lt;/p&gt;
&lt;h3 id=&#34;getting-practical&#34;&gt;Getting Practical&lt;/h3&gt;
&lt;p&gt;Over the past several months, I&amp;rsquo;ve been working to set blogging goals for myself using Agile principles to help me meet those goals and improve my content over time. Here&amp;rsquo;s how I, personally, get pragmatic with Agile blogging:&lt;/p&gt;
&lt;h4 id=&#34;set-a-goal&#34;&gt;Set a goal&lt;/h4&gt;
&lt;p&gt;Without a goal, I find it very easy to not blog, even though I enjoy it.&lt;/p&gt;
&lt;p&gt;My goal right now is simple but sustainable: I shoot for producing one blog post a week.&lt;/p&gt;
&lt;h4 id=&#34;block-off-time&#34;&gt;Block off time&lt;/h4&gt;
&lt;p&gt;I set aside chunks of time throughout my week to blog. Each time block is about 30 minutes in duration. To produce a post that I&amp;rsquo;m pretty satisfied with (ie, it&amp;rsquo;s useful and contributes something meaningful to my audience), I use about 2 to 3 of those time blocks per week, but almost never all in one sitting.&lt;/p&gt;
&lt;h4 id=&#34;write-publish-improve&#34;&gt;Write, publish, improve&lt;/h4&gt;
&lt;p&gt;My first-priority goal with each post is to get across that main point as clearly as possible. After a quick proofread, I schedule version 1.0 to publish.&lt;/p&gt;
&lt;p&gt;Of course there are going to be mistakes with that initial publish. I almost immediately circle back around and view the live post, noting areas of potential improvement. If I still have time in my 30-minute block, I make the improvements I can and publish them. If I don&amp;rsquo;t, I wait until my next blogging time-block, and make the improvements then.&lt;/p&gt;
&lt;p&gt;Since no post will ever be perfect, I work to make things as useful and meaningful as possible, minimizing error as best I can, of course. Sometimes feedback from my readers helps me find places where I can improve. I value that feedback, and often bump that advice to the top of my priority list for a given post. The point is that I write, publish, then &lt;em&gt;improve iteratively&lt;/em&gt;.&lt;/p&gt;
&lt;h4 id=&#34;examples-and-supplementary-content&#34;&gt;Examples and supplementary content&lt;/h4&gt;
&lt;p&gt;I write a lot about programming, and there are often times where a post could be enhanced with an example. I try my best not to pressure myself to build &lt;em&gt;everything&lt;/em&gt; for version 1.0 of the post. If I think a full-fledged example that someone can compile and run on their own machine would enhance things and provide additional value to my content, I&amp;rsquo;ll usually add that &amp;ldquo;feature” to a post &lt;em&gt;after&lt;/em&gt; the initial version in most cases.&lt;/p&gt;
&lt;p&gt;Doing it this way does a couple of things for me:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;It lets me get the main substance of the post out there sooner.&lt;/li&gt;
&lt;li&gt;It provides an opportunity to inform folks on my social media outlets one more time about the post, along with the enhancement.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&#34;summary&#34;&gt;Summary&lt;/h3&gt;
&lt;p&gt;Blogging with Agile in mind has been a significant help for my motivation to blog and has served to keep me on track with my one post a week goal. My hope is that by sharing how Agile principles have impacted me, you, too, will be inspired and empowered to start creating great content for us to enjoy! Happy (Agile) blogging!&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/10/23/7-reasons-you-should-write-about-swift/&#34; title=&#34;7 Reasons You Should Write About Swift&#34;&gt;7 Reasons You Should Write About Swift&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;
</description>
    </item>
    
    <item>
      <title>Distinguishing Between Multiple UIActionSheets with Swift</title>
      <link>https://www.andrewcbancroft.com/2014/11/16/distinguishing-between-multiple-uiactionsheets-with-swift/</link>
      <pubDate>Mon, 17 Nov 2014 04:17:39 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2014/11/16/distinguishing-between-multiple-uiactionsheets-with-swift/</guid>
      <description>&lt;p&gt;The challenge when dealing with the presentation and handling of more than one &lt;code&gt;UIActionSheet&lt;/code&gt; in a single View Controller is made clear by asking, &amp;ldquo;How am I going to tell which action sheet I&amp;rsquo;m dealing with so that I can handle the user&amp;rsquo;s choice appropriately?”&lt;/p&gt;
&lt;p&gt;Presumably, the user&amp;rsquo;s interaction with one of the action sheets will be different than the other(s), so you&amp;rsquo;ll need to think through how to distinguish between them, in order to respond to that interaction appropriately.&lt;/p&gt;
&lt;p&gt;I faced such a scenario in a recent project, and I thought I&amp;rsquo;d share my solution. Check out my &lt;a href=&#34;https://github.com/andrewcbancroft/DistinguishingActionSheetsExample&#34; title=&#34;GitHub Example&#34;&gt;GitHub example&lt;/a&gt; to dive in if you prefer learning by exploration!&lt;/p&gt;
&lt;h3 id=&#34;using-uiview-tags&#34;&gt;Using UIView Tags&lt;/h3&gt;
&lt;p&gt;Essentially, I chose to make use of the &lt;code&gt;tag&lt;/code&gt; property, which all UIView subclasses inherit. The &lt;a href=&#34;https://developer.apple.com/library/iOS//documentation/UIKit/Reference/UIView_Class/index.html#//apple_ref/occ/instp/UIView/tag&#34; title=&#34;UIView Class Reference - Tag Property&#34;&gt;UIView Class Reference documentation specifies&lt;/a&gt; that the &lt;code&gt;tag&lt;/code&gt; property can be used to identify the view at runtime:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;You can set the value of this tag and use that value to identify the view later.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3 id=&#34;example&#34;&gt;Example&lt;/h3&gt;
&lt;p&gt;Here&amp;rsquo;s a quick example showing the setting of this property so that the action sheet can be differentiated when it comes time to handle the user&amp;rsquo;s choice:&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;actionSheet1&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIActionSheet&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;actionSheet1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;tag&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;0&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;// set other properties, such as delegate, as well as buttons...&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;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;actionSheet2&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIActionSheet&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;actionSheet2&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;tag&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;1&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;// set other properties, such as delegate, as well as buttons...&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id=&#34;better-solution-in-swift&#34;&gt;Better Solution in Swift?&lt;/h3&gt;
&lt;p&gt;Simple enough, right? Here&amp;rsquo;s my only problem with the above implementation if I&amp;rsquo;m using Swift: We have language features available to us that allow us to avoid setting the tag property to the integer value in-line like I did in this code snippet.&lt;/p&gt;
&lt;p&gt;Here, instead of assigning in-line, I&amp;rsquo;m going to refactor and employ a Swift enumeration to help &lt;em&gt;name&lt;/em&gt; the tag. Underneath, there will still be integers involved, but my goal in using an enumeration is two-fold:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Identify the action sheet in code with a name. This should help my code be more coherent and readable.&lt;/li&gt;
&lt;li&gt;Encapsulate a single source of truth for the tag values and avoid &amp;ldquo;magic integers” in my code. While the integers assigned to the tags can be arbitrary, if they ever &lt;em&gt;do&lt;/em&gt; need to be changed, I change the enumeration, rather than changing each place where the tag is set or checked to perform branching logic.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The arguments are fundamentally the same as those I made when I &lt;a href=&#34;http://www.andrewcbancroft.com/2014/09/02/replace-magic-strings-with-enumerations-in-swift/&#34; title=&#34;Replace Magic Strings with Enumerations in Swift&#34;&gt;wrote about replacing magic strings in Swift&lt;/a&gt; and implement precisely the same strategy.&lt;/p&gt;
&lt;h3 id=&#34;refactored-example&#34;&gt;Refactored Example&lt;/h3&gt;
&lt;p&gt;The refactored version of the code snippet previously presented could look like this, then:&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;ActionSheetTag&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&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;// Integer values will be implicitly supplied; you could optionally set your own values&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;ActionSheet1&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;ActionSheet2&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 5&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 6&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 7&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;actionSheet1&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIActionSheet&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;n&#34;&gt;actionSheet1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;tag&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ActionSheetTag&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;ActionSheet1&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; 9&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;c1&#34;&gt;// set other properties, such as delegate, as well as buttons...&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;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;actionSheet2&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIActionSheet&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;actionSheet2&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;tag&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ActionSheetTag&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;ActionSheet2&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;13&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;c1&#34;&gt;// set other properties, such as delegate, as well as buttons...&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;It&amp;rsquo;s worth noting that rather than using an enumeration, I could have chosen to define a couple of constants at a scope visible to both my setting of the tag, and the conditional logic I&amp;rsquo;d use in my UIActionSheetDelegate callback. The end goal and result would be the same: Clarity, achieved by assigning &lt;em&gt;names&lt;/em&gt; to the tags, and the avoidance of &amp;ldquo;magic integers” appearing in my code.&lt;/p&gt;
&lt;h3 id=&#34;wrapping-up--uiactionsheetdelegate-implementation&#34;&gt;Wrapping Up – UIActionSheetDelegate Implementation&lt;/h3&gt;
&lt;p&gt;To see how to perform the conditional logic needed in the UIActionSheetDelegate callback method, take a look at this final code snippet:&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;actionSheet&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;actionSheet&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;UIActionSheet&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;clickedButtonAtIndex&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;buttonIndex&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;Int&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 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;tag&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ActionSheetTag&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;actionSheet&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;tag&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;tag&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;ActionSheet1&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;n&#34;&gt;handleActionSheet1Interaction&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;// Function definition omitted for brevity, but ideally you&amp;#39;d implement a function to do something in response to the user&amp;#39;s interaction with ActionSheet 1&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;ActionSheet2&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;handleActionSheet2Interaction&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;// // Function definition omitted for brevity, but ideally you&amp;#39;d implement a function to do something in response to the user&amp;#39;s interaction with ActionSheet 2&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;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; 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;Unknown action sheet.&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;p&#34;&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;h3 id=&#34;summary&#34;&gt;Summary&lt;/h3&gt;
&lt;p&gt;I&amp;rsquo;ve done it before, but by using Swift enumerations again in a new context, I was able to provide better clarity to the intention of my code when I had multiple UIActionSheet instances that I needed to handle in a single view controller. In addition to this clarity, I also encapsulated a single source of truth for identifying my UIActionSheet instances, rather than placing &amp;ldquo;magic integers” throughout my code. Have a look at (and play with) &lt;a href=&#34;https://github.com/andrewcbancroft/DistinguishingActionSheetsExample&#34; title=&#34;GitHub Example&#34;&gt;the example I&amp;rsquo;ve put up on GitHub&lt;/a&gt; for further study and improvement!&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/09/02/replace-magic-strings-with-enumerations-in-swift/&#34; title=&#34;Replace Magic Strings with Enumerations in Swift&#34;&gt;Replace Magic Strings with Enumerations in Swift&lt;/a&gt;
    &lt;/li&gt;
    &lt;li&gt;
      &lt;a href=&#34;http://www.andrewcbancroft.com/2014/10/01/swift-alternative-to-objective-c-macros/&#34; title=&#34;Swift Alternative to Objective-C Macros&#34;&gt;Swift Alternative to Objective-C Macros&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&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>Up and Running with Unity IOC Container for ASP.Net MVC</title>
      <link>https://www.andrewcbancroft.com/2014/10/27/up-and-running-with-unity-ioc-container-for-asp-net-mvc/</link>
      <pubDate>Mon, 27 Oct 2014 18:07:21 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2014/10/27/up-and-running-with-unity-ioc-container-for-asp-net-mvc/</guid>
      <description>&lt;p&gt;The process for creating an ASP.Net MVC web application that relies on the Unity Inversion of Control (IOC) container has gotten quite a bit more streamlined since I first started working with it.  However, since I don’t start brand new software projects often, it’s often the case that it takes me a few minutes to figure out which NuGet package to install to get things up and running. &lt;/p&gt;
&lt;p&gt;The following is a quick-reference for myself, and for anyone else who’s interested in getting up and running quickly with the Unity IOC container for an ASP.Net MVC project.&lt;/p&gt;
&lt;h3 id=&#34;assumptions&#34;&gt;Assumptions:&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Using Visual Studio 2013
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Using ASP.Net MVC 5&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Have already created a new, fresh solution (File –&amp;gt; New Project, etc)&lt;/ul&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;walkthrough&#34;&gt;Walkthrough&lt;/h3&gt;
&lt;p&gt;Begin by right-clicking your solution, and choosing to “Manage NuGet Packages for Solution…”:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2014/10/image.png&#34;&gt;&lt;img title=&#34;image&#34; style=&#34;border-left-width: 0px; border-right-width: 0px; border-bottom-width: 0px; display: inline; border-top-width: 0px&#34; border=&#34;0&#34; alt=&#34;image&#34; src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2014/10/image_thumb.png&#34; width=&#34;504&#34; height=&#34;367&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Next, choose to search for packages found at nuget.org.  In the search bar in the upper right, type in “Unity”, and press enter.  Choose to install “Unity bootstrapper for ASP.NET MVC”:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2014/10/SNAGHTMLb1d5c8.png&#34;&gt;&lt;img title=&#34;SNAGHTMLb1d5c8&#34; style=&#34;border-left-width: 0px; border-right-width: 0px; border-bottom-width: 0px; display: inline; border-top-width: 0px&#34; border=&#34;0&#34; alt=&#34;SNAGHTMLb1d5c8&#34; src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2014/10/SNAGHTMLb1d5c8_thumb.png&#34; width=&#34;504&#34; height=&#34;325&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2014/10/image1.png&#34;&gt;&lt;img title=&#34;image&#34; style=&#34;border-top: 0px; border-right: 0px; border-bottom: 0px; border-left: 0px; display: inline&#34; border=&#34;0&#34; alt=&#34;image&#34; src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2014/10/image_thumb1.png&#34; width=&#34;309&#34; height=&#34;398&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Now that the appropriate NuGet package is installed, you’re set to start using the Unity IOC Container. &lt;/p&gt;
&lt;h3 id=&#34;using-unity-for-dependency-injection-and-management&#34;&gt;Using Unity for Dependency Injection and Management&lt;/h3&gt;
&lt;p&gt;I’ve contrived an example and tried to show side-by-side views of the bare minimums you need to program in order to start taking advantage of Unity for managing your dependencies.   I’m going to show how to inject a concrete implementation of a very simple Interface into the HomeController. The steps go something like this:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Create an Interface (in this example, I called it IUnityExample) which specifies that an implementer of this interface must define an itWorksMessage method that returns a string.
&lt;ul&gt;
&lt;li&gt;Create a new class called UnityExample.   Specify that it implements IUnityExample in the class declaration line.  Implement the required itWorksMessage method.
&lt;ul&gt;
&lt;li&gt;In HomeController.cs, declare a private variable to hold an instance of an IUnityExample.
&lt;ul&gt;
&lt;li&gt;In HomeController.cs, declare a new constructor which takes an IUnityExample (to be injected by Unity) and sets the privately declared IUnityExample variable to the instance that’s passed in.
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Utilize the itWorksMessage somehow.  In my example, I set the return value to a new dynamic property called ViewBag.Message inside the Index method.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;In UnityConfig.cs, register IUnityExample and UnityExample with the container inside the RegisterTypes method.&lt;/ol&gt;
Let’s pause here and show that side-by-side view I referred to a second ago.  Perhaps visualizing the files in play will help bring the whole picture into view:&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2014/10/image5.png&#34;&gt;&lt;img title=&#34;image&#34; style=&#34;border-top: 0px; border-right: 0px; border-bottom: 0px; border-left: 0px; display: inline&#34; border=&#34;0&#34; alt=&#34;image&#34; src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2014/10/image_thumb5.png&#34; width=&#34;720&#34; height=&#34;308&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;At this point, we’re done with writing C# code, and we’re ready to move into the View layer.  This final step will access ViewBag.Message, and display it at the bottom of the page.  It’s fairly straight-forward.  Inside Views –&amp;gt; Home –&amp;gt; Index.cshtml, simply add a &lt;div&gt; that contains @ViewBag.Message:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2014/10/image3.png&#34;&gt;&lt;img title=&#34;image&#34; style=&#34;border-top: 0px; border-right: 0px; border-bottom: 0px; border-left: 0px; display: inline&#34; border=&#34;0&#34; alt=&#34;image&#34; src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2014/10/image_thumb3.png&#34; width=&#34;624&#34; height=&#34;217&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;To confirm everything works, run your solution and verify the results!&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2014/10/image4.png&#34;&gt;&lt;img title=&#34;image&#34; style=&#34;border-top: 0px; border-right: 0px; border-bottom: 0px; border-left: 0px; display: inline&#34; border=&#34;0&#34; alt=&#34;image&#34; src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2014/10/image_thumb4.png&#34; width=&#34;720&#34; height=&#34;263&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;summary&#34;&gt;Summary&lt;/h3&gt;
&lt;p&gt;In this article, you’ve seen how to use NuGet to install the required components to get up and running with the Unity Inversion of Control container.  Additionally, you saw a bare-bones example demonstrating the steps required to start using Unity to manage your dependencies with ASP.Net MVC.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
</description>
    </item>
    
    <item>
      <title>7 Reasons You Should Write About Swift</title>
      <link>https://www.andrewcbancroft.com/2014/10/23/7-reasons-you-should-write-about-swift/</link>
      <pubDate>Thu, 23 Oct 2014 12:00:13 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2014/10/23/7-reasons-you-should-write-about-swift/</guid>
      <description>&lt;p&gt;If Swift is your iOS/Mac development language of choice, my goal in this post is to persuade you that you should not only write Swift… you should write &lt;em&gt;about&lt;/em&gt; Swift. Here are 7 reasons (in the order in which they flowed out of my brain):&lt;/p&gt;
&lt;h3 id=&#34;1-swift-will-never-be-new-again&#34;&gt;1. Swift will never be new again.&lt;/h3&gt;
&lt;p&gt;How amazing is it that every single Swift developer (besides the ones at Apple) had the opportunity to &lt;em&gt;become&lt;/em&gt; a Swift developer &lt;strong&gt;simultaneously&lt;/strong&gt; when Apple announced the new language at WWDC in June of 2014. What an incredible opportunity!&lt;/p&gt;
&lt;p&gt;Swift is young, but it&amp;rsquo;s growing up fast! The content that&amp;rsquo;s published about Swift today is, in many cases, the first of its kind. &lt;em&gt;You&lt;/em&gt; could be that first publisher, helping newcomers to the language and to iOS/Mac development along. &lt;em&gt;You&lt;/em&gt; could be contributing the first thoughts, the first guides, the first perspectives to this new world.&lt;/p&gt;
&lt;p&gt;Has someone already beat you to the punch on something you&amp;rsquo;ve thought of writing about? Write it anyway! They didn&amp;rsquo;t share it the same way &lt;em&gt;you&lt;/em&gt; can share it with your unique writing style, background, illustrations, examples, etc. I can&amp;rsquo;t tell you how often I&amp;rsquo;ve read something on someone&amp;rsquo;s blog and it just didn&amp;rsquo;t click… But two or three blogs later, someone else had the perfect illustration that made total sense in my brain. You can be that someone else! &lt;em&gt;If&lt;/em&gt; you write, that is.&lt;/p&gt;
&lt;h3 id=&#34;2-you-know-something-i-dont-know&#34;&gt;2. You know something I don&amp;rsquo;t know.&lt;/h3&gt;
&lt;p&gt;Think you have nothing to share? Wrong. You do. &lt;em&gt;You know something I don&amp;rsquo;t know&lt;/em&gt; about Swift. You may know a &lt;em&gt;lot&lt;/em&gt; of somethings I don&amp;rsquo;t know about Swift. &lt;strong&gt;I want to learn from you&lt;/strong&gt;! Many others would join me in that sentiment. The greater the variety in terms of difficulty-level, content, and style, the better (in my opinion).&lt;/p&gt;
&lt;p&gt;Contribute to the learning of others – you&amp;rsquo;ll be glad you did (and we will, too)!&lt;/p&gt;
&lt;h3 id=&#34;3-writing-leads-to-better-understanding&#34;&gt;3. Writing leads to better understanding.&lt;/h3&gt;
&lt;p&gt;When you write about Swift, especially in the op-ed realm, you put yourself out there in what feels like a very vulnerable position. In some cases, you&amp;rsquo;ll be off-target. &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;I have been before&lt;/a&gt;. What I found is that folks were not hateful or insulting to me (after all, I&amp;rsquo;m just sharing bits of what I&amp;rsquo;m &lt;em&gt;learning&lt;/em&gt;). Comments led to very huge lightbulbs going off in my brain and &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;I wrote about those lightbulbs in a second post&lt;/a&gt;. I understand things better now that I&amp;rsquo;ve written, showed my lack of understanding, and written more, once I received constructive feedback that I needed from those who knew better than I.&lt;/p&gt;
&lt;p&gt;In other cases, you&amp;rsquo;ll write about Swift, and the very act of writing to help &lt;em&gt;others&lt;/em&gt; solidifies the concept you&amp;rsquo;re explaining in your &lt;em&gt;own&lt;/em&gt; mind. It&amp;rsquo;s astounding to me how the attempt to educate another illuminates things that could have never been illuminated any other way.&lt;/p&gt;
&lt;p&gt;Write what you know – it&amp;rsquo;s good for us, &lt;em&gt;and&lt;/em&gt; it&amp;rsquo;s good for you.&lt;/p&gt;
&lt;h3 id=&#34;4-writing-shapes-the-swift-developer-community&#34;&gt;4. Writing shapes the Swift developer community.&lt;/h3&gt;
&lt;p&gt;The Swift developer community is the first community that I&amp;rsquo;ve ever gotten involved in, and it&amp;rsquo;s truly been amazing to connect with other Swift developers. From Twitter and from direct contact through e-mail, I&amp;rsquo;ve had the opportunity to brainstorm with and learn from others who are also programming in Swift.&lt;/p&gt;
&lt;p&gt;The Swift developer community is new… it&amp;rsquo;s vibrant… and it&amp;rsquo;s active and waiting for &lt;em&gt;one more contributor&lt;/em&gt;. Will you be that new Twitter handle tweeting about Swift? Will you be that new blogger sharing your thoughts and ideas with the community? Join us. &lt;em&gt;Shape&lt;/em&gt; us.&lt;/p&gt;
&lt;h3 id=&#34;5-great-opportunities-may-lie-around-the-corner&#34;&gt;5. Great opportunities may lie around the corner.&lt;/h3&gt;
&lt;p&gt;Swift has brought me several great opportunities in terms of building business and building friendships. Who knows what&amp;rsquo;s next?&lt;/p&gt;
&lt;p&gt;Objective-C isn&amp;rsquo;t going anywhere, but neither is Swift! Employers, while not yet able to legitimately request Swift devs with 5+ years of experience (though some will undoubtedly try), will inevitably desire Swift developers. The sooner you embrace Swift, the more experience you&amp;rsquo;ll be able to report.&lt;/p&gt;
&lt;p&gt;But how amazing would it be to say to a future potential employer, &amp;ldquo;I&amp;rsquo;ve been writing Swift since June of 2014, but I&amp;rsquo;ve also been writing &lt;em&gt;about&lt;/em&gt; Swift at &lt;a href=&#34;https://www.myBlog.com&#34;&gt;www.myBlog.com&lt;/a&gt; for nearly as long.”?&lt;/p&gt;
&lt;p&gt;In my experience, Swift has also brought me new friendships and an expanded network. I &amp;ldquo;know” more Swift developers than I do developers of all other languages combined. I follow more Swift bloggers than any other language&amp;rsquo;s bloggers. I&amp;rsquo;d love to follow &lt;em&gt;your&lt;/em&gt; blog and engage with / share thoughts about what you&amp;rsquo;re learning.&lt;/p&gt;
&lt;p&gt;Who knows what else awaits? The potential for great opportunities are ripe right now. Join in!&lt;/p&gt;
&lt;h3 id=&#34;6-reading-is-more-time-efficient-for-many-write&#34;&gt;6. Reading is more time-efficient for many. Write!&lt;/h3&gt;
&lt;p&gt;I love screencasts. I listen to podcasts every time I&amp;rsquo;m in the car alone. But you know what? I can read a blog post or a transcript faster than I can watch a screencast or listen to a podcast. Written content about Swift has the potential to greatly increase the speed and efficiency at which others can solve their problems.&lt;/p&gt;
&lt;p&gt;Screencast – do it! Podcast – you bet!&lt;/p&gt;
&lt;p&gt;But don&amp;rsquo;t forget to write, too!&lt;/p&gt;
&lt;h3 id=&#34;7-your-written-words-will-outlast-you&#34;&gt;7. Your written words will outlast you&lt;/h3&gt;
&lt;p&gt;Not a shocker, obviously. This is the &amp;ldquo;leave a legacy” argument. One way to make a lasting impact is to write what you&amp;rsquo;re thinking and learning about this language that you&amp;rsquo;re intrigued by or maybe even passionate about.&lt;/p&gt;
&lt;p&gt;&lt;a title=&#34;Scott Hanselman&#34; href=&#34;http://www.hanselman.com&#34; target=&#34;_blank&#34;&gt;Scott Hanselman&lt;/a&gt; is one of my favorite people in the developer / educator world. &lt;a title=&#34;Scott Hanselman - Do They Deserve The Gift Of Your Keystrokes&#34; href=&#34;http://www.hanselman.com/blog/DoTheyDeserveTheGiftOfYourKeystrokes.aspx&#34; target=&#34;_blank&#34;&gt;He&amp;rsquo;s written about writing and impact, and he&amp;rsquo;s said it way better than I can&lt;/a&gt;. Essentially, his argument is that you&amp;rsquo;ve only got a finite number of keystrokes left in your life before you die. And &lt;a title=&#34;KeysLeft&#34; href=&#34;http://www.keysleft.com&#34; target=&#34;_blank&#34;&gt;he&amp;rsquo;s right&lt;/a&gt;. The point of the linked-to article is that if you desire your precious, remaining keystrokes to have the furthest reach and the highest potential impact, you should write… and not just write, but write on the web.&lt;/p&gt;
&lt;p&gt;Some love Stack Overflow. Others love forums, or Reddit, or whatever. I prefer blogging. &lt;em&gt;Whatever&lt;/em&gt; you favor, your writing on the web travels far and impacts many. I&amp;rsquo;ve received e-mails that have spawned blog posts and replies with link to the blog post, rather than a direct reply. Why? Well, if &lt;em&gt;one&lt;/em&gt; person is asking, others probably are too – why not put it out there for others to benefit from as well?!&lt;/p&gt;
&lt;h3 id=&#34;in-closing&#34;&gt;In Closing&lt;/h3&gt;
&lt;p&gt;I love reading about Swift and I&amp;rsquo;ve benefited from many in the community who are working hard to make programming in Swift a great experience for everyone.&lt;/p&gt;
&lt;p&gt;I enjoy writing about Swift, myself. I write about other things too, of course (see the right-hand sidebar). But for the 7 reasons just mentioned, I&amp;rsquo;ve been writing a lot on Swift these days, and I&amp;rsquo;ve really had a great time doing it.&lt;/p&gt;
&lt;p&gt;My hope is that this post has sparked some ideas in you, or perhaps even motivated you to get in the game and start contributing your share of bytes to the Swift conversation. &lt;a title=&#34;Twitter - @andrewcbancroft&#34; href=&#34;https://twitter.com/andrewcbancroft&#34; target=&#34;_blank&#34;&gt;Holler at me on Twitter&lt;/a&gt; if you&amp;rsquo;re currently publishing about Swift – I&amp;rsquo;d love to connect with what you&amp;rsquo;re creating!&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/11/20/agile-blogging/&#34; title=&#34;Agile Blogging&#34;&gt;Agile Blogging&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;</description>
    </item>
    
    <item>
      <title>Resolving “Variable used within its own initial value” Error in Swift</title>
      <link>https://www.andrewcbancroft.com/2014/10/20/resolving-variable-used-within-its-own-initial-value-error-in-swift-2/</link>
      <pubDate>Mon, 20 Oct 2014 13:50:52 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2014/10/20/resolving-variable-used-within-its-own-initial-value-error-in-swift-2/</guid>
      <description>&lt;p&gt;While experimenting with a few things today, I experienced this compiler error:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Variable used within its own initial value&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Let me describe the situation I was in…&lt;/p&gt;
&lt;p&gt;I was playing (in a playground, no less) with closures, trying to mimic some behavior that I&amp;rsquo;ve recently learned about in Scala.  Essentially, I was trying to implement a factorial function as a &lt;em&gt;closure&lt;/em&gt;, and I was trying to do it &lt;em&gt;recursively&lt;/em&gt; (that is, without using a for/while loop).  Here&amp;rsquo;s what I wanted to do:&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;factorial&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;n&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;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;if&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;n&lt;/span&gt; &lt;span class=&#34;p&#34;&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 class=&#34;p&#34;&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;mi&#34;&gt;1&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 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;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;n&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;*&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;factorial&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;n&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;-&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;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;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;If you&amp;rsquo;ve seen factorial before, the above implementation isn&amp;rsquo;t new.  The &amp;ldquo;base case” that will let the recursion stop is the expression &lt;code&gt;if (n == 0)&lt;/code&gt;, and the recursive case is in the &lt;code&gt;else&lt;/code&gt; block, where &lt;code&gt;factorial&lt;/code&gt; gets called &lt;em&gt;again&lt;/em&gt; within its own body&amp;rsquo;s definition.  Only problem is… this doesn&amp;rsquo;t work in Swift 1.0.&lt;/p&gt;
&lt;p&gt;Apparently, the closure (which is being initialized and assigned to the constant named &amp;ldquo;factorial”) hasn&amp;rsquo;t had a chance to fully initialize itself before the name &lt;code&gt;factorial&lt;/code&gt; is used within the body.&lt;/p&gt;
&lt;p&gt;The frustrating part is that &lt;a title=&#34;Rob Napier on Immutability and Swift&#34; href=&#34;http://robnapier.net/llama-calculus&#34; target=&#34;_blank&#34;&gt;I &lt;em&gt;really&lt;/em&gt; didn&amp;rsquo;t want to type the letters v-a-r&lt;/a&gt; to implement my solution.  But alas, as &lt;a title=&#34;Stack Overflow - Handle Closure Recursively&#34; href=&#34;http://stackoverflow.com/questions/25103534/how-to-handle-closure-recursivity&#34; target=&#34;_blank&#34;&gt;Stack Overflow&lt;/a&gt; says, the following solution to the &amp;ldquo;initial value” error works:&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;factorial&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;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&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;factorial&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;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;n&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;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; 4&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;n&lt;/span&gt; &lt;span class=&#34;p&#34;&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 class=&#34;p&#34;&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;mi&#34;&gt;1&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 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; 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;n&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;*&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;factorial&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;n&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;-&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;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; 8&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 9&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;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;n&#34;&gt;factorial&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;5&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;c1&#34;&gt;// Produces the correct result of 120&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Of course, there&amp;rsquo;s absolutely no reason for the implementation to be a closure – I was simply experimenting.  Here&amp;rsquo;s the solution that I actually prefer… a good ole named function 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;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;factorial&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;n&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; 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;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;n&lt;/span&gt; &lt;span class=&#34;p&#34;&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 class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 3&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;1&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 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; 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;n&#34;&gt;n&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;*&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;factorial&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;n&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;-&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;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; 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;n&#34;&gt;factorial&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;5&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;// Produces the correct result of 120&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt; &lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Rotate Animation in Swift</title>
      <link>https://www.andrewcbancroft.com/2014/10/15/rotate-animation-in-swift/</link>
      <pubDate>Thu, 16 Oct 2014 02:57:24 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2014/10/15/rotate-animation-in-swift/</guid>
      <description>&lt;p&gt;&lt;small&gt;Updated on December 14, 2015 – Swift 2.0 + new examples&lt;/small&gt;&lt;/p&gt;
&lt;p&gt;With this post, I intend to wrap up my series on animations as UIView Extensions in Swift… for now.  Truthfully, these ideas flowed out of a real-world app that I was working on, which required various simple animations (&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;fading in/out&lt;/a&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;sliding text&lt;/a&gt;, and now, rotating a view 360 degrees).&lt;/p&gt;
&lt;p&gt;Since I&amp;rsquo;ve given two other detailed walk-throughs on the topic, I&amp;rsquo;ll try to be to-the-point on this one.&lt;/p&gt;
&lt;p&gt;As with the others, I&amp;rsquo;ve created a &lt;a href=&#34;https://github.com/andrewcbancroft/SwiftRotateAnimation&#34; title=&#34;GitHub - SwiftRotateAnimation&#34;&gt;GitHub project&lt;/a&gt; for you to see the animation in action, and even modify to your liking.&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;fab fa-github fa-lg&#34;&gt;&lt;/i&gt; &lt;a title=&#34;GitHub - SwiftRotateAnimation&#34; href=&#34;https://github.com/andrewcbancroft/SwiftRotateAnimation&#34;&gt;Rotate Animation Example Project&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;
&lt;p&gt;&lt;a name=&#34;the-extension&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;the-extension&#34;&gt;The Extension&lt;/h3&gt;
&lt;p&gt;The following code adds a method to any UIView instance called &lt;code&gt;rotate360Degrees&lt;/code&gt;. The code can be placed in a Swift file called &amp;ldquo;UIViewExtensions.swift”:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-swift&#34; data-lang=&#34;swift&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt; 1&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;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;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;rotate360Degrees&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;CFTimeInterval&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; 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;rotateAnimation&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;CABasicAnimation&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;keyPath&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;transform.rotation&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;n&#34;&gt;rotateAnimation&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;fromValue&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; 7&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;rotateAnimation&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;toValue&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;CGFloat&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;M_PI&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;*&lt;/span&gt; &lt;span class=&#34;mf&#34;&gt;2.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; 8&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;rotateAnimation&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; 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;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;11&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;            &lt;span class=&#34;n&#34;&gt;rotateAnimation&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;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;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;rotateAnimation&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;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;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;The only critical thing to notice in the above code snippet is the value passed to the &lt;code&gt;CABasicAnimation&lt;/code&gt; constructor.  The &lt;code&gt;&amp;quot;transform.rotation&amp;amp;#8221;&lt;/code&gt; string is what sets things up to go spinning, and the string &lt;em&gt;must&lt;/em&gt; be typed exactly as-is for the animation to work.&lt;/p&gt;
&lt;p&gt;As in my previous animation posts, I provide myself a couple of parameters to set for a little bit of customization if I want it. Since the parameters have default values, the method can be invoked by writing &lt;code&gt;someUIViewInstance.rotate360Degrees()&lt;/code&gt; for simple cases.  For more &amp;ldquo;advanced” scenarios where you need to adjust how long the animation takes, or to perform some logic after the animation completes, you can pass in a duration value other than 1.0, assign a completionDelegate, or both, depending on your needs.&lt;/p&gt;
&lt;p&gt;Check out the &lt;a title=&#34;GitHub - SwiftRotateAnimation&#34; href=&#34;https://github.com/andrewcbancroft/SwiftRotateAnimation&#34; target=&#34;_blank&#34;&gt;GitHub example&lt;/a&gt; for details on how to configure things for the &lt;code&gt;completionDelegate&lt;/code&gt;.  I&amp;rsquo;ll be walking through that more &amp;ldquo;advanced” case shortly as well.&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;h3 id=&#34;example&#34;&gt;Example&lt;/h3&gt;
&lt;p&gt;Perhaps you&amp;rsquo;re asking, &amp;ldquo;Why spinning UIViews?”…&lt;/p&gt;
&lt;p&gt;In my example, I&amp;rsquo;ve proposed a simple button that would be used to refresh the view / data in a real-world scenario.  When the button is tapped, I want the button to rotate 360 degrees.&lt;/p&gt;
&lt;p&gt;In the &amp;ldquo;advanced” example, I want it to rotate continually until a process of some sort finishes, at which point the animation stops until initiated again.  Take a look:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2014/10/RotateAnimationExample.gif&#34;&gt;&lt;img class=&#34;size-full wp-image-5661 aligncenter&#34; src=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2014/10/RotateAnimationExample.gif&#34; alt=&#34;Rotate Animation Example&#34; width=&#34;357&#34; height=&#34;636&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;rotate-once&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;simple-case--rotate-once&#34;&gt;Simple Case – Rotate Once&lt;/h3&gt;
&lt;p&gt;Once the UIView extension is in place, the simple use case is… well… pretty simple:&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;refreshButton&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&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;@IBAction&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;refresh&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;self&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;refreshButton&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;rotate360Degrees&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;// Perhaps start a process which will refresh the UI...&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;&lt;a name=&#34;advanced&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;advanced8221-case--rotate-until-process-finishes&#34;&gt;&amp;ldquo;Advanced” Case – Rotate Until Process Finishes&lt;/h3&gt;
&lt;p&gt;In my example, I decided to simulate a long-running process by using a custom-built &lt;code&gt;Timer&lt;/code&gt; class, heavily inspired by &lt;a title=&#34;Samuel Mullen - Using Swift Closures with NSTimer&#34; href=&#34;http://www.samuelmullen.com/2014/07/using-swifts-closures-with-nstimer&#34; target=&#34;_blank&#34;&gt;Samuel Mullen&amp;rsquo;s implementation&lt;/a&gt; (with a few modifications to fit my needs).  If you&amp;rsquo;re looking through the &lt;a title=&#34;GitHub - SwiftRotateAnimation&#34; href=&#34;https://github.com/andrewcbancroft/SwiftRotateAnimation&#34; target=&#34;_blank&#34;&gt;GitHub example&lt;/a&gt;, try not to get too bogged down in the details of the &lt;code&gt;Timer&lt;/code&gt;, unless it just intrigues you.  In real life, you may decide perform a web service call to refresh your data model, or refresh your UI (or both).  Whatever the case may be, you&amp;rsquo;ll likely end up with similar logic:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Refresh button is tapped&lt;/li&gt;
&lt;li&gt;If the button isn&amp;rsquo;t already rotating, make it start&lt;/li&gt;
&lt;li&gt;Kick off a process that may take some time&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;animationDidStop&lt;/code&gt; callback is going to be invoked after the view has spun a full 360 degrees.  If the longish-running process is finished, the button can stop spinning.  Otherwise, it needs to spin around another time.  This will be repeated until the longish-running process is complete.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Confession:  I&amp;rsquo;m not entirely thrilled with the rampant mutability in my implementation, but I couldn&amp;rsquo;t figure out a way to do what I wanted in an immutable way.  It does work, however.  Just be aware that if you&amp;rsquo;re really a stickler for immutability in your classes, you&amp;rsquo;re going to hate this implementation (and I&amp;rsquo;d love to hear constructive feedback on how I could improve it!).  Here&amp;rsquo;s the code for the bullet-pointed process just outlined:&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;ViewController2&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;refreshButton&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&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;// var, var, var!  So much for immutability :/&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;var&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;isRotating&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; 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;shouldStopRotating&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; 6&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;timer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Timer&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;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;refresh&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 class=&#34;k&#34;&gt;if&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;isRotating&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;10&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;refreshButton&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;rotate360Degrees&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;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;11&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;            &lt;span class=&#34;c1&#34;&gt;// Perhaps start a process which will refresh the UI...&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;timer&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Timer&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;mf&#34;&gt;5.0&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;completionHandler&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;kc&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;shouldStopRotating&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&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;kc&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;timer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;start&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;isRotating&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&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;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;21&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;kc&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;shouldStopRotating&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;22&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;refreshButton&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;rotate360Degrees&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;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;23&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;24&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;reset&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;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;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;reset&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;29&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;isRotating&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;30&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;shouldStopRotating&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;31&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;32&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;summary&#34; class=&#34;jump-target&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;summary&#34;&gt;Summary&lt;/h3&gt;
&lt;p&gt;I tried to strike a balance between making these simple animations easy to call on my labels, buttons, and other UIView subclasses, and just shoving everything into a UIViewExtensions.swift file.  The related set of animations just seemed like a really nice use case for Swift extensions, and the strategy served me well in a recent project.  Hopefully the series has sparked some ideas in your mind for how to employ extensions to enhance the capabilities of a class so that your code is easier and cleaner to write.&lt;/p&gt;
&lt;p&gt;As always – thanks for reading!&lt;br&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 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;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;/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>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>Date, Date?, and the ToString Method in VB.Net</title>
      <link>https://www.andrewcbancroft.com/2014/10/03/dates-and-the-tostring-method-in-vb-net/</link>
      <pubDate>Fri, 03 Oct 2014 17:28:05 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2014/10/03/dates-and-the-tostring-method-in-vb-net/</guid>
      <description>&lt;p&gt;I just spent the last 45 minutes frustrated as to why I was getting the following exception&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2014/10/Input-string-was-not-in-the-correct-format.png&#34;&gt;&lt;img class=&#34;alignnone size-large wp-image-5421&#34; src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2014/10/Input-string-was-not-in-the-correct-format-1024x351.png&#34; alt=&#34;Input string was not in the correct format&#34; width=&#34;730&#34; height=&#34;250&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2014/10/Input-string-was-not-in-the-correct-format-1024x351.png 1024w, https://www.andrewcbancroft.com/wp-content/uploads/2014/10/Input-string-was-not-in-the-correct-format-300x102.png 300w, https://www.andrewcbancroft.com/wp-content/uploads/2014/10/Input-string-was-not-in-the-correct-format-1200x411.png 1200w, https://www.andrewcbancroft.com/wp-content/uploads/2014/10/Input-string-was-not-in-the-correct-format.png 1440w&#34; sizes=&#34;(max-width: 730px) 100vw, 730px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;ldquo;Input string was not in a correct format.” and &amp;ldquo;[InvalidCastException: Conversion from string &amp;ldquo;MMMM dd, yyyy” to type ‘Integer&amp;rsquo; is not valid.]&lt;/p&gt;
&lt;p&gt;I kept thinking to myself, &amp;ldquo;How hard can this possibly be?!!  I&amp;rsquo;ve done this a thousand times — Why is it wanting to convert my format string into an Integer??!  AAHHH!!”.  Consulting &lt;a title=&#34;MSDN - Custom Date and Time Format Strings&#34; href=&#34;http://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx?cs-save-lang=1&amp;cs-lang=vb#code-snippet-1&#34; target=&#34;_blank&#34;&gt;MSDN on the matter&lt;/a&gt; only confirmed that I knew what I was doing with the string formatter.&lt;/p&gt;
&lt;p&gt;What got me in the end is that it turns out I &lt;em&gt;didn&amp;rsquo;t&lt;/em&gt; know what I was doing with my &lt;em&gt;Date&lt;/em&gt; object.  In fact, I wasn&amp;rsquo;t dealing with a &lt;span class=&#34;theme:vs2012 lang:vbnet decode:true  crayon-inline&#34;&gt;Date&lt;/span&gt; object at all.  I was dealing with a &lt;span class=&#34;theme:vs2012 lang:vbnet decode:true  crayon-inline &#34;&gt;Date?&lt;/span&gt; object (that is, a &lt;span class=&#34;theme:vs2012 lang:vbnet decode:true  crayon-inline&#34;&gt;Nullable(of Date)&lt;/span&gt;. &lt;span class=&#34;theme:vs2012 lang:vbnet decode:true  crayon-inline&#34;&gt;Date&lt;/span&gt; and &lt;span class=&#34;theme:vs2012 lang:vbnet decode:true  crayon-inline&#34;&gt;Date?&lt;/span&gt;  are not the same. And it &lt;em&gt;matters&lt;/em&gt;, because the &lt;span class=&#34;theme:vs2012 lang:vbnet decode:true  crayon-inline &#34;&gt;ToString()&lt;/span&gt; method of &lt;span class=&#34;theme:vs2012 lang:vbnet decode:true  crayon-inline&#34;&gt;Date&lt;/span&gt; has _different overloads _than the &lt;span class=&#34;theme:vs2012 lang:vbnet decode:true  crayon-inline &#34;&gt;ToString()&lt;/span&gt; method of &lt;span class=&#34;theme:vs2012 lang:vbnet decode:true  crayon-inline&#34;&gt;Date?&lt;/span&gt;.  Only the &lt;span class=&#34;theme:vs2012 lang:vbnet decode:true  crayon-inline&#34;&gt;Date&lt;/span&gt; type allows you to put in a format string to fancy up how the date is displayed when it&amp;rsquo;s converted to a string.&lt;/p&gt;
&lt;h3 id=&#34;my-solution&#34;&gt;My solution:&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt; Wrap my &lt;span class=&#34;theme:vs2012 lang:vbnet decode:true  crayon-inline &#34;&gt;Date?&lt;/span&gt; object instance in a &lt;span class=&#34;theme:vs2012 lang:vbnet decode:true  crayon-inline&#34;&gt;CType&lt;/span&gt;, converting to type &lt;span class=&#34;theme:vs2012 lang:vbnet decode:true  crayon-inline&#34;&gt;Date&lt;/span&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt; Call &lt;span class=&#34;theme:vs2012 lang:vbnet decode:true  crayon-inline &#34;&gt;ToString()&lt;/span&gt; on the &lt;em&gt;converted&lt;/em&gt; value:&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;CType(someObject.dateInstance, Date).ToString(&amp;#34;MMMM dd, yyyy&amp;#34;)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Sanity… &lt;em&gt;recovered.&lt;/em&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;thedetails&#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-ensure-that-theclass-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-create-anxctestcaseclass-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-within-the-function-body-createanestedclass&#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-make-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-you-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;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-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-instantiate-the-class-youre-testing-and-pass-in-an-instance-of-themock-object-you-just-nested-in-the-test-function-to-yourclass-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-xctassert8230&#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>NuGet app.config / web.config Transformation Trouble</title>
      <link>https://www.andrewcbancroft.com/2014/06/16/nuget-appconfig-webconfig-transformation-trouble/</link>
      <pubDate>Mon, 16 Jun 2014 17:57:20 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2014/06/16/nuget-appconfig-webconfig-transformation-trouble/</guid>
      <description>&lt;p&gt;I recently hit a road bump when developing a NuGet package. Mid-way through making the package, I decided I wanted to include couple of transforms to add some default connection strings to an app.config or a web.config file.  Try as I may, I couldn&amp;rsquo;t get NuGet to apply the transforms to my project&amp;rsquo;s web.config file even though I was following &lt;a title=&#34;NuGet Configuration File and Source Code Transformations Documentation&#34; href=&#34;http://docs.nuget.org/docs/creating-packages/configuration-file-and-source-code-transformations&#34; target=&#34;_blank&#34;&gt;NuGet documentation&amp;rsquo;s&lt;/a&gt; instructions to a tee.&lt;/p&gt;
&lt;p&gt;It turns out that my problem was related to about 3 things all working together in tandem to defeat me:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;First&lt;/strong&gt;, the project I was testing the installation of this NuGet package on was already checked into source control (TFS).  To test out the package, I would install it and when things didn&amp;rsquo;t work how I wanted, I&amp;rsquo;d just &amp;ldquo;undo pending changes” to get everything back to the last stable state.  Or so I thought (more on this in a moment)…&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Second&lt;/strong&gt;, I had the idea to add app.config.transform and web.config.transform files to my NuGet package Content folder &lt;em&gt;after&lt;/em&gt; I&amp;rsquo;d already installed &amp;ldquo;version 1” of the package in my project, and rather than bump the version of the package with the transforms in it &lt;em&gt;up&lt;/em&gt;, I left it the same because I was still technically editing and troubleshooting the first version of the package (in my mind anyways).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Third&lt;/strong&gt;, my solution was configured for &amp;ldquo;NuGet Package Restore”:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2014/06/Enable-NuGet-Package-Restore.png&#34;&gt;&lt;img class=&#34;alignnone wp-image-1201 size-full&#34; src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2014/06/Enable-NuGet-Package-Restore.png&#34; alt=&#34;Enable NuGet Package Restore&#34; width=&#34;632&#34; height=&#34;514&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2014/06/Enable-NuGet-Package-Restore.png 632w, https://www.andrewcbancroft.com/wp-content/uploads/2014/06/Enable-NuGet-Package-Restore-300x243.png 300w&#34; sizes=&#34;(max-width: 632px) 100vw, 632px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4 id=&#34;so-what-was-going-on&#34;&gt;So what was going on?&lt;/h4&gt;
&lt;p&gt;Choosing &amp;ldquo;Enable NuGet Package Restore” keeps your NuGet packages folder contents from being checked into source control by adding a NuGet config file with a setting disabling source control integration.  We&amp;rsquo;d rather just let NuGet restore them if they&amp;rsquo;re missing when we build, rather than check in the packages folder to source control.&lt;/p&gt;
&lt;p&gt;What this meant for me, however, is that all my &amp;ldquo;undo pending changes” actions were doing absolutely nothing to undo the installation of the NuGet package I was developing and troubleshooting.  My troubleshooting workflow of [Install package -&amp;gt; Check web.config -&amp;gt; :[ &lt;em&gt;Still&lt;/em&gt; missing connection strings -&amp;gt; Undo pending changes -&amp;gt; Fiddle with NuGet Package -&amp;gt; Rinse and repeat] could have been infinite, because the &amp;ldquo;undo pending changes” step wasn&amp;rsquo;t undoing the NuGet package installation, because _the NuGet package files weren&amp;rsquo;t part of the pending change set _due to my &amp;ldquo;Enable NuGet Package Restore” setting.  The NuGet package was still in the packages folder on my hard disk, which had another hidden consequence: each subsequent &amp;ldquo;installation” of the NuGet package effectively did nothing because it was already there.  Remember, I didn&amp;rsquo;t bump the version number of the package, so I&amp;rsquo;m guessing NuGet already saw the files there and just updated the config file and called it good.  None of the updates I was making to the package were ever updated.&lt;/p&gt;
&lt;h4 id=&#34;solution&#34;&gt;Solution?&lt;/h4&gt;
&lt;p&gt;I went back to the last stable checked-in state for my solution.&lt;br&gt;
Next, I navigated to my solution folder by right-clicking my solution and choosing &amp;ldquo;Open Folder in File Explorer”:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2014/06/Open-Folder-in-File-Explorer.png&#34;&gt;&lt;img class=&#34;alignnone wp-image-1271 &#34; src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2014/06/Open-Folder-in-File-Explorer.png&#34; alt=&#34;Open Folder in File Explorer&#34; width=&#34;381&#34; height=&#34;407&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2014/06/Open-Folder-in-File-Explorer.png 500w, https://www.andrewcbancroft.com/wp-content/uploads/2014/06/Open-Folder-in-File-Explorer-280x300.png 280w&#34; sizes=&#34;(max-width: 381px) 100vw, 381px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I double-clicked on the packages folder:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2014/06/Packages-Folder.png&#34;&gt;&lt;img class=&#34;alignnone  wp-image-1281&#34; src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2014/06/Packages-Folder.png&#34; alt=&#34;Packages Folder&#34; width=&#34;586&#34; height=&#34;199&#34; srcset=&#34;https://www.andrewcbancroft.com/wp-content/uploads/2014/06/Packages-Folder.png 727w, https://www.andrewcbancroft.com/wp-content/uploads/2014/06/Packages-Folder-300x101.png 300w&#34; sizes=&#34;(max-width: 586px) 100vw, 586px&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Finally, I deleted the folder for the package I was developing.&lt;/p&gt;
&lt;p&gt;After these steps were performed, installing the NuGet package again worked perfectly – the config transforms were applied and life was good.&lt;/p&gt;
&lt;p&gt;Alternatively, I suppose I could have upped the version number of my package and things would have been fine as well.  But I always tend to make things more complicated than they need to be. :]&lt;/p&gt;
&lt;p&gt; &lt;/p&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>DotNetZip – Solving Mac Decompression Issues</title>
      <link>https://www.andrewcbancroft.com/2013/12/28/dotnetzip-solving-mac-decompression-issues/</link>
      <pubDate>Sat, 28 Dec 2013 20:55:27 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2013/12/28/dotnetzip-solving-mac-decompression-issues/</guid>
      <description>&lt;p&gt;As part of a ASP.Net Web API service that I&amp;rsquo;m co-developing, I have gotten familiar with the &lt;a title=&#34;DotNetZip Codeplex Page&#34; href=&#34;http://dotnetzip.codeplex.com/&#34; target=&#34;_blank&#34;&gt;DotNetZip Library&lt;/a&gt;.  It&amp;rsquo;s fantastic.  We&amp;rsquo;re using it to gather up a set of requested files from network storage, package them in a zip archive, and stream them back to a client via a web application.&lt;/p&gt;
&lt;p&gt;During testing, things worked great on Windows machines, but the moment we tested using a Mac, we ran into issues.&lt;/p&gt;
&lt;h2 id=&#34;synopsis&#34;&gt;Synopsis:&lt;/h2&gt;
&lt;p&gt;Problem:  On a Mac, a zip file would be downloaded, but it could not be extracted.  Double-clicking the zip file resulted in the creation of a .cpgz file.&lt;/p&gt;
&lt;p&gt;Solution:  In ASP.Net code, change&lt;/p&gt;
&lt;p&gt;HttpContext.Current.Response.ContentType = &amp;ldquo;application/zip”&lt;/p&gt;
&lt;p&gt;to&lt;/p&gt;
&lt;p&gt;HttpContext.Current.Response.ContentType = &amp;ldquo;application/octet-stream”&lt;/p&gt;
&lt;h2 id=&#34;problem-details&#34;&gt;Problem Details:&lt;/h2&gt;
&lt;p&gt;A zip archive would download as normal, but as things were wrapping up, Safari would try to decompress the file and it would fail.&lt;/p&gt;
&lt;p&gt;Error from Safari&amp;rsquo;s download manager area:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://andrewcbancroft.azurewebsites.net/wp-content/uploads/2013/10/DecompressionFailed.png&#34;&gt;&lt;img class=&#34;alignnone size-medium wp-image-585&#34; style=&#34;src=&amp;quot;http://andrewcbancroft.azurewebsites.net/wp-content/uploads/2013/10/DecompressionFailed-300x43.png&amp;quot;&#34; width=&#34;300&#34; height=&#34;43&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Furthermore, when attempting to simply extract the files directly using the built in Mac Archive Utility (by double-clicking the zip file), the utility would actually re-archive the file, rather than extract it.  The end result was the creation of a .cpgz file.  Attempting to decompress &lt;em&gt;that&lt;/em&gt; file would result in creating another .zip file, which, when double-clicked, created another .cpgz file, and so on, and so on.&lt;/p&gt;
&lt;p&gt;Archive Utility re-archiving the zip file when double-clicked (rather than &lt;em&gt;un&lt;/em&gt;-archiving it)&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://andrewcbancroft.azurewebsites.net/wp-content/uploads/2013/10/Screen-Shot-2013-10-10-at-4.13.48-PM.png&#34;&gt;&lt;img class=&#34;alignnone size-medium wp-image-586&#34; alt=&#34;Screen Shot 2013-10-10 at 4.13.48 PM&#34; src=&#34;http://andrewcbancroft.azurewebsites.net/wp-content/uploads/2013/10/Screen-Shot-2013-10-10-at-4.13.48-PM-300x106.png&#34; width=&#34;300&#34; height=&#34;106&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Creation of .cpgz file:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://andrewcbancroft.azurewebsites.net/wp-content/uploads/2013/10/Created_cpgz_file.png&#34;&gt;&lt;img class=&#34;alignnone size-full wp-image-584&#34; alt=&#34;Created_cpgz_file&#34; src=&#34;http://andrewcbancroft.azurewebsites.net/wp-content/uploads/2013/10/Created_cpgz_file.png&#34; width=&#34;280&#34; height=&#34;22&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Needless to say, the solution to this problem, though somewhat mysterious, was quite simple for me.&lt;/p&gt;
&lt;p&gt;In my ASP.Net code, I was writing the zip file to the Response OutputStream.  I had added a content type of &amp;ldquo;application/zip” to the response.  This is what was breaking the zip files on a Mac.  Buried &lt;a title=&#34;DotNetZip Library Forum&#34; href=&#34;http://dotnetzip.codeplex.com/discussions/59740&#34; target=&#34;_blank&#34;&gt;in the DotNetZip Library&amp;rsquo;s forum was a post&lt;/a&gt; recommending changing the content type to &amp;ldquo;application/octet-stream” instead.  That single changed fixed the issue for standard zip files!&lt;/p&gt;
&lt;h2 id=&#34;solution-details&#34;&gt;Solution Details:&lt;/h2&gt;
&lt;p&gt;Change&lt;/p&gt;
&lt;p&gt;HttpContext.Current.Response.ContentType = &amp;ldquo;application/zip”&lt;/p&gt;
&lt;p&gt;to&lt;/p&gt;
&lt;p&gt;HttpContext.Current.Response.ContentType = &amp;ldquo;application/octet-stream”&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>“Unexpected System Error” in DataStage Sequence Job</title>
      <link>https://www.andrewcbancroft.com/2013/04/16/unexpected-system-error-in-datastage-sequence-job/</link>
      <pubDate>Tue, 16 Apr 2013 14:15:54 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2013/04/16/unexpected-system-error-in-datastage-sequence-job/</guid>
      <description>&lt;p&gt;Today I was running into a mysterious error in DataStage as I was running a sequence job.  Things seemed to be going well until one of my Fact jobs at the end of the sequence reported “Aborted” in DataStage Director.  Looking into the log for this job revealed a rather vague error message:&lt;/p&gt;
&lt;p&gt;[StageName] DB2 function SQLFetch failed:  SQLSTATE = HY000: Native Error Code = -1,042: Msg = [IBM][CLI Driver][DB2/LINUXX8664] SQL1042C  An unexpected system error occurred.  SQLSTATE=58004 (CC_DB2Connection::queryServerHostName, file CC_DB2Connection.cpp, line 3,256)&lt;/p&gt;
&lt;p&gt;What’s strange is that the job, when re-compiled and run on its own, executes without error.  So it only fails when its run in the context of the sequence job.&lt;/p&gt;
&lt;p&gt;To fix the issue, I simply rearranged the order of the sequence at the end.  Here is a before and after look at the sequence:&lt;/p&gt;
&lt;h3 id=&#34;before&#34;&gt;Before:&lt;/h3&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2013/04/image.png&#34;&gt;&lt;img title=&#34;image&#34; style=&#34;border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px&#34; border=&#34;0&#34; alt=&#34;image&#34; src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2013/04/image_thumb.png&#34; width=&#34;515&#34; height=&#34;496&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;after&#34;&gt;After:&lt;/h3&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2013/04/image1.png&#34;&gt;&lt;img title=&#34;image&#34; style=&#34;border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px&#34; border=&#34;0&#34; alt=&#34;image&#34; src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2013/04/image_thumb1.png&#34; width=&#34;609&#34; height=&#34;208&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The sequence job runs every job now without error.  Now I only wish I knew &lt;em&gt;why&lt;/em&gt; this works – perhaps it’s an issue with server resources getting tied up if both Fact jobs are running at the same time?  I’ve decided to go with the above as my solution and move on, since neither Fact job takes an enormous amount of time to run.  Hopefully this solution will work for others as well!&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>DataStage Range Lookup Failure – What Gives?</title>
      <link>https://www.andrewcbancroft.com/2013/03/25/datastage-range-lookup-failure-what-gives/</link>
      <pubDate>Mon, 25 Mar 2013 17:52:13 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2013/03/25/datastage-range-lookup-failure-what-gives/</guid>
      <description>&lt;p&gt;I just spent the majority of my morning trying to figure out why in the world my range lookup kept failing.  Hopefully this will save you some time in troubleshooting &lt;em&gt;your&lt;/em&gt; range lookup failures.  To cut right to the chase, &lt;strong&gt;my resolution involved &lt;em&gt;sorting&lt;/em&gt; my reference source prior to doing the lookup&lt;/strong&gt;.  Adding a sort stage with the appropriate sort keys prior to doing the lookup solved my problem.  I probably learned this in a training at some point, but I just didn’t retain it.  So I learned it again the hard way.&lt;/p&gt;
&lt;p&gt;For those who’d like to see a bit more of the details involved, feel free to have a look below.&lt;/p&gt;
&lt;h3 id=&#34;scenario&#34;&gt;Scenario:&lt;/h3&gt;
&lt;p&gt;I have a list of funding transactions in my data pipeline that lack some information, namely, the student’s major at the time he/she was funded.  They key phrase there is “&lt;em&gt;at the time he/she was funded”.&lt;/em&gt; &lt;/p&gt;
&lt;p&gt;To assist in supplementing my fact record (the funding transaction) with this student demographic information, I have constructed a snapshot table in the data warehouse that captures a student’s major as a sort of “slowly changing dimension”.  Each time a change in the student’s major is detected, new records are inserted and effective and expiration dates are adjusted.  I’d like to supplement my funding record with this student major information by doing a lookup.  Here is the information I have to work with, just to be clear:&lt;/p&gt;
&lt;p&gt;My source data pipeline starts with my funding transactions.  It contains&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Student ID (Key)
&lt;ul&gt;
&lt;li&gt;Transaction Date (will help me with my lookup)
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Other columns that aren’t relevant to this particular task&lt;/ul&gt;
I also have a slowly changing dimension that tracks a historical record of each student’s major for various time intervals.  It contains&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Student ID (Key)&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Effective Date of the student’s major (will help with lookup)
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Expiration Date of the student’s major (will help with lookup)&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Student’s major (the information I want to use to supplement my fact row)&lt;/ul&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;range-lookup-my-failing-setup&#34;&gt;Range Lookup (My [failing] Setup):&lt;/h3&gt;
&lt;p&gt;To accomplish my goal of supplementing the fact record with the student’s major, I needed to do a range lookup.  Below are sample screenshots of how I was attempting to set things up:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;View of the job design surface:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2013/03/image9.png&#34;&gt;&lt;img title=&#34;image&#34; style=&#34;border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px&#34; border=&#34;0&#34; alt=&#34;image&#34; src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2013/03/image_thumb9.png&#34; width=&#34;523&#34; height=&#34;478&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;View of Lookup Stage:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2013/03/image10.png&#34;&gt;&lt;img title=&#34;image&#34; style=&#34;border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px&#34; border=&#34;0&#34; alt=&#34;image&#34; src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2013/03/image_thumb10.png&#34; width=&#34;702&#34; height=&#34;326&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Notice that I use StudentID as a key so that I match the funding row to a particular student.  Now, to get his/her major at the time they were funded, I do a range lookup on the DateFunded column…&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;View of Range Lookup on DateFunded:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2013/03/image11.png&#34;&gt;&lt;img title=&#34;image&#34; style=&#34;border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px&#34; border=&#34;0&#34; alt=&#34;image&#34; src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2013/03/image_thumb11.png&#34; width=&#34;725&#34; height=&#34;517&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;After compiling the job and running it, I would receive this error message: &lt;/p&gt;
&lt;p&gt;Lookup_StudentFieldOfStudy,0: Job aborting due to &lt;strong&gt;lookup failure&lt;/strong&gt; on link: List_StudentFieldOfStudy&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2013/03/image12.png&#34;&gt;&lt;img title=&#34;image&#34; style=&#34;border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px&#34; border=&#34;0&#34; alt=&#34;image&#34; src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2013/03/image_thumb12.png&#34; width=&#34;725&#34; height=&#34;121&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;problem-solved&#34;&gt;Problem Solved:&lt;/h3&gt;
&lt;p&gt;As stated in the introduction, to fix this problem, I simply added a sort stage prior to the lookup stage on the link serving as my reference (that is, on the link containing the value I’m trying to look up, which in my case was the student’s major/field of study).  Here are some screen shots of how my job looked after I fixed it:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;View of the job design surface:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2013/03/image13.png&#34;&gt;&lt;img title=&#34;image&#34; style=&#34;border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px&#34; border=&#34;0&#34; alt=&#34;image&#34; src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2013/03/image_thumb13.png&#34; width=&#34;691&#34; height=&#34;462&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;View of Sort Stage:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2013/03/SNAGHTML100fd47.png&#34;&gt;&lt;img title=&#34;SNAGHTML100fd47&#34; style=&#34;border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px&#34; border=&#34;0&#34; alt=&#34;SNAGHTML100fd47&#34; src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2013/03/SNAGHTML100fd47_thumb.png&#34; width=&#34;517&#34; height=&#34;397&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;An important note about the sort stage.  I needed to include &lt;strong&gt;two sort keys&lt;/strong&gt; – one to sort the students, and another to sort the effective dates of each student’s major(s).&lt;/p&gt;
&lt;p&gt;Everything else in the job remained exactly the same.  The results were a successfully running job with no lookup failures.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&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>
    
    <item>
      <title>Report Studio Bullet Chart – Missing Target</title>
      <link>https://www.andrewcbancroft.com/2013/03/06/report-studio-bullet-chart-missing-target/</link>
      <pubDate>Wed, 06 Mar 2013 16:34:36 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2013/03/06/report-studio-bullet-chart-missing-target/</guid>
      <description>&lt;p&gt;Today I was working on a bullet chart in Cognos Report Studio.  The chart was rendering, but the target bar would never appear.  Here’s the scenario I was in and how I resolved my issue:&lt;/p&gt;
&lt;h1 id=&#34;my-goal&#34;&gt;My Goal:&lt;/h1&gt;
&lt;p&gt;My goal was to show the average time to process an application against a target processing time for the current fiscal year.&lt;/p&gt;
&lt;h1 id=&#34;my-situation&#34;&gt;My Situation:&lt;/h1&gt;
&lt;p&gt;My “actual” value was being returned in my query to the data warehouse – It is an average of the days to process an application (defined in Framework Manager).&lt;/p&gt;
&lt;p&gt;My “target” value does not exist in the data warehouse, so I created a calculated item in my Report Studio query to hold the target value:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2013/03/SNAGHTML6c0eff.png&#34;&gt;&lt;img style=&#34;background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;&#34; title=&#34;SNAGHTML6c0eff&#34; alt=&#34;SNAGHTML6c0eff&#34; src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2013/03/SNAGHTML6c0eff_thumb.png&#34; width=&#34;722&#34; height=&#34;452&#34; border=&#34;0&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;my-problem&#34;&gt;My Problem:&lt;/h1&gt;
&lt;p&gt;I created a bullet chart and dragged the appropriate items to the appropriate place in the chart:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2013/03/image4.png&#34;&gt;&lt;img style=&#34;background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;&#34; title=&#34;image&#34; alt=&#34;image&#34; src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2013/03/image_thumb4.png&#34; width=&#34;515&#34; height=&#34;464&#34; border=&#34;0&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The result of this when I ran the report, however, was a chart with a bullet but no target:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2013/03/image5.png&#34;&gt;&lt;img style=&#34;background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;&#34; title=&#34;image&#34; alt=&#34;image&#34; src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2013/03/image_thumb5.png&#34; width=&#34;434&#34; height=&#34;124&#34; border=&#34;0&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;my-solution&#34;&gt;My Solution:&lt;/h1&gt;
&lt;p&gt;As it turns out, the Aggregation Function of my “Target Completion Time” was throwing things off for the chart.  This property’s default value is “Automatic”.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Changing the Aggregation Function from “Automatic” to “Calculated” fixed the issue&lt;/strong&gt;:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2013/03/image6.png&#34;&gt;&lt;img style=&#34;background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;&#34; title=&#34;image&#34; alt=&#34;image&#34; src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2013/03/image_thumb6.png&#34; width=&#34;725&#34; height=&#34;329&#34; border=&#34;0&#34; /&gt;&lt;/a&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2013/03/image7.png&#34;&gt;&lt;img style=&#34;background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;&#34; title=&#34;image&#34; alt=&#34;image&#34; src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2013/03/image_thumb7.png&#34; width=&#34;351&#34; height=&#34;284&#34; border=&#34;0&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;the-result&#34;&gt;The Result:&lt;/h1&gt;
&lt;p&gt;The bullet &lt;em&gt;and&lt;/em&gt; the target now appear on my chart:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2013/03/image8.png&#34;&gt;&lt;img style=&#34;background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;&#34; title=&#34;image&#34; alt=&#34;image&#34; src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2013/03/image_thumb8.png&#34; width=&#34;406&#34; height=&#34;114&#34; border=&#34;0&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Removing Chart Gridlines and Plot Area Background in Report Studio</title>
      <link>https://www.andrewcbancroft.com/2013/03/05/removing-chart-gridlines-plot-area-background-in-report-studio/</link>
      <pubDate>Tue, 05 Mar 2013 16:48:20 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2013/03/05/removing-chart-gridlines-plot-area-background-in-report-studio/</guid>
      <description>&lt;p&gt;The steps from IBM’s &lt;a href=&#34;http://pic.dhe.ibm.com/infocenter/cbi/v10r1m0/index.jsp?topic=%2Fcom.ibm.swg.im.cognos.ug_cr_rptstd.10.1.0.doc%2Fug_cr_rptstd_id6040custom_axes.html&#34; target=&#34;_blank&#34;&gt;official documentation&lt;/a&gt;, while they attempt to lead you in the direction of changing these properties of a chart, did not register with my brain for about an hour or so of clicking around, frustrated that I could not find the properties they were describing in the articl.  Just in case I could spare anyone else the frustration of trying to remove gridlines in Report Studio’s current default charts, I thought I’d post this quick article on how I solved my problem.&lt;/p&gt;
&lt;p&gt;The documentation states, “Step 1.  Select the Y-axis or the X-axis of the chart”.&lt;/p&gt;
&lt;p&gt;Easy enough, right?  Well as it turns out, Step 1 is where I was going wrong…&lt;/p&gt;
&lt;p&gt;Where was I clicking?  Not the Y-axis apparently.  I was clicking the &lt;em&gt;label&lt;/em&gt; for the Y-axis:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2013/03/image.png&#34;&gt;&lt;img style=&#34;background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px&#34; title=&#34;image&#34; border=&#34;0&#34; alt=&#34;image&#34; src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2013/03/image_thumb.png&#34; width=&#34;725&#34; height=&#34;280&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Once I was clicking the right thing, the solution to remove the grid lines from my chart was simple.  So where did I &lt;em&gt;need&lt;/em&gt; to click?&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2013/03/image1.png&#34;&gt;&lt;img style=&#34;background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px&#34; title=&#34;image&#34; border=&#34;0&#34; alt=&#34;image&#34; src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2013/03/image_thumb1.png&#34; width=&#34;725&#34; height=&#34;314&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The correct properties show up in the property inspector once you’re actually clicking the axis and not the axis label.  To remove the grid lines and the alternating colors in the plot area, simply redefine the Gridlines property:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2013/03/image2.png&#34;&gt;&lt;img style=&#34;background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px&#34; title=&#34;image&#34; border=&#34;0&#34; alt=&#34;image&#34; src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2013/03/image_thumb2.png&#34; width=&#34;380&#34; height=&#34;486&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Clear the checkboxes for “Show alternating color bands” and “Show major gridlines” to get rid of them on the chart:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2013/03/image3.png&#34;&gt;&lt;img style=&#34;background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px&#34; title=&#34;image&#34; border=&#34;0&#34; alt=&#34;image&#34; src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2013/03/image_thumb3.png&#34; width=&#34;288&#34; height=&#34;393&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Resolving “UDA-SQL-0569 Unable to load the driver manager library ( db2cli.dll )” with Framework Manager 10.1</title>
      <link>https://www.andrewcbancroft.com/2012/10/17/resolving-uda-sql-0569-unable-to-load-the-driver-manager-library-db2cli-dll-with-framework-manager-10-1/</link>
      <pubDate>Wed, 17 Oct 2012 15:29:23 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2012/10/17/resolving-uda-sql-0569-unable-to-load-the-driver-manager-library-db2cli-dll-with-framework-manager-10-1/</guid>
      <description>&lt;p&gt;When installing Framework Manager 10.1 and attempting to use it for the first time, things went down hill nearly immediately.  I made sure to install a copy of the DB2 client on my development machine, but I still ran into issues connecting to data sources in Framework Manager.&lt;/p&gt;
&lt;p&gt;For those who need to install a DB2 client, IBM provides those downloads &lt;a href=&#34;http://www-01.ibm.com/support/docview.wss?rs=4020&amp;amp;uid=swg21385217&#34;&gt;here&lt;/a&gt; for DB2 10.1.  For other versions, you can go &lt;a href=&#34;http://www-01.ibm.com/support/docview.wss?uid=swg27016878&#34;&gt;here&lt;/a&gt;.  [Added 3/19/2013]&lt;/p&gt;
&lt;p&gt;A good explanation of what each DB2 client &amp;ldquo;package” includes and what they&amp;rsquo;re used for can be found &lt;a href=&#34;http://www.db2dean.com/Previous/DB2Client.html&#34;&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Steps that I was taking:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Load Framework Manager and open a project&lt;/li&gt;
&lt;li&gt;Expand Data Sources&lt;/li&gt;
&lt;li&gt;Right-Click Data Source and click Test&lt;/li&gt;
&lt;li&gt;The following happens:&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2012/10/SNAGHTML194624.png&#34;&gt;&lt;img style=&#34;background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border-width: 0px;&#34; title=&#34;SNAGHTML194624&#34; alt=&#34;SNAGHTML194624&#34; src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2012/10/SNAGHTML194624_thumb.png&#34; width=&#34;666&#34; height=&#34;208&#34; border=&#34;0&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Text from dialog box:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;QE-DEF-0285 The logon failed.&lt;br&gt;
QE-DEF-0325 The logon failed for the following reason:&lt;br&gt;
RQP-DEF-0068 Unable to connect to at least one database during a multi-database attach to 1 database(s) in:&lt;br&gt;
DW&lt;br&gt;
UDA-SQL-0569 Unable to load the driver manager library ( db2cli.dll ).&lt;br&gt;
UDA-SQL-0571 The operating system returned an error message ( The specified module could not be found. ).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;How I resolved this:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;I simply moved C:Program FilesIBMSQLLIBBIN placed in PATH variable near the front of the list of directories. Depending on your installation directory, you may need to change the path above to fit your environment.  But this seems to have resolved my problem entirely.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>SSRS: Which server sent my e-mail subscription?</title>
      <link>https://www.andrewcbancroft.com/2012/07/25/ssrs-which-server-sent-my-e-mail-subscription/</link>
      <pubDate>Wed, 25 Jul 2012 13:56:24 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2012/07/25/ssrs-which-server-sent-my-e-mail-subscription/</guid>
      <description>&lt;p&gt;I was recently troubleshooting a seemingly bizarre issue I was having with one of our report subscriptions – the production version was running normally, but our customer suddenly began receiving a very old version of the same report subscription.  After disabling the subscription from all non-production servers that I knew about, the customer continued to receive the rogue e-mail.  I needed to figure out which “mystery” report server was sending the e-mail subscription.&lt;/p&gt;
&lt;p&gt;The answer was simple once I stepped back from the problem and remembered that there’s such a thing as a header to an e-mail that gives details about where the e-mail is coming from.&lt;/p&gt;
&lt;p&gt;Outlook 2010 provides a way for you to view an e-mail’s header by clicking File –&amp;gt; Info –&amp;gt; Properties:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2012/07/image.png&#34;&gt;&lt;img style=&#34;background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border-width: 0px;&#34; title=&#34;image&#34; src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2012/07/image_thumb.png&#34; alt=&#34;image&#34; width=&#34;576&#34; height=&#34;566&#34; border=&#34;0&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The Internet headers section is where to look – it’ll tell you which server was involved at the very beginning.&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2012/07/SNAGHTML1d27d81.png&#34;&gt;&lt;img style=&#34;background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border-width: 0px;&#34; title=&#34;SNAGHTML1d27d81&#34; src=&#34;http://www.andrewcbancroft.com/wp-content/uploads/2012/07/SNAGHTML1d27d81_thumb.png&#34; alt=&#34;SNAGHTML1d27d81&#34; width=&#34;577&#34; height=&#34;518&#34; border=&#34;0&#34; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;This turned out to be the key – after identifying the report server that was sending the e-mail to Exchange for delivery, I was able to disable the report subscription once and for all.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Troubleshooting SQL Server 2012 Reporting Services and SharePoint 2010 Integration (Part 2)</title>
      <link>https://www.andrewcbancroft.com/2012/06/30/troubleshooting-sql-server-2012-reporting-services-and-sharepoint-2010-integration-part-2/</link>
      <pubDate>Sat, 30 Jun 2012 19:44:36 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2012/06/30/troubleshooting-sql-server-2012-reporting-services-and-sharepoint-2010-integration-part-2/</guid>
      <description>&lt;p&gt;You can visit Part 1 of this troubleshooting series by &lt;a href=&#34;http://andrewcbancroft.com/2012/06/30/troubleshooting-sql-server-2012-reporting-services-and-sharepoint-2010-integration-part-1/&#34; title=&#34;Troubleshooting SQL Server 2012 Reporting Services and SharePoint 2010 Integration (Part 1)&#34;&gt;clicking here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;During our configuration of Reporting Services for SharePoint I ran into another common issue.&lt;/p&gt;
&lt;p&gt;I consulted MSDN once again and followed the directions &lt;a title=&#34;PowerPivot BI Semantic Model Connection (.bism) &#34; href=&#34;http://msdn.microsoft.com/en-us/library/gg471575.aspx&#34; target=&#34;_blank&#34;&gt;given at this MSDN article&lt;/a&gt;.  Even after following all these instructions, when I would attempt to create a new BI Semantic Model Connection in the SharePoint library, I would receive the following error:&lt;/p&gt;
&lt;div class=&#34;note&#34;&gt;
  Cannot connect to the server or database.
&lt;/div&gt;
&lt;p&gt;Even after adding the Reporting Services service account to the SSAS Tabular instance as a server administrator, we still hit this error.&lt;/p&gt;
&lt;p&gt;Once again I found little documentation out there as I was troubleshooting, presumably because SQL Server 2012 is still quite new.  Here&amp;rsquo;s what we did to fix our problem:&lt;/p&gt;
&lt;div class=&#34;note&#34;&gt;
  &lt;strong&gt;The Analysis Services service must be running under a domain account.&lt;/strong&gt;  When I installed SSAS, I took all the defaults for the server installation.  The SSAS service was running under some default built-in account which ended up being the problem.
&lt;/div&gt;
&lt;p&gt;Once we switched this to run under a domain account, I was able to create BI Semantic Model Connections using this library document type with no issues.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Troubleshooting SQL Server 2012 Reporting Services and SharePoint 2010 Integration (Part 1)</title>
      <link>https://www.andrewcbancroft.com/2012/06/30/troubleshooting-sql-server-2012-reporting-services-and-sharepoint-2010-integration-part-1/</link>
      <pubDate>Sat, 30 Jun 2012 19:06:39 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2012/06/30/troubleshooting-sql-server-2012-reporting-services-and-sharepoint-2010-integration-part-1/</guid>
      <description>&lt;p&gt;I recently worked with our SharePoint administrator to install SQL Server 2012 Reporting Services in SharePoint Integrated Mode to take advantage of PowerView.  By following the installation instructions &lt;a title=&#34;Install Reporting Services SharePoint Mode as a Single Server Farm&#34; href=&#34;http://msdn.microsoft.com/en-us/library/gg492276.aspx&#34; target=&#34;_blank&#34;&gt;found at this MSDN article&lt;/a&gt;, we were able to painlessly install both Reporting Services and the Reporting Services Add-in from the SQL Server installation media.&lt;/p&gt;
&lt;p&gt;Then began the configuration process.  Since everyone&amp;rsquo;s environment is different, I found it difficult to troubleshoot some of the odd behavior that we were running in to.  In Part 1 of this post I want to focus on what we did to overcome a specific error we were receiving:&lt;/p&gt;
&lt;p&gt;After installation, I created a Business Intelligence Center site without any trouble.  In the Connections library, I would attempt to add a Report Data Source and receive a SharePoint page that detailed the following error:&lt;/p&gt;
&lt;div class=&#34;note&#34;&gt;
  &lt;p&gt;
    This SQL Server Reporting Services (SSRS) functionality is not supported.  Use Central Administration to verify and fix one or more of the following issues:
  &lt;/p&gt;
  &lt;ul&gt;
    &lt;li&gt;
      A report server URL is not configured.  Use the SSRS Integration page to set it.
    &lt;/li&gt;
    &lt;li&gt;
      The SSRS service application proxy is not configured.  Use the SSRS service application pages to configure the proxy.
    &lt;/li&gt;
    &lt;li&gt;
      The SSRS service application is not mapped to this web application.  Use the SSRS service application pages to associate the SSRS service application proxy to the web application.
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;
&lt;p&gt;Other times, I would receive a little bit more of a generic message saying something to the effect of &amp;ldquo;This SQL Server Reporting Services (SSRS) functionality is not supported” or &amp;ldquo;Unsupported Reporting Services Functionality”.&lt;/p&gt;
&lt;p&gt;As it turns out, our specific problem was unrelated to any three of those bullet points.&lt;/p&gt;
&lt;p&gt;Our solution?&lt;/p&gt;
&lt;div class=&#34;note&#34;&gt;
  Make sure to install the &#34;Reporting Services Add-in for SharePoint Products&amp;#8221; to all Web Front-End (WFE) nodes of your SharePoint farm.
&lt;/div&gt;
&lt;p&gt;On the Feature Selection screen of SQL Server setup, you can simply choose &amp;ldquo;Reporting Services Add-in for SharePoint Products” on your other WFE nodes.  You don&amp;rsquo;t need to install the Database Engine Services or Reporting Services – SharePoint all over again…just the Add-in.&lt;/p&gt;
&lt;p&gt;We began to suspect it was something related to missing components on the other nodes because as I was troubleshooting, I noticed that if I attempted to create a Report Data Source enough times, I would be able to proceed to different stages of the creation process.  Sometimes I would hit the error right away.  Sometimes I would get to the page where I could fill out the connection details but when I would attempt to save the data source I would get one of the error pages described above.&lt;/p&gt;
&lt;p&gt;After we installed the Reporting Services Add-in on all WFE nodes, the problem stopped and we have not experienced any difficulty since.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Pointer Pointer #2</title>
      <link>https://www.andrewcbancroft.com/2012/06/24/pointer-pointer-2/</link>
      <pubDate>Sun, 24 Jun 2012 14:23:33 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2012/06/24/pointer-pointer-2/</guid>
      <description>&lt;p&gt;&lt;a href=&#34;http://andrewcbancroft.com/2012/06/17/pointer-pointer-1/&#34; title=&#34;Pointer Pointer #1&#34;&gt;Pointer Pointer #1 can be found here&lt;/a&gt; – it deals with how the &amp;ldquo;*” symbol is used in different places when dealing with pointers.&lt;/p&gt;
&lt;p&gt;A second pointer I have on pointers that helped me is this:&lt;/p&gt;
&lt;p&gt;A pointer stores an address of where some piece of data &amp;ldquo;lives” (begins its bit pattern) in memory.  What recently became clear to me is that a pointer, itself,_ _is &lt;em&gt;also&lt;/em&gt; stored as a bit pattern in memory.&lt;/p&gt;
&lt;p&gt;Here some example code and its output to help visualize what I&amp;rsquo;m saying:&lt;/p&gt;
&lt;p&gt;[cpp wraplines=”true”]&lt;br&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;/p&gt;
&lt;p&gt;int main(int argc, char *argv[])&lt;br&gt;
{&lt;br&gt;
int *intPointer;&lt;/p&gt;
&lt;p&gt;int i = 10;&lt;/p&gt;
&lt;p&gt;intPointer = &amp;amp;i;&lt;/p&gt;
&lt;p&gt;printf(&amp;ldquo;intPointer stores the address of i, which is: %pn&amp;rdquo;, intPointer);&lt;br&gt;
printf(&amp;ldquo;intPointer, itself, is stored at an address, which is different than the address intPointer stores: %pn&amp;rdquo;, &amp;amp;intPointer);&lt;/p&gt;
&lt;p&gt;return 0;&lt;br&gt;
}&lt;br&gt;
[/cpp]&lt;/p&gt;
&lt;p&gt;Output (on my machine):&lt;/p&gt;
&lt;p class=&#34;note&#34;&gt;
  intPointer stores the address of i, which is: &lt;strong&gt;0x7fff6bacab9c&lt;/strong&gt;&lt;br /&gt; intPointer, itself, is stored at an address, which is different than the address intPointer stores: &lt;strong&gt;0x7fff6bacaba0&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;This may or may not be obvious to you (it wasn&amp;rsquo;t to me until I understood a tiny bit of what&amp;rsquo;s happening a little further down in the computer&amp;rsquo;s layers of abstraction).  When it comes down to it, all of our C code is an abstraction.  C, and any high-level programming language removes the complexity of having to figure out how to lay down bit patterns in memory (among other things).&lt;/p&gt;
&lt;p&gt;The key insight I gained from this discovery is that &lt;em&gt;any&lt;/em&gt; type of variable, including pointers, that we declare and use in C code eventually translates to some bit pattern in memory that our computers use to do work with.  It took realizing that pointers, themselves were stored in memory to see this, but I&amp;rsquo;m glad it finally came to light because it&amp;rsquo;s helped de-mystify some of the mystery world of pointers.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Create Sound Clips from iTunes</title>
      <link>https://www.andrewcbancroft.com/2012/06/18/create-sound-clips-from-itunes/</link>
      <pubDate>Tue, 19 Jun 2012 00:48:38 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2012/06/18/create-sound-clips-from-itunes/</guid>
      <description>&lt;ol&gt;
&lt;li&gt;Open iTunes.&lt;/li&gt;
&lt;li&gt;(optional) Import any songs you&amp;rsquo;d like to make clips for.&lt;/li&gt;
&lt;li&gt;Select (click) the song that you&amp;rsquo;d like a sound clip for so that it&amp;rsquo;s the only one selected.&lt;/li&gt;
&lt;li&gt;Right-click that song and select &amp;ldquo;Get Info”.&lt;/li&gt;
&lt;li&gt;A window will appear – Click &amp;ldquo;Options” to access the screen to set your start and stop times.&lt;/li&gt;
&lt;li&gt;Check the boxes next to &amp;ldquo;Start Time” and &amp;ldquo;Stop Time”.&lt;/li&gt;
&lt;li&gt;Enter a start time in the text box next to &amp;ldquo;Start Time”.&lt;/li&gt;
&lt;li&gt;Enter a stop time in the text box next to &amp;ldquo;End Time”.&lt;/li&gt;
&lt;li&gt;Click &amp;ldquo;OK” to make that change to the song effective.&lt;/li&gt;
&lt;li&gt;Right-click that song again and select &amp;ldquo;Create AAC Version”.&lt;/li&gt;
&lt;li&gt;In the iTunes song list, you will now see two identically named songs.  The key differentiator between the two is the length of time the song is.  The sound clip you created will be less than or equal to the full-length song that it came from, but probably just less than if you truly created a sound &lt;em&gt;clip&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;One last time, right-click the &lt;em&gt;original&lt;/em&gt; song (the longer one), select &amp;ldquo;Get Info”, click &amp;ldquo;Options”, and &lt;em&gt;uncheck&lt;/em&gt; the &amp;ldquo;Start Time” and &amp;ldquo;Stop Time” check boxes.  If you don&amp;rsquo;t do this, the song will only play the timespan you specified every time you play it in iTunes.&lt;/li&gt;
&lt;li&gt;(optional) Rename the sound clip with an appropriate clip name.
&lt;ul&gt;
&lt;li&gt;Right-click the sound clip and click &amp;ldquo;Get Info”.&lt;/li&gt;
&lt;li&gt;Click the &amp;ldquo;Info” button this time to access the &amp;ldquo;Name” field.&lt;/li&gt;
&lt;li&gt;Type in an appropriate clip name in the &amp;ldquo;Name” field.&lt;/li&gt;
&lt;li&gt;Click &amp;ldquo;OK” to make that change effective.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;(optional) If you don&amp;rsquo;t want the sound clip to appear in your iTunes library, you can move it to another folder and remove it from the library so that it&amp;rsquo;s not junking up your normal song list.&lt;/li&gt;
&lt;li&gt;Right-click the sound clip.
&lt;ul&gt;
&lt;li&gt;Click &amp;ldquo;Show in Finder”&lt;/li&gt;
&lt;li&gt;In my opinion it&amp;rsquo;s always easiest to get the file somewhere that&amp;rsquo;s easy to find and work with it from there.  You can use the Desktop for this purpose if you&amp;rsquo;d like.  In Finder, you can press and hold the Command key, click the sound clip, and drag it to Desktop on the left.  The combo of Command+click+drag+let go on Desktop effectively moves it to that location.&lt;/li&gt;
&lt;li&gt;Close Finder so that you&amp;rsquo;re back on your iTunes window.&lt;/li&gt;
&lt;li&gt;Click the sound clip.&lt;/li&gt;
&lt;li&gt;Press the delete key and click &amp;ldquo;Delete Song” on the warning window.  This will remove it from your iTunes library.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Done!&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Pointer Pointer #1</title>
      <link>https://www.andrewcbancroft.com/2012/06/17/pointer-pointer-1/</link>
      <pubDate>Sun, 17 Jun 2012 19:45:05 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2012/06/17/pointer-pointer-1/</guid>
      <description>&lt;p&gt;Part of my confusion with the C family of languages has been how the * symbol worked in the various places it&amp;rsquo;s used in relation to pointers. A key insight helped de-mystify a lot for me:&lt;/p&gt;
&lt;p&gt;The * symbol is used as both a &lt;em&gt;type&lt;/em&gt; declaration &lt;em&gt;and&lt;/em&gt; as an &lt;em&gt;operator&lt;/em&gt; (the de-reference operator).&lt;/p&gt;
&lt;div&gt;
  Example:
&lt;/div&gt;
&lt;pre class=&#34;brush: c&#34;&gt;// * used as a type declaration
    int *intPointer;

    // declare a regular integer variable named i
    int i = 10;

    // set where intPointer points to by assigning it the address of i
    intPointer = &amp;i;

    // * used as the de-reference operator
    printf(&#34;Value returned when intPointer is de-referenced: %dn&#34;, *intPointer);
```

On line 2 the variable intPointer is declared to be a variable of _type_ pointer to an integer.

On line 8 I tell intPointer where to point to, namely the address of i in memory.  It&#39;s like intPointer is asking, &#34;Which integer do I point to?&amp;#8221; and the answer comes, &#34;This one&amp;#8230;the one at the address of i&amp;#8221;, which is expressed in C as &amp;i.

After intPointer is set to point to some spot (address) in memory, the _value_ of whatever it points to can be accessed by applying the de-reference operator, which just so happens to use the same * that was used to declare the variable intPointer on line 2. And that&#39;s precisely where my brain went cross-eyed.

Since * is used to declare a variable of type pointer to &lt;span style=&#34;text-decoration: underline;&#34;&gt;[some type of data]&lt;/span&gt; _and_ as the de-reference operator which gets at the value that a pointer variable points to, my mind had a hard time separating what was happening.

Line 11 is where intPointer is &#34;de-referenced&amp;#8221; to get the value stored at the location in memory where intPointer points to. It says, &#34;Follow intPointer to where it&#39;s pointing to in memory (which in this case is the address of the variable i), open the box there, and let me see the value that&#39;s at that spot.&amp;#8221;  When I run this code, the number 10 is printed to the console as expected.

In summary:  Pointer pointer #1 attempts to make clear that the * symbol&#39;s meaning is overloaded.  Not only is it used for multiplication, but when dealing with pointers it&#39;s used to declare a variable of type pointer to &lt;span style=&#34;text-decoration: underline;&#34;&gt;[some type of data]&lt;/span&gt; _and_ as the de-reference operator which gets at the value that a pointer variable points to.

[A second pointer pointer can be found here.][1]  It deals with the difference between the address the pointer holds and the address that holds the pointer.

 [1]: http://andrewcbancroft.com/2012/06/24/pointer-pointer-2/ &#34;Pointer Pointer #2&#34;</description>
    </item>
    
    <item>
      <title>From SQL Queries to Data Modeling</title>
      <link>https://www.andrewcbancroft.com/2012/06/16/from-sql-queries-to-data-modeling/</link>
      <pubDate>Sat, 16 Jun 2012 19:25:15 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2012/06/16/from-sql-queries-to-data-modeling/</guid>
      <description>&lt;p&gt;This past week I had the opportunity to attend Microsoft TechEd 2012. Lots and lots of great material presented – I&amp;rsquo;m pretty stoked about some of the amazing features of SQL Server 2012&amp;rsquo;s BI stack.&lt;/p&gt;
&lt;p&gt;During the course of the week I discovered something that has been emerging in my thought process for a while, but sort of hit the level of practicality as I saw one particular feature demonstrated: the BI Semantic Model. I walked away from session after session thinking, &amp;ldquo;Man, this is so doable now!!”. This was the single most impressive and perspective altering concept that flooded into my brain this week.&lt;/p&gt;
&lt;p&gt;There comes a time in every organization that necessitates a paradigm shift: One that involves a move &lt;em&gt;toward&lt;/em&gt; data &lt;em&gt;modeling&lt;/em&gt; and a &lt;em&gt;away&lt;/em&gt; from SQL query writing for the purpose of supplying data sets to reports. By no means am I suggesting that writing SQL will disappear for database / BI Developers – but as a means to serving as the backbone of an organization&amp;rsquo;s reporting infrastructure, there will inevitably come a time that employing more and more people capable of writing SQL will be unsustainable.&lt;/p&gt;
&lt;p&gt;The reasons for this are clear: As an enterprise continuously builds or purchases data collection systems, the users of those systems expect to be able to get &lt;em&gt;information&lt;/em&gt; back out. So they ask questions. Unpredictable combinations of questions. They are too numerous and too diverse for even a dedicated team of reporting people to write SQL to a database every time a business person has a question they want to ask of their data. For a while, parameterized reports suffice for most questions. But still the organization grows and evolves, and decision makers expect that they should be able to analyze information at their convenience, not at the mercy of the IT Department&amp;rsquo;s work schedule.&lt;/p&gt;
&lt;p&gt;Microsoft has worked hard to give BI developers effective ways to build models so as to enable us to write less SQL (at least when it comes to supplying answers to the multitude of questions that come our way) and empower our business folks to create their own reports that are based on the model we build. In my view, this is the direction that we must inevitably go. Without this, IT becomes a significant bottleneck to the progress that could be made from timely data analysis.&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;m personally looking forward to using SQL Server 2012 to promote better query-ability and visibility of the information my customers are dying to get their hands on!&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Contributing My Share of Bytes</title>
      <link>https://www.andrewcbancroft.com/2012/06/03/contributing-my-share-of-bytes/</link>
      <pubDate>Mon, 04 Jun 2012 03:16:33 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/2012/06/03/contributing-my-share-of-bytes/</guid>
      <description>&lt;p&gt;There is a lot to know. A &lt;em&gt;lot&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;I happen to be one of those individuals who gets geeked out very, very often over very, very nerdy things.&lt;/p&gt;
&lt;p&gt;As of late I&amp;rsquo;ve been really wanting an outlet to share what I&amp;rsquo;ve learned / am learn_ing._ The world doesn&amp;rsquo;t need another blog detailing the same things that are already out there – at least not a verbatim copy of information that&amp;rsquo;s available already.&lt;/p&gt;
&lt;p&gt;But the thing I&amp;rsquo;ve figured out is that everybody has a slant on how they glean understanding on something that&amp;rsquo;s unique to them. I can&amp;rsquo;t tell you how many times I&amp;rsquo;ve read multiple blogs full of code or with a perspective on an idea and sort of used the combination of them all to come up with a better understanding on ___ topic that ends up working for me.&lt;/p&gt;
&lt;p&gt;I recently read a tweet from Scott Hanselman&lt;br&gt;
&lt;a href=&#34;https://twitter.com/shanselman/status/208607212594147328&#34;&gt;here&lt;/a&gt; that caused something to just click in my mind: he encouraged blogging rather than emailing. This makes sense – emails and other forms of direct communication have limited potential for helping effect. A blog post and an email with a link to the blog post has much greater potential for impact.&lt;/p&gt;
&lt;p&gt;And then I took that and theorized about what could be possible if I just had an outlet to share what I&amp;rsquo;ve run across, because you never know when you might have the answer someone was looking for – they just needed you to share it in your own unique way that spoke to their particular scheme of processing information.&lt;/p&gt;
&lt;p&gt;So here&amp;rsquo;s to hoping that maybe possibly somehow someday someone will find something of interest somewhere in this blog of mine. For now i foresee posts about some of the following:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Thoughts on code that I&amp;rsquo;m learning / writing&lt;/li&gt;
&lt;li&gt;Stuff about SQL Server and Business Intelligence&lt;/li&gt;
&lt;li&gt;Discoveries from other realms of study&lt;/li&gt;
&lt;li&gt;Tutorials on things I&amp;rsquo;ve figured out and think might help someone someday&lt;/li&gt;
&lt;li&gt;Thoughts / reactions / responses to news&lt;/li&gt;
&lt;li&gt;Who knows what else…&lt;/li&gt;
&lt;/ol&gt;
</description>
    </item>
    
    <item>
      <title>Andrew Bancroft - Portfolio</title>
      <link>https://www.andrewcbancroft.com/portfolio/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://www.andrewcbancroft.com/portfolio/</guid>
      <description>&lt;section class=&#34;p-5 text-center container&#34;&gt;
  &lt;div class=&#34;row pt-5&#34;&gt;
    &lt;div class=&#34;col-lg-6 col-md-8 mx-auto&#34;&gt;
        &lt;div class=&#34;site-intro grav-inner&#34;&gt;
          &lt;div class=&#34;grav-grav&#34;&gt;
            &lt;img alt=&#34;Andrew Bancroft&#34;
              src=&#34;https://secure.gravatar.com/avatar/693d6ce36672340c22f55dd4f44f9f69?s=256&amp;amp;d=mm&amp;amp;r=g&#34;
              srcset=&#34;https://secure.gravatar.com/avatar/693d6ce36672340c22f55dd4f44f9f69?s=512&amp;amp;d=mm&amp;amp;r=g 2x&#34;
              class=&#34;avatar avatar-256 photo grav-hashed&#34; height=&#34;256&#34; width=&#34;256&#34;
              id=&#34;grav-693d6ce36672340c22f55dd4f44f9f69-0&#34;&gt;
          &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/section&gt;
&lt;section&gt;
&lt;div class=&#34;container&#34;&gt;
    &lt;h5&gt;Hi 👋🏻 I’m Andrew.&lt;/h5&gt;
    &lt;p&gt;
    I am a people-driven, creative, impactful &lt;span style=&#34;font-variant:small-caps;color:#3e8bc5&#34;&gt;leader, developer, and teacher&lt;/span&gt; with more than 15 years of experience helping others succeed with technology.
    &lt;/p&gt;
    &lt;p&gt;
    I have &lt;span style=&#34;font-variant:small-caps;color:#3e8bc5&#34;&gt;broad experience&lt;/span&gt; in data analytics, data governance, cloud platforms, software development, and education, and an &lt;span style=&#34;font-variant:small-caps;color:#3e8bc5&#34;&gt;established record&lt;/span&gt; of leading the delivery of solutions to complex technology problems that advance business objectives and delight customers.
    &lt;/p&gt;
&lt;/div&gt;
&lt;/section&gt;
&lt;main&gt;
&lt;nav class=&#34;navbar navbar-expand-lg bg-body-tertiary&#34;&gt;
  &lt;div class=&#34;container-fluid&#34;&gt;
    &lt;div class=&#34;navbar-collapse justify-content-center&#34; id=&#34;navbarNavAltMarkup&#34;&gt;
      &lt;div class=&#34;navbar-nav&#34;&gt;
        &lt;a class=&#34;nav-link&#34; aria-current=&#34;page&#34; href=&#34;#apps&#34;&gt;Apps&lt;/a&gt;
        &lt;a class=&#34;nav-link&#34; href=&#34;#courses&#34;&gt;Courses&lt;/a&gt;
        &lt;a class=&#34;nav-link&#34; href=&#34;#data&#34;&gt;Data &amp; Analytics&lt;/a&gt;
        &lt;a class=&#34;nav-link&#34; href=&#34;#robotics&#34;&gt;Robotics&lt;/a&gt;
        &lt;a class=&#34;nav-link&#34; href=&#34;#skill-assessments&#34;&gt;Skill Assessments&lt;/a&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/nav&gt;
&lt;div id=&#34;apps&#34; class=&#34;album py-5 bg-body-tertiary&#34;&gt;
    &lt;div class=&#34;container&#34;&gt;
        &lt;h1&gt;Apps&lt;/h1&gt;
        &lt;div class=&#34;row row-cols-1 row-cols-sm-2 row-cols-md-3 g-3&#34;&gt;
            &lt;div class=&#34;col d-flex align-items-stretch&#34;&gt;
                &lt;div class=&#34;card shadow-sm&#34;&gt;
                    &lt;a data-bs-toggle=&#34;modal&#34; data-bs-target=&#34;#noble-oe-modal&#34; role=&#34;button&#34;&gt;&lt;img src=&#34;noble-oe-main.png&#34;
                            class=&#34;card-img-top&#34; alt=&#34;Noble Open Enrollment&#34; /&gt;&lt;/a&gt;
                    &lt;div class=&#34;card-body d-flex flex-column&#34;&gt;
                        &lt;h5 class=&#34;card-title&#34;&gt;Open Enrollment&lt;/h5&gt;
                        &lt;h6 class=&#34;text-body-secondary&#34;&gt;Noble Research Institute&lt;/h6&gt;
                        &lt;p class=&#34;card-text&#34;&gt;I led the development effort for Noble&#39;s internally-built Open Enrollment app that allowed employees to:
                            &lt;ul&gt;
                                &lt;li&gt;Edit personal information&lt;/li&gt;
                                &lt;li&gt;Add and mangage dependents&lt;/li&gt;
                                &lt;li&gt;View and change enrollment elections&lt;/li&gt;
                                &lt;li&gt;Receive important follow-up notifications about their enrollment status&lt;/li&gt;
                            &lt;/ul&gt;
                        &lt;/p&gt;
                        &lt;div class=&#34;d-flex justify-content-between align-items-center mt-auto&#34;&gt;
                            &lt;small class=&#34;text-body-secondary&#34;&gt;ASP.Net MVC | C# | SQL Server | JQuery&lt;/small&gt;
                        &lt;/div&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
            &lt;/div&gt;
            &lt;div class=&#34;col d-flex align-items-stretch&#34;&gt;
                &lt;div class=&#34;card shadow-sm&#34;&gt;
                    &lt;a data-bs-toggle=&#34;modal&#34; data-bs-target=&#34;#noble-materials-inventory-modal&#34; role=&#34;button&#34;&gt;&lt;img src=&#34;noble-materials-inventory.png&#34;
                            class=&#34;card-img-top&#34; alt=&#34;Noble Materials Inventory&#34; /&gt;&lt;/a&gt;
                    &lt;div class=&#34;card-body d-flex flex-column&#34;&gt;
                        &lt;h5 class=&#34;card-title&#34;&gt;Materials Inventory&lt;/h5&gt;
                        &lt;h6 class=&#34;text-body-secondary&#34;&gt;Noble Research Institute&lt;/h6&gt;
                        &lt;p class=&#34;card-text&#34;&gt;I led the development of a biological materials inventory management system that enabled Noble&#39;s research staff to:
                            &lt;ul&gt;
                                &lt;li&gt;Manage biological material inventory entries&lt;/li&gt;
                                &lt;li&gt;Upload attachments&lt;/li&gt;
                                &lt;li&gt;Search for materials in the database&lt;/li&gt;
                            &lt;/ul&gt;
                        &lt;/p&gt;
                        &lt;div class=&#34;d-flex justify-content-between align-items-center mt-auto&#34;&gt;
                            &lt;small class=&#34;text-body-secondary&#34;&gt;ASP.Net MVC | C# | SQL Server | JQuery&lt;/small&gt;
                        &lt;/div&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
            &lt;/div&gt;
            &lt;div class=&#34;col d-flex align-items-stretch&#34;&gt;
                &lt;div class=&#34;card shadow-sm&#34;&gt;
                    &lt;a data-bs-toggle=&#34;modal&#34; data-bs-target=&#34;#noble-rdms-modal&#34; role=&#34;button&#34;&gt;&lt;img src=&#34;noble-rdms.png&#34;
                            class=&#34;card-img-top&#34; alt=&#34;Noble Research Data Management System&#34; /&gt;&lt;/a&gt;
                    &lt;div class=&#34;card-body d-flex flex-column&#34;&gt;
                        &lt;h5 class=&#34;card-title&#34;&gt;Research Data Management System&lt;/h5&gt;
                                                &lt;h6 class=&#34;text-body-secondary&#34;&gt;Noble Research Institute&lt;/h6&gt;
                            &lt;p class=&#34;card-text&#34;&gt;I contributed server-side code adjustments, and co-developed the file management features of Noble&#39;s Research Data Management System that allowed research staff to:
                            &lt;ul&gt;
                                &lt;li&gt;Create and manage projects&lt;/li&gt;
                                &lt;li&gt;Create and manage experiments within projects&lt;/li&gt;
                                &lt;li&gt;Upload and share results from processing samples&lt;/li&gt;
                            &lt;/ul&gt;
                        &lt;/p&gt;
                        &lt;div class=&#34;d-flex justify-content-between align-items-center mt-auto&#34;&gt;
                            &lt;small class=&#34;text-body-secondary&#34;&gt;ASP.Net Web Forms | C# | VB.Net | SQL Server | JQuery&lt;/small&gt;
                        &lt;/div&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
            &lt;/div&gt;
            &lt;div class=&#34;col d-flex align-items-stretch&#34;&gt;
                &lt;div class=&#34;card shadow-sm&#34;&gt;
                    &lt;a data-bs-toggle=&#34;modal&#34; data-bs-target=&#34;#noble-jr-beef-modal&#34; role=&#34;button&#34;&gt;&lt;img src=&#34;noble-jr-beef.png&#34;
                            class=&#34;card-img-top&#34; alt=&#34;Noble Jr. Beef&#34; /&gt;&lt;/a&gt;
                    &lt;div class=&#34;card-body d-flex flex-column&#34;&gt;
                        &lt;h5 class=&#34;card-title&#34;&gt;Jr. Beef&lt;/h5&gt;
                        &lt;h6 class=&#34;text-body-secondary&#34;&gt;Noble Research Institute&lt;/h6&gt;
                            &lt;p class=&#34;card-text&#34;&gt;I led the development of Noble&#39;s first native iOS app called Jr. Beef. This app enabled high school students who participated in Noble&#39;s annual steer raising competition to:
                            &lt;ul&gt;
                                &lt;li&gt;Create records of steers&lt;/li&gt;
                                &lt;li&gt;Record steer weights over time&lt;/li&gt;
                                &lt;li&gt;Log health and feeding events&lt;/li&gt;
                                &lt;li&gt;Add journal entries describing important notes and observations&lt;/li&gt;
                                &lt;li&gt;Review a steer&#39;s performance&lt;/li&gt;
                            &lt;/ul&gt;
                        &lt;/p&gt;
                        &lt;div class=&#34;d-flex justify-content-between align-items-center mt-auto&#34;&gt;
                            &lt;small class=&#34;text-body-secondary&#34;&gt;Objective-C | UIKit | SQL Server&lt;/small&gt;
                        &lt;/div&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
            &lt;/div&gt;
            &lt;div class=&#34;col d-flex align-items-stretch&#34;&gt;
                &lt;div class=&#34;card shadow-sm&#34;&gt;
                    &lt;a data-bs-toggle=&#34;modal&#34; data-bs-target=&#34;#noble-ag-tools-modal&#34; role=&#34;button&#34;&gt;&lt;img src=&#34;noble-ag-tools.png&#34;
                            class=&#34;card-img-top&#34; alt=&#34;Noble Research Data Management System&#34; /&gt;&lt;/a&gt;
                    &lt;div class=&#34;card-body d-flex flex-column&#34;&gt;
                        &lt;h5 class=&#34;card-title&#34;&gt;Ag Tools&lt;/h5&gt;
                        &lt;h6 class=&#34;text-body-secondary&#34;&gt;Noble Research Institute&lt;/h6&gt;
                            &lt;p class=&#34;card-text&#34;&gt;I contributed to the development of Ag Tools, a cross-platform mobile app that provided various agricultural calculators to farmers and ranchers.
                        &lt;/p&gt;
                        &lt;div class=&#34;d-flex justify-content-between align-items-center mt-auto&#34;&gt;
                            &lt;small class=&#34;text-body-secondary&#34;&gt;C# | Xamarin&lt;/small&gt;
                        &lt;/div&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
            &lt;/div&gt;
            &lt;div class=&#34;col d-flex align-items-stretch&#34;&gt;
                &lt;div class=&#34;card shadow-sm&#34;&gt;
                    &lt;a data-bs-toggle=&#34;modal&#34; data-bs-target=&#34;#express-chiropractic-modal&#34; role=&#34;button&#34;&gt;&lt;img src=&#34;express-chiropractic.png&#34;
                            class=&#34;card-img-top&#34; alt=&#34;Express Chiropractic&#34; /&gt;&lt;/a&gt;
                    &lt;div class=&#34;card-body d-flex flex-column&#34;&gt;
                        &lt;h5 class=&#34;card-title&#34;&gt;Express Chiropractic&lt;/h5&gt;
                        &lt;h6 class=&#34;text-body-secondary&#34;&gt;Bancroft Innovations and Technology Solutions&lt;/h6&gt;
                        &lt;p class=&#34;card-text&#34;&gt;My business partner and I created a cross-platform mobile app for Express Chiropractic to enhance the overall customer experience for their practice members. The app enabled people to:
                            &lt;ul&gt;
                                &lt;li&gt;Choose a home location&lt;/li&gt;
                                &lt;li&gt;Check if the location is currently open or closed before heading out the door&lt;/li&gt;
                                &lt;li&gt;View the location&#39;s regular weekly scheduled hours and any changes in the regular schedule&lt;/li&gt;
                                &lt;li&gt;Receive important announcements from clinics via push notification&lt;/li&gt;
                            &lt;/ul&gt;
                        &lt;/p&gt;
                        &lt;div class=&#34;d-flex justify-content-between align-items-center mt-auto&#34;&gt;
                            &lt;small class=&#34;text-body-secondary&#34;&gt;ReactNative | Firebase&lt;/small&gt;
                        &lt;/div&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
            &lt;/div&gt;
            &lt;div class=&#34;col d-flex align-items-stretch&#34;&gt;
                &lt;div class=&#34;card shadow-sm&#34;&gt;
                    &lt;a data-bs-toggle=&#34;modal&#34; data-bs-target=&#34;#express-chiropractic-admin-modal&#34; role=&#34;button&#34;&gt;&lt;img src=&#34;express-chiropractic-admin.png&#34;
                            class=&#34;card-img-top&#34; alt=&#34;Express Chiropractic Admin&#34; /&gt;&lt;/a&gt;
                    &lt;div class=&#34;card-body d-flex flex-column&#34;&gt;
                        &lt;h5 class=&#34;card-title&#34;&gt;Express Chiropractic Admin&lt;/h5&gt;
                        &lt;h6 class=&#34;text-body-secondary&#34;&gt;Bancroft Innovations and Technology Solutions&lt;/h6&gt;
                        &lt;p class=&#34;card-text&#34;&gt;My business partner and I built a back-end admin panel to allow Express Chiropractic employees to:
                            &lt;ul&gt;
                                &lt;li&gt;Customize text and images that the mobile app displays&lt;/li&gt;
                                &lt;li&gt;Manage regular weekly scheduled hours&lt;/li&gt;
                                &lt;li&gt;Make changes in schedule&lt;/li&gt;
                                &lt;li&gt;Send push notifications to clients&lt;/li&gt;
                            &lt;/ul&gt;
                        &lt;/p&gt;
                        &lt;div class=&#34;d-flex justify-content-between align-items-center mt-auto&#34;&gt;
                            &lt;small class=&#34;text-body-secondary&#34;&gt;React | Firebase&lt;/small&gt;
                        &lt;/div&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
            &lt;/div&gt;
            &lt;div class=&#34;col d-flex align-items-stretch&#34;&gt;
                &lt;div class=&#34;card shadow-sm&#34;&gt;
                    &lt;a data-bs-toggle=&#34;modal&#34; data-bs-target=&#34;#paradigm-hormones-modal&#34; role=&#34;button&#34;&gt;&lt;img src=&#34;paradigm-hormones.png&#34;
                            class=&#34;card-img-top&#34; alt=&#34;Paradigm Hormones&#34; /&gt;&lt;/a&gt;
                    &lt;div class=&#34;card-body d-flex flex-column&#34;&gt;
                    &lt;h5 class=&#34;card-title&#34;&gt;Paradigm Hormones&lt;/h5&gt;
                    &lt;h6 class=&#34;text-body-secondary&#34;&gt;Bancroft Innovations and Technology Solutions&lt;/h6&gt;
                        &lt;p class=&#34;card-text&#34;&gt;My business partner and I built a web application for Paradigm Hormones to:
                            &lt;ul&gt;
                                &lt;li&gt;Enable clinicians to enter patient parameters&lt;/li&gt;
                                &lt;li&gt;Assist clinicians in calculating appropriate hormone replacement levels&lt;/li&gt;
                            &lt;/ul&gt;
                        &lt;/p&gt;
                        &lt;div class=&#34;d-flex justify-content-between align-items-center mt-auto&#34;&gt;
                            &lt;small class=&#34;text-body-secondary&#34;&gt;React | Firebase&lt;/small&gt;
                        &lt;/div&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
            &lt;/div&gt;
            &lt;div class=&#34;col d-flex align-items-stretch&#34;&gt;
                &lt;div class=&#34;card shadow-sm&#34;&gt;
                    &lt;a data-bs-toggle=&#34;modal&#34; data-bs-target=&#34;#ten-read-ten-said-modal&#34; role=&#34;button&#34;&gt;&lt;img src=&#34;ten-read-ten-said.png&#34;
                            class=&#34;card-img-top&#34; alt=&#34;10 Read 10 Said&#34; /&gt;&lt;/a&gt;
                    &lt;div class=&#34;card-body d-flex flex-column&#34;&gt;
                    &lt;h5 class=&#34;card-title&#34;&gt;10 Read 10 Said&lt;/h5&gt;
                    &lt;h6 class=&#34;text-body-secondary&#34;&gt;Bancroft Innovations and Technology Solutions&lt;/h6&gt;
                        &lt;p class=&#34;card-text&#34;&gt;One of the first apps I built for myself as a way to facilitate the &#34;10 Read 10 Said&#34; memorization strategy:
                            &lt;ul&gt;
                                &lt;li&gt;Enter text to memorize&lt;/li&gt;
                                &lt;li&gt;Read the text out loud 10 times while looking at it&lt;/li&gt;
                                &lt;li&gt;Say the text again out loud 10 times without looking&lt;/li&gt;
                            &lt;/ul&gt;
                        &lt;/p&gt;
                        &lt;div class=&#34;d-flex justify-content-between align-items-center mt-auto&#34;&gt;
                            &lt;small class=&#34;text-body-secondary&#34;&gt;Swift | UIKit | SwiftUI&lt;/small&gt;
                        &lt;/div&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;courses&#34; class=&#34;album py-5 bg-body-tertiary&#34;&gt;
    &lt;div class=&#34;container&#34;&gt;
    &lt;h1&gt;Courses&lt;/h1&gt;
        &lt;div class=&#34;row row-cols-1 row-cols-sm-2 row-cols-md-3 g-3&#34;&gt;
            &lt;div class=&#34;col d-flex align-items-stretch&#34;&gt;
                &lt;div class=&#34;card shadow-sm&#34;&gt;
                    &lt;img src=&#34;ios-17-fundamentals-title.png&#34; class=&#34;card-img-top&#34; /&gt;
                    &lt;div class=&#34;card-body d-flex flex-column&#34;&gt;
                        &lt;p class=&#34;card-text&#34;&gt;This course will teach you to make apps using Xcode, Swift, and SwiftUI that support the features users will expect when they upgrade to iOS 17.&lt;/p&gt;
                        &lt;div class=&#34;d-flex justify-content-between align-items-center mt-auto&#34;&gt;
                            &lt;div class=&#34;btn-group&#34;&gt;
                                &lt;a href=&#34;https://bit.ly/ios-17-fundamentals&#34;&gt;&lt;button type=&#34;button&#34; class=&#34;btn btn-sm btn-outline-secondary&#34;&gt;View&lt;/button&gt;&lt;/a&gt;
                            &lt;/div&gt;
                            &lt;small class=&#34;text-body-secondary&#34;&gt;Dec 7, 2023&lt;/small&gt;
                            &lt;small class=&#34;text-body-secondary&#34;&gt;5h 5m&lt;/small&gt;
                        &lt;/div&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
            &lt;/div&gt;
            &lt;div class=&#34;col d-flex align-items-stretch&#34;&gt;
                &lt;div class=&#34;card shadow-sm&#34;&gt;
                    &lt;img src=&#34;building-ios-user-interfaces-with-swiftui-title.png&#34; class=&#34;card-img-top&#34; alt=&#34;Building iOS User Interfaces with SwiftUI&#34; /&gt;
                    &lt;div class=&#34;card-body d-flex flex-column&#34;&gt;
                        &lt;p class=&#34;card-text&#34;&gt;This course will teach you to make iOS apps with engaging user interfaces using Xcode, Swift, and SwiftUI.&lt;/p&gt;
                        &lt;div class=&#34;d-flex justify-content-between align-items-center mt-auto&#34;&gt;
                            &lt;div class=&#34;btn-group&#34;&gt;
                                &lt;a href=&#34;https://bit.ly/building-ios-ui-swiftui&#34;&gt;&lt;button type=&#34;button&#34; class=&#34;btn btn-sm btn-outline-secondary&#34;&gt;View&lt;/button&gt;&lt;/a&gt;
                            &lt;/div&gt;
                            &lt;small class=&#34;text-body-secondary&#34;&gt;Aug 22, 2021&lt;/small&gt;
                            &lt;small class=&#34;text-body-secondary&#34;&gt;3h 16m&lt;/small&gt; 
                        &lt;/div&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
            &lt;/div&gt;
            &lt;div class=&#34;col d-flex align-items-stretch&#34;&gt;
                &lt;div class=&#34;card shadow-sm&#34;&gt;
                    &lt;img src=&#34;ios-14-getting-started-title.png&#34; class=&#34;card-img-top&#34; alt=&#34;iOS 14: Getting Started&#34; /&gt;
                    &lt;div class=&#34;card-body d-flex flex-column&#34;&gt;
                        &lt;p class=&#34;card-text&#34;&gt;This course will teach you to make iOS apps with adaptive user interfaces that work on multiple devices using Xcode, Swift, and SwiftUI.&lt;/p&gt;
                        &lt;div class=&#34;d-flex justify-content-between align-items-center mt-auto&#34;&gt;
                            &lt;div class=&#34;btn-group&#34;&gt;
                                &lt;a href=&#34;https://bit.ly/iOS14-getting-started&#34;&gt;&lt;button type=&#34;button&#34; class=&#34;btn btn-sm btn-outline-secondary&#34;&gt;View&lt;/button&gt;&lt;/a&gt;
                            &lt;/div&gt;
                            &lt;small class=&#34;text-body-secondary&#34;&gt;Dec 3, 2020&lt;/small&gt;
                            &lt;small class=&#34;text-body-secondary&#34;&gt;4h 49m&lt;/small&gt;
                        &lt;/div&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
            &lt;/div&gt;
            &lt;div class=&#34;col d-flex align-items-stretch&#34;&gt;
                &lt;div class=&#34;card shadow-sm&#34;&gt;
                    &lt;img src=&#34;implementing-in-app-purchases-on-ios-title.png&#34; class=&#34;card-img-top&#34; alt=&#34;Implementing In-app Purchases on iOS&#34; /&gt;
                    &lt;div class=&#34;card-body d-flex flex-column&#34;&gt;
                        &lt;p class=&#34;card-text&#34;&gt;This course will teach you how to offer digital content as in-app purchases for your iOS apps.&lt;/p&gt;
                        &lt;div class=&#34;d-flex justify-content-between align-items-center mt-auto&#34;&gt;
                            &lt;div class=&#34;btn-group&#34;&gt;
                                &lt;a href=&#34;https://bit.ly/implementing-in-app-purchases-ios&#34;&gt;&lt;button type=&#34;button&#34; class=&#34;btn btn-sm btn-outline-secondary&#34;&gt;View&lt;/button&gt;&lt;/a&gt;
                            &lt;/div&gt;
                            &lt;small class=&#34;text-body-secondary&#34;&gt;Jun 8, 2020&lt;/small&gt;
                            &lt;small class=&#34;text-body-secondary&#34;&gt;2h 20m&lt;/small&gt;
                        &lt;/div&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
            &lt;/div&gt;
            &lt;div class=&#34;col d-flex align-items-stretch&#34;&gt;
                &lt;div class=&#34;card shadow-sm&#34;&gt;
                    &lt;img src=&#34;ios-debugging-fundamentals-title.png&#34; class=&#34;card-img-top&#34; alt=&#34;iOS Debugging Fundamentals&#34; /&gt;
                    &lt;div class=&#34;card-body d-flex flex-column&#34;&gt;
                        &lt;p class=&#34;card-text&#34;&gt;This course will teach you how to combine problem solving skills and Xcode’s debugging tools to maximize your bug fixing potential.&lt;/p&gt;
                        &lt;div class=&#34;d-flex justify-content-between align-items-center mt-auto&#34;&gt;
                            &lt;div class=&#34;btn-group&#34;&gt;
                                &lt;a href=&#34;https://bit.ly/ios-debugging-fundamentals&#34;&gt;&lt;button type=&#34;button&#34; class=&#34;btn btn-sm btn-outline-secondary&#34;&gt;View&lt;/button&gt;&lt;/a&gt;
                            &lt;/div&gt;
                            &lt;small class=&#34;text-body-secondary&#34;&gt;Dec 23, 2019&lt;/small&gt;
                            &lt;small class=&#34;text-body-secondary&#34;&gt;1h 25m&lt;/small&gt;
                        &lt;/div&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
            &lt;/div&gt;
            &lt;div class=&#34;col d-flex align-items-stretch&#34;&gt;
                &lt;div class=&#34;card shadow-sm&#34;&gt;
                    &lt;img src=&#34;ios-data-persistence-the-big-picture-title.png&#34; class=&#34;card-img-top&#34; alt=&#34;iOS Data Persistence: The Big Picture&#34; /&gt;
                    &lt;div class=&#34;card-body d-flex flex-column&#34;&gt;
                        &lt;p class=&#34;card-text&#34;&gt;This course will teach you the big picture possibilities of data persistence on iOS, what to expect if you choose one technology over another, and get the feel for what it’s like to use popular data solutions in code.&lt;/p&gt;
                        &lt;div class=&#34;d-flex justify-content-between align-items-center mt-auto&#34;&gt;
                            &lt;div class=&#34;btn-group&#34;&gt;
                                &lt;a href=&#34;https://bit.ly/ios-data-persistence-bp&#34;&gt;&lt;button type=&#34;button&#34; class=&#34;btn btn-sm btn-outline-secondary&#34;&gt;View&lt;/button&gt;&lt;/a&gt;
                            &lt;/div&gt;
                            &lt;small class=&#34;text-body-secondary&#34;&gt;Dec 23, 2019&lt;/small&gt;
                            &lt;small class=&#34;text-body-secondary&#34;&gt;1h 25m&lt;/small&gt;
                        &lt;/div&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
            &lt;/div&gt;
            &lt;div class=&#34;col d-flex align-items-stretch&#34;&gt;
                &lt;div class=&#34;card shadow-sm&#34;&gt;
                    &lt;img src=&#34;cloudkit-fundamentals-title.png&#34; class=&#34;card-img-top&#34; alt=&#34;CloudKit Fundamentals&#34; /&gt;
                    &lt;div class=&#34;card-body d-flex flex-column&#34;&gt;
                        &lt;p class=&#34;card-text&#34;&gt;This course will teach you the basics of using Apple’s flagship framework for remote data storage and sharing. Learn to model, save, and query data, synchronize data across devices, handle errors, and deploy to production.&lt;/p&gt;
                        &lt;div class=&#34;d-flex justify-content-between align-items-center mt-auto&#34;&gt;
                            &lt;div class=&#34;btn-group&#34;&gt;
                                &lt;a href=&#34;https://bit.ly/cloudkit-fundamentals&#34;&gt;&lt;button type=&#34;button&#34; class=&#34;btn btn-sm btn-outline-secondary&#34;&gt;View&lt;/button&gt;&lt;/a&gt;
                            &lt;/div&gt;
                            &lt;small class=&#34;text-body-secondary&#34;&gt;Sept 19, 2018&lt;/small&gt;
                            &lt;small class=&#34;text-body-secondary&#34;&gt;3h 31m&lt;/small&gt;
                        &lt;/div&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
            &lt;/div&gt;
            &lt;div class=&#34;col d-flex align-items-stretch&#34;&gt;
                &lt;div class=&#34;card shadow-sm&#34;&gt;
                    &lt;img src=&#34;core-data-fundamentals-with-swift-title.png&#34; class=&#34;card-img-top&#34; alt=&#34;Core Data Fundamentals with Swift&#34; /&gt;
                    &lt;div class=&#34;card-body d-flex flex-column&#34;&gt;
                        &lt;p class=&#34;card-text&#34;&gt;This course will give you the essential skills you need to architect an app to use Core Data. You&#39;ll learn to model, manage, and display data using Core Data in this course.&lt;/p&gt;
                        &lt;div class=&#34;d-flex justify-content-between align-items-center mt-auto&#34;&gt;
                            &lt;div class=&#34;btn-group&#34;&gt;
                                &lt;a href=&#34;https://bit.ly/ps-core-data-swift&#34;&gt;&lt;button type=&#34;button&#34; class=&#34;btn btn-sm btn-outline-secondary&#34;&gt;View&lt;/button&gt;&lt;/a&gt;
                            &lt;/div&gt;
                            &lt;small class=&#34;text-body-secondary&#34;&gt;Mar 30, 2017&lt;/small&gt;
                            &lt;small class=&#34;text-body-secondary&#34;&gt;4h 39m&lt;/small&gt;
                        &lt;/div&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
            &lt;/div&gt;
            &lt;div class=&#34;col d-flex align-items-stretch&#34;&gt;
                &lt;div class=&#34;card shadow-sm&#34;&gt;
                    &lt;img src=&#34;managing-xcode-project-dependencies-with-cocoapods-title.png&#34; class=&#34;card-img-top&#34; alt=&#34;Managing Xcode Project Dependencies with CocoaPods&#34; /&gt;
                    &lt;div class=&#34;card-body d-flex flex-column&#34;&gt;
                        &lt;p class=&#34;card-text&#34;&gt;This course will give you the essential skills you need to architect an app to use Core Data. You&#39;ll learn to model, manage, and display data using Core Data in this course.&lt;/p&gt;
                        &lt;div class=&#34;d-flex justify-content-between align-items-center mt-auto&#34;&gt;
                            &lt;div class=&#34;btn-group&#34;&gt;
                                &lt;a href=&#34;https://bit.ly/create-cocoapod-lib&#34;&gt;&lt;button type=&#34;button&#34; class=&#34;btn btn-sm btn-outline-secondary&#34;&gt;View&lt;/button&gt;&lt;/a&gt;
                            &lt;/div&gt;
                            &lt;small class=&#34;text-body-secondary&#34;&gt;Feb 1, 2016&lt;/small&gt;
                            &lt;small class=&#34;text-body-secondary&#34;&gt;1h 39m&lt;/small&gt;
                        &lt;/div&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;data&#34; class=&#34;album py-5 bg-body-tertiary&#34;&gt;
    &lt;div class=&#34;container&#34;&gt;
        &lt;h1&gt;Data &amp; Analytics&lt;/h1&gt;
        &lt;div class=&#34;row row-cols-1 row-cols-sm-2 row-cols-md-3 g-3&#34;&gt;
            &lt;div class=&#34;col d-flex align-items-stretch&#34;&gt;
                &lt;div class=&#34;card shadow-sm&#34;&gt;
                    &lt;a data-bs-toggle=&#34;modal&#34; data-bs-target=&#34;#beef-cattle-operations-dashboard-modal&#34; role=&#34;button&#34;&gt;&lt;img src=&#34;beef-cattle-operations-dashboard.png&#34;
                            class=&#34;card-img-top&#34; alt=&#34;US Beef Cattle Operations&#34; /&gt;&lt;/a&gt;
                    &lt;div class=&#34;card-body d-flex flex-column&#34;&gt;
                        &lt;h5 class=&#34;card-title&#34;&gt;US Beef Cattle Operations Dashboard&lt;/h5&gt;
                        &lt;h6 class=&#34;text-body-secondary&#34;&gt;Noble Research Institute&lt;/h6&gt;
                        &lt;p class=&#34;card-text&#34;&gt;In my role as Principal Data Architect, I created several dimensional models using data from the US Census of Agriculture and presented intuitive, query-able data sets to our product and marketing teams to assist in their outreach efforts to our nation&#39;s farmers and ranchers. This is an example of one of the &#34;starter&#34; dashboards I created for them.
                        &lt;/p&gt;
                        &lt;div class=&#34;d-flex justify-content-between align-items-center mt-auto&#34;&gt;
                            &lt;small class=&#34;text-body-secondary&#34;&gt;Azure Databricks | SQL | Power BI&lt;/small&gt;
                        &lt;/div&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;robotics&#34; class=&#34;album py-5 bg-body-tertiary&#34;&gt;
    &lt;div class=&#34;container&#34;&gt;
        &lt;h1&gt;Robotics&lt;/h1&gt;
        &lt;div class=&#34;row row-cols-1 row-cols-sm-2 row-cols-md-3 g-3&#34;&gt;
            &lt;div class=&#34;col d-flex align-items-stretch&#34;&gt;
                &lt;div class=&#34;card shadow-sm&#34;&gt;
                    &lt;a data-bs-toggle=&#34;modal&#34; data-bs-target=&#34;#jr-botball-modal&#34; role=&#34;button&#34;&gt;&lt;img src=&#34;jr-botball.png&#34;
                            class=&#34;card-img-top&#34; alt=&#34;Jr. Botball&#34; /&gt;&lt;/a&gt;
                    &lt;div class=&#34;card-body d-flex flex-column&#34;&gt;
                        &lt;h5 class=&#34;card-title&#34;&gt;Jr. Botball&lt;/h5&gt;
                        &lt;h6 class=&#34;text-body-secondary&#34;&gt;Noble Research Institute&lt;/h6&gt;
                        &lt;p class=&#34;card-text&#34;&gt;I volunteered to teach a group of 4th and 5th grade students from local elementary schools basic programming skills, the engineering design process, and robotics to prepare them for the Jr. Botball Challenge.
                        &lt;/p&gt;
                        &lt;div class=&#34;d-flex justify-content-between align-items-center mt-auto&#34;&gt;
                            &lt;small class=&#34;text-body-secondary&#34;&gt;C&lt;/small&gt;
                        &lt;/div&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;skill-assessments&#34; class=&#34;album py-5 bg-body-tertiary&#34;&gt;
    &lt;div class=&#34;container&#34;&gt;
    &lt;h1&gt;Skill Assessments&lt;/h1&gt;
        &lt;div class=&#34;row row-cols-1 row-cols-sm-2 row-cols-md-3 g-3&#34;&gt;
            &lt;div class=&#34;col d-flex align-items-stretch&#34;&gt;
                &lt;div class=&#34;card shadow-sm&#34;&gt;
                    &lt;img src=&#34;skill-iq-ios-swift.png&#34; class=&#34;card-img-top&#34; alt=&#34;Skill IQ: iOS and Swift Development&#34;/&gt;
                    &lt;div class=&#34;card-body d-flex flex-column&#34;&gt;
                        &lt;p class=&#34;card-text&#34;&gt;I co-developed assessment questions and performed technical review for Pluralsight&#39;s Skill IQ platform covering the following iOS and Swift development topics:&lt;/p&gt;
                        &lt;ul&gt;
                        &lt;li&gt;Using the Xcode IDE&lt;/li&gt;
                        &lt;li&gt;Building user interfaces with SwiftUI&lt;/li&gt;
                        &lt;li&gt;Building user interfaces with UIKit&lt;/li&gt;
                        &lt;li&gt;Persisting data in an application&lt;/li&gt;
                        &lt;li&gt;Adding push notofications to an application&lt;/li&gt;
                        &lt;li&gt;Testing an app with unit and UI tests&lt;/li&gt;
                        &lt;li&gt;Publishing an app to the App Store&lt;/li&gt;
                        &lt;li&gt;Storing data in memory&lt;/li&gt;
                        &lt;li&gt;Controlling program flow&lt;/li&gt;
                        &lt;li&gt;Working with functions&lt;/li&gt;
                        &lt;li&gt;Implementing custom types&lt;/li&gt;
                        &lt;li&gt;Defining instance and type methods&lt;/li&gt;
                        &lt;li&gt;Handling errors&lt;/li&gt;
                        &lt;li&gt;Writing asynchronous and parallel code&lt;/li&gt;
                        &lt;li&gt;Using higher-order functions&lt;/li&gt;
                        &lt;/ul&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
            &lt;/div&gt;
            &lt;div class=&#34;col d-flex align-items-stretch&#34;&gt;
                &lt;div class=&#34;card shadow-sm&#34;&gt;
                    &lt;img src=&#34;skill-iq-vue3.png&#34; class=&#34;card-img-top&#34; alt=&#34;Skill IQ: Vue 3&#34; /&gt;
                    &lt;div class=&#34;card-body d-flex flex-column&#34;&gt;
                        &lt;p class=&#34;card-text&#34;&gt;I co-developed assessment questions and performed technical review for Pluralsight&#39;s Skill IQ platform covering the following Vue 3 topics:&lt;/p&gt;
                        &lt;ul&gt;
                        &lt;li&gt;Crating and configuring a Vue application&lt;/li&gt;
                        &lt;li&gt;Registering and rendering components&lt;/li&gt;
                        &lt;li&gt;Implementing component reactivity&lt;/li&gt;
                        &lt;li&gt;Handling and emitting events&lt;/li&gt;
                        &lt;li&gt;Using built-in directives&lt;/li&gt;
                        &lt;li&gt;Passing data between components&lt;/li&gt;
                        &lt;li&gt;Working with built-in components&lt;/li&gt;
                        &lt;li&gt;Reusing functionality with mixins and composables&lt;/li&gt;
                        &lt;li&gt;Routing requests with vue-router&lt;/li&gt;
                        &lt;li&gt;Managing state with Pinia&lt;/li&gt;
                        &lt;/ul&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
            &lt;/div&gt;
            &lt;div class=&#34;col d-flex align-items-stretch&#34;&gt;
                &lt;div class=&#34;card shadow-sm&#34;&gt;
                    &lt;img src=&#34;skill-iq-solidjs.png&#34; class=&#34;card-img-top&#34; alt=&#34;Skill IQ: SolidJS&#34; /&gt;
                    &lt;div class=&#34;card-body d-flex flex-column&#34;&gt;
                        &lt;p class=&#34;card-text&#34;&gt;I co-developed assessment questions and performed technical review for Pluralsight&#39;s Skill IQ platform covering the following SolidJS topics:&lt;/p&gt;
                        &lt;ul&gt;
                        &lt;li&gt;Setting up a SolidJS project&lt;/li&gt;
                        &lt;li&gt;Building user interfaces with components&lt;/li&gt;
                        &lt;li&gt;Adding interactivity to a SolidJS application&lt;/li&gt;
                        &lt;li&gt;Working with lifecycle methods&lt;/li&gt;
                        &lt;li&gt;Implementing reactive utilities&lt;/li&gt;
                        &lt;li&gt;Using stores and their utilities&lt;/li&gt;
                        &lt;li&gt;Using component APIs&lt;/li&gt;
                        &lt;li&gt;Using secondary primitives&lt;/li&gt;
                        &lt;li&gt;Implementing rendering in a SolidJS application&lt;/li&gt;
                        &lt;li&gt;Using reactive control flow&lt;/li&gt;
                        &lt;li&gt;Using special Javascript Syntax Extension (JSX) attributes&lt;/li&gt;
                        &lt;/ul&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;
&lt;div class=&#34;modal fade&#34; id=&#34;ten-read-ten-said-modal&#34; tabindex=&#34;-1&#34; aria-labelledby=&#34;10 Read 10 Said Modal&#34; aria-hidden=&#34;true&#34;&gt;
    &lt;div class=&#34;modal-dialog modal-fullscreen&#34;&gt;
        &lt;div class=&#34;modal-content&#34;&gt;
            &lt;div class=&#34;modal-header&#34;&gt;
                &lt;h1 class=&#34;modal-title fs-5&#34; id=&#34;ten-read-ten-said-modal-label&#34;&gt;10 Read 10 Said&lt;/h1&gt;
                &lt;button type=&#34;button&#34; class=&#34;btn-close&#34; data-bs-dismiss=&#34;modal&#34; aria-label=&#34;Close&#34;&gt;&lt;/button&gt;
            &lt;/div&gt;
            &lt;div class=&#34;modal-body&#34;&gt;
                &lt;img src=&#34;ten-read-ten-said.png&#34; class=&#34;d-block w-50&#34; /&gt;
            &lt;/div&gt;
            &lt;div class=&#34;modal-footer&#34;&gt;
                &lt;button type=&#34;button&#34; class=&#34;btn btn-secondary&#34; data-bs-dismiss=&#34;modal&#34;&gt;Close&lt;/button&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;
&lt;div class=&#34;modal fade&#34; id=&#34;express-chiropractic-modal&#34; tabindex=&#34;-1&#34; aria-labelledby=&#34;Express Chiropractic Modal&#34; aria-hidden=&#34;true&#34;&gt;
    &lt;div class=&#34;modal-dialog modal-fullscreen&#34;&gt;
        &lt;div class=&#34;modal-content&#34;&gt;
            &lt;div class=&#34;modal-header&#34;&gt;
                &lt;h1 class=&#34;modal-title fs-5&#34; id=&#34;express-chiropractic-modal-label&#34;&gt;Express Chiropractic&lt;/h1&gt;
                &lt;button type=&#34;button&#34; class=&#34;btn-close&#34; data-bs-dismiss=&#34;modal&#34; aria-label=&#34;Close&#34;&gt;&lt;/button&gt;
            &lt;/div&gt;
            &lt;div class=&#34;modal-body&#34;&gt;
                &lt;img src=&#34;express-chiropractic.png&#34; class=&#34;d-block w-50&#34;/&gt;
            &lt;/div&gt;
            &lt;div class=&#34;modal-footer&#34;&gt;
                &lt;button type=&#34;button&#34; class=&#34;btn btn-secondary&#34; data-bs-dismiss=&#34;modal&#34;&gt;Close&lt;/button&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;
&lt;div class=&#34;modal fade&#34; id=&#34;express-chiropractic-admin-modal&#34; tabindex=&#34;-1&#34; aria-labelledby=&#34;Express Chiropractic Admin Modal&#34; aria-hidden=&#34;true&#34;&gt;
    &lt;div class=&#34;modal-dialog modal-fullscreen&#34;&gt;
        &lt;div class=&#34;modal-content&#34;&gt;
            &lt;div class=&#34;modal-header&#34;&gt;
                &lt;h1 class=&#34;modal-title fs-5&#34; id=&#34;express-chiropractic-admin-modal-label&#34;&gt;Express Chiropractic Admin&lt;/h1&gt;
                &lt;button type=&#34;button&#34; class=&#34;btn-close&#34; data-bs-dismiss=&#34;modal&#34; aria-label=&#34;Close&#34;&gt;&lt;/button&gt;
            &lt;/div&gt;
            &lt;div class=&#34;modal-body&#34;&gt;
                &lt;img src=&#34;express-chiropractic-admin.png&#34; class=&#34;d-block w-50&#34;/&gt;
            &lt;/div&gt;
            &lt;div class=&#34;modal-footer&#34;&gt;
                &lt;button type=&#34;button&#34; class=&#34;btn btn-secondary&#34; data-bs-dismiss=&#34;modal&#34;&gt;Close&lt;/button&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;
&lt;div class=&#34;modal fade&#34; id=&#34;paradigm-hormones-modal&#34; tabindex=&#34;-1&#34; aria-labelledby=&#34;Paradigm Hormones&#34; aria-hidden=&#34;true&#34;&gt;
    &lt;div class=&#34;modal-dialog modal-fullscreen&#34;&gt;
        &lt;div class=&#34;modal-content&#34;&gt;
            &lt;div class=&#34;modal-header&#34;&gt;
                &lt;h1 class=&#34;modal-title fs-5&#34; id=&#34;paradigm-hormones-modal-label&#34;&gt;Paradigm Hormones&lt;/h1&gt;
                &lt;button type=&#34;button&#34; class=&#34;btn-close&#34; data-bs-dismiss=&#34;modal&#34; aria-label=&#34;Close&#34;&gt;&lt;/button&gt;
            &lt;/div&gt;
            &lt;div class=&#34;modal-body&#34;&gt;
                &lt;img src=&#34;paradigm-hormones.png&#34; class=&#34;d-block w-50&#34;/&gt;
            &lt;/div&gt;
            &lt;div class=&#34;modal-footer&#34;&gt;
                &lt;button type=&#34;button&#34; class=&#34;btn btn-secondary&#34; data-bs-dismiss=&#34;modal&#34;&gt;Close&lt;/button&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;
&lt;div class=&#34;modal fade&#34; id=&#34;noble-rdms-modal&#34; tabindex=&#34;-1&#34; aria-labelledby=&#34;Noble Research Data Management System&#34; aria-hidden=&#34;true&#34;&gt;
    &lt;div class=&#34;modal-dialog modal-fullscreen&#34;&gt;
        &lt;div class=&#34;modal-content&#34;&gt;
            &lt;div class=&#34;modal-header&#34;&gt;
                &lt;h1 class=&#34;modal-title fs-5&#34; id=&#34;noble-rdms-modal-label&#34;&gt;Noble Research Data Management System&lt;/h1&gt;
                &lt;button type=&#34;button&#34; class=&#34;btn-close&#34; data-bs-dismiss=&#34;modal&#34; aria-label=&#34;Close&#34;&gt;&lt;/button&gt;
            &lt;/div&gt;
            &lt;div class=&#34;modal-body&#34;&gt;
                &lt;div id=&#34;noble-rdms-carousel&#34; class=&#34;carousel slide&#34; data-bs-theme=&#34;dark&#34;&gt;
                    &lt;div class=&#34;carousel-inner&#34;&gt;
                        &lt;div class=&#34;carousel-indicators&#34;&gt;
                            &lt;button type=&#34;button&#34; data-bs-target=&#34;#noble-rdms-carousel&#34; data-bs-slide-to=&#34;0&#34; class=&#34;active&#34; aria-current=&#34;true&#34; aria-label=&#34;Slide 1&#34;&gt;&lt;/button&gt;
                            &lt;button type=&#34;button&#34; data-bs-target=&#34;#noble-rdms-carousel&#34; data-bs-slide-to=&#34;1&#34; aria-label=&#34;Slide 2&#34;&gt;&lt;/button&gt;
                            &lt;button type=&#34;button&#34; data-bs-target=&#34;#noble-rdms-carousel&#34; data-bs-slide-to=&#34;2&#34; aria-label=&#34;Slide 3&#34;&gt;&lt;/button&gt;
                        &lt;/div&gt;
                        &lt;div class=&#34;carousel-item active&#34;&gt;
                            &lt;img src=&#34;noble-rdms-find-project.gif&#34; class=&#34;d-block w-50&#34; alt=&#34;Noble Research Data Management System - Find Project&#34;/&gt;
                            &lt;div class=&#34;carousel-caption d-none d-md-block&#34;&gt;
                                &lt;h5&gt;Find Project&lt;/h5&gt;
                            &lt;/div&gt;
                        &lt;/div&gt;
                        &lt;div class=&#34;carousel-item&#34;&gt;
                            &lt;img src=&#34;noble-rdms-upload-files.gif&#34; class=&#34;d-block w-50&#34; alt=&#34;Noble Research Data Management System - Upload Files&#34;&gt;
                            &lt;div class=&#34;carousel-caption d-none d-md-block&#34;&gt;
                                &lt;h5&gt;Upload Files&lt;/h5&gt;
                            &lt;/div&gt;
                        &lt;/div&gt;
                        &lt;div class=&#34;carousel-item&#34;&gt;
                            &lt;img src=&#34;noble-rdms-download-files.gif&#34; class=&#34;d-block w-50&#34; alt=&#34;Noble Research Data Management System - Download Files&#34;&gt;
                            &lt;div class=&#34;carousel-caption d-none d-md-block&#34;&gt;
                                &lt;h5&gt;Download Files&lt;/h5&gt;
                            &lt;/div&gt;
                        &lt;/div&gt;
                    &lt;/div&gt;
                    &lt;button class=&#34;carousel-control-prev&#34; type=&#34;button&#34; data-bs-target=&#34;#noble-rdms-carousel&#34; data-bs-slide=&#34;prev&#34;&gt;
                        &lt;span class=&#34;carousel-control-prev-icon text-seconary&#34; aria-hidden=&#34;true&#34;&gt;&lt;/span&gt;
                        &lt;span class=&#34;visually-hidden&#34;&gt;Previous&lt;/span&gt;
                    &lt;/button&gt;
                    &lt;button class=&#34;carousel-control-next&#34; type=&#34;button&#34; data-bs-target=&#34;#noble-rdms-carousel&#34; data-bs-slide=&#34;next&#34;&gt;
                        &lt;span class=&#34;carousel-control-next-icon text-seconary&#34; aria-hidden=&#34;true&#34;&gt;&lt;/span&gt;
                        &lt;span class=&#34;visually-hidden&#34;&gt;Next&lt;/span&gt;
                    &lt;/button&gt;
                &lt;/div&gt;
            &lt;/div&gt;
            &lt;div class=&#34;modal-footer&#34;&gt;
                &lt;button type=&#34;button&#34; class=&#34;btn btn-secondary&#34; data-bs-dismiss=&#34;modal&#34;&gt;Close&lt;/button&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;
&lt;div class=&#34;modal fade&#34; id=&#34;noble-oe-modal&#34; tabindex=&#34;-1&#34; aria-labelledby=&#34;Noble Open Enrollment&#34; aria-hidden=&#34;true&#34;&gt;
    &lt;div class=&#34;modal-dialog modal-fullscreen&#34;&gt;
        &lt;div class=&#34;modal-content&#34;&gt;
            &lt;div class=&#34;modal-header&#34;&gt;
                &lt;h1 class=&#34;modal-title fs-5&#34; id=&#34;noble-oe-modal-label&#34;&gt;Noble Open Enrollment&lt;/h1&gt;
                &lt;button type=&#34;button&#34; class=&#34;btn-close&#34; data-bs-dismiss=&#34;modal&#34; aria-label=&#34;Close&#34;&gt;&lt;/button&gt;
            &lt;/div&gt;
            &lt;div class=&#34;modal-body&#34;&gt;
                &lt;div id=&#34;noble-oe-carousel&#34; class=&#34;carousel slide&#34; data-bs-theme=&#34;dark&#34;&gt;
                    &lt;div class=&#34;carousel-inner&#34;&gt;
                        &lt;div class=&#34;carousel-indicators&#34;&gt;
                            &lt;button type=&#34;button&#34; data-bs-target=&#34;#noble-oe-carousel&#34; data-bs-slide-to=&#34;0&#34; class=&#34;active&#34; aria-current=&#34;true&#34; aria-label=&#34;Slide 1&#34;&gt;&lt;/button&gt;
                            &lt;button type=&#34;button&#34; data-bs-target=&#34;#noble-oe-carousel&#34; data-bs-slide-to=&#34;1&#34; aria-label=&#34;Slide 2&#34;&gt;&lt;/button&gt;
                            &lt;button type=&#34;button&#34; data-bs-target=&#34;#noble-oe-carousel&#34; data-bs-slide-to=&#34;2&#34; aria-label=&#34;Slide 3&#34;&gt;&lt;/button&gt;
                        &lt;/div&gt;
                        &lt;div class=&#34;carousel-item active&#34;&gt;
                            &lt;img src=&#34;noble-oe-change-address.gif&#34; class=&#34;d-block w-50&#34; alt=&#34;Open Enrollment Change Address&#34;&gt;
                            &lt;div class=&#34;carousel-caption d-none d-md-block&#34;&gt;
                                &lt;h5&gt;Change Address&lt;/h5&gt;
                            &lt;/div&gt;
                        &lt;/div&gt;
                        &lt;div class=&#34;carousel-item&#34;&gt;
                            &lt;img src=&#34;noble-oe-change-dependents.gif&#34; class=&#34;d-block w-50&#34; alt=&#34;Open Enrollment Change Dependents&#34;/&gt;
                            &lt;div class=&#34;carousel-caption d-none d-md-block&#34;&gt;
                                &lt;h5&gt;Change Dependents&lt;/h5&gt;
                            &lt;/div&gt;
                        &lt;/div&gt;
                        &lt;div class=&#34;carousel-item&#34;&gt;
                            &lt;img src=&#34;noble-oe-change-enrollments.gif&#34; class=&#34;d-block w-50&#34; alt=&#34;Open Enrollment Change Enrollments&#34;/&gt;
                            &lt;div class=&#34;carousel-caption d-none d-md-block&#34;&gt;
                                &lt;h5&gt;Change Enrollments&lt;/h5&gt;
                            &lt;/div&gt;
                        &lt;/div&gt;
                    &lt;/div&gt;
                    &lt;button class=&#34;carousel-control-prev&#34; type=&#34;button&#34; data-bs-target=&#34;#noble-oe-carousel&#34; data-bs-slide=&#34;prev&#34;&gt;
                        &lt;span class=&#34;carousel-control-prev-icon text-seconary&#34; aria-hidden=&#34;true&#34;&gt;&lt;/span&gt;
                        &lt;span class=&#34;visually-hidden&#34;&gt;Previous&lt;/span&gt;
                    &lt;/button&gt;
                    &lt;button class=&#34;carousel-control-next&#34; type=&#34;button&#34; data-bs-target=&#34;#noble-oe-carousel&#34; data-bs-slide=&#34;next&#34;&gt;
                        &lt;span class=&#34;carousel-control-next-icon text-seconary&#34; aria-hidden=&#34;true&#34;&gt;&lt;/span&gt;
                        &lt;span class=&#34;visually-hidden&#34;&gt;Next&lt;/span&gt;
                    &lt;/button&gt;
                &lt;/div&gt;
            &lt;/div&gt;
            &lt;div class=&#34;modal-footer&#34;&gt;
                &lt;button type=&#34;button&#34; class=&#34;btn btn-secondary&#34; data-bs-dismiss=&#34;modal&#34;&gt;Close&lt;/button&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;
&lt;div class=&#34;modal fade&#34; id=&#34;noble-materials-inventory-modal&#34; tabindex=&#34;-1&#34; aria-labelledby=&#34;Noble Materials Inventory&#34; aria-hidden=&#34;true&#34;&gt;
    &lt;div class=&#34;modal-dialog modal-fullscreen&#34;&gt;
        &lt;div class=&#34;modal-content&#34;&gt;
            &lt;div class=&#34;modal-header&#34;&gt;
                &lt;h1 class=&#34;modal-title fs-5&#34; id=&#34;noble-materials-inventory-modal-label&#34;&gt;Noble Materials Inventory&lt;/h1&gt;
                &lt;button type=&#34;button&#34; class=&#34;btn-close&#34; data-bs-dismiss=&#34;modal&#34; aria-label=&#34;Close&#34;&gt;&lt;/button&gt;
            &lt;/div&gt;
            &lt;div class=&#34;modal-body&#34;&gt;
                &lt;img src=&#34;noble-materials-inventory-demo.gif&#34; class=&#34;d-block w-50&#34;/&gt;
            &lt;/div&gt;
            &lt;div class=&#34;modal-footer&#34;&gt;
                &lt;button type=&#34;button&#34; class=&#34;btn btn-secondary&#34; data-bs-dismiss=&#34;modal&#34;&gt;Close&lt;/button&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;
&lt;div class=&#34;modal fade&#34; id=&#34;noble-jr-beef-modal&#34; tabindex=&#34;-1&#34; aria-labelledby=&#34;Jr. Beef&#34; aria-hidden=&#34;true&#34;&gt;
    &lt;div class=&#34;modal-dialog modal-fullscreen&#34;&gt;
        &lt;div class=&#34;modal-content&#34;&gt;
            &lt;div class=&#34;modal-header&#34;&gt;
                &lt;h1 class=&#34;modal-title fs-5&#34; id=&#34;noble-jr-beef-modal-label&#34;&gt;Jr. Beef&lt;/h1&gt;
                &lt;button type=&#34;button&#34; class=&#34;btn-close&#34; data-bs-dismiss=&#34;modal&#34; aria-label=&#34;Close&#34;&gt;&lt;/button&gt;
            &lt;/div&gt;
            &lt;div class=&#34;modal-body&#34;&gt;
                &lt;img src=&#34;noble-jr-beef-demo.gif&#34; class=&#34;d-block w-50&#34;/&gt;
            &lt;/div&gt;
            &lt;div class=&#34;modal-footer&#34;&gt;
                &lt;button type=&#34;button&#34; class=&#34;btn btn-secondary&#34; data-bs-dismiss=&#34;modal&#34;&gt;Close&lt;/button&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;
&lt;div class=&#34;modal fade&#34; id=&#34;noble-ag-tools-modal&#34; tabindex=&#34;-1&#34; aria-labelledby=&#34;Ag Tools&#34; aria-hidden=&#34;true&#34;&gt;
    &lt;div class=&#34;modal-dialog modal-fullscreen&#34;&gt;
        &lt;div class=&#34;modal-content&#34;&gt;
            &lt;div class=&#34;modal-header&#34;&gt;
                &lt;h1 class=&#34;modal-title fs-5&#34; id=&#34;noble-ag-tools-label&#34;&gt;Ag Tools&lt;/h1&gt;
                &lt;button type=&#34;button&#34; class=&#34;btn-close&#34; data-bs-dismiss=&#34;modal&#34; aria-label=&#34;Close&#34;&gt;&lt;/button&gt;
            &lt;/div&gt;
            &lt;div class=&#34;modal-body&#34;&gt;
                &lt;img src=&#34;noble-ag-tools-demo.gif&#34; class=&#34;d-block w-50&#34;/&gt;
            &lt;/div&gt;
            &lt;div class=&#34;modal-footer&#34;&gt;
                &lt;button type=&#34;button&#34; class=&#34;btn btn-secondary&#34; data-bs-dismiss=&#34;modal&#34;&gt;Close&lt;/button&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;
&lt;div class=&#34;modal fade&#34; id=&#34;jr-botball-modal&#34; tabindex=&#34;-1&#34; aria-labelledby=&#34;Jr. Botball Modal&#34; aria-hidden=&#34;true&#34;&gt;
    &lt;div class=&#34;modal-dialog modal-fullscreen&#34;&gt;
        &lt;div class=&#34;modal-content&#34;&gt;
            &lt;div class=&#34;modal-header&#34;&gt;
                &lt;h1 class=&#34;modal-title fs-5&#34; id=&#34;jr-botball-modal-label&#34;&gt;Jr. Botball&lt;/h1&gt;
                &lt;button type=&#34;button&#34; class=&#34;btn-close&#34; data-bs-dismiss=&#34;modal&#34; aria-label=&#34;Close&#34;&gt;&lt;/button&gt;
            &lt;/div&gt;
            &lt;div class=&#34;modal-body&#34;&gt;
                &lt;div id=&#34;jr-botball-carousel&#34; class=&#34;carousel slide&#34; data-bs-theme=&#34;dark&#34;&gt;
                    &lt;div class=&#34;carousel-inner&#34;&gt;
                        &lt;div class=&#34;carousel-indicators&#34;&gt;
                            &lt;button type=&#34;button&#34; data-bs-target=&#34;#jr-botball-carousel&#34; data-bs-slide-to=&#34;0&#34; class=&#34;active&#34; aria-current=&#34;true&#34; aria-label=&#34;Slide 1&#34;&gt;&lt;/button&gt;
                            &lt;button type=&#34;button&#34; data-bs-target=&#34;#jr-botball-carousel&#34; data-bs-slide-to=&#34;1&#34; aria-label=&#34;Slide 2&#34;&gt;&lt;/button&gt;
                        &lt;/div&gt;
                        &lt;div class=&#34;carousel-item active&#34;&gt;
                            &lt;img src=&#34;jr-botball-grab-bottle.gif&#34; class=&#34;d-block w-50&#34; alt=&#34;Jr. Botball - Grab Bottle&#34;&gt;
                        &lt;/div&gt;
                        &lt;div class=&#34;carousel-item&#34;&gt;
                            &lt;img src=&#34;jr-botball-follow-line.gif&#34; class=&#34;d-block w-50&#34; alt=&#34;Jr. Botball - Follow Line&#34;/&gt;
                        &lt;/div&gt;
                    &lt;/div&gt;
                    &lt;button class=&#34;carousel-control-prev&#34; type=&#34;button&#34; data-bs-target=&#34;#jr-botball-carousel&#34; data-bs-slide=&#34;prev&#34;&gt;
                        &lt;span class=&#34;carousel-control-prev-icon text-seconary&#34; aria-hidden=&#34;true&#34;&gt;&lt;/span&gt;
                        &lt;span class=&#34;visually-hidden&#34;&gt;Previous&lt;/span&gt;
                    &lt;/button&gt;
                    &lt;button class=&#34;carousel-control-next&#34; type=&#34;button&#34; data-bs-target=&#34;#jr-botball-carousel&#34; data-bs-slide=&#34;next&#34;&gt;
                        &lt;span class=&#34;carousel-control-next-icon text-seconary&#34; aria-hidden=&#34;true&#34;&gt;&lt;/span&gt;
                        &lt;span class=&#34;visually-hidden&#34;&gt;Next&lt;/span&gt;
                    &lt;/button&gt;
                &lt;/div&gt;
            &lt;/div&gt;
            &lt;div class=&#34;modal-footer&#34;&gt;
                &lt;button type=&#34;button&#34; class=&#34;btn btn-secondary&#34; data-bs-dismiss=&#34;modal&#34;&gt;Close&lt;/button&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;
&lt;div class=&#34;modal fade&#34; id=&#34;beef-cattle-operations-dashboard-modal&#34; tabindex=&#34;-1&#34; aria-labelledby=&#34;Beef Cattle Operations Dashboard&#34; aria-hidden=&#34;true&#34;&gt;
    &lt;div class=&#34;modal-dialog modal-fullscreen&#34;&gt;
        &lt;div class=&#34;modal-content&#34;&gt;
            &lt;div class=&#34;modal-header&#34;&gt;
                &lt;h1 class=&#34;modal-title fs-5&#34; id=&#34;beef-cattle-operations-dashboard-modal-label&#34;&gt;Beef Cattle Operations Dashboard&lt;/h1&gt;
                &lt;button type=&#34;button&#34; class=&#34;btn-close&#34; data-bs-dismiss=&#34;modal&#34; aria-label=&#34;Close&#34;&gt;&lt;/button&gt;
            &lt;/div&gt;
            &lt;div class=&#34;modal-body&#34;&gt;
                &lt;div id=&#34;beef-cattle-operations-dashboard-carousel&#34; class=&#34;carousel slide&#34; data-bs-theme=&#34;dark&#34;&gt;
                    &lt;div class=&#34;carousel-inner&#34;&gt;
                        &lt;div class=&#34;carousel-indicators&#34;&gt;
                            &lt;button type=&#34;button&#34; data-bs-target=&#34;#beef-cattle-operations-dashboard-carousel&#34; data-bs-slide-to=&#34;0&#34; class=&#34;active&#34; aria-current=&#34;true&#34; aria-label=&#34;Slide 1&#34;&gt;&lt;/button&gt;
                            &lt;button type=&#34;button&#34; data-bs-target=&#34;#beef-cattle-operations-dashboard-carousel&#34; data-bs-slide-to=&#34;1&#34; aria-label=&#34;Slide 2&#34;&gt;&lt;/button&gt;
                        &lt;/div&gt;
                        &lt;div class=&#34;carousel-item active&#34;&gt;
                            &lt;img src=&#34;beef-cattle-operations-dashboard.png&#34; class=&#34;d-block w-50&#34; alt=&#34;Beef Cattle Operations Dashboard&#34;&gt;
                        &lt;/div&gt;
                        &lt;div class=&#34;carousel-item&#34;&gt;
                            &lt;img src=&#34;cattle-sheep-goats-dashboard.png&#34; class=&#34;d-block w-50&#34; alt=&#34;Cattle/Sheep/Goats Dashboard&#34;/&gt;
                        &lt;/div&gt;
                    &lt;/div&gt;
                    &lt;button class=&#34;carousel-control-prev&#34; type=&#34;button&#34; data-bs-target=&#34;#beef-cattle-operations-dashboard-carousel&#34; data-bs-slide=&#34;prev&#34;&gt;
                        &lt;span class=&#34;carousel-control-prev-icon text-seconary&#34; aria-hidden=&#34;true&#34;&gt;&lt;/span&gt;
                        &lt;span class=&#34;visually-hidden&#34;&gt;Previous&lt;/span&gt;
                    &lt;/button&gt;
                    &lt;button class=&#34;carousel-control-next&#34; type=&#34;button&#34; data-bs-target=&#34;#beef-cattle-operations-dashboard-carousel&#34; data-bs-slide=&#34;next&#34;&gt;
                        &lt;span class=&#34;carousel-control-next-icon text-seconary&#34; aria-hidden=&#34;true&#34;&gt;&lt;/span&gt;
                        &lt;span class=&#34;visually-hidden&#34;&gt;Next&lt;/span&gt;
                    &lt;/button&gt;
                &lt;/div&gt;
            &lt;/div&gt;
            &lt;div class=&#34;modal-footer&#34;&gt;
                &lt;button type=&#34;button&#34; class=&#34;btn btn-secondary&#34; data-bs-dismiss=&#34;modal&#34;&gt;Close&lt;/button&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;
&lt;/main&gt;</description>
    </item>
    
  </channel>
</rss>