Protocol Conformance
Conformance is a type actually keeping its promise, by providing real code for every item on the protocol's checklist.
In this page:
Syntax
protocol ProtocolName {
var property: Type { get }
func method()
}
struct TypeName: ProtocolName { /* implement every requirement */ }
extension ExistingType: ProtocolName { /* add conformance */ }
Implementing All Requirements
To conform to a protocol, a type must provide a real implementation for every property and method the protocol lists as required.
Example: Implementing All Requirements
// Define a protocol `Describable` describing required behavior
protocol Describable {
var description: String { get }
// Define the function `summary` with no parameters and returning a String
func summary() -> String
}
// Define a struct `Book`
struct Book: Describable {
var title: String
var description: String {
// Return `"Book titled \(title)"` from this function
return "Book titled \(title)"
}
// Define the function `summary` with no parameters and returning a String
func summary() -> String {
// Return `"Summary: \(description)"` from this function
return "Summary: \(description)"
}
}
// Declare a constant `book`, set to `Book(title: "Swift Basics")`
let book = Book(title: "Swift Basics")
// Print `book.summary()` to the console
print(book.summary())
Login to try C/C++/Java/PHP code in the editor
Conforming via an Extension
Protocol conformance can be added to an existing type through an extension, keeping the conformance code separate from the original type definition.
Example: Conforming via an Extension
// Define a protocol `Named` describing required behavior
protocol Named {
var displayName: String { get }
}
// Define a struct `Product`
struct Product {
var title: String
}
// Extend `Product` with additional functionality
extension Product: Named {
var displayName: String {
// Return `"Product: \(title)"` from this function
return "Product: \(title)"
}
}
// Declare a constant `product`, set to `Product(title: "Headphones")`
let product = Product(title: "Headphones")
// Print `product.displayName` to the console
print(product.displayName)
Login to try C/C++/Java/PHP code in the editor
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
Common Mistakes
- Forgetting a property requirement's
{ get }vs{ get set }distinction --{ get set }requires the conforming type's property to be mutable. - Declaring conformance (
: ProtocolName) but forgetting to actually implement one of the required methods, causing a compile error. - Trying to conform a type to a protocol via an extension while also adding new stored properties there; extensions cannot add stored properties.
Chapter Summary
- A type must implement every property and method a protocol requires to conform.
{ get }in a protocol means read-only access is required;{ get set }requires a settable property too.- Conformance can be declared directly on the type or added later via an extension.
- The compiler checks conformance at compile time, so missing requirements are caught immediately.
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: