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

Structs in Swift

Structs are value types in Swift that are copied on assignment and come with a free memberwise initializer.

Structures & ClassesBeginner7 min readJul 8, 2026
Analogies

Introduction

A struct in Swift is a value type used to bundle related data and behavior into a single named type. Structs are used extensively throughout the Swift standard library — String, Array, and Dictionary are all implemented as structs. Because structs are value types, every time an instance is assigned to a new variable or passed to a function, a fresh copy of its data is made, so each copy can be modified independently without affecting the original.

🏏

Cricket analogy: A struct is like a scorecard template bundling runs and balls together into one value; just as String, Array, and Dictionary are built as structs, when a scorecard is copied to a second scorer's notebook, each copy can be updated independently without changing the original.

Syntax

swift
struct Point {
    var x: Int
    var y: Int
}

// Swift automatically generates a memberwise initializer
let origin = Point(x: 0, y: 0)

Explanation

The struct keyword introduces the type, followed by a name and a body containing stored properties. Because we declared two stored properties, x and y, Swift synthesizes a memberwise initializer automatically, so we did not have to write init(x:y:) ourselves. Structs also automatically gain a compiler-generated Equatable conformance in many cases when their stored properties are all equatable, though this is a separate feature from the memberwise initializer.

🏏

Cricket analogy: The struct keyword declaring x and y is like a scorecard template with runs and balls fields; Swift auto-generates the memberwise initializer, like a pre-printed scorecard needing no manual header, and auto-derives Equatable, like two scorecards being comparable automatically if all their fields match.

Example

swift
struct Point {
    var x: Int
    var y: Int
}

var a = Point(x: 1, y: 2)
var b = a          // b is a COPY of a
b.x = 99

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

Output

swift
a.x = 1
b.x = 99

Key Takeaways

  • Structs are value types — assigning or passing one copies its data.
  • Swift generates a memberwise initializer automatically for structs.
  • Modifying one copy never affects any other copy.
  • String, Array, and Dictionary are all structs in the standard library.
  • Structs cannot be inherited from — there is no struct inheritance in Swift.
  • Prefer structs by default unless you specifically need reference semantics.

Practice what you learned

Was this page helpful?

Topics covered

#Swift#SwiftProgrammingStudyNotes#Programming#StructsInSwift#Structs#Syntax#Explanation#Example#StudyNotes#SkillVeris