<?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 Amby Kros on Medium]]></title>
        <description><![CDATA[Stories by Amby Kros on Medium]]></description>
        <link>https://medium.com/@ambykros?source=rss-a562465482ad------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*gY-NQZf_CXLpOrjEk8JTqA.jpeg</url>
            <title>Stories by Amby Kros on Medium</title>
            <link>https://medium.com/@ambykros?source=rss-a562465482ad------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 00:06:16 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ambykros/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[Swap of two numbers]]></title>
            <link>https://medium.com/@ambykros/swap-of-two-numbers-c89db5b7117a?source=rss-a562465482ad------2</link>
            <guid isPermaLink="false">https://medium.com/p/c89db5b7117a</guid>
            <category><![CDATA[xor]]></category>
            <category><![CDATA[swap]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[mathematics]]></category>
            <category><![CDATA[bit-magic]]></category>
            <dc:creator><![CDATA[Amby Kros]]></dc:creator>
            <pubDate>Sat, 06 Apr 2019 06:43:41 GMT</pubDate>
            <atom:updated>2019-04-06T06:43:41.957Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*O1HXJKtyA1DDcvaTzDdQ5A.jpeg" /></figure><p>The most obvious way to achieve this is to use an extra variable.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/88596ca586068ae2ab67cd425882631d/href">https://medium.com/media/88596ca586068ae2ab67cd425882631d/href</a></iframe><p>To make it even more obvious we can use two extra variables.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/75e6f815e3f07236215710121a054ec1/href">https://medium.com/media/75e6f815e3f07236215710121a054ec1/href</a></iframe><p>But using additional variables requires additional memory. Fortunately, there is such a strong feature in the swift as a tuple that can do it on the fly.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7f2afa975260b38719856463572281e9/href">https://medium.com/media/7f2afa975260b38719856463572281e9/href</a></iframe><p>And also, we can solve the problem using simple mathematical operations, such as addition and subtraction, multiplication and division.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cc3edc2c7f0595eed6fb023575583c4b/href">https://medium.com/media/cc3edc2c7f0595eed6fb023575583c4b/href</a></iframe><p>And of course the solution using XOR. The XOR of two numbers <em>a</em> and <em>b</em> returns a number which has all the bits as 1 wherever bits of <em>a</em> and <em>b</em> differ.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e62c3253929b27a581f0b24a0ca3a410/href">https://medium.com/media/e62c3253929b27a581f0b24a0ca3a410/href</a></iframe><p>But what are the problems with Arithmetic and XOR solutions?</p><ol><li>If the numbers are too large, both arithmetic solutions can cause overflow.</li><li>If one of the numbers is 0, the multiplication and division approach (<em>swapUsingMultiplicationAndDevision </em>function) won’t work.</li><li>If to pass the same variable as parameters.</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/958931b65edec3b6299058f3beca2202/href">https://medium.com/media/958931b65edec3b6299058f3beca2202/href</a></iframe><p>Fortunately, we have two fuses — this is sound logic (because swapping the same variable makes no sense) and error.</p><p><strong>error: inout arguments are not allowed to alias each other</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c89db5b7117a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Swift Error handling]]></title>
            <link>https://medium.com/@ambykros/swift-error-handling-26616fec9ffa?source=rss-a562465482ad------2</link>
            <guid isPermaLink="false">https://medium.com/p/26616fec9ffa</guid>
            <category><![CDATA[do-catch]]></category>
            <category><![CDATA[try]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[error-handling]]></category>
            <category><![CDATA[throw]]></category>
            <dc:creator><![CDATA[Amby Kros]]></dc:creator>
            <pubDate>Sun, 22 Jul 2018 20:32:31 GMT</pubDate>
            <atom:updated>2018-07-22T20:32:31.128Z</atom:updated>
            <content:encoded><![CDATA[<h4>Try Try? Try! throws rethrows throw do catch</h4><h4>Description</h4><p>If there is a possibility that the property can be nil, we must use an optional.</p><p>If there is a possibility that function can fail, we can use throws.</p><p>It’s like if-else, only on steroids.</p><h4>Functions and initializers that throw</h4><p>You write the throws keyword in the function’s declaration after its parameters.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/556fbc2a73b3ccd4181700ebdc9e1efe/href">https://medium.com/media/556fbc2a73b3ccd4181700ebdc9e1efe/href</a></iframe><h4>Enum error</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/361baa7c1f201df2fbe6e0eaa8b2bc03/href">https://medium.com/media/361baa7c1f201df2fbe6e0eaa8b2bc03/href</a></iframe><h4>Struct error</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/38ded60b7914dbc2732bc7ff0c172280/href">https://medium.com/media/38ded60b7914dbc2732bc7ff0c172280/href</a></iframe><h4>Difference between try , try! , and try?</h4><p>try is used with a do catch statement and allows for a more detailed approach to error handling.</p><p>try? is used with if let or guard let. Ignores errors.</p><p>try! force unwraps code. App will crash if there is an error.</p><h4>Multiple throw</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c7aae39245ff1b4294e6e1d4ebc4e582/href">https://medium.com/media/c7aae39245ff1b4294e6e1d4ebc4e582/href</a></iframe><h4>Throws and rethrows</h4><blockquote>A function can be declared with the rethrows keyword to indicate that it throws an error only if one of its function parameters throws an error.</blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/780e1fdc55692c52af41712686ff35ed/href">https://medium.com/media/780e1fdc55692c52af41712686ff35ed/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=26616fec9ffa" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Swap of two integers without a third variable]]></title>
            <link>https://medium.com/@ambykros/swap-of-two-integers-without-a-third-variable-3b848f4654dd?source=rss-a562465482ad------2</link>
            <guid isPermaLink="false">https://medium.com/p/3b848f4654dd</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[swap]]></category>
            <dc:creator><![CDATA[Amby Kros]]></dc:creator>
            <pubDate>Sun, 22 Jul 2018 20:29:36 GMT</pubDate>
            <atom:updated>2018-07-22T20:29:36.991Z</atom:updated>
            <content:encoded><![CDATA[<iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e62c3253929b27a581f0b24a0ca3a410/href">https://medium.com/media/e62c3253929b27a581f0b24a0ca3a410/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3b848f4654dd" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Simple factory / Factory method / Abstract factory]]></title>
            <link>https://medium.com/@ambykros/simple-factory-factory-method-abstract-factory-b496a6cca7c4?source=rss-a562465482ad------2</link>
            <guid isPermaLink="false">https://medium.com/p/b496a6cca7c4</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[abstract-factory]]></category>
            <category><![CDATA[creational-patterns]]></category>
            <category><![CDATA[simple-factory]]></category>
            <category><![CDATA[factory-method]]></category>
            <dc:creator><![CDATA[Amby Kros]]></dc:creator>
            <pubDate>Sun, 22 Jul 2018 20:28:19 GMT</pubDate>
            <atom:updated>2018-07-22T20:28:19.967Z</atom:updated>
            <content:encoded><![CDATA[<h3>Without even simple factory</h3><p><strong>Conditions:</strong></p><p>A custom button that we use throughout the application.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fd68fdb1ccdfe08733ae2e5eeffbd10e/href">https://medium.com/media/fd68fdb1ccdfe08733ae2e5eeffbd10e/href</a></iframe><p><strong>Problem:</strong></p><p>If new requirements occurs, we have to make changes at many places.<br>It’s difficult to maintain the code. Creation must be normalized to ensure a consistent behavior within the application.</p><h3><strong>Simple factory</strong></h3><p>Creates a specific object with different implementations.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3ca1f089ee053a9e89a046e09a75c747/href">https://medium.com/media/3ca1f089ee053a9e89a046e09a75c747/href</a></iframe><p><strong>Problem:</strong></p><p>Violation of the Open Close Principle of SOLID principles in case of addition new button types. (We have to create a new class, add a new switch case and to make changes to the factory for a new type.)</p><h3>Factory method</h3><p>Provides an interface for creating objects, but the type of objects will be defined in the subclasses.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/94f548ef88aedf335a7442039ccc7e60/href">https://medium.com/media/94f548ef88aedf335a7442039ccc7e60/href</a></iframe><p>When new requirements come, we don’t make changes in existing code, we just create an additional Factory.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/86a446b14e64823afb50cd56ba05b6a7/href">https://medium.com/media/86a446b14e64823afb50cd56ba05b6a7/href</a></iframe><p><strong>Problem:</strong></p><p>But it can create only one kind of objects. If we need to create a family of related or dependent objects — abstract factory is a better solution.</p><h3>Abstract factory</h3><p>Provides an interface for creating families of related or dependent objects without specifying ta</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b0fc2764a350e31aded38d057718258b/href">https://medium.com/media/b0fc2764a350e31aded38d057718258b/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b496a6cca7c4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Association vs Aggregation vs Composition (vs Inheritance)]]></title>
            <link>https://medium.com/@ambykros/association-vs-aggregation-vs-composition-vs-inheritance-af483dd97b49?source=rss-a562465482ad------2</link>
            <guid isPermaLink="false">https://medium.com/p/af483dd97b49</guid>
            <category><![CDATA[aggregation]]></category>
            <category><![CDATA[association]]></category>
            <category><![CDATA[inheritance]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[composition]]></category>
            <dc:creator><![CDATA[Amby Kros]]></dc:creator>
            <pubDate>Sun, 22 Jul 2018 20:27:01 GMT</pubDate>
            <atom:updated>2018-07-22T20:27:01.533Z</atom:updated>
            <content:encoded><![CDATA[<h4>It’s all about relationships.</h4><p><strong>Inheritance</strong> — «is-a»/«is-a-kind-of» relationship</p><p><strong>Association</strong></p><ul><li><strong>Aggregation</strong> — «has-a»/uses relationship</li><li><strong>Composition</strong> — «part-of»/owns relationship</li></ul><h3><strong>Aggregation </strong>(weak association)</h3><p><strong>Relationship — </strong>one owner instance.</p><p><strong>Lifetime — </strong>no lifetime dependency.</p><p><em>If the library instance is destroyed, the books still exist. And vice versa.</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/133364da5f8796371909321064969af9/href">https://medium.com/media/133364da5f8796371909321064969af9/href</a></iframe><h3><strong>Composition</strong> (strong association or death relationship)</h3><p><strong>Relationship</strong> — one owner instance.</p><p><strong>Lifetime </strong>— lifetime of child instance depends on lifetime of owner instance.</p><p><em>If the e-reader instance is destroyed, the books will also be destroyed.</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f9fc566b750d4b29885588fed08f22f1/href">https://medium.com/media/f9fc566b750d4b29885588fed08f22f1/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=af483dd97b49" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>