Programming in Swift: Functions & Types

Jan 4 2022 Swift 5.5, iOS 15, Xcode 13

Part 5: Protocols & Inheritance

40. 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: 39. Challenge: Inheritance Next episode: 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'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.

You’ve already learned how to write basic initializers. You use them to set any values needed by your types at initialization time! But when inheritance is in the mix, you’ll need to incorporate a few more rules when writing your initializers.

47 var sports: [String]
49 init(firstName: String, lastName: String, sports: [String]) {

}
50 self.sports = sports
51 super.init(firstName: firstName, lastName: lastName)
  StudentAthlete(...... sports: ["Foosball"])
StudentAthlete(firstName: "Bernie", lastName: "Kosar")
49 … sports: [String] = []) {
49 … sports: [String]) {
54 override init(firstName: String, lastName: String) {

}
55 self.sports = sports
   super.init(firstName: firstName, lastName: lastName)
55 … = []
36 required init…
54 required init…
49 //  init(firstName: String, lastName: String, sports: [String]) {
//    self.sports = sports
//    super.init(firstName: firstName, lastName: lastName)
//  }
  
  required init(firstName: String, lastName: String, sports: [String] = []) {

49 init(firstName: String, lastName: String, sports: [String]) {
    self.sports = sports
    super.init(firstName: firstName, lastName: lastName)
  }
  
  required init(firstName: String, lastName: String) {
57 self.init(firstName: <#T##String#>, lastName: <#T##String#>, sports: <#T##[String]#>)
57 self.init(firstName: firstName, lastName: lastName, sports: [])
54 required init(firstName: String, lastName: String) {
    self.init(firstName: firstName, lastName: lastName, sports: [])
  }
54 required convenience init(firstName: String, lastName: String) {
45 convenience init(transfer: Student) {

}
46 self.init(firstName: transfer.firstName, lastName: transfer.lastName)
47 grades = transfer.grades
36 let rudy = StudentAthlete(firstName: "Daniel"…
…
39 StudentAthlete(transfer: rudy)
39 …rudy).sports
65  convenience init(transfer: StudentAthlete) {
    
  }
self.init(firstName: transfer.firstName, lastName: transfer.lastName, sports: transfer.sports)
grades = transfer.grades