O'Reilly logo

Swift Functional Programming - Second Edition by Dr. Fatih Nayebi

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Protocols as types

Any protocol that we define will become a fully-fledged type to use in our code. We can use a protocol as follows:

  • A parameter type or return type in a function, method, or initializer
  • The type of a constant, variable, or property
  • The type of items in an array, dictionary, or another container

Let's look at the following example:

protocol ExampleProtocol {     var simpleDescription: String { get }     mutating func adjust() } // Classes, enumerations and structs can all adopt protocols. class SimpleClass: ExampleProtocol {     var simpleDescription: String = "A very simple class example"     var anotherProperty: Int = 79799     func adjust() {         simpleDescription += "Now 100% adjusted..."     } } var aSimpleClass = SimpleClass() aSimpleClass.adjust() ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required