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.
Instance of a Struct and Class
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 init
method.
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.
Methods in Structs and Classes
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
Differences btw Structs and Classes
Though we have used the Struct
and 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.
A 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.
A 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 reference type
.
The struct
example above act differently to the class
in that after we made dog1
equal dog2
. Whenever we change any value of either of dog1
or 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 class
.
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 👇
Top comments (3)
This is a great article that explains the difference between the the
Struct
andClass
. I now understand the difference between the two. Great work!I do think this article missed to explain the use cases of both and how you could use them.
When do I use what?
Some comments may only be visible to logged-in visitors. Sign in to view all comments.