DAY #16

Structs vs Classes (Part 1 of 2)

Classes

Swift classes are reference types. This means if you assign a class to a field, you are passing a reference around. In order to copy a class, you will have to make a shallow or deep copy.

Structs

Swift structs are value types. This means if you are passing a copy around. Every time you move a struct, you create a new copy of the struct.

Why?

First thing to note is that Structs are not unique to Swift. They are present in many languages, such as C, C#, C++, golang. Some languages implement structs as derivatives of other data structures, and some langagues do not have them at all (java, python). Structs are generally more performant if used right - which is a distinct advantage. However, the biggest advantage is probably just the differences in copying versus referencing. Structs help you move your code towards a more data-oriented approach.

Originally published 04/18/2021 @ https://pittcsc.org/ Discord

Published here on 09/09/2022. Blog published date reflects the original date of publication.