What does it mean to extend a built-in type in Swift?
Extending a built-in type means adding new features like methods, computed properties, or initializers to existing types such as Int, String, or Array without changing their original code.
Click to reveal answer
beginner
How do you declare an extension for a built-in type in Swift?
Use the keyword extension followed by the type name, then add new methods or properties inside curly braces. For example:
extension Int { func squared() -> Int { return self * self } }
Click to reveal answer
intermediate
Can extensions add stored properties to built-in types in Swift?
No, extensions cannot add stored properties. They can only add computed properties, methods, initializers, and nested types.
Click to reveal answer
beginner
Why might you want to extend a built-in type?
To add useful functionality that fits your app's needs without subclassing or rewriting the original type. For example, adding a method to format a number or check a string's content.
Click to reveal answer
intermediate
Example: What does this Swift extension do?<br><pre>extension String {<br> var isPalindrome: Bool {<br> let cleaned = self.lowercased().filter { $0.isLetter }<br> return cleaned == String(cleaned.reversed())<br> }<br>}</pre>
It adds a computed property isPalindrome to String that returns true if the string reads the same forwards and backwards, ignoring case and non-letter characters.
Click to reveal answer
Which keyword is used to add new functionality to a built-in type in Swift?
Aoverride
Binheritance
Cextension
Dprotocol
✗ Incorrect
The extension keyword is used to add new methods or properties to existing types.
Can you add stored properties to a built-in type using an extension?
AYes, always
BNo, never
COnly if the type is a class
DOnly if the property is static
✗ Incorrect
Extensions cannot add stored properties to any type, only computed properties or methods.
What is the value of 5.squared() if extension Int { func squared() -> Int { self * self } } is defined?
A25
B10
C5
DError
✗ Incorrect
The method returns the number multiplied by itself, so 5 * 5 = 25.
Which of these can you add to a built-in type using an extension?
AChange stored property values
BStored properties
CChange existing method implementations
DComputed properties
✗ Incorrect
Extensions can add computed properties but cannot override or change existing methods or stored properties.
Why are extensions useful in Swift?
AThey let you add functionality without subclassing
BThey replace the original type's code
CThey allow modifying private properties
DThey make types mutable
✗ Incorrect
Extensions let you add new features to existing types without subclassing or changing original code.
Explain how to extend a built-in type in Swift and what you can add with an extension.
Think about adding new abilities to a type without changing its original code.
You got /5 concepts.
Describe a real-life example where extending a built-in type would be helpful.
Imagine you want to add a shortcut to check or transform data on a common type.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of extending built-in types in Swift?
easy
A. To change the stored properties of existing types
B. To delete existing methods from built-in types
C. To add new methods or computed properties to existing types
D. To create subclasses of built-in types
Solution
Step 1: Understand what extensions do
Extensions allow adding new methods or computed properties to existing types without subclassing.
Step 2: Identify what extensions cannot do
Extensions cannot add stored properties or delete existing methods.
Final Answer:
To add new methods or computed properties to existing types -> Option C
Quick Check:
Extensions add methods/properties = B [OK]
Hint: Remember: extensions add features, not stored data [OK]
Common Mistakes:
Thinking extensions can add stored properties
Confusing extensions with subclassing
Believing extensions can remove methods
2. Which of the following is the correct syntax to extend the built-in Int type with a method isEven() that returns a Bool?
easy
A. extension Int { func isEven() -> Bool { return self % 2 == 0 } }
B. extend Int { func isEven() -> Bool { return self % 2 == 0 } }
C. extension Int { func isEven { return self % 2 == 0 } }
D. extension Int { isEven() -> Bool { return self % 2 == 0 } }
Solution
Step 1: Identify the correct keyword for extension
Swift uses the keyword extension to add methods to existing types.
Step 2: Check method syntax
The method must have parentheses and a return type: func isEven() -> Bool. The body uses self % 2 == 0.
Final Answer:
extension Int { func isEven() -> Bool { return self % 2 == 0 } } -> Option A
Quick Check:
Correct extension syntax = D [OK]
Hint: Use 'extension' keyword and full method signature [OK]
Common Mistakes:
Using 'extend' instead of 'extension'
Omitting parentheses in method declaration
Missing 'func' keyword before method name
3. Consider this Swift code:
extension String {
func reversedString() -> String {
return String(self.reversed())
}
}
let word = "hello"
print(word.reversedString())
What is the output when this code runs?
medium
A. Error: reversed() not found
B. hello
C. nil
D. olleh
Solution
Step 1: Understand the extension method
The method reversedString() returns a new String made by reversing the characters of the original string.
extension Double {
var squared: Double {
return self * self
}
var storedValue: Double = 0.0
}
medium
A. Extensions cannot add stored properties like 'storedValue'
B. Computed properties must use 'func' keyword
C. The 'squared' property must be a method, not a property
D. The extension must be declared as 'class extension'
Solution
Step 1: Review extension property rules
Extensions can add computed properties but cannot add stored properties.
Step 2: Identify the stored property
The line var storedValue: Double = 0.0 tries to add a stored property, which is not allowed.
Final Answer:
Extensions cannot add stored properties like 'storedValue' -> Option A
Quick Check:
No stored properties in extensions = A [OK]
Hint: Remember: only computed properties allowed in extensions [OK]
Common Mistakes:
Trying to add stored properties in extensions
Confusing computed properties with stored properties
Thinking extensions require 'class' keyword
5. You want to extend the built-in Array type in Swift to add a method secondElement() that returns the second element if it exists, or nil otherwise. Which of the following extensions correctly implements this?
hard
A. extension Array {
func secondElement() -> Element {
return self[1]
}
}
The method should return an optional Element (Element?) because the second element might not exist.
Step 2: Verify index and condition
Arrays are zero-indexed, so the second element is at index 1. The condition should check if count > 1 to safely access index 1.
Step 3: Compare options
The correct one uses ternary operator: return self.count > 1 ? self[1] : nil. The non-optional return crashes on short arrays. The computed property uses index 2 (third element). The if-else checks count > 0, allowing index 1 access when count == 1, causing runtime crash.