From the course: Swift 5: Protocol-Oriented Programming

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Conforming to protocols via extensions

Conforming to protocols via extensions

From the course: Swift 5: Protocol-Oriented Programming

Start my 1-month free trial

Conforming to protocols via extensions

- [Narrator] Developers often rely on code they do not own. That's the case when working with SDKs and third-party libraries. I create a new playground called ProtocolConformance. Now, let's assume that we want to build a cryptography framework. We start with a protocol, but first, I'm going to to remove the generated code, we only need to keep the foundation framework and the rest will be deleted. So let's create the protocol, I'm going to call it encrypting, and it defines a single-method requirement, the XOR method. It takes a parameter called key of type onsite integer, and it returns option of self. We can adopt this protocol in our types easily. If I create a structure like TaggedData, Xcode will help out and create the stubs, and I could provide the implementation here. Now, what if I want to make the string type conform to the protocol? We can't change the implementation of the string's structure as we did with…

Contents