<?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 Alex Reilly on Medium]]></title>
        <description><![CDATA[Stories by Alex Reilly on Medium]]></description>
        <link>https://medium.com/@twof?source=rss-5b077703b03c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*xLwuD7ffMps4IPP8E7lJAA.jpeg</url>
            <title>Stories by Alex Reilly on Medium</title>
            <link>https://medium.com/@twof?source=rss-5b077703b03c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 04:37:33 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@twof/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[The Magic of iOS Unit Testing with XCTest and Swift 3]]></title>
            <link>https://medium.com/ios-seminar/the-magic-of-ios-unit-testing-with-xctest-and-swift-3-8889c838b911?source=rss-5b077703b03c------2</link>
            <guid isPermaLink="false">https://medium.com/p/8889c838b911</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[ios]]></category>
            <dc:creator><![CDATA[Alex Reilly]]></dc:creator>
            <pubDate>Mon, 06 Feb 2017 17:58:17 GMT</pubDate>
            <atom:updated>2017-02-06T17:58:17.105Z</atom:updated>
            <content:encoded><![CDATA[<p>I’m just going to jump right in. (<a href="https://github.com/twof/FRPMS/tree/master/1117-th/RxOpenWeather-starter">Example Project</a>)</p><h3>Unit Testing is Important</h3><p>There’s no point in asking why it’s important because I’m going to tell you right now that it is. Could you go your whole life without writing a single test? Sure. Will you be less productive? Yes. Will you write more bug-ridden code? Also yes. Testing is important.</p><p>Testing is especially important today because you aren’t passing numbers around on a backend. You’re making an iOS app and more likely than not, your iOS app is making network calls. Let’s say you decide that testing isn’t important and you’re going to try out each new bit of code manually. You’re a bit of a ways into your app so you’ve got a login screen and a few other views so to test our your new feature, you do the following:</p><p>1. Launch the simulator<br>2. Wait for your app to install<br>3. Wait for your app to launch<br>4. Sign into your app<br>5. Navigate through a few views<br>6. Press the bit that triggers your new code<br>7. View results and iterate</p><p>And if the results aren’t as expected, you change your code and do everything over again. That’s slow. What does this same process look like when you decide testing is important?</p><p>1. Write a test for your new function or alter an old test if your new code is already covered<br>2. Run your test<br>3. View results and iterate</p><p>And if the results aren’t as expected you can skip step one for every future iteration. Testing is important.</p><h3>How?</h3><p>Testing is a little different depending on which platform and language you’re using so this is a fair question.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/386/1*LrgYzQiWyLJ_XkXP4_W0nA.png" /></figure><p>You’re going to click the highlighted symbol. This is where all of your tests will live. Then you’ll click the plus at the bottom left of the screen and then New Unit Test Target, selecting your app in the screen that pops up. Once you’ve done that select your new target and click the plus again and then New Unit Test Class. We’re going to be writing tests for our OpenWeatherAPIHelper class, so naturally we’ll name the test class “OpenWeatherAPITests”.</p><p>There’s one last step before we start writing. Navigate over to the class you’ll be testing, pop out the right panel and make sure your test class is checked under Target Membership</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/520/1*gQZaPeCKMxLB5ov2OiF4mg.png" /></figure><p>This allows us to use classes and variables within the file which makes sense seeing as we’ll be testing classes and variables within the file.</p><h3>Dealing with Cocoapods</h3><p>You may have already done this, but you have to change your Podfile setup if you want your pods to work while testing. I like to have all my pods during both testing and production so my setup looks like this:</p><pre>def shared_pods<br>  pod &#39;RxSwift&#39;<br>  pod &#39;RxCocoa&#39;<br>  pod &#39;RxAlamofire&#39; <br>  pod &#39;ObjectMapper<br>end</pre><pre>target &#39;RxOpenWeather&#39; do<br>  use_frameworks!</pre><pre>shared_pods<br>end</pre><pre>target ‘RxOpenWeatherTests’ do<br>  use_frameworks!<br>  <br>  shared_pods<br>end</pre><p>TL;DR, you need to set up a separate target for each new testing target.</p><h3>Writing Some Code</h3><p>Finally we get around to writing some code.</p><p>You’ve been given some boilerplate by XCode so we’ll go through each block and talk about what it’s for.</p><pre>override func setUp() {</pre><pre>    super.setUp()</pre><pre>}</pre><p>setup() is for, as it says, setting up. It’s run before the tests in the class so this is a good place to create an instance of the class you’ll be testing along with anything else you might need. For example, if you find yourself using the same constants in a bunch of tests, you might consider moving their instantiation to setup().</p><p>For our case, we’re testing RxOpenWeatherMapAPI so we’ll create an instance in setup.</p><pre>class OpenWeatherAPITests: XCTestCase {</pre><pre>    var openWeatherAPI: RxOpenWeatherMapAPI! = nil</pre><pre>    override func setUp() {</pre><pre>        super.setUp()</pre><pre>        openWeatherAPI = RxOpenWeatherMapAPI()</pre><pre>    }<br>}</pre><p>This is more or less what a baseline setup() function looks like.</p><p>After setup() you’ll see teardown() . This badboy is run after all of your tests have completed whether they succeeded or failed. For our purposes we don’t have anything we need to do afterwards so teardown() will remain empty.</p><p>Now for the meat of OpenWeatherAPITests . The next function you’ll see is testExample() . We’re going to replace it with our own function we’ll call testBuildWeatherURL() because we’ll be testing RxOpenWeatherAPI.buildURLForCurrentWeather() with this function. Function names should be as descriptive as possible because that’s what we’ll be seeing in our console when a test fails. buildURLForCurrentWeather() takes a city name and the unit we want our temperatures to be in and returns a URL that will be used to make an API call later. Because it’s so simple, it’s really easy to test. All we have to do is pass in some reasonable parameters, capture the returned value, and Assert that the the returned value is what we might expect it to be. Assert is common in testing across loads of platforms and testing framework so now that you know testing is important, you’re bound to see it again. In our case we’ll be using XCTAssert from XCTest which will take a boolean and some message in the form of a string to print out if the boolean assertion fails.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8889c838b911" width="1" height="1" alt=""><hr><p><a href="https://medium.com/ios-seminar/the-magic-of-ios-unit-testing-with-xctest-and-swift-3-8889c838b911">The Magic of iOS Unit Testing with XCTest and Swift 3</a> was originally published in <a href="https://medium.com/ios-seminar">iOS Seminar</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Swift Generics]]></title>
            <link>https://medium.com/ios-seminar/advanced-swift-generics-1955499b85df?source=rss-5b077703b03c------2</link>
            <guid isPermaLink="false">https://medium.com/p/1955499b85df</guid>
            <category><![CDATA[taylor-swift]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[generics]]></category>
            <dc:creator><![CDATA[Alex Reilly]]></dc:creator>
            <pubDate>Fri, 27 Jan 2017 02:23:57 GMT</pubDate>
            <atom:updated>2017-01-29T00:04:39.070Z</atom:updated>
            <content:encoded><![CDATA[<p>Generics are pretty cool. What are generics though? In general, you want to write the least code possible to carry out a task, and one of the best ways of accomplishing such a goal is reuse. Being able to reuse a piece of code for similar tasks can greatly reduce time and energy put into a program, and generics are just the tool for the job.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f030c76f241213ae5690a87ead79df3e/href">https://medium.com/media/f030c76f241213ae5690a87ead79df3e/href</a></iframe><p>Let’s look at the above function. It’s a great function. Does exactly what it says it does, but what if you want to swap some doubles? You could write another function.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ff714bec7afd9f714522b7d900edfa0e/href">https://medium.com/media/ff714bec7afd9f714522b7d900edfa0e/href</a></iframe><p>Now we have two functions, and we could keep going if we wanted to. One for Float, Long, String…you get the idea. It would be nuts if we wanted to cover a bunch of cases. Lucky for us, Swift gives us generics.</p><p>Let’s see what that would look like.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c485b68197e6424d125274a100f7299f/href">https://medium.com/media/c485b68197e6424d125274a100f7299f/href</a></iframe><p>Breaking it down, T represents whatever type is passed in. The parameters swapTwoValues takes both need to be the same type (ie. can’t pass (a: String, b: Double)), but they can be any type! This is nice because our potentially infinite number of functions that we would have had to write before now becomes just one!</p><p>There’s much more we can make generic than just functions. Types for example can be generic, in fact you’ve probably already seen a generic type or two in the form of Array&lt;&gt; or Dictionary&lt;&gt;.</p><p>Let’s suppose we were making a Linked List class. I don’t know about you, but I’d start by establishing a Node class. It might look a bit like this.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ebb321d61a071de17f9a6c7bd77704a6/href">https://medium.com/media/ebb321d61a071de17f9a6c7bd77704a6/href</a></iframe><p>Looks okay I guess, but what if I want to store Strings or even multiple types in one list? This is the perfect opportunity to implement a generic class.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/811c53715a6dce9744d6e840c8929b2c/href">https://medium.com/media/811c53715a6dce9744d6e840c8929b2c/href</a></iframe><p>Beautiful! Now every time we want to add a new node to our Linked list, we can initialize it with Node&lt;Int&gt;(content: 2, next: nil) or Node&lt;String&gt;(content: &quot;hello&quot;, next: nil) or anything really. Just depends how we’re feeling at the moment. In fact we can combine our generic function know how and this newfound generic type knowhow!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f3ac91f6fef447deb622da5ffd7b40e5/href">https://medium.com/media/f3ac91f6fef447deb622da5ffd7b40e5/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1955499b85df" width="1" height="1" alt=""><hr><p><a href="https://medium.com/ios-seminar/advanced-swift-generics-1955499b85df">Swift Generics</a> was originally published in <a href="https://medium.com/ios-seminar">iOS Seminar</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to automatically copy screenshots to your clipboard (macOS)]]></title>
            <link>https://medium.com/@twof/how-to-automatically-copy-screenshots-to-your-clipboard-macos-4253da5ac57?source=rss-5b077703b03c------2</link>
            <guid isPermaLink="false">https://medium.com/p/4253da5ac57</guid>
            <category><![CDATA[applescript]]></category>
            <category><![CDATA[apple]]></category>
            <category><![CDATA[copy]]></category>
            <category><![CDATA[terminal]]></category>
            <category><![CDATA[mac]]></category>
            <dc:creator><![CDATA[Alex Reilly]]></dc:creator>
            <pubDate>Tue, 24 Jan 2017 20:27:45 GMT</pubDate>
            <atom:updated>2017-01-24T20:31:43.756Z</atom:updated>
            <content:encoded><![CDATA[<p>If you take as many screenshots as I do, you may have run into this problem. Typically when taking a screenshot, I want to share it immediately and the easiest way to do that is to paste it to whichever document or chat I took the screenshot for. I could use ^⇧⌘4 to copy the image directly, but then I wouldn’t have a copy to refer back to later. My oddball needs required a custom solution.</p><h3>Step 1:</h3><p>Pop open Terminal and paste in the following command replacing the path with your own desired screenshot location. The default is the desktop but I prefer to keep my desktop clean.</p><pre>defaults write com.apple.screencapture location ~/Pictures/Screenshots</pre><p>This alters the location where your screenshots are saved.</p><h3>Step 2:</h3><p>Open Finder and navigate over to the folder you’ll now be saving screenshots. Right click on the folder and in the context menu click Services&gt;Folder Actions Setup…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ywSZ0Q5vjZmHXUACBnOq8w.png" /></figure><h3>Step 3:</h3><p>In the window that should have just popped up, select “add” and then “Edit Script” in the lower right hand corner.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ley1q2XiXnJzTFFSxd4cBQ.png" /></figure><h3>Step 4:</h3><p>A default script will pop up. All the default does is let you know when a new item has been placed in your folder of choice. That’s pretty lame so we’re going to replace it with the following.</p><pre><strong>on</strong> <strong>adding folder items to</strong> this_folder after receiving added_items</pre><pre><strong>    set the clipboard to</strong> (<strong>read</strong> (<strong>last</strong> <em>item</em> <strong>of</strong> added_items) as <em>TIFF picture</em>)</pre><pre><strong>end</strong> <strong>adding folder items to</strong></pre><p>The whole thing should look something like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MsGYzNR25IffSWCJV5OQSw.png" /></figure><p>Go ahead and save and you’re good to go!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0PhzjVry9pncdfBrcwmJKg.gif" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4253da5ac57" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[On The Optimization of Bubble Sort]]></title>
            <link>https://medium.com/@twof/on-the-optimization-of-bubble-sort-e1096d93d478?source=rss-5b077703b03c------2</link>
            <guid isPermaLink="false">https://medium.com/p/e1096d93d478</guid>
            <category><![CDATA[tech]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Alex Reilly]]></dc:creator>
            <pubDate>Sun, 01 May 2016 02:24:52 GMT</pubDate>
            <atom:updated>2016-05-01T02:32:44.827Z</atom:updated>
            <content:encoded><![CDATA[<p>Bubble sort is inefficient. Like really really really inefficient. But you see, I didn’t know that when I first started learning C++, so with pen and paper while I sat in Calculus, I wrote bubble sort, a list of ten numbers, and proceeded to sort them by hand.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xtMkLvZkclXUSScNgV5xSg.jpeg" /></figure><p>I like playing games. If I could take that feeling of setting a score for yourself, knowing that it can be beaten, then working towards and eventually beating that score into a capsule, I’d be a drug addict. When it came to sorting my little handwritten lists of numbers, the parallels were immediately apparent. As someone who doesn’t enjoy the act of writing, the first score I cared about was the number of passes the algorithm would have to go through before the list was sorted. Once I started turning my scribbles into code and putting them into XCode, the score I chased was time. It became a game of millisecond golf that’s still continues today. Note that I don’t expect any of what I did to be novel. I’m sure it’s all been done before. This is about me more than it is about bubble sort.</p><p>Let’s start by looking at the generic bubble sort. It looks something like this.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d2917e4f2872b0b9b3b2d26749412d26/href">https://medium.com/media/d2917e4f2872b0b9b3b2d26749412d26/href</a></iframe><p>The basic idea is it runs through an array, and if the index after the one you’re looking at is less, you swap the two and set sorted to false. If any swaps have happened in the last pass, do the entire thing over again. If not, the list is sorted.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5b93ddc18072e224f66aa4e2d79f4743/href">https://medium.com/media/5b93ddc18072e224f66aa4e2d79f4743/href</a></iframe><p>This is what it looks like. Each row represents one pass. Using this algorithm, sorting a list that contains 1 million randomly generated integers takes 2242928.0ms. That’s about 37 and minutes. That’s absurdly long. So what can we do to fix this? Let’s start by looking at where this basic bubble sort fails.</p><p>If you follow the numbers through each pass, you’ll notice a few things. The first being that the 10 gets pushed all the way into position right off the bat. This is because bubble sort by nature will push the highest element left out of position during each pass into position. This is a really nice feature. We automatically know that some elements are sorted after each pass, and can therefore ignore those elements.</p><p>Doing that brings our time down to 1626360.0ms or 27 minutes and 58 seconds which seems odd. It’s not that much! Why? By neglecting the final elements in the list, we eliminate some of the unnecessary comparisons, but the number of swaps remains the same. Sadly, swapping is where most of the time is spent, so limiting the comparisons doesn’t help us as much as one might hope.</p><p>So how do we do that? This is the first solution I came up with when I was sitting in calculus class. What we’re going to do is start from the ends of the array (the first and last elements) and move towards the center making comparisons and swapping as necessary. In the above example, 10 had to be swapped all the way down the list which is expensive computationally. By instead swapping from opposite ends of the list, we manage to move elements that find themselves far from their rightful positions a good distance closer, therefore allowing the sort to be far less computationally expensive!</p><p>With that, we bring our time down to 1095600.0ms or 18 minutes and 49 seconds. We’ve cut our original time down by over a third! How great is that? In the words of the late Billy Mays “But wait! There’s more!” I’m going to again remind you, dear reader, that the code you’re about to see was written when I was just getting started with C++. DRY was completely foreign to me. I tell you this so you don’t hurt me when you see this monstrosity:</p><p><a href="https://github.com/twof/SwapSort/blob/master/SwapSort.cpp">twof/SwapSort</a></p><p>I’m so, so sorry. Really I am. Hopefully you’ll forgive me if I explain. It didn’t take me long to realize that by further subdividing the array, then preforming swap sorts on each subdivision, we can bring down the total amount of swapping even further, but I was an idiot, so I decided the best way to do it would be manually. That thousand+ line program you’re looking at, is the result of what I thought would be the best way to swap sort the whole array, then in halves, quarters, 8ths, 16ths, 32nds, and 64ths. All that shitty code did pay off though, because a 0ne million element integer array can now be sorted in 25650.0ms. That’s about 26 seconds or ~1/70th of the time of our original bubble sort.</p><p>At the point I was at in my education, I wasn’t able to go any further unless I wanted to spend a few hours copy/pasting, so that was it for me then. But of course, here I sit writing this so I must have a reason, right? Right you are! I picked up where I left a few days ago and finished a much shorter V2.0 that night. Here is that V2.0.</p><p><a href="https://github.com/twof/SwapSortV2/blob/34bbd2044a2f1e699f058273df9f87df41595f55/main.cpp">twof/SwapSortV2</a></p><p>What have I done here? For one, I rolled up that massive block from V1.0 so that not only does looking at the thing no longer cause dysentery, it will also continue subdividing until it can’t anymore. Total runtime: 9162.0ms or 1/200th of the original. But I’m still not satisfied so let’s go a little further.</p><p>There’re a few months in between the above and what I’m writing now so bear with me for a bit here. I turned my bubble sort into a <a href="https://en.wikipedia.org/wiki/Cocktail_shaker_sort">Cocktail Shaker Sort</a> which is the bidirectional version of the bubble sort. By doing this, I know that one additional element on both ends of the array is in place after each pass. I also adapted the swap sort portion to ignore the in place elements rather than checking them on each pass. With that plus some tweaking, we’re down to ~3150 ms or 1/712th of the original.</p><p><a href="https://github.com/twof/SwapSortV2/blob/070f900b32ee3b7be59b35c0adcd5f70e294e4b2/main.cpp">twof/SwapSortV2</a></p><p>After doing all that though, I suspect I may be able to squeeze a little more speed out of this thing if I only bubble sort in one direction and account for that in the swap sort. Let’s look at why.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a3d4962d296942e25789b7f043eea7fb/href">https://medium.com/media/a3d4962d296942e25789b7f043eea7fb/href</a></iframe><p>Notice that by shifting the entire sort over one element every time rather than cutting off the ends, the elements being compared are more likely to be different from the elements compared in the previous pass. By doing this, we’re also able to implement another bubblesort optimization method. Occasionally by chance, more than one value will fall into place. We can detect that by keeping track of the last swapped element, and comparing up the that point.</p><p>But I think I’m going to leave off with that cliffhanger. I fear that if I don’t publish this now I’ll keep hacking away at this problem and never publish anything, and what’s the point of coding if no one ever sees your work?</p><p>Thus</p><p>To be continued…</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e1096d93d478" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>