<?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 Rohit Kumar on Medium]]></title>
        <description><![CDATA[Stories by Rohit Kumar on Medium]]></description>
        <link>https://medium.com/@rohitku.prajapati?source=rss-11ecee428083------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*BgZUXquKoxAI0oCN8PQdKQ.jpeg</url>
            <title>Stories by Rohit Kumar on Medium</title>
            <link>https://medium.com/@rohitku.prajapati?source=rss-11ecee428083------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 02:39:49 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@rohitku.prajapati/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[Understanding Reference Types and Value Types in Swift: A Comprehensive Guide with Examples]]></title>
            <link>https://medium.com/@rohitku.prajapati/understanding-reference-types-and-value-types-in-swift-a-comprehensive-guide-with-examples-6e9b4abb8482?source=rss-11ecee428083------2</link>
            <guid isPermaLink="false">https://medium.com/p/6e9b4abb8482</guid>
            <category><![CDATA[interview-questions]]></category>
            <category><![CDATA[structs-vs-classes]]></category>
            <category><![CDATA[interview]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[interview-tips]]></category>
            <dc:creator><![CDATA[Rohit Kumar]]></dc:creator>
            <pubDate>Wed, 22 Jan 2025 04:22:38 GMT</pubDate>
            <atom:updated>2025-01-22T04:23:52.088Z</atom:updated>
            <content:encoded><![CDATA[<p>When preparing for a Swift interview, one of the most commonly asked topics is the difference between <strong>reference types</strong> and <strong>value types</strong>. Interviewers may ask you to explain the concept, provide examples, or dive deeper into related topics like mutability, memory management, and performance. Today we will cover all these aspects with simple examples to ensure you’re well-prepared.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*u0I3sr0PNK0ccAeI" /><figcaption>Photo by <a href="https://unsplash.com/@sigmund?utm_source=medium&amp;utm_medium=referral">Sigmund</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p><strong>What Are Value Types and Reference Types ?</strong></p><ul><li><strong>Value Types</strong>: When a value type is assigned to a variable, copied, or passed to a function, a new copy of the data is created. Changes to one instance do not affect the other.</li><li><strong>Reference Types</strong>: Reference types share a single instance. When assigned or passed, they share the same memory, meaning changes to one instance affect all references.</li></ul><p><strong>Examples of Value and Reference Types</strong></p><p>1. Value Type Example: Struct</p><pre>struct Point {<br>    var x: Int<br>    var y: Int<br>}<br><br>var point1 = Point(x: 1, y: 2)<br>var point2 = point1 // Copies the data<br>point2.x = 5<br><br>print(point1.x) // Output: 1 (unchanged)<br>print(point2.x) // Output: 5 (changed)</pre><p>Here, point1 and point2 are separate instances. Changing point2 does not affect point1.</p><p>2. Reference Type Example: Class</p><pre>class Point {<br>    var x: Int<br>    var y: Int<br><br>    init(x: Int, y: Int) {<br>        self.x = x<br>        self.y = y<br>    }<br>}<br><br>let point1 = Point(x: 1, y: 2)<br>let point2 = point1 // Shares the same memory<br>point2.x = 5<br><br>print(point1.x) // Output: 5 (affected by point2)<br>print(point2.x) // Output: 5</pre><p>Here, point1 and point2 reference the same object. Changing one affects the other.</p><p><strong>Key Differences Between Value and Reference Types</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VyPu4O36na5OZnhijPYXnQ.png" /></figure><h4><strong>Interview Questions and Answers</strong></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*5zePn9h7eBU83kfy" /><figcaption>Photo by <a href="https://unsplash.com/@walls_io?utm_source=medium&amp;utm_medium=referral">Walls.io</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><ol><li><strong>When should you use structs over classes?</strong></li></ol><p><strong>Use structs when:</strong></p><ul><li>The data is small and simple.</li><li>You want immutability by default.</li><li>You need performance due to frequent copying.</li></ul><p><strong>Use classes when:</strong></p><ul><li>You need shared state or reference counting.</li><li>The object has complex behaviour and relationships.</li></ul><p><strong>2. How does Swift handle mutability for structs and classes?</strong></p><ul><li><strong>Structs</strong>: Mutability is controlled by the var keyword. If the struct is declared as let, you cannot modify its properties.</li><li><strong>Classes</strong>: Even if declared as let, you can modify its properties (if they are mutable).</li></ul><p><strong>Example:</strong></p><pre>struct Point {<br>    var x: Int<br>}<br>var mutablePoint = Point(x: 1)<br>mutablePoint.x = 2 // Allowed<br><br>let immutablePoint = Point(x: 1)<br>// immutablePoint.x = 2 // Error: Cannot modify a let struct</pre><pre>class Point {<br>    var x: Int = 1<br>}<br>let point = Point()<br>point.x = 2 // Allowed: Property is mutable</pre><p><strong>3. What is copy-on-write, and how does it apply to value types?</strong></p><p>Swift optimizes value types with <strong>copy-on-write</strong>. A copy is only made when the value is modified. Until then, the original memory is shared. This reduces unnecessary copying and improves performance.</p><pre>var array1 = [1, 2, 3]<br>var array2 = array1 // No copy yet (shared memory)<br>array2.append(4)    // Now a copy is made for array2<br><br>print(array1) // Output: [1, 2, 3]<br>print(array2) // Output: [1, 2, 3, 4]</pre><p><strong>4. Can structs have methods like classes?</strong></p><p>Yes, structs can have methods. However, to modify properties within a struct method, the method must be marked as mutating.</p><pre>struct Point {<br>    var x: Int<br><br>    mutating func moveBy(_ delta: Int) {<br>        x += delta<br>    }<br>}<br><br>var point = Point(x: 5)<br>point.moveBy(3)<br>print(point.x) // Output: 8</pre><p><strong>5. How do reference types affect memory management?</strong></p><p>Reference types use <strong>Automatic Reference Counting (ARC)</strong>. ARC tracks the number of references to a class instance and deallocates it when the reference count drops to zero. Be cautious of <strong>retain cycles</strong> in closures or when using strong references.</p><pre>class Person {<br>    var name: String<br>    var friend: Person? // Can create a retain cycle<br><br>    init(name: String) {<br>        self.name = name<br>    }<br>}<br><br>var john: Person? = Person(name: &quot;John&quot;)<br>var jane: Person? = Person(name: &quot;Jane&quot;)<br>john?.friend = jane<br>jane?.friend = john // Retain cycle<br>john = nil<br>jane = nil // Instances are not deallocated</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6e9b4abb8482" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>