Difference between Class and Struct in Swift - Simplified

Difference between Class and Struct in Swift - Simplified

Imagine a class like a shared document. When many people open the same document, they're all looking at the same thing.

Oct 19, 2023·

2 min read

As you know Simplified Series is all about simplifying the concept by eliminating too many technical details. Let's simplify the differences between classes and structs in Swift:

Classes

  • Reference Type: Imagine a class like a shared document. When many people open the same document, they're all looking at the same thing. If someone makes changes, everyone sees those changes because they're looking at the same paper.

  • Inheritance: Classes can have children, like a family tree. A child can inherit traits from their parents. For example, a "Car" class can have a child class like "SportsCar" that inherits features from the parent class.

  • Mutable: You can change a class after you create it. It's like being able to erase and rewrite on a piece of paper.

Structs

  • Value Type: Think of a struct like a personal notebook. When you copy it, you get your own new notebook. If you make notes in your notebook, it doesn't affect someone else's notebook because they each have their own separate pages.

  • No Inheritance: Structs can't have children or parents. Each struct is on its own; they can't inherit anything from other structs.

  • Immutable: Once you create a struct, you can't change it. It's like having a notebook with pages that can't be erased or rewritten.

If you want to create something with shared properties that many parts of your code can use and change, use a class. If you want something individual that can't be changed after it's made, use a struct. Choose the one that fits your needs.

Follow me for more such content.

More Swift Tutorials

Did you find this article valuable?

Support Jay Tillu by becoming a sponsor. Any amount is appreciated!