<?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 S. Wajahat Ali on Medium]]></title>
        <description><![CDATA[Stories by S. Wajahat Ali on Medium]]></description>
        <link>https://medium.com/@swajahatali0?source=rss-25f79feb2786------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*QF2ylzF-GRZ93Ie7.jpg</url>
            <title>Stories by S. Wajahat Ali on Medium</title>
            <link>https://medium.com/@swajahatali0?source=rss-25f79feb2786------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 27 Nov 2026 18:35:40 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@swajahatali0/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[Everything You Need to Know About Protocols With Initializers in Swift]]></title>
            <link>https://medium.com/@swajahatali0/everything-you-need-to-know-about-protocols-with-initializers-in-swift-880cae6f66e9?source=rss-25f79feb2786------2</link>
            <guid isPermaLink="false">https://medium.com/p/880cae6f66e9</guid>
            <category><![CDATA[swift-programming]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[swift-5]]></category>
            <dc:creator><![CDATA[S. Wajahat Ali]]></dc:creator>
            <pubDate>Mon, 17 Nov 2025 19:47:32 GMT</pubDate>
            <atom:updated>2025-11-17T19:47:32.677Z</atom:updated>
            <content:encoded><![CDATA[<p>Recently, while working with protocols, I came across a situation where I had to define an initializer inside a protocol so that every conforming type would be required to implement it.</p><p>Here’s the protocol I created:</p><pre>protocol HomeAssembling {<br>    init(dependency: Dependency)<br>}</pre><p>The moment I tried to implement this protocol in a class (let’s say HomeAssembly), the compiler immediately threw an error:</p><blockquote>“Initializer requirement ‘init(dependency:)’ can only be satisfied by a ‘required’ initializer in non-final class ‘HomeAssembly’.”</blockquote><p>Swift suggests two ways to fix this:</p><h3>1. Add required to the initializer</h3><pre>class HomeAssembly: HomeAssembling {<br>    required init(dependency: Dependency) {<br>        // implementation<br>    }<br>}</pre><p>Why does Swift require this?</p><p>Because if another class ever subclasses HomeAssembly, Swift must ensure that the initializer coming from the protocol is still guaranteed to exist. Marking it required forces all subclasses to either implement or inherit that initializer.</p><h3>What does “inherit that initializer” mean?</h3><p>In Swift, when a <strong>class</strong> implements a protocol initializer, every <strong>subclass</strong> must also fulfil that protocol requirement.</p><p>A subclass can do that in <strong>two</strong> ways:</p><h4>1️⃣ By implementing the initializer itself</h4><pre>protocol HomeAssembling {<br> init(dependency: Dependency)<br>}<br><br>class HomeAssembly: HomeAssembling {<br>    required init(dependency: Dependency) {<br>        print(&quot;HomeAssembly initialized&quot;)<br>    }<br>}</pre><p>This is obvious: the subclass writes its own initializer.</p><h4>2️⃣ By inheriting the initializer from its parent</h4><p>This is the part the sentence refers to.</p><p>If the subclass does <strong>not</strong> implement its own initializer, Swift will try to <em>inherit</em> (i.e., automatically reuse) the parent’s initializer — <strong>but only if that parent initializer is marked </strong><strong>required</strong>.</p><pre>class AdvancedHomeAssembly: HomeAssembly {<br>    // No initializer here!<br>    // This class automatically inherits<br>    // `init(dependency:)` from HomeAssembly<br>}</pre><h3>2. Make the class final</h3><p>If you don’t want anyone to subclass your class, you can mark it as final:</p><pre>final class HomeAssembly: HomeAssembling {<br>    init(dependency: Dependency) {<br>        // implementation<br>    }<br>}</pre><p>Since a final class cannot be subclassed, Swift knows there’s no need for the required keyword.</p><h3>What About Structs?</h3><p>If your conforming type is a struct, you don’t need the required keyword at all:</p><pre>struct HomeAssembly: HomeAssembling {<br>    init(dependency: Dependency) {<br>        // implementation<br>    }<br>}</pre><p>This is because <strong>structs don’t support inheritance</strong>, so there’s no need for Swift to enforce initializer propagation.</p><p>This is the core idea behind protocol initializers in Swift:<br>When a protocol requires an initializer, classes must either mark it required or be declared final, while structs just implement it directly.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=880cae6f66e9" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>