Days 1 to 12 of the “100 Days of Swift” course make up the “Introduction to Swift” section.
Day 11‘s lesson covered protocols, extensions, and protocol extensions. The specific sections were: protocols, protocol inheritance, extensions, protocol extensions, and protocol-oriented programming.
Thoughts about Day 11
At the beginning of Day 11’s lesson on Swift protocols I was a bit confused about how and why protocols are used while at the same time feeling a sense of familiarity about Swift’s concept of protocols. It was during my study of section 5 on protocol-oriented programming did it dawn on me why Swift protocols had a sense of familiarity about them… they are very similar, if not equivalent, to Java interfaces. The bit of confusion about Swift protocols that I had at the beginning of today’s lesson immediately went away.
The background music for today’s study session was streamed from YouTube and titled “Deep Focus Music with 432 Hz Tuning and Binaural Beats for Concentration – Study Music, Relaxation“.
My Day 11 notes taken from my paper notebook
- Protocols
- Swift protocols are a way of describing what properties and methods something must have. This “way of describing” is known as adopting or conforming to a protocol.
- protocols cannot spawn instances of itself. Stated another way, we cannot create instances of a protocol.
- protocols are descriptions of what we want.
- protocols let us define how structs, classes, and enums ought to work; also what methods and properties the structs, classes, and enums should have.
- protocols allow us to treat our data in general terms.
- it is not possible for a protocol to contain only a single set property if the protocol contains a property. If a protocol contains a property then the property must have at minimum a get.
- Protocol inheritance
- one protocol can inherit from another protocol.
- protocols can inherit from multiple protocols.
- protocol inheritance helps to reduce duplication in related protocols by breaking apart protocols into unique pieces that can be combined, like LEGO blocks, to create individually specific protocols.
- protocol properties, when defined, must have at minimum a { get } after it.
- Extensions
- extensions allow you to add methods to existing types to make the types do things they were not originally designed to do.
- extensions do not allow stored properties.
- extensions allow computed properties that are returned.
- conformance grouping = grouping of protocols based on what the protocols conform to.
- definition of conform = to act in harmony or similarity.
- purpose grouping = grouping of protocols based on a specific purpose or task that the protocol must accomplish.
- Protocol extensions
- protocol extensions let you extend a whole protocol so that all conforming types get the changes.
- Protocol-oriented programming
- protocol-oriented programming (POP) is crafting your code around protocols and protocol extensions.
- Revelation that just now hit me: Swift protocols are similar, if not equivalent, to Java interfaces.
- from the Optional reading. There is a link to a video of Dave Abraham’s talk at WWDC 2015 about protocol-oriented programming. Make sure to watch it.
- any Swift type can conform to protocols.
- protocols cannot contain other protocols.
- Protocols and extension summary
- no notes recorded.
Today’s total study time: 2 hours 30 minutes
100 Days of Swift cumulative study time: 30 hours
[Note: those are actual study time values after subtracting break-time minutes from the Day’s study session]