Reference types vs Value types

  • Classes are Reference types
  • Enum and Struct are Value types

Reference types: assignment means pointing to same address location

Value types: assignment means copy to the new thing.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/2d03c2b9-fc35-4034-98bd-2334ecd38f7a/Reference_Type_vs_Value_Type.png

Mutability

  • even if be declared by let keyword (means be constant), classes can be modified
  • for struct, if it's be declared as constant with let keyword, it cannot be modified. It's still true if it's be declared as variable with var keyword. But in this case, you can modified it with mutating keyword in function declaration
  • enum doesn't have stored property, so it doesn't relate here.

deinitializer

deinit is only available for classes. Because classes are reference types, so they are concerned by Reference counting process.

Generated init

only with struct, swift generates init methods base on properties.

Inheritance

only class supports inheritance.

Stored property

only enum cannot contain a Stored Property. Stored Property là một property của class hoặc struct, có thể được gán giá trị (để phân biệt với computed property)

Properties - The Swift Programming Language (Swift 5.2)