Recall & Review
beginner
What is a method in a Swift struct?
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:<br>
struct Example {<br> func greet() {<br> print("Hello")<br> }<br>}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.<br>Example:<br>
struct Circle {<br> var radius: Double<br> func area() -> Double {<br> return 3.14 * radius * radius<br> }<br>}Click to reveal answer
What keyword must you add to a struct method that changes its properties?
✗ Incorrect
The 'mutating' keyword tells Swift that the method will modify the struct's properties.
Where do you define methods in a Swift struct?
✗ Incorrect
Methods are defined inside the struct's curly braces to access its properties.
Can a method in a struct return a value?
✗ Incorrect
Methods in structs can return values just like functions.
Why do some struct methods need the 'mutating' keyword?
✗ Incorrect
Structs are value types, so methods that change properties must be marked 'mutating'.
Which of these is a valid method inside a struct?
✗ 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.