Structs and Classes in Swift — What, When and How
Tags: ios, swift
Published: 2018-01-11 16:32:38

---
title: Structs and Classes in Swift — What, When and How
published: true
description: Structs and Classes in Swift
tags: ios, swift
---

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:

{% gist https://gist.github.com/agoiabel/f003a81d406fa4da0ca0c9196aac366b %}

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:

{% gist https://gist.github.com/agoiabel/866f424c0ecc39a1dd4db05ce35b91fe %}

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&nbsp;Class

To access a struct, we have to call it and pass the default properties they need like below:

{% gist https://gist.github.com/agoiabel/ddef231f3c98aa520b7872943770de29 %}

`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.

{% gist https://gist.github.com/agoiabel/e0c992bce2055c09eedb2937400aa640 %}

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&nbsp;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

{% gist https://gist.github.com/agoiabel/2517f867b0b2521da903996cb160c3fd %}

Once we have declared our method, we can access them by creating an instance of them then we use the&nbsp;`.` notation to access the `method` name. We can access the `Dog Struct` or `Class` like below

{% gist https://gist.github.com/agoiabel/643964eabf18036bda1f01e886a34d0f %}

### Differences btw Structs and&nbsp;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

{% gist https://gist.github.com/agoiabel/a29af8c1a8ad570e39b5807a8dcc8784 %}

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`.

{% gist https://gist.github.com/agoiabel/2ebd80066604c8dcbadcaa736c018a80 %}

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_ 👏_&nbsp;. Thanks for your time and make sure to follow me or drop your comment below_ 👇