100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Swift

Classes in Swift

Classes are reference types in Swift that support inheritance and are managed automatically via ARC.

Structures & ClassesBeginner8 min readJul 8, 2026
Analogies

Introduction

A class in Swift is a reference type used to model entities that need shared, mutable state or identity — for example a network manager, a view controller, or an object that multiple parts of a program need to observe and mutate together. Unlike structs, classes support inheritance, letting one class build on and extend the behavior of another. Class instances are managed by Automatic Reference Counting (ARC), which deallocates an instance once nothing references it anymore.

🏏

Cricket analogy: A shared scoreboard object that every commentator's app references and updates in real time is like a Swift class — it has identity, and just as a franchise's core coaching staff can be inherited season to season, ARC keeps the scoreboard alive only as long as some broadcast feed still points to it.

Syntax

swift
class Animal {
    var name: String

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

    func makeSound() {
        print("...")
    }
}

Explanation

Unlike a struct, a class does not get a free memberwise initializer, so we must write our own init(name:) and assign the parameter to the stored property via self.name. Because classes are reference types, any variable holding an instance actually holds a reference to that instance's storage on the heap; copying the reference (via assignment or passing to a function) does not copy the underlying object.

🏏

Cricket analogy: Unlike a struct, a PlayerProfile class doesn't get a free memberwise initializer, so you write init(name:) and assign self.name = name yourself; handing a teammate a reference to that profile object doesn't clone the player, it just gives them another way to point at the same stats.

Example

swift
class Animal {
    var name: String
    init(name: String) { self.name = name }
}

let a = Animal(name: "Rex")
let b = a          // b references the SAME instance as a
b.name = "Buddy"

print("a.name = \(a.name)")
print("b.name = \(b.name)")

Output

swift
a.name = Buddy
b.name = Buddy

Key Takeaways

  • Classes are reference types — variables hold a reference to shared storage.
  • Classes must define their own initializers; there is no automatic memberwise init.
  • Classes support single inheritance, unlike structs.
  • Instances are managed automatically by ARC (Automatic Reference Counting).
  • Mutating an instance through one reference is visible through every other reference to it.

Practice what you learned

Was this page helpful?

Topics covered

#Swift#SwiftProgrammingStudyNotes#Programming#ClassesInSwift#Classes#Syntax#Explanation#Example#OOP#StudyNotes#SkillVeris