Methods inside structs let you add actions that the struct can do. This helps keep data and behavior together, like a mini toolbox.
Methods in structs in Swift
struct StructName { // properties func methodName() { // code to do something } mutating func changeMethod() { // code that changes properties } }
Use func to define a method inside a struct.
If the method changes properties, mark it with mutating.
area() that calculates the circle's area.struct Circle { var radius: Double func area() -> Double { return 3.14 * radius * radius } }
increment() that changes the count property, so it uses mutating.struct Counter { var count = 0 mutating func increment() { count += 1 } }
This program creates a rectangle, prints its area, doubles its size using a mutating method, then prints the new area.
struct Rectangle { var width: Double var height: Double func area() -> Double { return width * height } mutating func doubleSize() { width *= 2 height *= 2 } } var rect = Rectangle(width: 3, height: 4) print("Area before doubling: \(rect.area())") rect.doubleSize() print("Area after doubling: \(rect.area())")
Remember to use mutating only when the method changes the struct's properties.
Methods help keep your code clean and related data and actions together.
Methods are functions inside structs that let structs do things.
Use mutating for methods that change properties.
Methods help organize code by combining data and behavior.
Practice
What is the main purpose of a method inside a Swift struct?
Solution
Step 1: Understand what methods do in structs
Methods are functions inside structs that define behaviors or actions the struct can perform.Step 2: Differentiate methods from data storage
Properties store data, while methods perform actions using that data.Final Answer:
To define actions the struct can perform -> Option AQuick Check:
Methods = actions in structs [OK]
- Confusing methods with properties
- Thinking methods create new types
- Mixing methods with import statements
Which keyword must be used before a method in a Swift struct if it changes the struct's properties?
Solution
Step 1: Recall Swift's rule for modifying struct properties
Struct methods that change properties must be marked withmutatingto allow modification.Step 2: Identify the correct keyword
Among the options, onlymutatingenables property changes inside struct methods.Final Answer:
mutating -> Option DQuick Check:
Change properties = mutating [OK]
- Using override in structs (only for classes)
- Confusing static with mutating
- Omitting mutating keyword causes errors
What will be the output of this Swift code?
struct Counter {
var count = 0
mutating func increment() {
count += 1
}
}
var myCounter = Counter()
myCounter.increment()
print(myCounter.count)Solution
Step 1: Understand the mutating method increment()
The method increases count by 1 and is marked mutating, so it can change the property.Step 2: Trace the code execution
myCounter starts with count = 0, increment() adds 1, so count becomes 1, then printed.Final Answer:
1 -> Option BQuick Check:
mutating method changes count to 1 [OK]
- Forgetting mutating causes error
- Expecting count to stay 0
- Thinking print outputs nil
Identify the error in this Swift struct method and how to fix it:
struct Score {
var points = 0
func addPoint() {
points += 1
}
}Solution
Step 1: Check if method modifies a property
addPoint() tries to increase points by 1, which modifies the struct's property.Step 2: Recall Swift rule for modifying properties in structs
Methods that modify properties must be markedmutating, otherwise compiler error occurs.Final Answer:
Method must be markedmutatingto changepoints-> Option CQuick Check:
Modify property in struct method = mutating needed [OK]
- Omitting mutating keyword
- Changing var to let (makes property immutable)
- Thinking method must return value
Given this struct, which method correctly resets the level property to 1?
struct Game {
var level = 5
// Which method resets level to 1?
}Solution
Step 1: Identify method that changes a property
Resetting level means changing the property, so method must be mutating.Step 2: Check each option
A lacks mutating, so cannot modify property. B is mutating but only returns 1 without changing level. C is static and cannot access instance property. D is mutating and correctly sets level to 1.Final Answer:
mutating func reset() { level = 1 } -> Option AQuick Check:
Change property in struct method = mutating func [OK]
- Forgetting mutating keyword
- Using static func to access instance properties
- Returning value instead of changing property
Ready to prove it?
Pass the test and this pattern moves unknown
