Difference between a struct and a class in Swift.
Every swift developer who has at least 6 months experience will know about Struct, Enum, and Class. Everyone use them on a regular basis without knowing what they are and when and where to use them. I will try to make it simple.
Value Types and Reference Types
Reference: TreeHouse, Apple doc.
It is important to know the difference between value and reference types when talking about Enums, Structs, and Classes. Whenever you create an enum
or a struct
, it is a value type and a Class
is a reference type.
- Value Type: Struct , Enum
- Reference Type: Class
When you pass a class object around your program, you are actually passing a reference to that object, so different parts of your program can share and modify your object. When you pass a structure [ or enum] around your program, what gets passes around is a copy of the structure. So modifications to structures don’t get shared.
One of the major benefits of value types is that they are thread-safe not requiring any synchronization.
Please read my article on Value Type…