<?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 Adrian Śliwa on Medium]]></title>
        <description><![CDATA[Stories by Adrian Śliwa on Medium]]></description>
        <link>https://medium.com/@adrianmsliwa?source=rss-e68597695f05------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*APXfNcyGwtCaaJxkCIjqRg.jpeg</url>
            <title>Stories by Adrian Śliwa on Medium</title>
            <link>https://medium.com/@adrianmsliwa?source=rss-e68597695f05------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 24 Apr 2026 05:22:50 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@adrianmsliwa/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[Static let pattern]]></title>
            <link>https://medium.com/@adrianmsliwa/static-let-pattern-efcd74958e95?source=rss-e68597695f05------2</link>
            <guid isPermaLink="false">https://medium.com/p/efcd74958e95</guid>
            <category><![CDATA[static]]></category>
            <category><![CDATA[constant]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[etl]]></category>
            <dc:creator><![CDATA[Adrian Śliwa]]></dc:creator>
            <pubDate>Tue, 08 Jan 2019 19:45:54 GMT</pubDate>
            <atom:updated>2019-01-08T19:45:54.532Z</atom:updated>
            <content:encoded><![CDATA[<p>When you would like to create constant in Swift there are many ways to do that. Which one is the best depends on your taste. The most important thing about the constant is the fact the you cannot change its value.</p><p>In this article I would like to present a way I am mostly creating constant and a way I am using them.</p><p>Most of the time I’m creating constant in the extension of a type of given constant. For example when I would like to create UIImage constant I am doing something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/84d731dc2a0ef1e2168b5cd486f5375d/href">https://medium.com/media/84d731dc2a0ef1e2168b5cd486f5375d/href</a></iframe><p>I would like to point two important things here. First one is the fact that this constant will be create at the first use. Documentation says:</p><pre>Stored type properties are lazily initialized on their first access. They are guaranteed to be initialized only once, even when accessed by multiple threads simultaneously, and they do not need to be marked with the lazy modifier</pre><p>The second thing is the fact that this constant will be stored in the memory since first access until the termination of the application. Keep in mind that storing big object this way is not the best idea.</p><p>Now we can use this image in following way:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2e277055bcae458b9053891fa9d91c51/href">https://medium.com/media/2e277055bcae458b9053891fa9d91c51/href</a></iframe><p>One other potential use for this pattern is for localizing strings.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/099856cd2f2265e79d11388d55541516/href">https://medium.com/media/099856cd2f2265e79d11388d55541516/href</a></iframe><p>In similar way we can set button’s title:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e3f9d71b55a92f6cf582e075073a174d/href">https://medium.com/media/e3f9d71b55a92f6cf582e075073a174d/href</a></iframe><p>I am using this pattern mostly for Strings, UIColors, and really small images from application’s Assets. I really like the way I can use them.</p><p>Global variables are also lazy so the only significant advantage of using static let pattern is nice organization of this constants in the type extension.</p><p>My github: <a href="https://github.com/adiki/">https://github.com/adiki/</a></p><p>Previous article: <a href="https://medium.com/p/a20269eb6fcc?source=your_stories_page---------------------------">A way to handle NSNotification’s observation token</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=efcd74958e95" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A way to handle NSNotification’s observation token]]></title>
            <link>https://medium.com/@adrianmsliwa/a-way-to-handle-nsnotifications-observation-token-a20269eb6fcc?source=rss-e68597695f05------2</link>
            <guid isPermaLink="false">https://medium.com/p/a20269eb6fcc</guid>
            <category><![CDATA[notifications]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[disposable]]></category>
            <category><![CDATA[rxswift]]></category>
            <dc:creator><![CDATA[Adrian Śliwa]]></dc:creator>
            <pubDate>Fri, 07 Dec 2018 21:28:35 GMT</pubDate>
            <atom:updated>2019-01-08T19:46:30.154Z</atom:updated>
            <content:encoded><![CDATA[<p>If you are working with closure based NSNotification’s implementation, you have to deal with returned `tokens`. The simplest way to store reference to them is to keep them in a property:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0f944e57fcdcd3ce3e52f812eab00ec1/href">https://medium.com/media/0f944e57fcdcd3ce3e52f812eab00ec1/href</a></iframe><p>Situation complicates when we have to store more than one token. <br>We can solve it by keeping references in the array:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5a29e2dcd3567f2d5803ce9f640bdf6a/href">https://medium.com/media/5a29e2dcd3567f2d5803ce9f640bdf6a/href</a></iframe><p>If we wouldn’t like to create temporary constants willEnterForegroundToken didBecomeActiveToken, there are at least two ways to solve it.</p><p>Let’s start with more obvious one:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/46f79e3cebbcf77e2518c7e40c62899b/href">https://medium.com/media/46f79e3cebbcf77e2518c7e40c62899b/href</a></iframe><p>I leave it to you to judge this implementation. <br>I would like to share with you another way to handle it. First step is to introduce ReferencesBag:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/257655635caa1cf9ef53d644f30c6d2a/href">https://medium.com/media/257655635caa1cf9ef53d644f30c6d2a/href</a></iframe><p>It’s a class inspired by Disposables concept which I got know when I was working with RxSwift.</p><p>Next step is to extend NSObjectProtocol protocol:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/49ea1b32e59221413d2bd66958adf42e/href">https://medium.com/media/49ea1b32e59221413d2bd66958adf42e/href</a></iframe><p>Having those two pieces we can write something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6e2b826e1a78467fe82d29f8cb614841/href">https://medium.com/media/6e2b826e1a78467fe82d29f8cb614841/href</a></iframe><p>Which one is the best? It depends on your taste. I just wanted to present you an alternative approach.</p><p>My github: <a href="https://github.com/adiki/">https://github.com/adiki/</a></p><p>Next article: <a href="https://medium.com/p/efcd74958e95?source=your_stories_page---------------------------">Static let pattern</a><br>Previous article: <a href="/p/fe5f4e83c386?source=your_stories_page---------------------------">Wrapper design pattern by example using SFAuthenticationSession and ASWebAuthenticationSession</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a20269eb6fcc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Wrapper design pattern by example using SFAuthenticationSession and ASWebAuthenticationSession]]></title>
            <link>https://medium.com/@adrianmsliwa/wrapper-design-pattern-by-example-using-sfauthenticationsession-and-aswebauthenticationsession-fe5f4e83c386?source=rss-e68597695f05------2</link>
            <guid isPermaLink="false">https://medium.com/p/fe5f4e83c386</guid>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[design-patterns]]></category>
            <category><![CDATA[adapter]]></category>
            <category><![CDATA[wrapper]]></category>
            <dc:creator><![CDATA[Adrian Śliwa]]></dc:creator>
            <pubDate>Fri, 30 Nov 2018 17:02:21 GMT</pubDate>
            <atom:updated>2018-12-31T11:37:59.632Z</atom:updated>
            <content:encoded><![CDATA[<p>Long, long time ago (a couple of years ;)) I was reading a book called <a href="https://en.wikipedia.org/wiki/Design_Patterns"><em>Design Patterns: Elements of Reusable Object-Oriented Software</em></a><strong><em>. </em></strong>I was a young software developer then and I really liked that book. But there was a problem. I wasn’t able to use its potential because I didn’t have enough experience to know how to use those pattern correctly.</p><p>Recently I heard a lot of comments that some people after reading that book start using most of those pattern in any place they think they could use it. This is of course wrong. When you are young developer it’s really nice to know that that book exists and after some time of developing software you realising that you are using those pattern naturally and quite often you are giving them names different then you can find in any book of design patterns.</p><p>Today I would like to talk about one of design patterns called Wrapper. Wikipedia says something like this about it: “Wrapper pattern is a software design pattern that allows the interface of an existing class to be used as another interface. It is often used to make existing classes work with others without modifying their source code.”</p><p>The problem we are facing working with SFAuthenticationSession and ASWebAuthenticationSession is as follows:</p><p>SFAuthenticationSession was introduces in iOS 11 and deprecated in iOS 12.<br>ASWebAuthenticationSession was introduced in iOS 12.</p><p>If you are lucky as an iOS application developers and you have to support at most one previous version of iOS there is some nice way how to hide usage of those classes under one simple class.</p><p>The really nice property of those two implementations is the fact that they have the same methods with the same signatures. We can use that fact and create a protocol with them:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/93ecbf63bf2be7fb90a0748a492a2854/href">https://medium.com/media/93ecbf63bf2be7fb90a0748a492a2854/href</a></iframe><p>Having that protocol we can extend SFAuthenticationSession and ASWebAuthenticationSession and add conformance to AuthenticationSessionProtocol and because the fact that those classes have those methods implemented already we don’t have to do anything else.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8d9aa788bd009b5208c86b9a7c9d41d2/href">https://medium.com/media/8d9aa788bd009b5208c86b9a7c9d41d2/href</a></iframe><p>Now we cen create our wrapper class which will also conform to AuthenticationSessionProtocol:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/38989d4bb9d3b6f299aa369414c187e2/href">https://medium.com/media/38989d4bb9d3b6f299aa369414c187e2/href</a></iframe><p>To be honest that’s it. We have simple wrapper which nicely hides usage of SFAuthenticationSession and ASWebAuthenticationSession and nicely adapts to the system version which is installed on a device which is running our application.</p><p>My github: <a href="https://github.com/adiki/">https://github.com/adiki/</a></p><p>Next article: <a href="/p/a20269eb6fcc?source=your_stories_page---------------------------">A way to handle NSNotification’s observation token</a><br>Previous article: <a href="/p/f88685805079?source=your_stories_page---------------------------">How to pollute your code with external dependency (and how to not do that)?</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fe5f4e83c386" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to pollute your code with external dependency (and how to not do that)?]]></title>
            <link>https://medium.com/@adrianmsliwa/how-to-pollute-your-code-with-external-dependency-and-how-to-not-do-that-f88685805079?source=rss-e68597695f05------2</link>
            <guid isPermaLink="false">https://medium.com/p/f88685805079</guid>
            <category><![CDATA[dependency-inversion]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[dependencies]]></category>
            <category><![CDATA[type-erasure]]></category>
            <category><![CDATA[core-data]]></category>
            <dc:creator><![CDATA[Adrian Śliwa]]></dc:creator>
            <pubDate>Sun, 18 Nov 2018 07:56:41 GMT</pubDate>
            <atom:updated>2018-12-31T11:38:15.724Z</atom:updated>
            <content:encoded><![CDATA[<h3>How to pollute your code with external dependency (and how to not do that).</h3><p>At the beginning I would like to mention that this topic is the most important one from my list of things I would like to share and write about. This concept is not exclusive to iOS developers and I think all of us who write more code than just “hello world” should be familiar with it.</p><p>Let’s dive in.</p><p>How to pollute your code with external dependency? There is a simple answer for that: just use it everywhere! Answering that we can do something more interesting.</p><p>Firstly I would like to present short theoretical concept I will use in this post. When I’m developing any application I don’t want to have a situation in my code that my application logic depends on external dependency. What I want to have in my codebase is the state where external frameworks (and my extensions of them) are implementing protocols provided by the application. To achieve this I have to use <a href="https://en.wikipedia.org/wiki/Dependency_inversion_principle">Dependency inversion principle</a> (one of the five SOLID rules).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/716/1*fbNXFBR349mN0x49gnpW_g.png" /><figcaption><a href="https://commons.wikimedia.org/wiki/File:Dependency_inversion.png">https://commons.wikimedia.org/wiki/File:Dependency_inversion.png</a></figcaption></figure><p>Above diagram exactly presents what I want to achieve. I want to have a state where all arrows are pointing towards the application component (any external component depends on the application). This is also one of the core concept of all app architectures with layers (these layers point towards to the center: <a href="http://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html">example</a>).</p><p>To explain it deeply I want to present the code which is implementing this concept. I want to show you implementation of the objects provider which is responsible for delivering collection of objects (e.g. for feed purposes). This implementation will use CoreData (external dependency) which will be responsible for accessing objects from the storage and delivering them to the app. The usage of CoreData will be implemented in a way where application itself won’t be even aware that it’s using CoreData for this purpose.</p><p>Let’s start with protocol which will be used by the app:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/846929c957f061d2b03e22f5303f00b6/href">https://medium.com/media/846929c957f061d2b03e22f5303f00b6/href</a></iframe><p>There are two responsibilities for the objects implementing ObjectsProvider protocol the first one is to return number of objects in the collection and the second one is to return the object at given index.</p><p>When we have this protocol we can implement CoreData component which will implement this protocol, but before that I would like to introduce new protocol called Convertible which will help us to prevent returning NSFetchRequestResult instances to the rest of the app (it’s wrong because it’s CoreData type).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3544bf26dba7314291ce490f2f58fc80/href">https://medium.com/media/3544bf26dba7314291ce490f2f58fc80/href</a></iframe><p>Example implementation of this protocol is presented below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0bb807fa0b2d5fa8f2225a526fd49bb3/href">https://medium.com/media/0bb807fa0b2d5fa8f2225a526fd49bb3/href</a></iframe><p>Returning to our CoreData implementation of ObjectsProvider protocol. The implementation of that is really simple:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e374ea1c54184d5341d7a69ace9dfbb3/href">https://medium.com/media/e374ea1c54184d5341d7a69ace9dfbb3/href</a></iframe><p>It looks really nice. Let’s try to use it in our code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f1922b2674694f6c12cba6d3954ff52b/href">https://medium.com/media/f1922b2674694f6c12cba6d3954ff52b/href</a></iframe><p>Ups, it doesn’t compile. Let’s see at the error: protocol &#39;ObjectsProvider&#39; can only be used as a generic constraint because it has Self ot associated type requirements. Unfortunately it’s won’t be easily fixed by adding &lt;GIF&gt; after ObjectsProvider. To fix it we have to implement it in this way:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5e64e0bf32d5189c62aa64442d10341a/href">https://medium.com/media/5e64e0bf32d5189c62aa64442d10341a/href</a></iframe><p>It work’s but I don’t think it’s elegant solution. Let’s see what will happen when we will try to use this GIFsDataSource in some ViewController:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1930af9f32f9c7efdced8beb20651781/href">https://medium.com/media/1930af9f32f9c7efdced8beb20651781/href</a></iframe><p>This is nightmare! Using GIFsViewController will only produce more of this syntactical noise! There has to be a way to fix it somehow!</p><p>Fortunately there is a technique which can help. It’s called Type Erasure. You can find more details about it <a href="https://medium.com/swiftworld/swift-world-type-erasure-5b720bc0318a">here</a>. I will show only final result of this trick:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ddde85aadb64d267e9104b9b3bf31fc8/href">https://medium.com/media/ddde85aadb64d267e9104b9b3bf31fc8/href</a></iframe><p>Let’s look how can we define GIFsDataSource now:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/12bd564ca1448ff021887b3015755577/href">https://medium.com/media/12bd564ca1448ff021887b3015755577/href</a></iframe><p>and the usage:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fa28bc44293e79228b5e9e2f004f533c/href">https://medium.com/media/fa28bc44293e79228b5e9e2f004f533c/href</a></iframe><p>I think it’s wonderful implementation. When I’m using gifsProvider in my code I’m only interested of accessing objects from some collection. If I don’t know actual implementation of ObjectsProvider I cannot tell if objects come from CoreData, Realm or even RAM memory. Because that fact you can easily change implementation of the provider without affecting your codebase significantly.</p><p>You can find real usage of this concept in one of my public projects on my github. It’s available here: <a href="https://github.com/adiki/giftainer">https://github.com/adiki/giftainer</a></p><p>Next article: <a href="/p/fe5f4e83c386?source=your_stories_page---------------------------">Wrapper design pattern by example using SFAuthenticationSession and ASWebAuthenticationSession</a><br>Previous article: <a href="https://medium.com/p/bccb34ad6f2c?source=your_stories_page---------------------------">Simple trick to improve handling cells in UITableView and UICollectionView</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f88685805079" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Simple trick to improve handling cells in UITableView and UICollectionView]]></title>
            <link>https://medium.com/@adrianmsliwa/simple-trick-to-improve-handling-cells-in-uitableview-and-uicollectionview-bccb34ad6f2c?source=rss-e68597695f05------2</link>
            <guid isPermaLink="false">https://medium.com/p/bccb34ad6f2c</guid>
            <category><![CDATA[uicollectionview]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[uitableview]]></category>
            <category><![CDATA[dry]]></category>
            <category><![CDATA[dont-repeat-yourself]]></category>
            <dc:creator><![CDATA[Adrian Śliwa]]></dc:creator>
            <pubDate>Sat, 10 Nov 2018 12:23:26 GMT</pubDate>
            <atom:updated>2018-12-31T11:38:29.832Z</atom:updated>
            <content:encoded><![CDATA[<p>Today’s post should be really simple and short. It’s my second post ever and I truly believe that posts should be short and consistent to encourage the reader to dive into the content.<br>Let’s start!</p><p>In most of projects I’m working on, I’m not using storyboards. I’m familiar with them, I know how to use them, I see and understand their strong and weak parts. Knowing these facts I still prefer to write my projects using code only.</p><p>When we are working with UICollectionView one of the first things we have to do is to register our subclass of UICollectionViewCell:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a26b7598a40002f823dcd8320cc55999/href">https://medium.com/media/a26b7598a40002f823dcd8320cc55999/href</a></iframe><p>Of course it’s not the best practise to use string literal here and in the place of dequeuing cells. We can improve it by creating static identifier constant and then reuse it in those two places in the code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8e2aef013e5aa600d34cafb746a3facc/href">https://medium.com/media/8e2aef013e5aa600d34cafb746a3facc/href</a></iframe><p>I hope all of you will agree with me that this solution is much much better then the previous one when we were using string literal. The question is if we could do it even better? Let’s try!</p><p>We can take advantage of the fact that AnyClass type have description implemented by default. Let’s look at implementation which is using that fact:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/81e6e2c56d3c633a5646ed14f6b1027f/href">https://medium.com/media/81e6e2c56d3c633a5646ed14f6b1027f/href</a></iframe><p>We can take another step and extend UICollectionView implementation to make it more convenient.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4c4c3a3f283816b3c62c6d886d070530/href">https://medium.com/media/4c4c3a3f283816b3c62c6d886d070530/href</a></iframe><p>Now registering is really easy:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c18f9af662d135cbd90a20947ff22318/href">https://medium.com/media/c18f9af662d135cbd90a20947ff22318/href</a></iframe><p>I think it’s really nice extension I hope you like it too.</p><p>Summarising the current progress, we implemented really consistent method to register cells in UICollectionView. In the second part of this article I would like improve dequeuing mechanism.</p><p>Traditionally I was using something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a3c4beedb4da29ea0511c703fefaf04a/href">https://medium.com/media/a3c4beedb4da29ea0511c703fefaf04a/href</a></iframe><p>It’s not bad implementation but the problem with it, is the fact that we will be reusing this pattern all over in different parts of the app. Let’s try to wrap this pattern in UICollectionView extension.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c71cbe0fbe111e8d7d5eb6dc584ac4f8/href">https://medium.com/media/c71cbe0fbe111e8d7d5eb6dc584ac4f8/href</a></iframe><p>Now dequeuing looks really nice too:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/96120a050dc9a11c0be523428c0345fd/href">https://medium.com/media/96120a050dc9a11c0be523428c0345fd/href</a></iframe><p>The really important point to notice here is the fact that someCell constant is of SomeCell type.</p><p>That’s the final implementation of this improvement. I think it’s wonderful and one of the most important parts of it for me, is the fact that we don’t duplicate registering and dequeuing pattern all over the place. It’s simple rule: Don’t Repeat Yourself. Swift language is really helpful here to achieve this kind of effect. I hope that after reading this article you will be able to implement similar solution for UITableView.</p><p>My github: <a href="https://github.com/adiki/">https://github.com/adiki/</a></p><p>Next week: <a href="https://medium.com/p/f88685805079?source=your_stories_page---------------------------">How to pollute your code with external dependency (and how to not do that)?</a><br>Previous week: <a href="/p/171102ef9ada?source=your_stories_page---------------------------">Extensible extension of a Micro Auto Layout DSL</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bccb34ad6f2c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Extensible extension of a Micro Auto Layout DSL]]></title>
            <link>https://medium.com/@adrianmsliwa/extensible-extension-of-a-micro-auto-layout-dsl-171102ef9ada?source=rss-e68597695f05------2</link>
            <guid isPermaLink="false">https://medium.com/p/171102ef9ada</guid>
            <category><![CDATA[autolayout]]></category>
            <category><![CDATA[dsl]]></category>
            <category><![CDATA[ios]]></category>
            <dc:creator><![CDATA[Adrian Śliwa]]></dc:creator>
            <pubDate>Sat, 03 Nov 2018 17:20:58 GMT</pubDate>
            <atom:updated>2018-12-31T11:38:41.560Z</atom:updated>
            <content:encoded><![CDATA[<p>This is my first blog post ever, that’s why I would like to quickly introduce myself. My name is Adrian Śliwa and I’m an iOS applications developer. During my several years of iOS experience I always wanted to be up to date with iOS concepts. Recently I discovered an incentive inside myself to start sharing my ideas and thoughts of iOS applications development. That’s why I’m here writing my first blog post for you. I hope you will enjoy it. <br>So let’s start.</p><p>On October 27, 2017 Chris Eidhof released his post about Micro Auto Layout DSL: <a href="http://chris.eidhof.nl/post/micro-autolayout-dsl/">http://chris.eidhof.nl/post/micro-autolayout-dsl/</a>. I was really inspired by his work and I started using it in my projects because his implementation was very simple and I could manage the code by myself. His concept was based on introducing special typealias:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/64de2d18571a6cf1e101e86e4751893e/href">https://medium.com/media/64de2d18571a6cf1e101e86e4751893e/href</a></iframe><p>In his blog post he is explaining how using this simple concept and using KeyPath‘s we can achieve something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0b92bd73460aa24665aafad0c1252891/href">https://medium.com/media/0b92bd73460aa24665aafad0c1252891/href</a></iframe><p>Next part of the post assumes you are at least briefly familiar with Chris’es concept and his implementation. If you would like to get to know the details of it please visit his website here: <a href="http://chris.eidhof.nl/post/micro-autolayout-dsl/">http://chris.eidhof.nl/post/micro-autolayout-dsl/</a>.</p><p>There is a limitation with the implementation of above DSL: each equal method is returning only one Constraint:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3dae629542dd6c2e7926ba7f15d8a4bf/href">https://medium.com/media/3dae629542dd6c2e7926ba7f15d8a4bf/href</a></iframe><p>Second limit is related with overloaded addSubview implementation:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d709cf09ec2ce2e49d41d3d874c85847/href">https://medium.com/media/d709cf09ec2ce2e49d41d3d874c85847/href</a></iframe><p>This method is accepting [Constraint]. To explain why it is a limitation I would like to share with you what I would like to achieve with this kind of DSL. I would like to express only with one helper method that I would like to pin all subview’s edges to all edges of its superview:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c9f87b34ebeba0581bc12a7d1dff6de6/href">https://medium.com/media/c9f87b34ebeba0581bc12a7d1dff6de6/href</a></iframe><p>To enable this concept pinAllEdges function has to return list of Constraints instead of returning only one. Let’s look at pinAllEdges implementation:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/94b1d4aa6a6795d3aedea6fe0c4eab9b/href">https://medium.com/media/94b1d4aa6a6795d3aedea6fe0c4eab9b/href</a></iframe><p>As you can notice I’m adding results of equal functions. To make it possible I was not overloading + operator. To make that trick possible I used the fact that + operator is implemented for arrays. Let’s look at my equal function signature:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8d5041b135944d5f7bc01674901252d7/href">https://medium.com/media/8d5041b135944d5f7bc01674901252d7/href</a></iframe><p>This equal function is returning [Constraint]. Because of that fact I can mix results of my helper functions with results of equal function:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bb0ac7e7d56f41326234271700c5ac7f/href">https://medium.com/media/bb0ac7e7d56f41326234271700c5ac7f/href</a></iframe><p>To make it all working overloaded addSubview method has to be modified a little:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/37775769e4c53cf4a014dc682657dfac/href">https://medium.com/media/37775769e4c53cf4a014dc682657dfac/href</a></iframe><p>Now addSubview accepts [[Constraint]] instead of [Constraint]. It’s creating actual NSLayoutConstraints and flattening list of lists to just a list of NSLayoutConstraint and at the end it’s activating all of constraints.</p><p>This powerful trick enables you to introduce your own helper layout methods. It’s very flexible and because of that fact I called it extensible extension. Let’s say you would like to have a helper to pin subview to the center of its parent. It’s very easy to add using extended version of Micro Auto Layout DSL:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3900c0d1c49a0cfb8aae089cbc65cbcb/href">https://medium.com/media/3900c0d1c49a0cfb8aae089cbc65cbcb/href</a></iframe><p>It’s wonderful, isn’t it?</p><p>The whole implementation of my improvement can be found here: <a href="https://gist.github.com/adiki/6cc4e3b16b3f933f7181c76bc6a52a8d">https://gist.github.com/adiki/6cc4e3b16b3f933f7181c76bc6a52a8d</a></p><p>That’s it! I hope you enjoyed it ;)</p><p>My github: <a href="https://github.com/adiki/">https://github.com/adiki/</a></p><p>Next week: <a href="/p/bccb34ad6f2c?source=your_stories_page---------------------------">Simple trick to improve handling cells in UITableView and UICollectionView</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=171102ef9ada" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>