Swift Rewind : The Fundamentals of Data Modeling
Season 3 — Episode 4: Structs Unveiled in Swift
Swift Rewind : The Fundamentals of Data Modeling
Season 3 — Episode 4: Structs Unveiled in Swift

So far, we’ve seen that enums are great for representing a single value with optional associated data. However, they aren’t ideal for modeling larger or more complex systems.
For that, Swift provides structs and classes — both are used to model real-world entities with properties (data) and methods (behaviour).
📮 In the upcoming episodes, we’ll explore:
▪ Structs vs Classes (differences, similarities, and use cases)
▪ A hands-on Command-Line Tool (CLT) project modelling a town facing a monster infestation. main.swift is the entry point of the program in CLT project. Code executes top to bottom.
▪ ️Using structs and classes together to represent real-world scenarios. Basically custom types which we will define in separate files and use in main.swift
Example: — Town.swift → defines the Town struct. main.swift → creates and uses a Town instance. This separation improves readability and debugging, especially in larger projects.
import Foundation
📥 What Does import Foundation Mean?
- Imports Apple’s Foundation framework
- Provides pre-built types and utilities (like
String,Date, etc.) - Acts as a core building block for Swift programs.
Structures
A struct is a type used to group related data together under a single unit. It helps model real-world entities by combining properties (data) in one place.
Creating a Struct
// Let’s define a Town struct in a separate file: -> File → New File → Name: Town.swift
//Town.swift
import Foundation
// Structs Syntax
struct Town {
// variables to hold on to some data which model characteristics of town called Properties.
var population = 5422
var numberOfLights = 4
}
// On instance creation it will default to having population of 5,422 and 4 stoplights.
- Properties store data about the struct
- They can be mutable (
var) or immutable (let) - Here, both properties have default values
Using a Struct
// In main.swift File
import Foundation
var myTown = Town()
print("Population: \(myTown.population), noOfStopLights: \(myTown.numberOfLights)")
- Above created instance in main.swift, by name of type followed by empty parentheses Town() which calls the default initializer.
- The instance is stored in
myTown. Use dot syntax (myTown.population) to access properties. - Default values are automatically assigned on creation.
Instance Methods
You can define functions inside a struct — these are called instance methods because they belong to a specific instance. ( Basically a method, as associated with a particular type)
struct Town {
var population = 5422
var numberOfLights = 4
func printTownDescription() {
print("Population: \(population), noOfStopLights: \(numberOfLights)")
}
}
// Using the Method
var myTown = Town()
// ❌ Not recommended
// print("Population: \(myTown.population), noOfStopLights: \(myTown.numberOfLights)")
// ✅ Better approach - To use instance method, need to call it on instance of that type.
myTown.printTownDescription()
👉 Instance methods are called using dot syntax on an instance (myTown.printTownDescription())
Mutating Methods
What if you want a method to modify properties of a struct? In Swift, you must mark such methods with the mutating keyword.
//Town.swift
import Foundation
struct Town {
var population = 5422
var numberOfLights = 4
// Takes no arguments and returns nothing
func printTownDescription() {
print("Population: \(population), noOfStopLights: \(numberOfLights)")
}
mutating func changePopulation(by amount: Int) {
population += amount
}
}
Note: Both enums and structs require mutating keyword on methods to change value of an instance’s properties.
💡 Why mutating is Needed ?
Structs are value types, so their data is copied by default. mutating tells the compiler that this method is allowed to modify the original instance (self) instead of a copy.
// In main.swift File
import Foundation
var myTown = Town()
myTown.changePopulation(by: 500)
myTown.printTownDescription()
// Population: 5922, noOfStopLights: 4
By default, values passed into functions are copied, so changes inside the function don’t affect the original value (unless marked inout).
Similarly, in structs and enums:
- Methods receive an implicit
self - By default,
selfis immutable (a copy)
👉 The mutating keyword changes this behavior. It tells the compiler to treat self as **inout, allowing the method to modify the original instance instead of a copy**.
⭐️ You’ve just completed Episode 4 of Season 3:
In this episode, we introduced structs in Swift, learned how to model real-world data using properties, and explored instance and mutating methods to add behavior and modify state.
Stay tuned — more calm, clear, and practical Swift concepts are coming next. Let’s keep rewinding Swift, one clean concept at a time.
💬 Found this helpful? Give this post a like and share it with a fellow iOS developer revisiting Swift fundamentals.
메타데이터
- post_id
- d2919b809eb0
- slug
- swift-rewind-the-fundamentals-of-data-modeling-d2919b809eb0
- url
- https://medium.com/@krishhna.ios/swift-rewind-the-fundamentals-of-data-modeling-d2919b809eb0
- canonical_url
- https://medium.com/@krishhna.ios/swift-rewind-the-fundamentals-of-data-modeling-d2919b809eb0
- author_url
- https://medium.com/@krishhna.ios
- status
- ok
- fetched_at
- 2026-07-14 12:17:41