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

Structs vs Classes in Swift

Structs use value semantics while classes use reference semantics — the defining choice you make for every custom Swift type.

Structures & ClassesIntermediate9 min readJul 8, 2026
Analogies

Introduction

Choosing between a struct and a class is one of the most important design decisions in Swift, and it is a favorite interview topic. The core difference is semantics: structs are value types that are copied on assignment, while classes are reference types where assignment shares the same underlying instance. Apple's official guidance is to prefer structs by default, and reach for a class only when you specifically need reference semantics, such as shared mutable state, identity, inheritance, or interoperability with Objective-C APIs.

🏏

Cricket analogy: Choosing struct vs class is like deciding whether a scorecard is a personal copy each fan takes home (value type, copied) or the single official scoreboard everyone shares and updates live (reference type); Apple says default to your own copy unless you need the shared scoreboard for team selection and captaincy identity.

Syntax

swift
struct StructPoint { var x = 0, y = 0 }
class ClassPoint    { var x = 0, y = 0 }

Explanation

Even though StructPoint and ClassPoint look nearly identical, they behave very differently. Assigning a StructPoint to a new variable copies its x and y values, so the two variables are fully independent afterward. Assigning a ClassPoint copies only the reference, so both variables point to the same object in memory, and mutating one is visible through the other. Structs also cannot be subclassed or extended via inheritance, whereas classes can form inheritance hierarchies.

🏏

Cricket analogy: Assigning a StructPoint is like handing a friend a photocopy of your scorecard — they can scribble on it and your original stays clean; assigning a ClassPoint is like both of you looking at the same live scoreboard — one person's correction changes what the other sees too, and structs can't be subclassed like a bowling action can't be inherited.

Example

swift
struct StructPoint { var x = 0 }
class ClassPoint    { var x = 0 }

var s1 = StructPoint(); var s2 = s1
s2.x = 10

let c1 = ClassPoint(); let c2 = c1
c2.x = 10

print("s1.x = \(s1.x), s2.x = \(s2.x)")
print("c1.x = \(c1.x), c2.x = \(c2.x)")

Output

swift
s1.x = 0, s2.x = 10
c1.x = 10, c2.x = 10

Key Takeaways

  • Structs use value semantics; classes use reference semantics.
  • Copies of a struct are independent; copies of a class reference are not.
  • Apple recommends preferring structs unless you specifically need class features.
  • Only classes support inheritance and Objective-C interoperability.
  • Classes are useful when you need shared, mutable identity across a program.
  • String, Array, and Dictionary are structs, showing structs are the norm, not the exception, in Swift.

Practice what you learned

Was this page helpful?

Topics covered

#Swift#SwiftProgrammingStudyNotes#Programming#StructsVsClassesInSwift#Structs#Classes#Syntax#Explanation#OOP#StudyNotes#SkillVeris