Protocol extensions let us define default behavior for conforming types without defining a base class. In this demo, explore the benefits of protocol extensions using a case study.
- [Instructor] Providing default behavior…could save us a lot of time when implementing…the conforming types.…Yet, as you may know, we can't provide implementation…in the protocol body.…Luckily, we can provide default method…and property implementations through a protocol extension.…So we have our three protocols,…BinaryRepresentable, BinaryPersistable…and Base64Encodable.…We declare protocol extensions…using the extension keyword.…It's the same syntax we used to create type extensions.…
So let's provide default implementations…for the BinaryPersistable protocol.…I'm creating the extension,…and then I simply copy and paste…the method declarations from the protocol.…I'm going to start with the initializer.…To load the data, we rely on the data initializer…which loads the data from the URL.…
Let data equals Data.init,…and we need the variant which takes a contentsOf parameter,…and we pass in the URL we passed to our initializer.…Note that init contentsOf might throw an error…if the location cannot be read.…Thus, we need to use the try keyword…
- Comparing object-oriented programming with protocol-oriented programming
- Methods and class-bound protocols
- Adopting a protocol
- Declaring asynchronous behavior
- Preparing and implementing fallback logic
- Implementing an app using protocol-oriented programming
Skill Level Intermediate
1. Protocol-Oriented Programming
4. Implement the Weather App Using POP
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.