Swift Struct and Class Difference

Struct

  • Value type
  • Copied when passed or assigned
  • Copy on write
  • Functional programming
  • No inheritance
  • “Free” init initializes ALL vars
  • Mutability must be explicitly stated
  • Your “go-to” data structure

Class

  • Reference type
  • Passed around via pointers
  • Automatically reference counted
  • Object-oriented programming
  • Inheritance (single)
  • “Free” init initializes NO vars
  • Always mutable
  • Used in specific circumstances
  • The ViewModel in MVVM is always a class

I’m working on creating a Udemy and Skillshare about SwiftUI development. Hit the comment below if interested when it goes live.

Leave a Reply

Your email address will not be published. Required fields are marked *