Debug School

rakesh kumar
rakesh kumar

Posted on

Difference between struct,class and enum

Difference between class ,struct and enum
When to Use Which?
TABULAR COMPARISION

Purpose

A struct is used to group related data and methods.

A class is used to create objects that can share the same data through references.

An enum is used to represent a fixed set of possible values or states.

Example
struct Student {
    var name: String
}

class Employee {
    var name: String

    init(name: String) {
        self.name = name
    }
}

enum PaymentStatus {
    case pending
    case completed
    case failed
}

let student = Student(name: "Ashwani")
let employee = Employee(name: "Ravi")
let status = PaymentStatus.completed

print(student.name)
print(employee.name)
print(status)

Output:

Ashwani
Ravi
completed
Enter fullscreen mode Exit fullscreen mode

Value Type and Reference Type

A struct is a value type.

A class is a reference type.

An enum is also a value type.

Example

struct Student {
    var name: String
}

class Employee {
    var name: String

    init(name: String) {
        self.name = name
    }
}

enum Direction {
    case north
    case south
}
Enter fullscreen mode Exit fullscreen mode
  1. Copy Behavior

When a struct or enum is assigned to another variable, a separate copy is created.

When a class object is assigned to another variable, both variables refer to the same object.

Struct example
struct Student {
    var name: String
}

var student1 = Student(name: "Ashwani")
var student2 = student1

student2.name = "Ravi"

print(student1.name)
print(student2.name)

Output:

Ashwani
Ravi
Class example
class Employee {
    var name: String

    init(name: String) {
        self.name = name
    }
}

let employee1 = Employee(name: "Ashwani")
let employee2 = employee1

employee2.name = "Ravi"

print(employee1.name)
print(employee2.name)

Output:

Ravi
Ravi
Enum example
enum Direction {
    case north
    case south
}

var direction1 = Direction.north
var direction2 = direction1

direction2 = .south

print(direction1)
print(direction2)

Output:

north
south
Enter fullscreen mode Exit fullscreen mode
  1. Inheritance

Only a class supports inheritance.

A struct and an enum cannot inherit from another struct or enum.

Example
class Person {
    func introduce() {
        print("I am a person")
    }
}

class Developer: Person {
    func code() {
        print("I write Swift code")
    }
}

let developer = Developer()

developer.introduce()
developer.code()

Output:

I am a person
I write Swift code
5. Initialization
Enter fullscreen mode Exit fullscreen mode

A struct automatically gets a memberwise initializer.

A class usually requires an explicit initializer when properties do not have default values.

An enum is created using one of its predefined cases.

Struct example
struct Student {
    var name: String
    var age: Int
}

let student = Student(name: "Ashwani", age: 30)

print(student.name)
print(student.age)

Output:

Ashwani
30
Class example
class Employee {
    var name: String
    var salary: Double

    init(name: String, salary: Double) {
        self.name = name
        self.salary = salary
    }
}

let employee = Employee(name: "Ravi", salary: 50000)

print(employee.name)
print(employee.salary)

Output:

Ravi
50000.0
Enum example
enum UserRole {
    case student
    case trainer
    case admin
}

let role = UserRole.trainer

print(role)

Output:

trainer
Enter fullscreen mode Exit fullscreen mode
  1. Mutability

For a struct, both the property and the instance must be declared with var to modify the value.

For a class, a var property can be changed even when the object reference is declared using let.

For an enum, the value can change only when its variable is declared with var.

Struct example
struct Student {
    var name: String
}

var student = Student(name: "Ashwani")

student.name = "Ravi"

print(student.name)

Output:

Ravi
Class example
class Employee {
    var name: String

    init(name: String) {
        self.name = name
    }
}

let employee = Employee(name: "Ashwani")

employee.name = "Ravi"

print(employee.name)

Output:

Ravi
Enum example
enum Status {
    case offline
    case online
}

var status = Status.offline

status = .online

print(status)

Output:

online
Enter fullscreen mode Exit fullscreen mode
  1. Stored Data and Cases

A struct and a class can contain stored properties.

An enum mainly contains cases. It cannot contain normal stored instance properties, but its cases can carry associated values.

Example
struct Product {
    var name: String
    var price: Double
}

class User {
    var name: String

    init(name: String) {
        self.name = name
    }
}

enum PaymentResult {
    case success(amount: Double)
    case failure(message: String)
}

let product = Product(name: "Laptop", price: 75000)
let user = User(name: "Ashwani")
let result = PaymentResult.success(amount: 2500)

print(product.name)
print(user.name)

switch result {
case .success(let amount):
    print("Payment successful: ₹\(amount)")

case .failure(let message):
    print("Payment failed: \(message)")
}

Output:

Laptop
Ashwani
Payment successful: ₹2500.0
Enter fullscreen mode Exit fullscreen mode

Best Use

Use struct for independent data models.

Use class when shared state, inheritance, or object identity is required.

Use enum when a value must be selected from a fixed set of options.

Example

struct Address {
    var city: String
    var country: String
}

class BankAccount {
    var balance: Double

    init(balance: Double) {
        self.balance = balance
    }
}

enum BookingStatus {
    case pending
    case confirmed
    case cancelled
}

let address = Address(city: "Bengaluru", country: "India")
let account = BankAccount(balance: 10000)
let bookingStatus = BookingStatus.confirmed

print(address.city)
print(account.balance)
print(bookingStatus)

Output:


Bengaluru
10000.0
confirmed
Enter fullscreen mode Exit fullscreen mode

TABULAR COMPARISION

When to Use Which?

Top comments (0)