Programming in Swift: Functions & Types

Jan 4 2022 · Swift 5.5, iOS 15, Xcode 13

Part 5: Protocols & Inheritance

42. Protocols

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 41. Challenge: Initializers Next episode: 43. Protocols & Extensions

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Notes: 42. Protocols

Update Notes: This course was originally recorded in 2019. It has been reviewed and all content and materials updated as of October 2021.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

So far, you’ve learned about three named types: enumerations, structures, and classes. There’s just one more to learn about: Protocols. You’ve already used protocols! CaseIterable was a protocol we used a few times on enumerations. And if you worked through Your First and Second SwiftUI app courses, you definitely used the “View” protocol.

32 class Animal {
//  let name: String
//
//  required init(name: String) {
//    self.name = name
//  }
//
//  func speak() { }
}
32 protocol Animal {
33 var name: String { get }

//  required init(name: String) {
class Dog: Animal {
43  let name: String
  var tricksLearnedCount: Int
class Cat: Animal {
61 let name: String
  
  override func speak() {
35 required init(name: String) {
    self.name = name
  }

//  func speak() { }
35   init(name: String)
//  required init(name: String) {
  var name: String { get }

  init(name: String)

//  func speak() { }
61 required init(name: String) {
    self.name = name
  }
  
  override func speak() {
45 self.tricksLearnedCount = tricksLearnedCount
    self.name = name
  }
37 func speak()
53 func speak() {
65 func speak() {
65 //  func speak() {
//    print("My name is \(name). Please leave me alone. I must look at this wall.")
//  }
65   func speak() {
    print("My name is \(name). Please leave me alone. I must look at this wall.")
  }
let animals: [Animal] = [Dog(name: "Fang"), Cat(name: "Mr. Midnight")]
77 Animal(name: "Animal")