Greetings folks!
Hope this blog finds you well.
So today we will be talking about quite a fun topic which is surprisingly asked in a lot of iOS developer interviews.I recently read in a Medium Blog about this guy's Apple Interview. The question was “the difference between struct and classes”. For some context as to why this is surprising to me, I am an entry-level iOS developer right now, a newbie in my field. But that question for the holy Apple interview seemed a bit too easy. I learned this topic in my first week. So I thought why not start here?
I thought this was an easy one. But I guess it's always the easy things that are the most deceptive ones, aren’t they?
Turns out I just knew the tip of the iceberg and didn’t fully understand what I was using a million times in my code.
Let's start with the topic. Firstly, both struct and classes are ways of Data Modelling. Think of them as a big cardboard box you use while packing.
Struct is a value type. what this means is, it “stores” the value of the data.
Let's understand this with an example ->
So here I have created a struct called "books".This stores two variables inside it, which are "name" and "author".
What structs do is that they create member-wise initializers automatically. So you don’t need to pass in a value before declaring the struct. It just needs to know the type of data that you have provided, like in our case “author” variable will always be a “String”. So Swift knows what to expect.
But this is essentially not the case for Classes.
Classes are reference types. They don’t actually store this data, but they point to it. They reference to it.
Now what happens in our classes is that it does not create a member-wise initializer automatically like our struct did. So you can’t simply change the struct keyword with a class keyword.
If you try to do this, Swift is going to complain very loudly 😤
Now, you have three options with you
You can create a default value. But tbf, this is wasteful. and so not essentially efficient.
You can create an optional. But this is clumsy, you will have optionals all over your code and no one wants to deal with that kind of a mess, right? 😬
The best option you have is to create your own initializers. Now let's see how you do it. The keyword for initializers is “init”.
First, you pass in the variables inside the init. And then declare that this property conforms to the local variable created.
Now, I hope you are clear on this. Also, there’s something called class inheritance. But that's a topic for another time.
Let's now roll on to the important part.
🪄When do you use a struct and when a class?🪄
It can be confusing at the start cause they seem a lot similar to each other. But what I need you to understand is the core difference.
Let's do this with an example.
Here I have created myFav book. Now, lets see what happens if I try to copy it
okay, so what happened here is. The copy created stands alone as an individual. No changes that I make to this copy shall reflect in the original struct.
Now, let's try something similar in a class.
myFav has changed. It is no longer its original version.
Let me get back to the question.
You would want to use a class when you are handling a database that you control. Suppose I have a cupcake ordering app, if the user changes the flavour of the cupcake she wants in one of the views, I want that change to reflect in the entire database of my app.
You use a struct when you do not have control over the data that is being passed in. When data is being provided by an external source, you can’t trust it. You surely don’t want the data to be reflected in the entire app and possibly corrupt your app. So use struct in this case.
I hope it is clear to you now.
Enjoy playing around with structures and classes. And remember, as you move along in this wonderful journey of becoming an iOS developer, you will find it easy and instinctive to know what kind of data modelling you want to use in that particular scenario. Don't forget to have fun with Swift. Experiment more in the Playground.
Until next time, I bid you goodbye.
♡
July 18,2024