<?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 Maciej Krolikowski on Medium]]></title>
        <description><![CDATA[Stories by Maciej Krolikowski on Medium]]></description>
        <link>https://medium.com/@maciejkrolikowski?source=rss-a69d412c2e23------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*JBDVSNx3q5cEMStJ.</url>
            <title>Stories by Maciej Krolikowski on Medium</title>
            <link>https://medium.com/@maciejkrolikowski?source=rss-a69d412c2e23------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 08:14:28 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@maciejkrolikowski/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[Internal Implementations of Dictionary and Set in Swift]]></title>
            <link>https://blog.stackademic.com/set-dictionary-hard-questions-straightforward-answers-e93966eae4ac?source=rss-a69d412c2e23------2</link>
            <guid isPermaLink="false">https://medium.com/p/e93966eae4ac</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[data-structures]]></category>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[data-structure-algorithm]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Maciej Krolikowski]]></dc:creator>
            <pubDate>Wed, 02 Aug 2023 08:43:02 GMT</pubDate>
            <atom:updated>2023-08-03T07:03:07.636Z</atom:updated>
            <content:encoded><![CDATA[<p>In today’s article, we will explore the internal implementations of Dictionary and Set in Swift language, and how these implementations influence their specific behaviors. By the end of this article, we will understand:</p><ul><li>why does the Dictionary store values in an unordered way?</li><li>why does the Set store values in an unordered way?</li><li>why doesn’t the Set contain duplicated values?</li></ul><p>In the Swift language, there are three types of Collections: Array, Dictionary, and Set</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jVTyTO3DnttIKncvV5pIAQ.png" /></figure><p><strong>Array</strong>, probably the most frequently used collection type, is usually used to populate <em>Lists</em> in <em>SwiftUI</em> or <em>TableView</em> in <em>UIKit</em>. Arrays are index-based collections, meaning each value has a paired index, and the elements follow a specific order.</p><p><strong>Set</strong> differs in that it doesn’t have associated indexes. Values in a Set can only be accessed by iterating through it. Compared to Array, Set cannot hold duplicate values, and its values are unordered. The following example illustrates this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0b71f7804d36f5dd04968bda153e12eb/href">https://medium.com/media/0b71f7804d36f5dd04968bda153e12eb/href</a></iframe><p>As you can see there is only one “Poland” and the order of elements doesn’t match our specified order.</p><p><strong>Dictionary</strong> allows us to fetch values using associated keys, and similar to a set, its values are unordered.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2700561aec5a05653d541c845b8da371/href">https://medium.com/media/2700561aec5a05653d541c845b8da371/href</a></iframe><p>Now that we understand the differences between Array, Set, and Dictionary, let’s dive into the documentation and check what requirements need to be satisfied in order to use each of the collection types.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1b62e46cc3113fac5d7be1a495403f14/href">https://medium.com/media/1b62e46cc3113fac5d7be1a495403f14/href</a></iframe><p>Adding any value to an <em>Array</em> is straightforward since <em>Elements</em> doesn’t need to conform to any protocol.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2be3359472563cc6d34274e48fcbd133/href">https://medium.com/media/2be3359472563cc6d34274e48fcbd133/href</a></iframe><p>For a Set, its elements must conform to the <a href="https://developer.apple.com/documentation/swift/hashable"><em>Hashable</em></a> protocol.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ba9e30c762f329bee5037bd0d8cde2c8/href">https://medium.com/media/ba9e30c762f329bee5037bd0d8cde2c8/href</a></iframe><p>Similarly, a dictionary’s <em>Key</em> needs to conform to the Hashable protocol. Conformance to the <em>Hashable</em> protocol ensures that a type can produce a unique integer hash value.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/463f773a90d67e6032a2c8ae16c3fffd/href">https://medium.com/media/463f773a90d67e6032a2c8ae16c3fffd/href</a></iframe><p>For exactly the same values, the same hash values will be returned. By default, all value types implement the <em>Hashable</em> protocol.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/42a246252ce764b24e7c25dc806566fd/href">https://medium.com/media/42a246252ce764b24e7c25dc806566fd/href</a></iframe><p>Creating a hash value from multiple values is performed by using a <em>Hasher</em>. We call combine on values and once we finish, we call a finalize function that returns a hash value. The order of values combination is important. In the following example, <em>hasher3</em>, unlike <em>hasher1</em> and <em>hasher2</em>, starts the combination with <em>“text”</em> followed by <em>23</em>. As a result, it generates a unique hash.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/89be14c7730972affbfcca7c5cdc4381/href">https://medium.com/media/89be14c7730972affbfcca7c5cdc4381/href</a></iframe><p>Hash values can also be generated from <a href="https://developer.apple.com/swift/blog/?id=10">reference types</a> — <em>Classes</em>. All we have to do is conform to the <em>Hashable</em> protocol and implement two methods. In the following example, as struct is a value type and its values are value types, we don’t need to add an == and <em>hash</em> method. For exactly the same values, two different types will return the same hash value.</p><p>It’s crucial to understand how <em>Hashable</em> works to comprehend the implementation of a <em>Dictionary</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hQ8yxuZqB3TZT6VT6p3Q1w.png" /></figure><p>You may have noticed that hash value can take large numbers, so the hash value needs to be converted to a reasonable index via Dictionary’s internal function. There are sometimes situations where different keys are converted to the same index in an array. This is known as a <em>hash collision</em>. To handle this, each element is a <a href="https://en.wikipedia.org/wiki/Linked_list">linked list</a>. All of the linked list’s nodes consist of a key and a value and a reference to the next code. In the above example inserting the value with a key of <em>“FIN”</em> starts with hashing a key and then conversion to the index of the array. Under index <em>1</em> there is already the element with the key <em>“PL”</em>. Since the keys “FIN” and “PL” are not equal, the new element is added as the next node in the linked list. Element replacement would occur when we add a new element with a key that already exists in the <strong>HashTable</strong>.</p><p>The construction of the Set is similar. Instead of a hashable <em>Key,</em> we use a hashable <em>Value</em>.</p><p>With the knowledge of data structures and algorithms used in <em>Set</em> and <em>Dictionary,</em> we can answer previously stated questions.</p><p><strong>Question 1</strong>: why does the <em>Dictionary</em> store values in an unordered way?</p><p><strong><em>Answer:</em></strong> We never know where in a <em>HashTable</em> certain key will be placed.</p><p><strong>Question 2</strong>: why does the <em>Set</em> store values in an unordered way?</p><p><strong><em>Answer:</em></strong> We never know where in <em>HashTable</em> values will be placed.</p><p><strong>Question 3:</strong> why doesn’t the Set contain duplicated values?</p><p><strong><em>Answer 3:</em></strong> Two of the same values will be assigned to the same index in the <em>HashTable</em> so a new inserted value will overwrite the old one.</p><p>In the end, it’s worth knowing about the search complexity benefits of the <em>Set</em> over the <em>Array</em>. When searching for an element in the array, the program needs to iterate through all elements and compare them against the searched element. In the case of a Set, the searchable value is converted into numeric representations that point to a place in memory where this value is located. So the Set’s search complexity is <em>O(1)</em>, whereas Array’s complexity is <em>O(n)</em>.</p><p>Ok, that’s it for now. If this article has been of value to you, I kindly ask you to consider sharing it with others who might benefit from it.</p><p><em>Thank you for reading until the end. Please consider following the writer and this publication. Visit </em><a href="https://stackademic.com/"><strong><em>Stackademic</em></strong></a><em> to find out more about how we are democratising free programming education around the world.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e93966eae4ac" width="1" height="1" alt=""><hr><p><a href="https://blog.stackademic.com/set-dictionary-hard-questions-straightforward-answers-e93966eae4ac">Internal Implementations of Dictionary and Set in Swift</a> was originally published in <a href="https://blog.stackademic.com/">Stackademic</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Preventing Memory Leaks Using XCTests]]></title>
            <link>https://medium.com/better-programming/preventing-memory-leaks-using-xctests-5fee5e1fa7c5?source=rss-a69d412c2e23------2</link>
            <guid isPermaLink="false">https://medium.com/p/5fee5e1fa7c5</guid>
            <category><![CDATA[ios-app-development]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[unit-testing]]></category>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Maciej Krolikowski]]></dc:creator>
            <pubDate>Fri, 06 May 2022 07:35:24 GMT</pubDate>
            <atom:updated>2022-05-11T11:56:41.802Z</atom:updated>
            <content:encoded><![CDATA[<h4>Reduce your reliance on manual tests in iOS</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*xK-ceSzDhU4qi_EV" /><figcaption>Photo by <a href="https://unsplash.com/@williamtm?utm_source=medium&amp;utm_medium=referral">William Hook</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>While developing Swift apps, we are exposed to some risks. One of them is memory leaks.</p><p><a href="https://docs.swift.org/swift-book/LanguageGuide/AutomaticReferenceCounting.html">Automatic Reference Counter</a> (ARC) releases an object from the memory, only in cases where there are no strong references to that object. Furthermore when two reference types have a reference to each other, non of them will ever be released from the memory, which causes a memory leak.</p><p>To investigate such an issue we can use Xcode Instruments or Memory Graph Debugger. These tools allow an in-depth analysis of the occurred leaks. However, the question we should answer is whether there are any ways to actually prevent memory leaks in the first place?</p><p>The answer is yes, by the use of unit tests. Execution of automated tests allows catching bugs in an application before passing this application on to the QA team or the clients themselves. Let’s take a look at how to implement such protection.</p><p>Let’s consider a simple view controller with a button.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/38bcc752fdb85b8186bac17cc39e1e3f/href">https://medium.com/media/38bcc752fdb85b8186bac17cc39e1e3f/href</a></iframe><p>Inside the action handler, a strong reference to self, causes retain cycle between button and LeakingViewController. As you can see, even in this simple example, we can create a memory leak. Now let’s write a test that will alert us about an issue.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/856662bbc5f686e68f7aa46d9791258f/href">https://medium.com/media/856662bbc5f686e68f7aa46d9791258f/href</a></iframe><p>After creating a view controller, you can notice some mysterious block called addTeardownBlock. This block is executed after exiting the current test method — similarly to the tearDown method. It holds a weak reference to a viewController in order to not create a retain cycle. In our example after finishing a test code, the button and the viewController will hold each other references, creating a retain cycle so addTeardownBlock is able to access the viewController’s reference, which in turn informs us about the memory leak.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oPeD0TRKoSnsiYhw4f2BLg.png" /></figure><p>Weakifying <em>self</em> in the button’s handler closure will get rid of the memory leak, satisfying the tests at the same time.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/17d9fa4475f89f69724c897f7a535bdb/href">https://medium.com/media/17d9fa4475f89f69724c897f7a535bdb/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JXWY6tA_IEqnAo12qq-6Gw.png" /></figure><p>As you can see, a small overlooking can lead to a memory leak. Let’s extend this example by adding another presenter class — known from the MVP pattern.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1f0ca12123ee07c7df30cb4b8304f218/href">https://medium.com/media/1f0ca12123ee07c7df30cb4b8304f218/href</a></iframe><p>The LeakingViewControllerPresenter conforms to the LeakingViewControllerDelegate, which gives a possibility to call startLoading() method, but also the LeakingViewController can call the buttonClicked() on the LeakingViewControllerPresenter. By not making the delegate property weak, we’re introducing another retain cycle, which is proven by our test.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lOD-1PsA-pBT2uZLwcxonA.png" /></figure><p>Let’s quickly fix a <em>presenter</em> class.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8b650edfe60719a92ad9735b04830f4b/href">https://medium.com/media/8b650edfe60719a92ad9735b04830f4b/href</a></iframe><p>And our test starts passing again.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GAB5OvU3wYy4tM1PxXrZ6g.png" /></figure><p>As the code starts growing there can be many other causes that can lead to memory leaks.</p><p>Dealing with them, by using automatic unit testing, can significantly improve the quality of your app and save time, that you would otherwise have to spend on tedious manual debugging.</p><p>Even if you are not sure if your code will trigger a retain cycle, it’s always good to use this method to gain confidence in your code.</p><h4><em>Reference</em></h4><p><a href="https://www.essentialdeveloper.com/">https://www.essentialdeveloper.com</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5fee5e1fa7c5" width="1" height="1" alt=""><hr><p><a href="https://medium.com/better-programming/preventing-memory-leaks-using-xctests-5fee5e1fa7c5">Preventing Memory Leaks Using XCTests</a> was originally published in <a href="https://betterprogramming.pub/">Better Programming</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Boost your productivity with scripts in Swift using Marathon ]]></title>
            <link>https://medium.com/@maciejkrolikowski/boost-your-productivity-with-scripts-in-swift-using-marathon-6f95ab72b9fa?source=rss-a69d412c2e23------2</link>
            <guid isPermaLink="false">https://medium.com/p/6f95ab72b9fa</guid>
            <category><![CDATA[script]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[development]]></category>
            <category><![CDATA[xcode]]></category>
            <dc:creator><![CDATA[Maciej Krolikowski]]></dc:creator>
            <pubDate>Mon, 26 Mar 2018 20:30:44 GMT</pubDate>
            <atom:updated>2018-06-13T07:45:50.639Z</atom:updated>
            <content:encoded><![CDATA[<p>In the process of iOS development there are always situations when you do a repetitive tasks throughout the project. Some of them you may notice at the first glance and some later on. It can be:</p><ul><li>Managing all project assets, sometimes resizing them to fit proper dimensions and at the end adding to Xcode project.</li><li>Creating new screen’s folder structure with all necessary files like ViewControllers, Views, ViewModels, etc.</li></ul><p>Before <a href="https://github.com/fastlane/fastlane">Fastlane</a> and <a href="https://github.com/quicktype/quicktype-xcode">Quicktype</a> there were tasks that you’re used to do manually :</p><ul><li>Deploying app on Testflight/Fabric</li><li>Creating models base on API JSON responses</li></ul><p>The good news is that the iOS community is growing and there is more and more tools, such as <a href="https://github.com/krzysztofzablocki/Sourcery">Sourcery</a> or <a href="https://github.com/SwiftGen/SwiftGen">SwiftGen</a>, which can automate daily development processes. But what if none of them meets your needs? Should you keep going and continue doing everything manually and repetitively? Or maybe you should automate them? Ok, let’s write some script but…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/502/1*EwmJRUKClkKMzLaYJRHDLw.jpeg" /></figure><p>Actually there is no need to use Bash, Python, Ruby or other scripting languages since Swift gives you ability to write scripts. In combination with <a href="https://github.com/JohnSundell/Marathon">Marathon</a> it’s really easy to write and install a scripts on OSX. Marathon generates an Xcode project and allows to use dependencies within script code.</p><p>Lets’ write a program that will generate custom app’s screen folder structure with all needed classes. In this case it’s going to be <em>Login</em> screen folder.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/304/1*EUQlgfVNVin-Bg30yrm-fg.png" /></figure><p>Every file will contain boiler plate code with all necessary libraries already imported.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*m_ou7J9zAdOTI1Njj7tcFA.png" /></figure><p>First, let’s install Marathon. At the time of writing this post, Homebrew has not yet supported latest version of Marathon so I recommend that you to install it by Swift Package Manager. Open the terminal and type:</p><pre>$ git clone https://github.com/JohnSundell/Marathon.git<br>$ cd Marathon<br>$ make</pre><p>Once Marathon is installed successfully the second step is to create <em>FilesGenerator</em> project</p><pre>$ ➜   marathon create FilesGenerator<br>$ 🐣  Created script at FilesGenerator.swift<br>$ ✏️  Opening FilesGenerator.xcodeproj/</pre><p>It should open up an Xcode project containing <strong>main.swift</strong> file</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*k6NdysKxTaW8TtkwjusZ0A.png" /></figure><p>Because we’re going to do operations on files it’s good to add helper dependency called <strong><em>Files</em></strong>. Let’s do it by executing command:</p><pre>$ marathon add https://github.com/JohnSundell/Files.git</pre><p>Close your Xcode project and open it again by typing:</p><pre>$ marathon edit FilesGenerator.swift</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*I3u1kyfezuRrrogJuFBkIQ.png" /></figure><p>You should see new <em>Dependencies </em>catalogue with <em>Files</em> dependancy added. Now we’re ready for the third step-coding. Look at the beyond code which is long but pretty self-explanatory. It takes <strong><em>screenName</em></strong><em> </em>as a parameter of <strong><em>init</em></strong> function and base on it, generates a files with proper names and boilerplate codes.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/05723abb3efc3ce02b3feed32e6b7048/href">https://medium.com/media/05723abb3efc3ce02b3feed32e6b7048/href</a></iframe><p>Once you elaborated a code let’s copy it and paste to <strong>main.swift. </strong>Then go to terminal and type</p><pre>$ marathon run FilesGenerator.swift</pre><p>In current directory you will see a new folder called <strong>Login. </strong>Now all you have to do is drag and drop it to Xcode project.</p><p>So it took couple of minutes and we automated a repetitive process you do every time during developing an app. You can customise a script base on your class implementation and app’s architecture. Feel free to check my repo where I add new files generators <a href="https://github.com/mkkroliks/Swift-Files-Generator">https://github.com/mkkroliks/Swift-Files-Generator</a></p><p><em>Thanks for visiting my post. If you like automation please hit 👏🏼</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6f95ab72b9fa" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>