A Swift extension lets you add new features to an existing class, struct, or enum without changing its original code.
Click to reveal answer
beginner
How do extensions add capability without modifying the original type?
Extensions add new methods, computed properties, or functionality separately, so the original type's code stays untouched and safe.
Click to reveal answer
beginner
Why is it useful that extensions don't modify original code?
It keeps the original code stable and lets you add features later, making your code easier to maintain and less error-prone.
Click to reveal answer
intermediate
Can extensions override existing methods in Swift?
No, extensions cannot override existing methods. They only add new functionality without changing existing behavior.
Click to reveal answer
beginner
Give a real-life example of why extensions are like adding new tools without changing the toolbox.
Imagine a toolbox you bought. Extensions are like adding new tools to the box without changing the tools already inside. You get more tools but the original ones stay the same.
Click to reveal answer
What can Swift extensions add to a type?
ANew methods and computed properties
BStored properties
CChange existing methods
DDelete existing properties
✗ Incorrect
Extensions can add new methods and computed properties but cannot add stored properties or change existing methods.
Why do extensions not modify the original type's code?
ABecause they override the original type
BBecause they create a copy of the type
CBecause they delete original code first
DBecause they add functionality separately
✗ Incorrect
Extensions add functionality separately, so the original code remains unchanged.
Which of these is NOT possible with Swift extensions?
AAdding stored properties
BAdding new initializers
CAdding computed properties
DAdding new methods
✗ Incorrect
Extensions cannot add stored properties, only computed properties and methods.
What is a key benefit of using extensions?
AThey make the original code harder to read
BThey slow down the program
CThey allow adding features without touching original code
DThey delete old code automatically
✗ Incorrect
Extensions let you add features without modifying the original code, keeping it stable and clean.
If you want to add a new method to a struct without changing its source, what should you use?
ASubclassing
BExtension
CProtocol
DInheritance
✗ Incorrect
Extensions let you add new methods to structs without modifying their original source.
Explain in your own words why Swift extensions add capability without modifying the original type.
Think about how you can add new tools to a toolbox without changing the tools already inside.
You got /3 concepts.
Describe a situation where using an extension is better than changing the original code.
Imagine you want to add something new but don’t want to risk breaking what already works.
You got /3 concepts.
Practice
(1/5)
1. Why do Swift extensions add capability without modifying the original type?
easy
A. Because they require subclassing the original type
B. Because they add new features without changing existing code
C. Because they delete old methods before adding new ones
D. Because they replace the original type completely
Solution
Step 1: Understand what extensions do
Extensions add new methods or properties to existing types without altering the original source code.
Step 2: Recognize the benefit
This allows adding features safely without changing or breaking existing code.
Final Answer:
Because they add new features without changing existing code -> Option B
Quick Check:
Extensions add features without modification = D [OK]
Hint: Extensions add, not change, original code [OK]
Common Mistakes:
Thinking extensions replace original types
Believing extensions delete old methods
Confusing extensions with subclassing
2. Which of the following is the correct syntax to add a method using an extension in Swift?
easy
A. extension MyType { func newMethod() { } }
B. extend MyType { func newMethod() { } }
C. extension MyType: { func newMethod() { } }
D. extension MyType func newMethod() { }
Solution
Step 1: Recall Swift extension syntax
Extensions start with the keyword 'extension' followed by the type name and braces enclosing new members.
4. Identify the error in this Swift extension code:
extension Int {
func squared() -> Int {
return self * self
}
}
let result = 5.squared
print(result)
medium
A. Cannot extend Int type
B. squared() must be static method
C. self keyword not allowed in extensions
D. Missing parentheses when calling squared()
Solution
Step 1: Check method call syntax
The method squared() is defined correctly, but calling 5.squared misses parentheses.
Step 2: Understand method call requirements
In Swift, methods must be called with parentheses even if no parameters, so 5.squared() is correct.
Final Answer:
Missing parentheses when calling squared() -> Option D
Quick Check:
Method call needs () = B [OK]
Hint: Call methods with () even if no parameters [OK]
Common Mistakes:
Calling methods without parentheses
Thinking Int cannot be extended
Believing self is disallowed in extensions
5. You want to add a computed property isEven to Int using an extension that returns true if the number is even. Which code correctly achieves this without modifying the original Int type?
hard
A. extension Int { static var isEven: Bool { self % 2 == 0 } }
B. extension Int { func isEven() -> Bool { self % 2 == 0 } }
C. extension Int { var isEven: Bool { self % 2 == 0 } }
D. extension Int { var isEven: Bool { return self / 2 == 0 } }
Solution
Step 1: Understand computed property syntax
Computed properties use 'var name: Type { expression }' without parentheses.
Step 2: Check each option
extension Int { var isEven: Bool { self % 2 == 0 } } correctly defines a computed property isEven returning true if self % 2 == 0. extension Int { func isEven() -> Bool { self % 2 == 0 } } uses a method, not a property. extension Int { static var isEven: Bool { self % 2 == 0 } } incorrectly uses static. extension Int { var isEven: Bool { return self / 2 == 0 } } uses wrong logic (division instead of modulo).
Final Answer:
extension Int { var isEven: Bool { self % 2 == 0 } } -> Option C
Quick Check:
Computed property with modulo check = A [OK]
Hint: Computed properties use var without parentheses [OK]
Common Mistakes:
Using func instead of var for computed property
Making property static when it should be instance
Using division instead of modulo for even check
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.