A method in a Swift struct is a function defined inside the struct that can perform actions using the struct's properties or parameters.
Click to reveal answer
beginner
How do you define a method inside a Swift struct?
You define a method inside a Swift struct by writing a function inside the struct's curly braces, like this:
struct Example { func greet() { print("Hello") } }
Click to reveal answer
intermediate
What keyword do you use to modify properties inside a method of a struct?
You use the mutating keyword before the method to allow it to change properties of the struct.
Click to reveal answer
intermediate
Why do methods in structs sometimes need the 'mutating' keyword?
Because structs are value types, methods that change their properties must be marked with 'mutating' to tell Swift the method will modify the struct itself.
Click to reveal answer
beginner
Can methods in structs return values? Give an example.
Yes, methods in structs can return values. Example:
struct Circle { var radius: Double func area() -> Double { return 3.14 * radius * radius } }
Click to reveal answer
What keyword must you add to a struct method that changes its properties?
Amutating
Bchanging
Cvar
Dmodifying
✗ Incorrect
The 'mutating' keyword tells Swift that the method will modify the struct's properties.
Where do you define methods in a Swift struct?
AInside the struct's curly braces
BOutside the struct
COnly in classes, not structs
DIn a separate file
✗ Incorrect
Methods are defined inside the struct's curly braces to access its properties.
Can a method in a struct return a value?
AOnly if marked mutating
BNo, methods cannot return values
COnly if the struct is a class
DYes, methods can return values
✗ Incorrect
Methods in structs can return values just like functions.
Why do some struct methods need the 'mutating' keyword?
ABecause it makes the method faster
BBecause structs are value types and need explicit permission to change
CBecause classes require it
DBecause methods always need 'mutating'
✗ Incorrect
Structs are value types, so methods that change properties must be marked 'mutating'.
Which of these is a valid method inside a struct?
Avar greet = "Hi"
Bclass greet() {}
Cfunc greet() { print("Hi") }
Dlet greet = func() {}
✗ Incorrect
Methods are functions defined inside structs, like 'func greet() { ... }'.
Explain how to create and use a method inside a Swift struct.
Think about how you write a function inside the struct and then use it.
You got /3 concepts.
Describe why the 'mutating' keyword is important for methods in structs.
Consider how structs behave differently from classes.
You got /3 concepts.
Practice
(1/5)
1.
What is the main purpose of a method inside a Swift struct?
easy
A. To define actions the struct can perform
B. To store data values
C. To create a new struct type
D. To import external libraries
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 A
Quick Check:
Methods = actions in structs [OK]
Hint: Methods = actions inside structs, properties = data [OK]
Common Mistakes:
Confusing methods with properties
Thinking methods create new types
Mixing methods with import statements
2.
Which keyword must be used before a method in a Swift struct if it changes the struct's properties?
easy
A. final
B. override
C. static
D. mutating
Solution
Step 1: Recall Swift's rule for modifying struct properties
Struct methods that change properties must be marked with mutating to allow modification.
Step 2: Identify the correct keyword
Among the options, only mutating enables property changes inside struct methods.
Final Answer:
mutating -> Option D
Quick Check:
Change properties = mutating [OK]
Hint: Use mutating for methods that change struct properties [OK]
C. Method must be marked mutating to change points
D. No error, code runs fine
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 marked mutating, otherwise compiler error occurs.
Final Answer:
Method must be marked mutating to change points -> Option C
Quick Check:
Modify property in struct method = mutating needed [OK]
Hint: Add mutating keyword to methods that change struct properties [OK]
Common Mistakes:
Omitting mutating keyword
Changing var to let (makes property immutable)
Thinking method must return value
5.
Given this struct, which method correctly resets the level property to 1?
struct Game {
var level = 5
// Which method resets level to 1?
}
hard
A. mutating func reset() { level = 1 }
B. mutating func reset() -> Int { return 1 }
C. static func reset() { level = 1 }
D. func reset() { level = 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 A
Quick Check:
Change property in struct method = mutating func [OK]
Hint: Use mutating func to change struct properties inside methods [OK]
Common Mistakes:
Forgetting mutating keyword
Using static func to access instance properties
Returning value instead of changing property
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.