Programming in Swift: Functions & Types

Jan 4 2022 Swift 5.5, iOS 15, Xcode 13

Part 5: Protocols & Inheritance

43. Protocols & Extensions

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: 42. Protocols Next episode: 44. Challenge: 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'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.

We’ve gone over how inheriting from a class is different than adopting a protocol. But it’s possible for a protocol to inherit from another protocol. And as we’ll see in a moment, unlike with superclasses, a protocol can inherit from more than one other protocol.

protocol AloofAnimal: Animal { }
42 extension AloofAnimal {
 
 }
43 func speak() {
    print("My name is \(name). Please leave me alone. I must look at this wall.")
  }
74 Cat: AloofAnimal {
74 Cat: AloofAnimal { }
40 protocol Aloof {
  
}
41 var name: String { get }
44 extension Aloof {
  var greeting: String {
    
  }
}
46 return "My name is \(name). Please leave me alone."
50 protocol AloofAnimal: Aloof, Animal { }
54 print("\(greeting) I must look at this wall.")
class Cat {
  let name: String
  
  required init(name: String) {
    self.name = name
  }
}

extension Cat: AloofAnimal { }
func speak() {
  print(greeting + "Meow!")
}
extension Int {
  
}
func isEven(_ value: Int) -> Bool {
  value % 2 == 0
}
func isOdd(_ value: Int) -> Bool {
  (value + 1) % 2 == 0
}
var isEven(_ value: Int) -> Bool {
  value % 2 == 0
}
var isOdd(_ value: Int) -> Bool {
  (value + 1) % 2 == 0
}
var isEven😺:❌(_ value: Int) ->❌ Bool {
  value % 2 == 0
}
var isOdd😺:❌(_ value: Int) ->❌ Bool {
  (value + 1) % 2 == 0
}
var isEven😺:❌(_ value: Int) ->❌ Bool {
  self % 2 == 0
}
var isOdd😺:❌(_ value: Int) ->❌ Bool {
  (self + 1) % 2 == 0
}
5.isOdd
5.isEven
extension Numeric {

}
extension Numeric {
  var squared: Self {  }
}
extension Numeric {
  var squared: Self { self * self }
}
5.squared
5.5.squared
Time(day: .friday, hour: 17)
init(day: Weekday) {
  self.day = day
}
  var hour: UInt = 0
😺}

extension Time {🛑
  init(day: Weekday) {
    self.day = day
  }
}
Time()
Time(day: .wednesday)