Structs and Classes are object oriented programming concept in swift, the main reason for using them is so you can more closely associate behaviours together. Assuming I have a dog, it must have a name, belongs to a breed and has an age. I can group this property of a dog using either a class or a structure like below:
The name, breed and age are the properties of a dog represented by the Struct above can also be represented using a class like below:
Struct and Class has a lot of similarities which include defining properties to store value, define methods to provide functionalities, define initializers to set up their initial state, can be extended to expand their functionality beyond a default implementation etc.
To access a struct, we have to call it and pass the default properties they need like below:
Struct automatically bind the properties which are the name, breed and age in our Dog Struct when we create a new instance of the Dog but we have to manually do that in classes by defining the
Once we add properties to a class, we are require to define the
init() method which in other languages represent the
constructor() method but
struct does that automatically for us.
We can declare functions within our class/struct. Functions within class/struct are called
methods. In our dog object, we can define a method to get the dog’s details like below
Once we have declared our method, we can access them by creating an instance of them then we use the
. notation to access the
method name. We can access the
Dog Struct or
Class like below
Though we have used the
Class almost the same way but I need us to know that they are different to each other and there are use cases when we have to use one over the other. Lets outline the differences:
Struct is a value type while
Classes are reference type.
Value type is a type whose value is copied when it is assigned to a variable or constant, or when it is passed to a function.
Reference type is not copied when assigned to a variable or constant, or when they are passed to a function. Rather than a copy, a reference to the same existing instance is used instead.
I will show some code snippet below to make you understand better
We created a new instance of Dog and called it
dog1, and assign the
dog1 instance to
dog2 which means
dog1 should be equal to
dog2. We then changed the value of
dog1 name to
JaneDony. This automatically changes the name of
dog2 too, since they are equals to each other and are
struct example above act differently to the
class in that after we made
dog2. Whenever we change any value of either of
dog2, it will not cause a change on the other one. Hence
dog1.name in the above code will not change when
dog2 name changes.
Another major difference is that we can not use
inheritance when we use
struct but we can with
I am happy to share this article with you. If you’ve enjoyed this article, do show support by giving a few claps 👏 . Thanks for your time and make sure to follow me or drop your comment below 👇