Class vs Struct in Swift
Class and struct are general-purpose of a template to defines bellow characteristics.
Class and struct looks similar except the keyword used to define. Below example is just a template definition.
What are common things in struct and class?
- Both has Property, function, initializer, protocol and extension.
- Define subscript to provide access to their values.
- Failable init can be used in both struct and class
- both can access property/function using dot syntax
.e.g: structObjectName.propertyname or classObjectName.propertyname
- both as default init method i.e
To know more on initializer in swift. click here
Things Available Only in Class:
- deinit can be used only with class.
- Classes can inherit the characteristics of another class. Struct can’t inherit from other struct.
- Reference counting allows more than one references.
- Type of class instance can be checked at runtime.
Things Available Only in Struct:
- Struct has default member wise initializer. For class we need to define explicitly.
- Structs have the benefit of mutation in safety as you can trust that no other part of your app is changing the data at the same time.
- Because of mutation in safety benefit, struct is helpful in multi-threading.
Difference between struct and class:
- Class uses keyword
classand Struct uses keyword
- For modifying the self property in struct from a function you need to define
mutatingat the beginning of function. Class doesn’t required anything.
- One of the most difference is value vs reference type.
- If you create struct instance with let property, you can’t modify its properties. If you create a class instance with let property, you can modify its properties. i.e:
To resolve above error, you can change
varfor the struct object i.e
var b = B()
Value vs reference types:
- Classes are reference type : which means reference to a location in the memory where class instance is stored.
- Struct and enumeration are value type: which means each reference has it’s own copy of data
- If you share/assign instance of class to different references, all these references will have a single shared data. That means any changes in that class instance will be available to every reference
- If you share/assign instance of struct to different references, every reference will creates its own memory to store the data. Any changes in struct in one reference will not affect in other reference.
In above example for struct, bstruct and aStruct refer to different memory location. Because of this changing name in bstruct reference will not affect the data in aStruct reference
In above example for class, b and a refer to same memory location. Because of this changing name in b reference will affect the data in a reference.
Why class and struct are different?
Memory allocation for both class and struct is different. Because of this difference, class and struct behaves differently while sharing its instances.
Value types are stored in stack memory and reference type are stored in heap memory.
Finally you need to decide which one we need to use whether class or struct. My recommendation is to go with struct.
How can you decide which one is better?
- If you want to compare object by reference using
===then go for class and If you want to compare object by reference using
==then go fo struct.
- Use struct when you need mutable copy and use class when you need single copy.
To know more about stack and heap memory management in swift. Please click here
I hope you find this blog helpful. If you enjoyed it, feel free to hit the clap button below 👏 to help others find it! and follow me on Medium. Thanks for reading. ❤❤❤❤