What if you could add new superpowers to your code without ever touching its original heart?
Why extensions add capability without modifying in Swift - The Real Reasons
Imagine you have a favorite recipe book, but you want to add new recipes without rewriting or damaging the original pages.
Manually copying and changing the recipe book is slow and risky. You might lose original recipes or make mistakes that ruin the book.
Extensions let you add new recipes on separate pages that attach to the book, so the original stays safe and you get new dishes easily.
class Dog { func bark() { print("Woof") } } // To add a new trick, you must change Dog class directly
extension Dog { func sit() { print("Sitting") } }
// Adds new behavior without touching original Dog codeExtensions let you grow your code's abilities safely and flexibly, like adding new tools without breaking old ones.
You have a basic app for photos, and later want to add filters. Extensions let you add filter functions without changing the original photo code.
Extensions add new features without changing original code.
They keep code safe and easy to maintain.
They help your programs grow step-by-step like adding new pages to a book.
Practice
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 BQuick Check:
Extensions add features without modification = D [OK]
- Thinking extensions replace original types
- Believing extensions delete old methods
- Confusing extensions with subclassing
Solution
Step 1: Recall Swift extension syntax
Extensions start with the keyword 'extension' followed by the type name and braces enclosing new members.Step 2: Check each option
extension MyType { func newMethod() { } } matches the correct syntax: 'extension MyType { func newMethod() { } }'. Others have syntax errors.Final Answer:
extension MyType { func newMethod() { } } -> Option AQuick Check:
Correct extension syntax = A [OK]
- Using 'extend' instead of 'extension'
- Adding colon after type name
- Omitting braces around methods
struct Person { var name: String }
extension Person {
func greet() -> String {
return "Hello, \(name)!"
}
}
let p = Person(name: "Anna")
print(p.greet())Solution
Step 1: Understand the extension adds greet() method
The extension adds a greet() method to Person that returns "Hello, \(name)!" using the instance's name.Step 2: Check the instance and method call
Creating Person(name: "Anna") and calling greet() returns "Hello, Anna!".Final Answer:
Hello, Anna! -> Option AQuick Check:
Extension method works, output = Hello, Anna! [OK]
- Assuming extension methods can't access properties
- Expecting compile error for extension method
- Confusing string interpolation syntax
extension Int {
func squared() -> Int {
return self * self
}
}
let result = 5.squared
print(result)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 DQuick Check:
Method call needs () = B [OK]
- Calling methods without parentheses
- Thinking Int cannot be extended
- Believing self is disallowed in extensions
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?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 CQuick Check:
Computed property with modulo check = A [OK]
- Using func instead of var for computed property
- Making property static when it should be instance
- Using division instead of modulo for even check
Ready to prove it?
Pass the test and this pattern moves unknown
