Recall & Review
beginner
What is a method in Swift?
A method is a function that is defined inside a class, struct, or enum. It describes behaviors or actions that instances of that type can perform.
Click to reveal answer
beginner
How do you define a method inside a Swift struct?
You write a function inside the struct's curly braces. For example:<br>
struct Dog {<br> func bark() {<br> print("Woof!")<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
beginner
Can methods in Swift have parameters and return values?
Yes! Methods can take parameters and return values just like regular functions. This lets them perform actions using input and give back results.
Click to reveal answer
intermediate
What is the difference between instance methods and type methods in Swift?
Instance methods work on an instance of a type (like a specific dog). Type methods work on the type itself (like all dogs). Type methods use the
static keyword. Note: Classes can also use the class keyword for overridable type methods.Click to reveal answer
How do you call a method named
bark() on an instance dog of a struct Dog?✗ Incorrect
You call instance methods using dot notation on the instance: dog.bark()
Which keyword allows a method in a struct to change its properties?
✗ Incorrect
The mutating keyword lets methods modify properties inside structs.
Where do you define methods in Swift?
✗ Incorrect
Methods are functions inside classes, structs, or enums.
What keyword do you use to define a type method in Swift?
✗ Incorrect
Type methods use the static keyword to belong to the type itself. Note: Classes can also use the class keyword for overridable type methods.
Can methods in Swift return values?
✗ Incorrect
Methods can have return values just like regular functions.
Explain how to add a method to a Swift struct and how to call it on an instance.
Think about writing a function inside the struct and then using the instance name followed by a dot and the method name.
You got /3 concepts.
Describe the purpose of the mutating keyword in Swift methods.
Remember structs are like copies, so you need special permission to change them inside methods.
You got /3 concepts.