Programming in Swift: Functions & Types

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

Part 5: Protocols & Inheritance

41. Challenge: Initializers

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: 40. Initializers Next episode: 42. 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: 41. Challenge: Initializers

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

It’s class initializer Challenge Time! Find the challenge in the next page of the playground for this part of the course, and give the exercises your best shot. Pause the video now, and come back to compare solutions after you’re done.

39 class Animal {
  var name: String
    
  func speak() { }
}
42 required init(name: String) {
    self.name = name
  }
class Dog: Animal {
  
}
56 var tricksLearnedCount: Int
58 required init(name: String) {
    <#code#>
  }
58  tricksLearnedCount = 0
    super.init(name: name)
    speak()
override func speak() {
    <#code#>
  }
65 print("Bow wow! My name is \(name)!")
69 Dog(name: "Shadow")
64 init(name: String, tricksLearnedCount: Int) {

}
65 self.tricksLearnedCount = tricksLearnedCount
    super.init(name: name)
    speak()
58 convenience required init(name: String) {
    self.init(name: name, tricksLearnedCount: 0)
    tricksLearnedCount = 0
    super.init(name: name)
    speak()
  }
58 convenience required init(name: String) {
    self.init(name: name, tricksLearnedCount: 0)
  }
Dog(name: "Chance", tricksLearnedCount: 3)
extension Dog {
  convenience init(tricksLearnedCount: Int = .max) {
    
  }
}
self.init(name: "Tramp", tricksLearnedCount: tricksLearnedCount)
Dog().tricksLearnedCount