Programming in Swift: Functions & Types

Jan 4 2022 Swift 5.5, iOS 15, Xcode 13

Part 5: Protocols & Inheritance

42. Protocols

Lesson Complete

Play Next Lesson
Next
Save for later
About this episode
See versions

See course reviews

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

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've reached locked video content where the transcript will be shown as obfuscated text.

You can unlock the rest of this video course, and our entire catalogue of books and videos, with a kodeco.com Professional subscription.

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")