What is a class in Swift?
Updated May 17, 2026
Short answer
A class in Swift is a blueprint for creating objects that combine data (properties) and behavior (methods). Classes support features such as inheritance, reference semantics, and object-oriented programming patterns. When you create an instance of a class, multiple variables can refer to the same underlying object in memory.
Deep explanation
A class defines the structure and behavior of an object. It describes what data an object stores and what actions it can perform.
In Swift, a class is created using the class keyword:
class Person { var name: String var age: Int
init(name: String, age: Int) { self.name = name self.age = age }
func introduce() { print("Hi, my name is \(name)") }}This class defines:
- Properties:
nameage
- Initializer:
init(name:age:)creates and sets up new objects.
- Method:
introduce()defines behavior.
Creating class instances
An instance is an actual object created from a class.
Example:
let person = Person(name: "Alice", age: 25)
person.introduce()Output:
Hi, my name is AliceThe class is the blueprint, while person is an object created from that blueprint.
Properties in classes
Properties store data associated with an object.
Example:
class Car { var brand: String var speed: Int
init(brand: String, speed: Int) { self.brand = brand self.speed = speed }}Each instance has its own property values:
let car1 = Car(brand: "Toyota", speed: 80)let car2 = Car(brand: "Honda", speed: 100)Here:
car1.brandis"Toyota".car2.brandis"Honda".
Methods in classes
Methods are functions defined inside a class that describe object behavior.
Example:
class Counter { var value = 0
func increment() { value += 1 }}
let counter = Counter()
counter.increment()
print(counter.value)Output:
1Methods can access and modify the object's properties.
Reference semantics
One of the most important differences between classes and structs in Swift is that classes are reference types.
When you assign a class instance to another variable, both variables point to the same object.
Example:
class Person { var name: String
init(name: String) { self.name = name }}
let person1 = Person(name: "Alice")let person2 = person1
person2.name = "Bob"
print(person1.name)Output:
BobChanging person2 also changes person1 because both variables reference the same object.
Classes vs structs
Swift has both classes and structs, but they behave differently.
| Feature | Class | Struct |
|---|---|---|
| Type | Reference type | Value type |
| Inheritance | Supported | Not supported |
| Stored in memory | Reference to object | Value copy |
Can use deinit | Yes | No |
| Default choice in Swift | Less common | More common |
Example of struct copying:
struct Point { var x: Int}
var point1 = Point(x: 10)var point2 = point1
point2.x = 20
print(point1.x)Output:
10The value was copied, unlike a class instance.
Inheritance
Classes support inheritance, which allows one class to reuse and extend another class.
Example:
class Animal { func makeSound() { print("Some sound") }}
class Dog: Animal { override func makeSound() { print("Bark") }}
let dog = Dog()
dog.makeSound()Output:
BarkDog inherits from Animal and overrides its behavior.
Initializers
Classes require initialization before use. Initializers ensure stored properties have valid values.
Example:
class User { let id: Int var username: String
init(id: Int, username: String) { self.id = id self.username = username }}The initializer guarantees that every User object has an ID and username.
Deinitializers
Classes can define a deinit method that runs when an object is removed from memory.
Example:
class FileManager { deinit { print("Object removed from memory") }}deinit is commonly used for cleanup tasks such as releasing resources.
When to use a class
Classes are useful when:
- Multiple parts of an application need to share the same object.
- You need inheritance.
- The object's identity matters.
- You need reference behavior.
- You need lifecycle management with
deinit.
Common examples:
- View controllers in iOS applications.
- Network managers.
- Database managers.
- Shared services.
Real-world example
A common iOS example is a user session manager that stores the currently logged-in user.
class UserSession { var username: String? var isLoggedIn: Bool = false
func login(username: String) { self.username = username self.isLoggedIn = true }
func logout() { username = nil isLoggedIn = false }}
let session = UserSession()
session.login(username: "alex")
print(session.username ?? "No user")Output:
alexThe application can keep a reference to the same UserSession object across different parts of the app. Because it is a class, changes made through one reference are visible through other references to the same instance.
Common mistakes
- * Assuming classes behave like structs and automatically create copies when assigned to another variable.
- * Forgetting to initialize all stored properties before an instance can be created.
- * Using classes everywhere when a struct would provide safer value semantics.
- * Creating inheritance hierarchies that are unnecessarily complex.
- * Forgetting that class instances can be shared and modified from multiple places.
- * Using strong references carelessly and causing memory leaks through reference cycles.
- * Confusing a class definition with an instance created from that class.
Follow-up questions
- What is the difference between a class and a struct in Swift?
- Why are classes called reference types in Swift?
- What is inheritance in Swift classes?
- When should you choose a class instead of a struct?
- What is the purpose of deinit in a Swift class?
- What happens when two variables reference the same class instance?