{"id":9232,"date":"2023-12-08T21:26:05","date_gmt":"2023-12-08T21:26:05","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=9232"},"modified":"2023-12-08T21:26:19","modified_gmt":"2023-12-08T21:26:19","slug":"structs-and-classes-in-swift","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/structs-and-classes-in-swift\/","title":{"rendered":"Structs and Classes in Swift"},"content":{"rendered":"<p>I&#8217;ve previously posted about structs and classes in Swift, let&#8217;s look a little more in depth into the subject<\/p>\n<p><strong>Differences between a struct and class<\/strong><\/p>\n<p>As you may know, structs use value semantics whilst classes use reference semantics. In other words if you do something like this<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nstruct Point {\r\n   var x: Double\r\n   var y: Double\r\n}\r\n\r\nlet pt1 = Point(x: 1, y: 10)\r\nlet pt2 = pt1\r\n<\/pre>\n<p>Your <em>pt1.x, pt1.y<\/em> values are copied to <em>pt2.x, pt2.y<\/em>. If you therefore alter <em>p1<\/em> this does no affect <em>p2<\/em>. Swift using COW (copy on write) so this is more performant than one might think if you were making lots of copies &#8211; in other words the copying takes place only when a value is changed &#8211; so thing lazy copying of values.<\/p>\n<p>Structs are stored on the stack and hence are very performant in terms of creation etc. Ofcourse if you&#8217;re making many changes to copies of a struct then things are less performant due to all the copying that needs to happen (even with COW). <\/p>\n<p>Ofcourse, in a more complicated scenario, such as the Point also having a member variable which is a class, then these will be stored on the stack, but with references to the heap based classes. This is an issue for structs, if a struct contain multiple reference types it&#8217;ll incur reference counting for each reference type in the struct.<\/p>\n<p>Classes on the other hand use reference semantics and therefore if we rewrite the about for a class. Then <em>p2<\/em> is a reference to <em>p1<\/em>. Any changes to <em>p1<\/em> will be reflected in <em>p2<\/em><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nclass Point {\r\n   var x: Double\r\n   var y: Double\r\n}\r\n\r\nlet pt1 = Point(x: 1, y: 10)\r\nlet pt2 = pt1\r\n<\/pre>\n<p>Classes are stored on the heap which means that allocation will be les performant than a struct, but ofcourse if you&#8217;re passing around a reference to a class, this is more performant. However because a reference type copies the reference pointer it does ofcourse lend itself to issues around mutation of state and this becomes more of an issue when we add in concurrency.<\/p>\n<p>The reference types use reference counting to handle auto-deletion (i.e. garbage collection). This does mean that a reference type will also require more space that a struct to track it&#8217;s reference counter. Also due to it using the heap, again potential concurrency issues means the OS must lock memory etc.<\/p>\n<p>If you&#8217;re using String or the likes in a struct, you&#8217;re storing a reference type, hence this will incur reference counting overhead. If we&#8217;re using a String to ultimately represent known values, for example UP, DOWN, OUT_OF_SERVICE then not only will it be more type safe to use an enum, from a performance perspective this would be better, for example we can declare a status enum with a String type as a raw backing value like this<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nenum Status: String {\r\n   case up = &quot;UP&quot;\r\n   case down = &quot;DOWN&quot;\r\n   case outOfService = &quot;OUT_OF_SERVICE&quot;\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve previously posted about structs and classes in Swift, let&#8217;s look a little more in depth into the subject Differences between a struct and class As you may know, structs use value semantics whilst classes use reference semantics. In other words if you do something like this struct Point { var x: Double var y: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[286],"tags":[],"class_list":["post-9232","post","type-post","status-publish","format-standard","hentry","category-swift"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9232","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=9232"}],"version-history":[{"count":5,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9232\/revisions"}],"predecessor-version":[{"id":10318,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9232\/revisions\/10318"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=9232"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=9232"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=9232"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}