Class Vs Struct Vs Enum

Last updated on September 14th, 2020

Class

  • Supports Inheritance, DeInitializers.
  • Class instances are reference types. During assignment/function call, it is the reference (address) that gets copied and not the value.

Struct

  • Does not support Inheritance, DeInitializers etc.
  • Struct instances are value types. During assignment/function call, it is the entire object (content) that gets copied.

Note: In Swift the types String, Array, Dictionary etc are all implemented as Structs, causing them to be copied always. However Swift uses Copy On Write to avoid unnecessary copying.

Enum

  • Does not support Inheritance, DeInitializers etc.
  • Enum instances are value types. During assignment/function call, it is the entire object that gets copied.

Leave a Reply

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