Recall & Review
beginner
What is an extension in Swift?
An extension in Swift adds new functionality to an existing class, struct, enum, or protocol without modifying the original source code.
Click to reveal answer
beginner
How do you declare an extension for a struct named
Person?Use the syntax: <br>
extension Person {<br> // new methods or properties<br>}Click to reveal answer
intermediate
Can extensions add stored properties to types in Swift?
No, extensions cannot add stored properties. They can only add computed properties, methods, initializers, and conform to protocols.
Click to reveal answer
beginner
What is the benefit of using extensions in Swift?
Extensions help organize code better, add functionality to types you don’t own, and conform to protocols without subclassing.
Click to reveal answer
beginner
Show an example of adding a method
greet() to a struct Person using extension.extension Person {<br> func greet() {<br> print("Hello!")<br> }<br>}Click to reveal answer
Which of the following can you NOT add using a Swift extension?
✗ Incorrect
Extensions cannot add stored properties, only computed properties, methods, and initializers.
How do you start an extension declaration for a class named
Car?✗ Incorrect
The correct syntax is
extension Car { }.Can extensions in Swift add protocol conformance to existing types?
✗ Incorrect
Extensions can add protocol conformance to any type.
Which keyword is used to declare an extension in Swift?
✗ Incorrect
The keyword
extension is used to declare extensions.What can extensions NOT do?
✗ Incorrect
Extensions cannot override existing methods.
Explain what Swift extensions are and why they are useful.
Think about how you can add features to existing types without changing their original code.
You got /5 concepts.
Describe the syntax of declaring an extension and what you can add inside it.
Remember the basic structure and what features extensions support.
You got /7 concepts.