Skip to content

Classes vs. Structs

If you think of these two types, let’s first start by stating the differences between the two.

Class – a reference type

So, after we define our class and after we made an object personA, we basically made a pointer to a certain memory location where the actual object resides.

If we have another object and we set it to be equal to our first object – what did we do actually? We set personB, which is also a pointer, to point to the same memory location where personA is pointing.

That’s why it’s called a reference type and that’s why if we change name of personA, name is also changed in personB. Because they’re pointing to the same memory location and if either one of them change the content of that memory location – both objects will have that change.

Struct – a value type

Struct on the other hand is value type. That means that after we create personA and immediately after that we create personB which is assigned to be the personA – we basically copied a whole personA object, to the personB object. What did we achieve by doing this? Well, after we change the name of personA, it will not change name of personB.

So, the famous question which one should I choose?

We all know what Object-Oriented Programming is and what brilliancy it gave to the developers society. Possibility to think in object manner is rather brilliant. Also, the way we avoid DRY (Don’t Repeat Yourself) principle in our code, etc. Classes are really good – no question about it.

Structs on the other hand, really reduce complexity. If we speak about multi-threaded environment, structs are safer, bug free so to speak. Also, structs are passed around as value type so they don’t have reference counts. Because of that, they can’t get caught up in a reference cycle memory leaks. In terms of speed, this post on Stack Overflow bench-marked that structs are almost 1000 times faster than the classes. That is a huge number.

However, although many would favor struct over a class, that does not mean that you should totally avoid using classes. Watch for your architecture and think carefully. In some places, classes can really boost your architectural design.

In Swift Programming Language, Apple gave a recommendation of what are good candidates for classes and what for structs. They ended that paragraph with this sentence:

In all other cases, define a class, and create instances of that class to be managed and passed by reference. In practice, this means that most custom data constructs should be classes, not structures.

Published iniOS

4 Comments

  1. Luka Dimitrov Luka Dimitrov

    Hvala na objašnjenju. Nema veze sa temom al me zanima da li u struct-u moramo da imamo init? Na nekim kursevima se objasnjava kako struct dobije free initializer?

    • djalfirevic djalfirevic

      Pozdrav Luka. Drago mi je da ti se svideo clanak. Ne, kada su u pitanju strukture, konstruktor, tj. initializer se dobija implicitno. Nije lose da se kao praksa usvoji da se ipak napise taj initializer, jer je kod citljiviji, ali ne moras. Bitno je samo da ako se odlucis za jednu od te dve opcije, da ostanes konzistentan.

      • Luka Dimitrov Luka Dimitrov

        Jasno! Hvala . Nadam se da ces redovno kaciti ovakva objasnjenja. Pozdrav!

        • djalfirevic djalfirevic

          Nema na cemu Luka. Naravno 😉

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: