Class Vs Struct in Swift

In this article we will go through the differences between Class and Struct in Swift.
We will consider all the differences but will take a deep dive into the main difference. i.e. The Class is reference type whereas Struct is value type in Swift. By this I mean that the Class instances are hold as addresses whereas Struct instances are hold as values in memory, and passed around as references and values respectively in your code. This is the root difference due to which many other differences occur in the way Class and Struct behave in Swift.

Lets dive into this difference.

Consider the following Class

class PersonClass {
    var firstName = ""
    var lastName = ""
}

Next we will create the instance of PersonClass, change its properties and assign it to anther instance of PersonClass.

var firstPerson = PersonClass()
    firstPerson.firstName = "Sadiq"
    firstPerson.lastName = "Aadil"
var secondPerson = firstPerson

Lets now change firstName of secondPerson instance.

secondPerson.firstName = "Nehal"

Will the firstName of firstPerson get changed due to this?
YES!. We can print firstPerson.firstName to confirm this.

print(firstPerson.firstName)

Screen Shot 2017-07-19 at 5.26.20 PM.png

This is what happens with a type that is stored as reference rather than value. When you assign one reference type to another, what actually gets assigned is the reference. Both assignee and assigned instance point to same instance.
I would like to add one more thing here, before moving on to Struct. What happens if instead of defining firstPerson as var I define it with let constant. Will I be able to change its properties firstName and lastName? YES! Because for reference types, let means the reference must remain constant. In other words, you can’t change the instance the constant references, but you can mutate the instance itself.

let firstPerson = PersonClass()
    firstPerson.firstName = "Sadiq" // OK with Class. Not with Struct.
    firstPerson.lastName = "Aadil" // OK with Class. Not with Struct.
let scondPerson = PersonClass()
    firstPerson = scondPerson // Error: cannot assign to value: 'firstPerson' is a 'let' constant

Lets now see the counterpart, value type, Struct.

We will consider the same example with struct, and you will see how Struct instances behave.

Lets define the Struct, PersonStruct

struct PersonStruct {
    var firstName = ""
    var lastName = ""
}

Ww will now create an instance of PersonStruct, change its properties and assign to the second instance of PersonStruct.

var firstStructPerson = PersonStruct()
    firstStructPerson.firstName = "Sadiq"
    firstStructPerson.lastName = "Aadil"
var seconStructPerson = firstStructPerson

We will now change firstName of seconStructPerson.

seconStructPerson.firstName = "Nehal"

Will the above assignment change the firstName of firstStructPerson. NO!! This is what happens when you assign a value type to another. The value actually gets copied into the assigned instance and both are maintained as separate entities.
We can confirm this printing firstStructPerson.firstName.

print(firstStructPerson.firstName)

Screen Shot 2017-07-19 at 6.08.05 PM

Lets face the same question that we faced with Class. What happens if instead of defining firstStructPerson as var I define it with let constant. Will I be able to change its properties firstName and lastName? NO!. This time the answer is NO.

let firstStructPerson = PersonStruct()
firstStructPerson.firstName = "Sadiq" //Error: cannot assign to property: 'firstStructPerson' is a 'let' constant

firstStructPerson.lastName = "Aadil" //Error: cannot assign to property: 'firstStructPerson' is a 'let' constant

For value types, let means the instance must remain constant. No properties of the instance will ever change, regardless whether the property is declared with let or var

So, that was the main difference between Class and Struct. Few derived differences because of this difference are

  • Reference counting allows more than one reference to a class instance.
  • For Class we have identity operators === and !== which are used to check whether two variables or constants refer to the same instance of Class. The question for identity operators does not arise for Struct because two different Struct variables or constants can not point to same instance. You can try applying identity operator to Struct types. You will get compile time error.

Another difference between Class and Struct is that you get a Memberwise Initializersr for Struct but not for Class. For example though we have not defined any initializer for PersonStruct, we can initialize a person like this

var aPerson = PersonStruct(firstName: "Sadiq", lastName: "Aadil")

This is not possible with Class.
There are some other differences, which I hope don’t require any explanations.
These are the differences I am referring from Apple Docs. Classes have the following capabilities which Structs don’t have.

  • Inheritance enables one class to inherit the characteristics of another.
  • Type casting enables you to check and interpret the type of a class instance at runtime.
  • Deinitializers enable an instance of a class to free up any resources it has assigned.
    • Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s