How to Create a Protocol in Swift
What you’ll build or solve
You’ll create a protocol that defines required methods and properties.
When this approach works best
Creating a protocol works best when:
Learn Swift on Mimo
- Multiple types share behavior but do not share a base class.
- You want abstraction without inheritance.
- You design APIs that depend on capabilities instead of concrete types.
- You prepare code for testing with interchangeable implementations.
This is unnecessary when only one concrete type exists and abstraction adds no clarity.
Prerequisites
- Xcode installed
- Basic Swift syntax
- Understanding of structs and classes
No additional setup is required.
Step-by-step instructions
Step 1: Declare a protocol with requirements
Use the protocol keyword followed by a name and a set of required members.
Swift
protocol Drivable {
func start()
func stop()
}
Protocols only declare requirements. They do not store data or provide implementation by default.
You can require properties as well:
Swift
protocol Identifiable {
var id: String { get }
var name: String { get set }
}
{ get }means read-only.{ get set }means the property must be mutable.
You can also inherit from another protocol:
Swift
protocol Vehicle {
func start()
}
protocol ElectricVehicle: Vehicle {
func charge()
}
Any type conforming to ElectricVehicle must implement both start() and charge().
What to look for
- Protocol names begin with an uppercase letter.
- Protocols only declare requirements, not stored properties.
- Types conform using
struct Car: Drivable. - Protocols can be used as parameter types in functions.
- Default implementations can be added later using extensions.
- Swift enforces full conformance at compile time.
Examples you can copy
Example 1: Basic protocol declaration
Swift
protocol Printable {
func printDetails()
}
This defines one required method.
Example 2: Protocol with multiple property requirements
Swift
protocol User {
var username: String { get }
var age: Int { get set }
}
This forces conforming types to provide a readable username and a readable and writable age.
Example 3: Protocol inheritance
Swift
protocol Readable {
func read()
}
protocol Writable {
func write()
}
protocol ReadWrite: Readable, Writable {}
ReadWrite now requires both read() and write().
Common mistakes and how to fix them
Mistake 1: Trying to store properties in a protocol
What you might do:
Swift
protocol Invalid {
var name: String = ""
}
Why it breaks:
Protocols cannot store values. They only define requirements.
Correct approach:
Swift
protocol Valid {
var name: String { get }
}
Store data inside conforming types instead.
Mistake 2: Forgetting mutability rules
What you might do:
Swift
protocol Person {
var age: Int { get }
}
Then try to modify it in a conforming type:
Swift
struct Member: Person {
var age: Int
}
Why it can cause confusion:
The protocol only guarantees read access. Other code using Person cannot assume it can modify age.
Correct approach:
Declare { get set } when modification is required.
Swift
protocol Person {
var age: Int { get set }
}
Troubleshooting
If you see “Type does not conform to protocol,” check for missing methods or properties.
If you get a mutability error, verify you used { get } or { get set } correctly.
If inheritance fails, confirm the parent protocol is defined before use.
If naming conflicts appear, review method signatures for mismatches.
Quick recap
- Use
protocolto declare a blueprint of requirements. - Define required methods and properties inside braces.
- Use
{ get }or{ get set }for property access control. - Inherit from other protocols using a colon.
- Protocols do not store data.
- Swift enforces complete conformance at compile time.
Join 40M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot