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
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
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
a.x = 1
b.x = 99Key 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
1. What kind of type is a Swift struct?
2. What initializer does Swift generate automatically for a struct with stored properties?
3. What happens when you assign one struct instance to another variable?
4. Can a struct inherit from another struct in Swift?
5. Which of these standard library types is implemented as a struct?
Was this page helpful?
You May Also Like
Classes in Swift
Classes are reference types in Swift that support inheritance and are managed automatically via ARC.
Structs vs Classes in Swift
Structs use value semantics while classes use reference semantics — the defining choice you make for every custom Swift type.
Properties and Methods in Swift
Properties store or compute values on a type, while methods define the functions and behavior attached to it.
Enumerations in Swift
Learn how Swift enums define related values, support associated and raw values, and integrate with switch statements.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics