Difference Between Structs, Enum, And Class In Swift

In this blog, we will explore the differences between structs, Enum, and classes in Swift.

Structs in Swift

Structs are value types; their values are copied and each variable keeps its own set of values. When we set out to name "Pravesh", the "Ayush" struct in aStudentStruct does not get changed.
  1. struct Student {  
  2.    var name: String  
  3.    init(name: String) {  
  4.       self.name = name  
  5.    }  
  6. }  
  7. var aStudentStruct = Student(name:"Ayush")  
  8. var bStudentStruct = aStudentStruct // aStudentStruct and bStudentStruct are two structs with the same value!  
  9. bStudentStruct.name = "Pravesh"  
  10. println(aStudentStruct.name) // "Ayush"  
  11. println(bStudentStruct.name) // "Pravesh"   

Class in Swift

A class is a reference type and passed by reference. Also, classes have Inheritance which allows one class to inherit the characteristics of another.
Here's an example of a Swift class. When the name is changing, the instance referenced by both variables is updated. "Ayush" is now "Pravesh" everywhere the "Ayush" name was ever referenced.
  1. class StudentClass {  
  2.    var name: String  
  3.    init(name: String) {  
  4.       self.name = name  
  5.    }  
  6. }  
  7. var aStudentClass = StudentClass(name:"Ayush")  
  8. var bStudentClass = aStudentClass // aStudentClass and bStudentClass are refrence with the same instance!  
  9. bStudentClass.name = "Pravesh"  
  10. println(aStudentClass.name) // "Pravesh"  
  11. println(bStudentStruct.name) // "Pravesh"   
Structs and classes in Swift have many things in common. Both can:
  1. Define properties to store values.
  2. Define methods to provide functionality.
  3. Define subscripts to provide access to their values using subscript syntax.
  4. Define initializers to set up their initial state.
  5. Be extended to expand their functionality beyond a default implementation.
  6. Conform to protocols to provide standard functionality of a certain kind.
Classes have additional capabilities that structures don't have:
  1. Inheritance enables one class to inherit the characteristics of another.
  2. Typecasting enables you to check and interpret the type of a class instance at runtime.
  3. Deinitializers enable an instance of the class to free up any resources it has assigned.
  4. Reference counting allows more than one reference to a class

Enum in Swift

An enumeration defines a common type for a group of related values and enables you to work with those values in a type-safe way within your code, the enumeration in Swift is first-class types in their own right.
Enum is considered as a structured data type that can be modified without needing to change, say, a String or Int multiple times within yours.
  1. let myString = "Pravesh"
  2. if myString == "Pravesh Dubey" {
  3.    // Doesn't execute because "Pravesh Dubey" is the value assigned to "myString"
  4. }
With an enum, we can avoid this and never worry about changing the same thing more than once.
  1. enum MyEnum: String {
  2.    case Test = "Pravesh"
  3. }
  4. let enumValue = MyEnum.Test
  5. if enumValue == MyEnum.Test {
  6.    // Will execute because we can reassign the value of "MyEnum.Test" unless we do so within "MyEnum"
  7. }
Enums are initialized by one of a finite number of cases, are completely defined by their case, and should always show  the valid instance of that case when instantiated.