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:

  • 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.

protocolDrivable {
funcstart()
funcstop()
}

Protocols only declare requirements. They do not store data or provide implementation by default.

You can require properties as well:

protocolIdentifiable {
varid:String {get }
varname:String {getset }
}
  • { get } means read-only.
  • { get set } means the property must be mutable.

You can also inherit from another protocol:

protocolVehicle {
funcstart()
}

protocolElectricVehicle:Vehicle {
funccharge()
}

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

protocolPrintable {
funcprintDetails()
}

This defines one required method.


Example 2: Protocol with multiple property requirements

protocolUser {
varusername:String {get }
varage:Int {getset }
}

This forces conforming types to provide a readable username and a readable and writable age.


Example 3: Protocol inheritance

protocolReadable {
funcread()
}

protocolWritable {
funcwrite()
}

protocolReadWrite: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:

protocolInvalid {
varname:String=""
}

Why it breaks:

Protocols cannot store values. They only define requirements.

Correct approach:

protocolValid {
varname:String {get }
}

Store data inside conforming types instead.


Mistake 2: Forgetting mutability rules

What you might do:

protocolPerson {
varage:Int {get }
}

Then try to modify it in a conforming type:

structMember:Person {
varage: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.

protocolPerson {
varage:Int {getset }
}

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 protocol to 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.