← Back to Swift Course | Chapter 8: Structs & Classes | Lesson 3 of 9

Struct vs Class

Structs are like handing someone a photocopy of your paper -- they get their own separate copy -- while classes are like handing someone the same original paper -- there's still only one.
Syntax
swift
struct StructName { var property: Type }      // value type: copied
class ClassName {                             // reference type: shared
    var property: Type
    init(property: Type) { self.property = property }
}

Value Semantics vs Reference Semantics

The core difference is copying behavior: a struct instance is duplicated when assigned or passed around, while a class instance is shared by reference.

Example: Value Semantics vs Reference Semantics

markup
// Define a struct `ValueBox`
struct ValueBox { var content: Int }
// Define a class `ReferenceBox`
class ReferenceBox { var content: Int; init(content: Int) { self.content = content } }

// Declare a mutable variable `v1`, initialized to `ValueBox(content: 1)`
var v1 = ValueBox(content: 1)
// Declare a mutable variable `v2`, initialized to `v1`
var v2 = v1
v2.content = 2
// Print "Struct copies: v1=\(v1.content), v2=\(v2.content)" to the console
print("Struct copies: v1=\(v1.content), v2=\(v2.content)")

// Declare a constant `r1`, set to `ReferenceBox(content: 1)`
let r1 = ReferenceBox(content: 1)
// Declare a constant `r2`, set to `r1`
let r2 = r1
r2.content = 2
// Print "Class shares: r1=\(r1.content), r2=\(r2.content)" to the console
print("Class shares: r1=\(r1.content), r2=\(r2.content)")

Choosing Between Struct and Class

Structs are usually preferred for simple data models without identity, while classes suit cases needing shared mutable state, reference identity, or inheritance.

Note: A good rule of thumb: start with a struct, and only switch to a class if you have a specific reason to need reference semantics.

Example: Choosing Between Struct and Class

markup
// Define a struct `Coordinate`
struct Coordinate { let lat: Double; let lon: Double }
// Declare a constant `home`, set to `Coordinate(lat: 40.0, lon: -75.0)`
let home = Coordinate(lat: 40.0, lon: -75.0)
// Print "Home at \(home.lat), \(home.lon)" to the console
print("Home at \(home.lat), \(home.lon)")
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Defaulting to class for everything out of habit from other languages; Swift and Apple's own guidance favor structs unless reference semantics or inheritance are specifically needed.
  2. Not realizing structs support protocols and extensions just like classes, without needing inheritance.
  3. Forgetting only classes support inheritance from another class; structs cannot inherit from another struct.
Chapter Summary
  • Structs are value types (copied on assignment); classes are reference types (shared on assignment).
  • Only classes support inheritance from another class.
  • Structs cannot deinitialize with deinit; only classes can, since only they have a lifecycle tied to references.
  • Apple recommends preferring structs by default, using classes when shared mutable state or inheritance is genuinely needed.
🔒

Chapter Quiz — Complete all 9 topics to unlock

0/9 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.