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
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.
📋 Quick Reference
No blanks this time — just a brief and an outline. Build it, run it, and check your output against the example in the comments.
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.