Class vs Struct

Introduction

Welcome to Lesson 8. There are two objectives. First, you will learn the fundamental difference between structs and classes by understanding value types and reference types. Second, you will discover why structs do not provide inheritance.

Problem

  1. What are the differences besides inheritance?
  2. When to use structs over classes?

Design Class

To demonstrate the difference between classes and structs, first design a class, HumanClass that has a name property.

  1. class HumanClass {
  2. var name: String
  3. init(name: String) {
  4. self.name = name
  5. }
  6. }

Create Instance

  1. var humanClassObject = HumanClass(name: "Bob")
  2. humanClassObject.name // "Bob"

Create another instance that “copies” humanClassObject

  1. let newHumanClassObject = humanClassObject

Change the name property of newHumanClassObject to “Bobby”.

  1. humanClassObject.name = "Bobby"
  2. newHumanClassObject.name // "Bobby"

The name property of newHumanClassObject has been changed to “Bobby” as well.

Design Struct

Let us find out if the same behavior occurs with an object created with structs

  1. struct HumanStruct {
  2. var name: String
  3. init(name: String) {
  4. self.name = name
  5. }
  6. }

Create Instance

Create another instance that “copies” structObject

  1. var structObject = HumanStruct(name: "Bob")
  2. let newStructObject = structObject

Change the name property of structObject to “Bobby”.

  1. structObject.name = "Bobby"
  2. newStructObject.name // "Bob"

On the contrary, the change on the name property of structObject has no effect on newStructObject.name.

The graph below shows the fundamental difference between value types vs reference types.

The difference in value type and reference type

Classes

For classes, when you create an instance that “copies”, the new instance shares the object with the original instance.

  1. let newHumanClassObject = humanClassObject // share

Structs

For structs, when you create an instance that “copies”, the new instance creates an identical copy of the object, and the object is not shared.

  1. let newStructObject = structObject // copy-paste

Mutability

Due to the fundamental difference, there is an interesting discrepancy when it comes to creating instances with let.

Mutability in Classes

Let us create an instance with let and attempt to mutate its property.

  1. class BobClass {
  2. var name = "Bob"
  3. }
  4. // Create Instance
  5. let bobClass = BobClass()
  6. bobClass.name = "Bobby"
  7. let newBobClass = bobClass
  8. newBobClass.name = "Bob the Developer"
  9. bobClass.name // "Bob the Developer"

Although the instance, bobClass has been created with let, the property of it has been mutated. It is possible since the instance does not own the object. Instead, it has a reference to theBobClass object in the cloud/RAM.

No Mutability in Structs

  1. struct BobStruct {
  2. var name = "Bob"
  3. }
  4. let bobStruct = BobStruct()
  5. bobStruct.name
  6. bobStruct.name = "Bob Lee"
  7. // Error: Immutable

With structs, when an instance is created with a constant, let, it’s not possible to mutate its property since the instance has its own copy and the let protects anyone from interfering with its own instance.

Important: Value types “store”, while reference types “point”.

When to use Struct/Value Type?

  • 10,000 - 1,000,000 faster than classes - Stack Overflow
  • No mutability due to no relationship between objects
  • Non-nested structure

Resources

To learn more about cons of Object Oriented Programming, you may read the beginning of Intro to Protocol Oriented Programming.

References

If you’re subclassing, you are doing it wrong - Hector MatosAll evidence points to OOP being bullshitObject Oriented Programming is exceptionally bad Why Choose Struct Over Class?

Source Code

1008_class_struct_difference.playground

Conclusion

You’ve learned the distinction betweenclasses and structs when it comes to creating objects. Instances with classes have references to the object while withstructs store. As a result, classes are called reference types while structs and enums, are value types.

At this point, you may find no incentive to learn the difference. You are right. For small apps, it doesn’t matter. But, once you start implementing closures and delegates, and other reference related tasks, you would soon discover a little more complexity which you will learn how to solve in Chapter 5. You will appreciate value types along with Protocol Oriented Programming soon.