Struct vs. Class in Swift

Struct vs. Class in Swift

Structs are value types - A value type is a type whose value is copied when it’s assigned to a variable or constant, or when it’s passed to a function.

Classes are Reference-based. Unlike value types, reference types are not copied when they’re assigned to a variable or constant, or when they’re passed to a function. Rather than a copy, a reference to the same existing instance is used.

Structures and classes in Swift have many things in common. The major difference between structs and classes is that they live in different places in memory. Structs live on the Stack(that's why structs are fast) and Classes live on Heap in RAM.

Comparing Structures and Classes

Definition Syntax

Structures and classes have a similar definition syntax. You introduce structures with the struct keyword and classes with the class keyword. Both place their entire definition within a pair of braces:

No alt text provided for this image

Similarities

Structures and classes in Swift have many things in common. Both can:

  • Define properties to store values
  • Define methods to provide functionality
  • Define subscripts to provide access to their values using subscript syntax
  • Define initializers to set up their initial state
  • Be extended, using extension keyword, to expand their functionality beyond a default implementation
  • Conform to protocols to provide standard functionality of a certain kind.
  • Naming conventions are the same in Both. Both have the first Capital letter in their names

Differences

Classes have additional capabilities that structures don’t:

  • Inheritance - which enables one class to inherit the characteristics of another class.
  • Typecasting - Which enables you to check and interpret the type of a class instance at runtime.
  • De-initializers - Which enables an instance of a class to free up any resources it has assigned.
  • Reference counting - Which allows more than one reference to a class instance.

Structs have additional capabilities that classes don’t:

  • In a struct, you get a free initializer, even if you don’t create an initializer. You can initialize struct properties using the default struct initializer.
  • Unlike classes, you can’t change a class’s properties inside any method but can perform any operation. If you wanna mutate(change) any struct property, you’ve to write a mutating keyword to any method’s signature, which is updating any struct property.
  • Structs are truly mutable. If you create a constant of a struct type, you can’t even change properties of that constant’s properties after first initialization. If you want, declare a struct type variable as “var”.

No alt text provided for this image

Mutating Struct

Be Careful❗️ If you declare a constant(let) of a struct type, you can’t call the mutating method ever. So to call any mutating method declare struct type as “var”.

No alt text provided for this image


Struct vs. Class - Which One is Better?

It's kind of saying like which is better, a horse or a mule. They both have their pros and cons. Now similar to a mule, a struct is infertile - you can't inherit from structs or try to subclass in a struct.

  • When you need inheritance, then you'll need to use classes. And, also, classes will allow you to work with Objective-C code.
  • So when you need those components that are written in Objective-C, then you can't interact with them using a struct. So use classes in that case.

The official Apple advice is that you should use a struct by default whenever you want to create a new custom object, and only turn it into a class when you find that you need inheritance or you need to be able to work with Objective-C code. And then, in that case, turn the struct into a class. So in certain cases, you still need to rely on classes, but start with a struct and only go up if needed. This is the same advice for Swift access levels. Start with the least inclusive, most “private” access level, and only increase its access level as and when needed.

Conclusion

No alt text provided for this image

To dig into details, Lookup Swift Docs about Structures and Classes



How the memory management works for Struct, As ARC works for reference types.

To view or add a comment, sign in

More articles by Muhammad Asad

  • How Swift Code Turns into a Final App

    Ever wonder what actually happens when you hit Run in Xcode? Your Swift file doesn’t go straight to your iPhone — it…

    1 Comment
  • Transcribing Audio to Text in SwiftUI

    Let's develop Speech-to-text functionality using SwiftUI and the Speech Framework. The article covers the basics of…

    5 Comments

Others also viewed

Explore content categories