juniorSwift

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:

Swift
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:
  • name
  • age
  • 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:

Swift
let person = Person(name: "Alice", age: 25)
person.introduce()

Output:

TEXT
Hi, my name is Alice

The class is the blueprint, while person is an object created from that blueprint.

Properties in classes

Properties store data associated with an object.

Example:

Swift
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:

Swift
let car1 = Car(brand: "Toyota", speed: 80)
let car2 = Car(brand: "Honda", speed: 100)

Here:

  • car1.brand is "Toyota".
  • car2.brand is "Honda".

Methods in classes

Methods are functions defined inside a class that describe object behavior.

Example:

Swift
class Counter {
var value = 0
func increment() {
value += 1
}
}
let counter = Counter()
counter.increment()
print(counter.value)

Output:

TEXT
1

Methods 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:

Swift
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:

TEXT
Bob

Changing person2 also changes person1 because both variables reference the same object.

Classes vs structs

Swift has both classes and structs, but they behave differently.

FeatureClassStruct
TypeReference typeValue type
InheritanceSupportedNot supported
Stored in memoryReference to objectValue copy
Can use deinitYesNo
Default choice in SwiftLess commonMore common

Example of struct copying:

Swift
struct Point {
var x: Int
}
var point1 = Point(x: 10)
var point2 = point1
point2.x = 20
print(point1.x)

Output:

TEXT
10

The value was copied, unlike a class instance.

Inheritance

Classes support inheritance, which allows one class to reuse and extend another class.

Example:

Swift
class Animal {
func makeSound() {
print("Some sound")
}
}
class Dog: Animal {
override func makeSound() {
print("Bark")
}
}
let dog = Dog()
dog.makeSound()

Output:

TEXT
Bark

Dog inherits from Animal and overrides its behavior.

Initializers

Classes require initialization before use. Initializers ensure stored properties have valid values.

Example:

Swift
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:

Swift
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.

Swift
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:

TEXT
alex

The 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?

More Swift interview questions

View all →