<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Joe Spadafora on Medium]]></title>
        <description><![CDATA[Stories by Joe Spadafora on Medium]]></description>
        <link>https://medium.com/@spadafiva?source=rss-209bd5bbca54------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*xT1ZEb6pa6kAWcccil0lhQ.jpeg</url>
            <title>Stories by Joe Spadafora on Medium</title>
            <link>https://medium.com/@spadafiva?source=rss-209bd5bbca54------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 22:10:38 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@spadafiva/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Computed Variables vs Property Observers — Disambiguation]]></title>
            <link>https://medium.com/swiftjoe/computed-variables-vs-property-observers-disambiguation-c2e31393997c?source=rss-209bd5bbca54------2</link>
            <guid isPermaLink="false">https://medium.com/p/c2e31393997c</guid>
            <category><![CDATA[xcode]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[ios-app-development]]></category>
            <dc:creator><![CDATA[Joe Spadafora]]></dc:creator>
            <pubDate>Tue, 11 Jul 2017 14:02:01 GMT</pubDate>
            <atom:updated>2017-07-11T14:02:01.815Z</atom:updated>
            <content:encoded><![CDATA[<p>So, when I first started programming in Swift, one of the things that I found confusing is Computed Properties {get set} and Property Observers { willSet didSet }. Visually, both look very similar and both can trigger side effects in other areas of the code or change things when the values are set to them.</p><p>I want to spend a couple of minutes explaining both and their similarities and differences. Both can be incredibly useful tools for streamlining your code and making it less verbose and more modular.</p><h3>Computed Properties</h3><p>Computed properties don’t have any storage value. Much like quantum theory (I think? I’m not a quantum physicist), their value is not known until they are interacted with. Instead of a holding a value, they hold a small closure that generates the value dynamically at the time that it is accessed. Computed properties can be read only { get } or can be read-write { get set } properties.</p><p>I’m a really big fan of using computed properties with <a href="https://medium.com/@spadafiva/using-getters-and-setters-with-user-defaults-973446a0cea8">User Defaults</a>, and there’s lots of other compelling cases for what you can do with this, like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a6a01ebe0d874deb74a276e2ff4407c8/href">https://medium.com/media/a6a01ebe0d874deb74a276e2ff4407c8/href</a></iframe><p>Another case recently where I ended up using this was to deal with geofencing. I was using an api that returned latitude, longitude , and radius_mile. The only problem was that apple’s CLCircularRegion expects a distance in meters! This was an obvious case for using a computed variable:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/902e3e184faa4bbe79e55a6805e419b9/href">https://medium.com/media/902e3e184faa4bbe79e55a6805e419b9/href</a></iframe><h3>Property Observers</h3><p>Property observers are “regular” variables, but with a twist. Any time that their value is set (after initialization), they give a chance for code to be run in response to an observed change. They look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a684d5f58972461f5f6dd9155a581d64/href">https://medium.com/media/a684d5f58972461f5f6dd9155a581d64/href</a></iframe><p>This looks very similar to the computed variables, but it’s important to remember that items or name in the example above always has a value, which is not dynamically generated every time it is accessed.</p><p>Property observers are great when you need to perform a certain action every time something is updated, especially when it can be updated from multiple different points in your code.</p><p>I’ve personally found that it works well when creating your own delegate pattern. For example, consider the case of a toy I’m basing on one that I enjoyed in my childhood (but have hopefully altered enough so that I don’t get sued =) ):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5d6ef473c28a35abc3a22e3d2f840e4c/href">https://medium.com/media/5d6ef473c28a35abc3a22e3d2f840e4c/href</a></iframe><p>Notice how startNewGame() and setNewRandomAction() both modify current action. Instead of placing the logic for the delegate callback in both places, it’s cleaner and simpler to place it in the property observer on the currentAction variable.</p><p>You can check it out by combining the snippet above with this code below in a playground (<a href="https://gist.github.com/spadafiva/b0ebfa706299a015fb8e9c0010f531dd">or click here for the full example</a>):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e26a65f8c0bef0db208adcbd25f5ac9b/href">https://medium.com/media/e26a65f8c0bef0db208adcbd25f5ac9b/href</a></iframe><p>Hopefully this article can help to explain some of the differences between these two quite different but very similar looking types of variables!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c2e31393997c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swiftjoe/computed-variables-vs-property-observers-disambiguation-c2e31393997c">Computed Variables vs Property Observers — Disambiguation</a> was originally published in <a href="https://medium.com/swiftjoe">swiftjoe</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[What does “extension” do in Swift?]]></title>
            <link>https://medium.com/swiftjoe/what-does-extension-do-in-swift-e47bb4787561?source=rss-209bd5bbca54------2</link>
            <guid isPermaLink="false">https://medium.com/p/e47bb4787561</guid>
            <category><![CDATA[ios-app-development]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Joe Spadafora]]></dc:creator>
            <pubDate>Mon, 27 Feb 2017 22:52:57 GMT</pubDate>
            <atom:updated>2017-02-27T22:52:57.026Z</atom:updated>
            <content:encoded><![CDATA[<p>There are lots of things that I like about the Swift language. In my opinion, it is a beautifully crafted language with very thoughtful opinions about what programming should be. One of my personal favorite features about the language is the ability to use the extension keyword to bake more functionality into already existing classes.</p><p>Using extension we can cut down on boilerplate code and remove items like utility classes and instead put properties and functions right where they belong.</p><p>Imagine this: You have 2 different view controllers where your user can receive a UIAlertController informing them of some piece of information like an error or another notification with a single button.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2d37c840e2204a0ea7b0ada50f164512/href">https://medium.com/media/2d37c840e2204a0ea7b0ada50f164512/href</a></iframe><p>This code is not DRY. We can make it much cleaner by using an extension to reduce lots of the boilerplate code. Extensions are awesome because we can basically stitch on new functionality to classes that are already defined and existing without having to make a subclass, and can sort of “retroactively” affect other classes that have been fully scoped out and created.</p><p>Here we use the extension keyword to add a new function that will affect every UIViewController and anything that subclasses UIViewController:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f7fa507cfafb2eb42709623b010dab96/href">https://medium.com/media/f7fa507cfafb2eb42709623b010dab96/href</a></iframe><p>With this little piece of code, we can now re-write the previous snippet as:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6e7caa81f7a8c54154e2136d46f4f1ff/href">https://medium.com/media/6e7caa81f7a8c54154e2136d46f4f1ff/href</a></iframe><p>Another cool way to use extension is if you often have to format your strings or other data in similar way throughout your app. For example, let’s say you use the same format for a date string throughout the app:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/af5ddf600429cfee1f6de2b51210e53c/href">https://medium.com/media/af5ddf600429cfee1f6de2b51210e53c/href</a></iframe><p>So, now anywhere in your app you need that particular formatting for your date string (maybe showing details of an event or perhaps a time that a photograph or other item was taken.) you can simply type thatDate.myAppDateString to get the beautifully formatted string. You can easily use that formatted string just the way that you want it everywhere in your app that you already have a date object. Now imagine later you need to change how that string looks throughout your entire app….it’s simply a few lines of code ALL IN ONE PLACE.</p><p>ᕙ( ~ . ~ )ᕗ</p><p>Extensions get even more powerful when you can use generics and apply them to arrays and collections, as well as using them as protocol extensions to add functionality to the concrete classes created from your protocols…but that’s a topic for another day.</p><p>Next time you have some code smell or see the same code repeated in your controllers or model objects, maybe the cleanest solution for your problem will be an extension!</p><p>See you next week!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e47bb4787561" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swiftjoe/what-does-extension-do-in-swift-e47bb4787561">What does “extension” do in Swift?</a> was originally published in <a href="https://medium.com/swiftjoe">swiftjoe</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Organizing your Api Endpoints]]></title>
            <link>https://medium.com/swiftjoe/organizing-your-api-endpoints-b54ee53778cf?source=rss-209bd5bbca54------2</link>
            <guid isPermaLink="false">https://medium.com/p/b54ee53778cf</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[api]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[ios-app-development]]></category>
            <dc:creator><![CDATA[Joe Spadafora]]></dc:creator>
            <pubDate>Mon, 20 Feb 2017 22:43:05 GMT</pubDate>
            <atom:updated>2017-02-20T22:43:05.488Z</atom:updated>
            <content:encoded><![CDATA[<p>A tip to make working with multiple environments easy.</p><p>In my day to day job working, I’m lucky enough to work with a really great set of people that do an awesome job making easy to consume APIs. For our app we have both Development and Production endpoints, and we switch between them as we are working on each new release.</p><p>So, I have a few goals in switching between api endpoints:</p><ol><li>Make it EASY. It shouldn’t be complicated or time consuming</li><li>Eliminate as many ways of making typos as possible.</li><li>Make it so that if there are multiple different API services (Image Recognition, Database, Payment Solution), that we are able to switch between all of them at the same time.</li><li>Make the place where the switch occurs as readable as possible for others looking at the code.</li></ol><p>So, as a developer, I like to be as DRY as possible, and my goal was to try to hit all four of these goals. I’ve made a way of doing it that I’ve found helpful, and maybe you will find it helpful as well. (Also, if you have an idea for a better way or a different way of doing it, please share!)</p><p>First, I start by making a new enum EnvironmentType . For many cases, it might be as simple as this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8f8b8cf13ecfc232714e94c027adef54/href">https://medium.com/media/8f8b8cf13ecfc232714e94c027adef54/href</a></iframe><p>If your team uses more endpoints than this, or you have other options, or you want to be able switch between different versions, obviously feel free to tweak it as needed. (This could be something like developmentV1, developmentV2, productionV1 for the different cases if you need it.)</p><p>After creating that, I create a class to manage the Environment. It’s pretty simple and straightforward.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a0f51304cfac31eda06cee1a9f561879/href">https://medium.com/media/a0f51304cfac31eda06cee1a9f561879/href</a></iframe><p>I’ve made a couple of choices here. Let me walk through them.</p><ol><li>I’ve made the environment current implicitly unwrapped. This is to make sure that the configure function MUST be called before using the class. If it isn’t called, the app is going to crash on launch.</li><li>I’ve created a function configure to set up the current environment variable.</li></ol><p>Okay, so we’ve got a few pieces setup, now let’s talk a little bit about how to setup the class responsible for making your web calls. Here’s the setup I use for making sure that it uses the correct information:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/99f77e20841d362b48eee25892785089/href">https://medium.com/media/99f77e20841d362b48eee25892785089/href</a></iframe><p>So, now we’ve set up our api so that we can write just one variable baseUrlString and use that as part of all of our different urls in our api class. From here, we would just go ahead and configure all of the different web calls to use that same api string.</p><p>I’m sure that we all have our favorite ways of actually building and using the API itself (and that’s a bit beyond the scope of this article).</p><p>Okay, now the last piece that makes everything work.</p><ol><li>Let’s add a variable to hold the current environment in the app delegate to keep it available for the app (so that it stays in scope for the duration of the app).</li><li>Call the Environment.configure function to set the current environment.</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/113aec2f56d53e20603715888d69fdab/href">https://medium.com/media/113aec2f56d53e20603715888d69fdab/href</a></iframe><p>Okay, now that everything is done, our app and our api will use the correct paths for the development environment.</p><p>So, here are the things I like about this way of doing things.</p><ol><li>Setting this up for the first time isn’t hard.</li><li>Both of the endpoints are already in the network api class(es), and so after you add them and make sure that there are no typos, you don’t have to ever change them (unless of course the actual endpoint urls change).</li><li>Switching from .production to .environment is only 1 value, and that value is checked at compile time.</li><li>If you have multiple different API endpoints as part of your app, this changes all of them in one call. (Let say you have an API providing your user data, something like intercom powering your customer service, and something managing your payments.) You change this in one place and it changes it for all of them.</li><li>You use only one variable baseUrlString to create all of your urls in your API class, so you minimize typos and repetition.</li></ol><p>So, I’ve found this useful for myself. What do you think?</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b54ee53778cf" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swiftjoe/organizing-your-api-endpoints-b54ee53778cf">Organizing your Api Endpoints</a> was originally published in <a href="https://medium.com/swiftjoe">swiftjoe</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Making Your Own Delegates]]></title>
            <link>https://medium.com/swiftjoe/making-your-own-delegates-44ca0e320271?source=rss-209bd5bbca54------2</link>
            <guid isPermaLink="false">https://medium.com/p/44ca0e320271</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[ios]]></category>
            <dc:creator><![CDATA[Joe Spadafora]]></dc:creator>
            <pubDate>Tue, 14 Feb 2017 02:12:50 GMT</pubDate>
            <atom:updated>2017-02-14T02:12:50.687Z</atom:updated>
            <content:encoded><![CDATA[<p>If you’ve spent some time working in iOS in Swift, you may have noticed that you see the word Delegate everywhere. Delegation is a powerful pattern that can be really useful, and it’s easier than you might think to create your own delegates.</p><p>In thinking about delegates, a good way to explain them might be a metaphor of a wedding. At a wedding, there’s a ceremony, some sort of music and dinner gets served. If we made a class to model that, it might look something like this.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/733260fa3ebc6b37cf1392ef4c547cab/href">https://medium.com/media/733260fa3ebc6b37cf1392ef4c547cab/href</a></iframe><p>So, we’ve got all the basics covered, but this code isn’t reusable. In a real wedding, sometimes everything comes with the venue, but other times it’s provided by outside vendors. That’s exactly the same function a Delegate does for us. In Swift, starting with a protocol for your delegate is a good idea, since it lines up well with the function of a delegate.</p><p>Let’s start by making our protocols. Protocols are a cool way of saying that anything that conforms to this protocol will implement the stuff here. It’s basically swift’s way of saying, “Hey, I promise that if you meet a Insert name of Protocol here, that it will be able to do X, Y, Z.” The rest of the program or class can then depend on the delegate to definitely have those features.</p><p>So, like I said, to “Delegate-ize” our class, we need three delegates, Ceremony, Music and Food.</p><p>So, our Music Delegate needs to be able to play music and take song requests and return true or false that they will be accepted. Our food delegate needs to provide some sort of food, and our ceremony delegate is responsible for performing the vows.</p><p>First let’s make the ceremony delegate and modify the venue class.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5722619a34dd97a10a1fdfeb93213b08/href">https://medium.com/media/5722619a34dd97a10a1fdfeb93213b08/href</a></iframe><p>Okay, we added the CeremonyDelegate protocol, and modified the class. Basically instead of just having it say “Do you, do you?” we can set it up so that the ceremony can easily be provided just by replacing the delegate instead.</p><p>So, now, maybe we could have the guy from the princess bride as the officiant.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d1524da085d75d2364c640e8b4f1c232/href">https://medium.com/media/d1524da085d75d2364c640e8b4f1c232/href</a></iframe><p>Cool. Let’s add the other two delegates:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ba6779c68210dea4dab1d59189faa792/href">https://medium.com/media/ba6779c68210dea4dab1d59189faa792/href</a></iframe><p>So, with all that finished, we change our wedding venue class to this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/efcd40e585ed2651fe1580c03f9b4a24/href">https://medium.com/media/efcd40e585ed2651fe1580c03f9b4a24/href</a></iframe><p>We now have 3 delegates who are going to handle all of the different functions, just like in a wedding we might have a caterer, a DJ and an officiant. Instead of hard-coding in all of the default values, we are going to defer to our delegate to handle all of the different delegate functions. Let’s put all of this to some good use now by adding a MusicDelegate and a FoodDelegate.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c70d9fc915b66a13f1227a16a8392585/href">https://medium.com/media/c70d9fc915b66a13f1227a16a8392585/href</a></iframe><p>Since we now have classes that conform to all of the different delegates, we can now put them to use.</p><p>Now, immediately there are a few benefits to doing this in our code.</p><ol><li>Classes can become reusable and more generic when we split out the code that handles the heavy lifting for specific features.</li><li>Splitting some of the logic out of the main class allows for smaller classes as the different responsibilities are separated out and each delegate is only responsible for some of the code. Having code with a distinct concrete job makes it easier to reason about the code and to modify.</li><li>Making a delegate can make it easier to change your code later. For example, let’s say you were using something like Parse or Firebase as the backend for your app, but later decide to roll your own server. If you’ve made a NetworkServiceDelegate, then all you have to do is make your new network code conform to the new delegate protocol and won’t have to modify the rest of the code.</li></ol><p>To see how it all works, try copying the code below into a playground and messing around with it.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5414358cca5d37be0d97b017626c657c/href">https://medium.com/media/5414358cca5d37be0d97b017626c657c/href</a></iframe><p>This is just a silly example, but imagine this could be a table that is re-usable for different types of media like videos with a MediaDelegate, responsible for returning information about / presenting media. You can easily change the behavior of a class without having to completely all of the logic that handles it. This is exactly how UITableViews/UICollectionViews work with their delegates and data sources and we can harness this power ourselves.</p><p>Hopefully this helps give you some ideas about the delegate pattern and some thoughts about how you might be able to use it. What are some other ways that you use the delegate pattern in your programming?</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=44ca0e320271" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swiftjoe/making-your-own-delegates-44ca0e320271">Making Your Own Delegates</a> was originally published in <a href="https://medium.com/swiftjoe">swiftjoe</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Using Getters and Setters with User Defaults]]></title>
            <link>https://medium.com/swiftjoe/using-getters-and-setters-with-user-defaults-973446a0cea8?source=rss-209bd5bbca54------2</link>
            <guid isPermaLink="false">https://medium.com/p/973446a0cea8</guid>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Joe Spadafora]]></dc:creator>
            <pubDate>Mon, 06 Feb 2017 21:56:07 GMT</pubDate>
            <atom:updated>2017-02-08T15:26:19.389Z</atom:updated>
            <content:encoded><![CDATA[<p>Most people know that user defaults is a really simple way to store lightweight data and to keep track of user preferences. By using computed properties, you can streamline how you store items into user defaults.</p><p>Most apps have some places where settings are stored, usually small bits of information or a simple Bool value. It could be something like enabling/disabling haptic feedback, storing a name to greet your user, or keeping track of a logged in user id.</p><p>Let’s take the example of storing a retrieved user id. One method for doing it might be something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b55991f0977b2c61b3a6a9edeb3bdbf3/href">https://medium.com/media/b55991f0977b2c61b3a6a9edeb3bdbf3/href</a></iframe><p>This is mostly my opinion, but I believe that if something is just a standard value then why wrap it in a function? Personally, I prefer to simply make it a computed variable.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a3d9690997aad18c05104a3caabddb90/href">https://medium.com/media/a3d9690997aad18c05104a3caabddb90/href</a></iframe><p>I think this has a few benefits over using the functions to set and retrieve the value.</p><ol><li>First the value looks just like any other variable that we have, and it would look cleaner in a function call doFunctionWith(id: userId) looks much nicer to me than doFunctionWith(id: getIdFromDefaults()).</li><li>The logic to retrieve and set the variable is right there with the variable declaration. No need to look in two different places if you need to change logic of the code.</li><li>The community making the language design choices for swift seems to prefer this look for values. Take for example UIColor.redColor() is now UIColor.redColor.</li></ol><p>One of the items for me that makes this really powerful and really compelling is that we can use this to increase readability of things like preferences and toggles. Let’s imagine we have a way to choose what theme color our app uses. That might be easy to represent with an enum.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fae5cebc7b906a6c33e74348d5d22c36/href">https://medium.com/media/fae5cebc7b906a6c33e74348d5d22c36/href</a></iframe><p>Now, you might be thinking…wait, why do it as an Int? If we make this an Int, we’re going to get a few behaviors for free out of user defaults. First, when we save to UserDefaults, it is possible to Archive the color to UserDefaults using NSKeyedArchiver, but that might be more useful if we were trying to save a color generated by our user.</p><p>Second, with this, we can use the rawValue initializer of the Enum to allow us to have a default value. When UserDefaults tries to return an Int, if it can’t find it, then it returns 0 instead.</p><p>So, what would this look like when we put it into action in our code? Let’s imagine we have a tableview to select the theme color for our app. We might see some code that looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4d76e9aa53fe84d63e57d6e342cad466/href">https://medium.com/media/4d76e9aa53fe84d63e57d6e342cad466/href</a></iframe><p>And with the enum being an Int, UserDefaults returns 0 if nothing is saved there, which would return the first case. If we ever wanted to change the default case, all we need to do is change the order of the enum.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=973446a0cea8" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swiftjoe/using-getters-and-setters-with-user-defaults-973446a0cea8">Using Getters and Setters with User Defaults</a> was originally published in <a href="https://medium.com/swiftjoe">swiftjoe</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Cleaning Up CollectionViews and TableViews using Enums]]></title>
            <link>https://medium.com/swiftjoe/cleaning-up-collectionviews-and-tableviews-using-enums-63589b9564f3?source=rss-209bd5bbca54------2</link>
            <guid isPermaLink="false">https://medium.com/p/63589b9564f3</guid>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[mobile-app-development]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Joe Spadafora]]></dc:creator>
            <pubDate>Mon, 30 Jan 2017 19:31:08 GMT</pubDate>
            <atom:updated>2017-01-31T15:23:35.933Z</atom:updated>
            <content:encoded><![CDATA[<p>UITableViews and UICollectionViews can very easily get out of hand. Especially if you have a table where you need to display different types of items in different sections.</p><p>Let’s imagine an app where there are 4 sections in a tableview. The first section, section 0, might be one that displays an error banner on top of the collection if there is some sort of issue (Wifi being off, no bluetooth, etc.). Section 1 would contain a list of friends you’ve already connected with. Section 2 contains a list of friends that have sent invitations to you. Section 3 contains a cell that shows a background image if there is nothing in section 1 or 2, letting you know that you should pull to refresh.</p><p>Setting up the different sections might look something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9d06690d0f317334e39435bdfc5f1ec5/href">https://medium.com/media/9d06690d0f317334e39435bdfc5f1ec5/href</a></iframe><p>So, you’ve done the same thing throughout the rest of your controller, and everything works fine. Then, a day later, after setting up different cell sizes, logic and handling, the UX designers on your team decide that the invitations should be ON TOP of the friends.</p><p>So…you go through all of the 800 different delegate methods to fix it, changing lots of code…And you’ve now wasted an hour or so rewriting everything to work.</p><p>Is there a better way? Yes. It’s a simple solution with Enums and a simple extension on Int. First, make an enum with all the different sections of your table. This will allow you to do two great things: Use switch statements to make sure that you are covering all your cases, and abstract away the number of each section.</p><p>Adding the extension to Int is just some syntactic sugar.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5c0f4f07b0c840cabaffaff306f7994f/href">https://medium.com/media/5c0f4f07b0c840cabaffaff306f7994f/href</a></iframe><p>With this now added, you can see the code is now more readable, cleaner, and easier to understand.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7e8cc0dcead3c2fdcf3670e18fe55c42/href">https://medium.com/media/7e8cc0dcead3c2fdcf3670e18fe55c42/href</a></iframe><p>Now, the real beauty of this, is that to change the order of the sections, the only thing you have to do is switch the order of the cases in your enumeration. What was once a tedious task to go through and modify all of the existing code for the delegate methods is fixed in the blink of an eye.</p><p>Hopefully this cool trick helped you!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=63589b9564f3" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swiftjoe/cleaning-up-collectionviews-and-tableviews-using-enums-63589b9564f3">Cleaning Up CollectionViews and TableViews using Enums</a> was originally published in <a href="https://medium.com/swiftjoe">swiftjoe</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>