Struct vs Class — Swift

BN
iOS World
Published in
4 min readJan 20, 2023
Photo by Caspar Camille Rubin on Unsplash

Struct

In Swift, structs are used to define custom data types that hold a collection of related values. Structs are value types, which means that when you assign an instance of a struct to a variable or constant, or pass it as an argument to a function, a copy of the struct is created.

Structs have properties, which are used to store the values associated with an instance of the struct, and methods, which are used to define the behavior of the struct. Structs can also have initializers, which are used to set up an instance of the struct, and computed properties, which are used to calculate values based on other properties.

Structs are lightweight and efficient, and are well-suited for small, simple data structures. Some examples of when to use structs include simple data types like a point on a graph or a color. They are also useful for handling data that does not need to be shared between multiple instances, such as unique data of a single user.

Structs cannot inherit from other structs or classes, they can conform to protocols though. This means that they can adopt certain methods, properties, and behaviors defined in a protocol, but they can’t inherit properties and methods from other structs or classes.

In summary, structs are a lightweight and efficient way to define custom data types in Swift, they are passed by value, they can’t inherit from other structs or classes and they are well-suited for small, simple data structures that don’t need to be shared between multiple instances.

struct Point {
var x: Double
var y: Double
}

var point1 = Point(x: 1.0, y: 2.0)
var point2 = point1
point2.x = 3.0

print(point1) // prints "Point(x: 1.0, y: 2.0)"
print(point2) // prints "Point(x: 3.0, y: 2.0)"

Class

In Swift, classes are used to define custom data types that hold a collection of related values and behavior. Classes are reference types, which means that when you assign an instance of a class to a variable or constant, or pass it as an argument to a function, a reference to the instance is created, not a copy.

Classes have properties, which are used to store the values associated with an instance of the class, and methods, which are used to define the behavior of the class. Classes can also have initializers, which are used to set up an instance of the class, and computed properties, which are used to calculate values based on other properties.

Classes are more powerful and flexible than structs, and are well-suited for more complex data structures and behavior. Some examples of when to use classes include more complex data types like a person or a bank account. They are also useful for handling shared data, such as data that needs to be shared between multiple instances, like a shared database or a shared user session.

Classes can inherit from other classes, this feature is called inheritance. This means that a class can inherit the properties and methods of another class, allowing developers to reuse code and create a hierarchy of classes. Classes can also have deinitializers, which are methods that are called when an instance of the class is deallocated.

In summary, classes are a powerful and flexible way to define custom data types in Swift, they are passed by reference, they can inherit from other classes and they are well-suited for more complex data structures and behavior. They are also useful for handling shared data between multiple instances.

class Person {
var name: String
var age: Int

init(name: String, age: Int) {
self.name = name
self.age = age
}

func sayHello() {
print("Hello, my name is \(name) and I am \(age) years old.")
}
}

let person1 = Person(name: "John", age: 30)
let person2 = person1
person2.name = "Jane"

print(person1.name) // prints "Jane"
print(person2.name) // prints "Jane"

person1.sayHello() // prints "Hello, my name is Jane and I am 30 years old."

Difference Between Struct vs Class

Structs:

  • Structs are value types, which means that when you assign an instance of a struct to a variable or constant, or pass it as an argument to a function, a copy of the struct is created.
  • Structs cannot inherit from other structs or classes, they can conform to protocols though.
  • Structs are automatically initialized with a memberwise initializer, which means that you can initialize an instance of the struct by providing values for its properties.
  • Structs are lightweight and efficient, and are well-suited for small, simple data structures.
  • Examples of when to use structs include simple data types like a point on a graph or a color.

Classes:

  • Classes are reference types, which means that when you assign an instance of a class to a variable or constant, or pass it as an argument to a function, a reference to the instance is created, not a copy.
  • Classes can inherit from other classes, this feature is called inheritance.
  • Classes can have deinitializers, which are methods that are called when an instance of the class is deallocated.
  • Classes are more powerful and flexible than structs, and are well-suited for more complex data structures and behavior.
  • Examples of when to use classes include more complex data types like a person or a bank account.

--

--