Programming in Swift: Functions & Types

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

Part 5: Protocols & Inheritance

43. Protocols & Extensions

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

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: 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.

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)