Extensions
Extensions let you add new methods, computed properties, initializers, and protocol conformances to any type — including Int , String , and Array — without subclassing or editing the original source.
Learn Extensions in our free Swift course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.
Part of the free Swift course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
What You'll Learn in This Lesson
- • Add methods and computed properties to existing types
- • Extend standard-library types like Int and String
- • Add protocol conformance in a dedicated extension
- • Understand why extensions can't add stored properties
- • Add initializers without losing the memberwise init
- • Use extensions to organize a type's code
1️⃣ Adding Methods & Computed Properties
The simplest use of an extension is bolting new behaviour onto an existing type. Here we give Int a computed property and a method — and they read as if they were built in.
2️⃣ Extending Standard-Library Types
You can extend types you don't own. Adding convenience computed properties to String keeps call sites clean and expressive.
3️⃣ Protocol Conformance via Extension
A popular pattern is to declare a type plainly, then add each protocol conformance in its own extension. This keeps the core definition tidy and groups related code.
Your turn. Fill in the blanks to extend two standard types.
Pro Tips
- 💡 Group by purpose. One extension per protocol conformance reads beautifully.
- 💡 Remember: computed only. Need to store data? Add it to the type itself, not an extension.
- 💡 Extend the standard library for domain helpers like date.isWeekend .
- 💡 Add inits in extensions to keep the free memberwise initializer on structs.
Common Errors (and the fix)
- "Extensions must not contain stored properties" — switch to a computed property, or move storage into the type.
- Trying to override a method in an extension — extensions add members; they don't override existing ones.
- "Type 'X' does not conform to protocol" — your extension is missing a required member of that protocol.
- Losing the memberwise init — adding a custom init in the struct body removes it; add the custom init in an extension instead.
📋 Quick Reference
Task
Code
Note
Extend a type
extension Int { … }
add members
Computed prop
var sq: Int { self*self }
no storage
Method
func isEven() -> Bool
new behaviour
Conformance
extension P: Equatable { … }
organized
Constraint
extension Array where Element == Int
conditional
Frequently Asked Questions
Mini-Challenge: Extend Array
No blanks this time — just a brief and an outline. Build it, run it, and check your output against the example in the comments.
🎉 Lesson Complete!
- ✅ Extensions add methods, computed properties, inits, and conformances
- ✅ You can extend any type, including Int and String
- ✅ Extensions can't add stored properties (memory layout is fixed)
- ✅ Add protocol conformance in a dedicated extension for clarity
- ✅ Constrained extensions add behaviour only when conditions hold
- ✅ Next lesson: Generics
Practice quiz
What does an extension let you do?
- Subclass a final class
- Delete methods from a type
- Add new functionality to an existing type
- Rename a type
Answer: Add new functionality to an existing type. Extensions add new methods, computed properties, initializers, and conformances to existing types.
Which keyword introduces an extension?
- extension
- extend
- category
- augment
Answer: extension. You write 'extension TypeName { ... }' to extend a type.
Can an extension add STORED properties?
- Yes, any kind
- Only for classes
- Only static stored properties
- No — only computed properties are allowed
Answer: No — only computed properties are allowed. Extensions cannot add stored properties; they can add computed properties and methods.
Can you extend types you don't own, like Int or String?
- No, only your own types
- Yes — you can extend any type, including the standard library's
- Only in the same file
- Only structs
Answer: Yes — you can extend any type, including the standard library's. Extensions work on any type, including Int, String, and Array from the standard library.
Can an extension add a computed property?
- Yes
- No
- Only get-only ones to classes
- Only to protocols
Answer: Yes. Extensions can add computed properties (with get, and optionally set).
Can you add protocol conformance via an extension?
- No
- Only at declaration time
- Yes — extension MyType: SomeProtocol { ... }
- Only for enums
Answer: Yes — extension MyType: SomeProtocol { ... }. A common pattern is 'extension MyType: SomeProtocol { ... }' to organize conformance separately.
Can an extension add a method to a protocol?
- No
- Yes — protocol extensions provide default implementations
- Only computed properties
- Only to classes
Answer: Yes — protocol extensions provide default implementations. Protocol extensions add methods that become default implementations for all conformers.
What kind of initializer can an extension add to a struct?
- A designated init that replaces the memberwise one
- A deinitializer
- None
- A convenience-style init (without removing the memberwise init)
Answer: A convenience-style init (without removing the memberwise init). An extension can add an initializer to a struct while preserving the automatic memberwise initializer.
Do extensions let you override existing methods of a type?
- Yes, that's their main use
- No — extensions add new functionality, not override existing members
- Only in subclasses
- Only for final types
Answer: No — extensions add new functionality, not override existing members. Extensions add to a type; they are not for overriding a type's existing methods.
Why are extensions useful for code organization?
- They make code run faster
- They reduce memory
- They let you group related methods and conformances into focused chunks
- They hide a type's properties
Answer: They let you group related methods and conformances into focused chunks. Extensions let you split a type's functionality into logically grouped, readable sections.