Swift Structs
A struct (short for structure) groups related properties and behaviors into a single named type. Think of a struct as a blueprint for a product — every iPhone made from the same blueprint shares the same design, but each physical phone is its own independent object.
Defining a Struct
struct Point {
var x: Double
var y: Double
}
let origin = Point(x: 0, y: 0)
let corner = Point(x: 5.0, y: 3.5)
print("Origin: \(origin.x), \(origin.y)") // Output: Origin: 0.0, 0.0
print("Corner: \(corner.x), \(corner.y)") // Output: Corner: 5.0, 3.5Swift automatically creates a memberwise initializer — the Point(x:y:) call above. You do not need to write an init method unless you want custom behavior.
Diagram: Struct as a Blueprint
struct Point Instance A Instance B +------------+ +-----------+ +-----------+ | x: Double | →→→ | x: 0.0 | | x: 5.0 | | y: Double | →→→ | y: 0.0 | | y: 3.5 | +------------+ +-----------+ +-----------+ Blueprint Separate copy Separate copy
Adding Methods to a Struct
Structs can contain functions called methods. Methods operate on the struct's own properties.
struct Rectangle {
var width: Double
var height: Double
func area() -> Double {
return width * height
}
func perimeter() -> Double {
return 2 * (width + height)
}
}
let room = Rectangle(width: 5.0, height: 3.0)
print("Area: \(room.area())") // Output: Area: 15.0
print("Perimeter: \(room.perimeter())") // Output: Perimeter: 16.0Mutating Methods
Structs are value types. By default, a method cannot change the struct's own properties. Mark the method mutating to allow modifications.
struct Counter {
var count = 0
mutating func increment() {
count += 1
}
mutating func reset() {
count = 0
}
}
var c = Counter()
c.increment()
c.increment()
print(c.count) // Output: 2
c.reset()
print(c.count) // Output: 0Structs Are Value Types
When you assign a struct to a new variable, Swift copies it. Changes to the copy do not affect the original.
var a = Point(x: 1.0, y: 2.0)
var b = a // b is an independent copy
b.x = 99.0
print(a.x) // Output: 1.0 (unchanged)
print(b.x) // Output: 99.0 (only b changed)Computed Properties
A computed property runs code each time it is accessed instead of storing a value.
struct Circle {
var radius: Double
var diameter: Double {
return radius * 2
}
var area: Double {
return 3.14159 * radius * radius
}
}
let c = Circle(radius: 5.0)
print(c.diameter) // Output: 10.0
print(c.area) // Output: 78.53975Property Observers
Property observers run code automatically when a stored property changes.
struct BankAccount {
var balance: Double {
didSet {
if balance < 0 {
print("Warning: Account is overdrawn!")
}
}
}
}
var account = BankAccount(balance: 100.0)
account.balance = -50.0
// Output: Warning: Account is overdrawn!Static Properties and Methods
Static members belong to the struct itself, not to any individual instance.
struct MathConstants {
static let pi = 3.14159
static let e = 2.71828
static func circleArea(radius: Double) -> Double {
return pi * radius * radius
}
}
print(MathConstants.pi)
print(MathConstants.circleArea(radius: 3.0))Custom Initializer
You can write a custom init to control how instances are created.
struct Temperature {
var celsius: Double
init(fahrenheit: Double) {
celsius = (fahrenheit - 32) * 5 / 9
}
}
let bodyTemp = Temperature(fahrenheit: 98.6)
print(bodyTemp.celsius) // Output: 37.0When to Use a Struct
| Situation | Prefer |
|---|---|
| Simple data model (point, color, size) | Struct |
| Independent copies needed | Struct |
| Shared identity needed | Class |
| Inheritance required | Class |
Apple recommends structs by default. Switch to classes only when you specifically need shared reference behavior or inheritance.
Summary
Structs group properties and methods into a named value type. Swift copies structs on assignment, so each instance is fully independent. Use mutating for methods that change properties, computed properties for derived values, and static members for type-level data. Structs are the preferred building block in modern Swift code.
