{"id":29,"date":"2025-01-30T18:12:14","date_gmt":"2025-01-30T18:12:14","guid":{"rendered":"https:\/\/chanchalgeek.com\/code.chanchalgeek.com\/?p=29"},"modified":"2025-01-30T18:12:15","modified_gmt":"2025-01-30T18:12:15","slug":"what-is-difference-between-value-type-and-reference-type-in-swift","status":"publish","type":"post","link":"https:\/\/chanchalgeek.com\/code.chanchalgeek.com\/index.php\/2025\/01\/30\/what-is-difference-between-value-type-and-reference-type-in-swift\/","title":{"rendered":"What is difference between value type and reference type in Swift?"},"content":{"rendered":"\n<p>In Swift there are certain types called value types and others are called reference types. <\/p>\n\n\n\n<p><code>Struct<\/code>, <code>String<\/code>, <code>Array<\/code>, <code>Dictionary<\/code>, <code>Set<\/code> are <strong>values<\/strong> types<\/p>\n\n\n\n<p><code>Classes<\/code>, <code>functions<\/code>, <code>closures<\/code> are <strong>reference<\/strong> types.<\/p>\n\n\n\n<p>Starting from <code>String<\/code> type, check the following code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"swift\" class=\"language-swift line-numbers\">let city = \"Glasgow\"\nvar anotherCity = city\nanotherCity = \"Edenburgh\"\nprint(\"Value of city: \\(city)\")\nprint(\"Value of anotherCity: \\(anotherCity)\")<\/code><\/pre>\n\n\n\n<p>Output is:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"swift\" class=\"language-swift\">Value of city: Glasgow\nValue of anotherCity: Edenburgh<\/code><\/pre>\n\n\n\n<p>Changing the value of <code>anotherCity<\/code> didn&#8217;t affect the value of variable <code>city<\/code>. <\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>On contrary, let&#8217;s see the behavior of reference types. Starting from class reference type:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"swift\" class=\"language-swift line-numbers\">class Car {\n    var name: String = \"\"\n}\n\nlet car1 = Car()\ncar.name = \"Toyota Camery\"\n\nlet car2 = car\ncar2.name = \"Mercedes Benz\"\n\nprint(\"Car1 name: \\(car1.name)\")\nprint(\"Car2 name: \\(car2.name)\")<\/code><\/pre>\n\n\n\n<p>Output is:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">Car1 name: Mercedes Benz\nCar2 name: Mercedes Benz<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In Swift there are certain types called value types and others are called reference types. Struct, String, Array, Dictionary, Set are values types Classes, functions,&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[5,6,7],"class_list":["post-29","post","type-post","status-publish","format-standard","hentry","category-interview","tag-interview","tag-ios","tag-swift"],"menu_order":0,"_links":{"self":[{"href":"https:\/\/chanchalgeek.com\/code.chanchalgeek.com\/index.php\/wp-json\/wp\/v2\/posts\/29","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/chanchalgeek.com\/code.chanchalgeek.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/chanchalgeek.com\/code.chanchalgeek.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/chanchalgeek.com\/code.chanchalgeek.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/chanchalgeek.com\/code.chanchalgeek.com\/index.php\/wp-json\/wp\/v2\/comments?post=29"}],"version-history":[{"count":5,"href":"https:\/\/chanchalgeek.com\/code.chanchalgeek.com\/index.php\/wp-json\/wp\/v2\/posts\/29\/revisions"}],"predecessor-version":[{"id":34,"href":"https:\/\/chanchalgeek.com\/code.chanchalgeek.com\/index.php\/wp-json\/wp\/v2\/posts\/29\/revisions\/34"}],"wp:attachment":[{"href":"https:\/\/chanchalgeek.com\/code.chanchalgeek.com\/index.php\/wp-json\/wp\/v2\/media?parent=29"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/chanchalgeek.com\/code.chanchalgeek.com\/index.php\/wp-json\/wp\/v2\/categories?post=29"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/chanchalgeek.com\/code.chanchalgeek.com\/index.php\/wp-json\/wp\/v2\/tags?post=29"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}