Understanding Classes and Structures in Swift
- Chris Wong
- Jun 13, 2023
- 2 min read
Updated: Jul 1, 2023

Swift is a powerful and versatile programming language, and one of the key features that makes it so flexible is its support for custom data types. In this post, we'll explore two fundamental building blocks of Swift's type system: classes and structures. We'll dive into their differences and help you understand when to use each.
What is a Class?
A class is a blueprint for creating objects, which are instances of the class. Classes define the properties (data) and methods (behavior) that their instances can have. They are reference types, which means that when you create an instance of a class and assign it to a variable or constant, you're actually creating a reference to the instance, rather than a copy of it.
Here's a simple example of a class in Swift:
class Dog {
var name: String
var age: Int
init(name: String, age: Int) {
self.name = name
self.age = age
}
func bark() {
print("Woof, woof!")
}
}
What is a Structure?
A structure is similar to a class in that it also defines a blueprint for creating custom data types. Structures, like classes, can have properties and methods. However, structures are value types, which means that when you create an instance of a structure and assign it to a variable or constant, you're creating an independent copy of the instance.
Here's a simple example of a structure in Swift:
struct Point {
var x: Double
var y: Double
func distance(to point: Point) -> Double {
let deltaX = x - point.x
let deltaY = y - point.y
return sqrt(deltaX * deltaX + deltaY * deltaY)
}
}
Key Differences Between Classes and Structures
Now that we have a basic understanding of classes and structures, let's examine their main differences.
1. Reference vs. Value Types:
- When you work with classes, you're dealing with reference. This means that if you assign a class instance to another variable, both variables point to the same instance.
- With structures, you're working with *values*. When you assign a structure instance to another variable, a completely separate copy of the instance is created.
2. Inheritance:
- Classes allow inheritance, letting you create a new class that inherits the properties and methods of an existing class. This is useful for building a hierarchy of related classes and reusing code.
- Structures don't support inheritance, meaning each structure defines a unique and independent type.
3. Deinitializers:
- Classes can have deinitializers, special methods that are called when an instance is deallocated. This is helpful for performing cleanup before an instance is removed from memory.
- Structures don't have deinitializers.
4. Type casting:
- With classes, you can use type casting to check and interpret the type of a class instance at runtime.
- Structures don't support type casting.
When to Use Classes and Structures
Choose a structure when you need a simple data type with value semantics, and opt for a class when you need more advanced features like inheritance or reference semantics. Swift encourages the use of value types (like structures and enumerations) whenever possible, as they are generally more efficient and safer when executing concurrent code.
In conclusion, understanding the differences between classes and structures in Swift is crucial for designing efficient and maintainable code. By carefully considering when to use each, you can take full advantage of Swift's powerful type system.
Comments