Programming in Swift: Functions & Types

Jan 4 2022 Swift 5.5, iOS 15, Xcode 13

Part 5: Protocols & Inheritance

41. Challenge: Initializers

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: 40. Initializers Next episode: 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'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.

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