Introduction
An extension in Swift adds new functionality to an existing type, such as a class, struct, enum, or protocol, even if you do not have access to the original source code. Extensions can add computed properties, instance and type methods, initializers, subscripts, and protocol conformance. However, extensions cannot add stored properties or override existing functionality — they can only extend, not replace, what a type already does.
Cricket analogy: Adding a custom strikeRate computed property to Apple's built-in Int type via an extension is like a broadcaster overlaying a strike-rate graphic onto official scorecard data they don't own the source for, adding insight but not rewriting the underlying scorecard.
Syntax
extension Int {
func squared() -> Int {
return self * self
}
}Explanation
The keyword extension is followed by the name of the type being extended, here Int, Swift's built-in integer type. Inside the braces, a new method squared() is defined that operates on self, the integer value the method is called on. Because Int is a type from the Swift standard library, extensions let you add behavior to it as if you had written it yourself, without subclassing or modifying the original type definition.
Cricket analogy: extension Int { func squared() -> Int { self * self } } is like adding a custom runsSquared() stat to the standard scoring system, where self refers to the specific score you called it on, without rewriting how scores are stored.
Example
extension String {
var isPalindrome: Bool {
let cleaned = self.lowercased()
return cleaned == String(cleaned.reversed())
}
}
let word = "Level"
print(word.isPalindrome)Output
trueKey Takeaways
- Extensions add new functionality to existing types, including ones you don't own like Int or String.
- Extensions can add computed properties, methods, initializers, subscripts, and protocol conformance.
- Extensions cannot add stored properties or override existing methods.
- Extensions keep related functionality organized separately from the original type definition.
- Extensions are commonly used to make a type conform to a protocol in a focused, readable block.
Practice what you learned
1. What can a Swift extension add to an existing type?
2. Can extensions add stored properties to a type?
3. Which of these types can be extended in Swift?
4. What happens if you try to override an existing method using an extension?
Was this page helpful?
You May Also Like
Protocols in Swift
Learn how Swift protocols define blueprints of methods and properties that structs, classes, and enums can conform to.
Structs vs Classes in Swift
Structs use value semantics while classes use reference semantics — the defining choice you make for every custom Swift type.
Protocol-Oriented Programming in Swift
Explore Swift's protocol-oriented design philosophy, which favors composing small protocols over deep class inheritance.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics