Syntax

Swift Define a Protocol Syntax

In Swift, define a protocol: describes required behavior — protocol Shape { func area() -> Double }

Quick Answer

Syntax
protocol Shape { func area() -> Double }
SwiftSyntax

Define a Protocol

Code example
protocol Shape { func area() -> Double }

Describes required behavior.

Learn More

How to define a protocol in Swift

  1. Open your Swift file or editor.
  2. Copy the example, then replace its sample names and values for your code.

Swift Define a Protocol Syntax

Use protocol Shape { func area() -> Double }.

Related Syntax References

Frequently asked questions

What is the Swift syntax for define a protocol?

Use this pattern: protocol Shape { func area() -> Double }

Can I copy this Swift code example?

Yes. Use Copy code and adapt the example to your program.

Is this Swift syntax different on Mac or Windows?

No. The language syntax is the same across operating systems.

Official source · How we verify